-
Notifications
You must be signed in to change notification settings - Fork 2
/
payment.js
337 lines (313 loc) · 13.7 KB
/
payment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*jslint node: true */
"use strict";
var hashnethelper = require('./hashnethelper.js');
var transationVersion = require('./constants').transationVersion;
var NRG_PRICE = 0;
var eventBus = require('./event_bus.js');
var ecdsaSig = require('./signature.js');
const webHelper = require("./webhelper.js");
const constants = require('./constants');
const objectHash = require('./object_hash.js');
const header = {'Content-Type': 'application/json'};
const device = require("./device.js");
const _ = require('lodash');
var light = require('./light.js');
const config = require('./conf.js')
const zero = '000000000000000000';
var mutex = require('./mutex.js');
var Decimal = require('decimal.js');
var db = require('./db.js');
var utils = require('./utils.js');
/**
* 获取NRG_PRICE
* @returns {number}
*/
async function getNrgPrice(){
// if(!NRG_PRICE) {
// var tranNrgPrice = setInterval(async function () {
if(!NRG_PRICE) {
NRG_PRICE = await hashnethelper.getNRGPrice();
}
return NRG_PRICE;
// if(NRG_PRICE) {
// clearInterval(tranNrgPrice)
// return NRG_PRICE;
// eventBus.emit('nrgPrice',NRG_PRICE);
// }
// }, 2 * 1000);
// }else{
// return NRG_PRICE;
// }
}
/**
* 构造交易结构
* @param data
* @returns {{fromAddress: *, toAddress: *, amount: string, timestamp: number, remark: string, vers: string, pubkey: *, type: number, fee: string, nrgPrice: number}}
*/
async function transactionMessage(data,cb) {
var Bitcore = require('bitcore-lib');
NRG_PRICE = await getNrgPrice();
if(!NRG_PRICE) return cb(('error,unable to get nrgPrice'),null);
try{
let amount = (data.amount + "").split('.')[0];
let amountP = (data.amount + "").split('.')[1] ? (data.amount + "").split('.')[1] : '';
let amountPoint = amountP+zero.substring(-1,zero.length-amountP.length);
let amountstr = (amount+amountP).replace(/\b(0+)/gi,"")+zero.substring(-1,zero.length-amountP.length);
let Base64 = require('./base64Code');
let noteBase64 = data.note ? Base64.encode(data.note) :'';
let fee = noteBase64 ? ((noteBase64.length * 1.0 /1024) * constants.NRG_PEER_KBYTE + constants.BASE_NRG).toString(): constants.BASE_NRG.toString();
let stablesFrom = await light.findStable3(data.fromAddress);
// let stablesTo = new Decimal(stablesFrom).sub(data.amount).sub(new Decimal(fee*NRG_PRICE/1000000000000000000)).toString();
// let compareStables = new Decimal(stablesTo) >0
let stablesTo = new Decimal(data.amount).add(new Decimal(fee*NRG_PRICE / 1000000000000000000)).toFixed();
if (new Decimal(stablesFrom).comparedTo(stablesTo) < 0) {
// if (!compareStables ||(compareStables && stablesTo.substr(0,1) == "-")) {
return cb("not enough spendable funds from " + data.to_address + " for " + (parseInt(data.fee) + parseInt(data.amount)));
}
let obj =
{
fromAddress: data.fromAddress,
toAddress: data.to_address,
amount: amountstr,
timestamp: Math.round(Date.now()),
remark: noteBase64,
vers: transationVersion,
pubkey: data.pubkey,
type: 1,
fee: fee,
nrgPrice: NRG_PRICE,
}
var xPrivKey = new Bitcore.HDPrivateKey.fromString(data.xprivKey);
let buf_to_sign = objectHash.getUnitHashToSign(obj);
let pathSign = "m/44'/0'/0'/0/0";
let privKeyBuf = xPrivKey.derive(pathSign).privateKey.bn.toBuffer({size:32});
let signature = ecdsaSig.sign(buf_to_sign, privKeyBuf);
obj.signature = signature;
cb(null,obj);
}catch (e) {
cb(e.toString(),null);
}
}
/**构造文本交易结构
* @param data
* @returns {{fromAddress: *, toAddress: *, amount: string, timestamp: number, remark: string, vers: string, pubkey: *, type: number, fee: string, nrgPrice: number}}
*/
async function transactionContext(data,cb) {
var Bitcore = require('bitcore-lib');
NRG_PRICE = await getNrgPrice();
if (!NRG_PRICE) return cb(('error,unable to get nrgPrice'), null);
try {
let Base64 = require('./base64Code');
let noteBase64 = data.note ? Base64.encode(data.note) : '';
let fee = noteBase64 ? (noteBase64.length * 1.0 / 1024) * constants.NRG_PEER_KBYTE + constants.BASE_NRG.toString() : constants.BASE_NRG.toString();
let obj =
{
fromAddress: data.change_address,
timestamp: Math.round(Date.now()),
context: noteBase64,
vers: transationVersion,
pubkey: data.pubkey,
type: 4,
fee: fee,
nrgPrice: NRG_PRICE,
}
var xPrivKey = new Bitcore.HDPrivateKey.fromString(data.xprivKey);
let buf_to_sign = objectHash.getUnitHashToSign(obj);
let pathSign = "m/44'/0'/0'/0/0";
let privKeyBuf = xPrivKey.derive(pathSign).privateKey.bn.toBuffer({size: 32});
let signature = ecdsaSig.sign(buf_to_sign, privKeyBuf);
obj.signature = signature;
cb(null, obj);
} catch (e) {
cb(e.toString());
}
}
/**构造合约交易
* @param data
*/
async function contractTransactionData(opts,cb) {
var Bitcore = require('bitcore-lib');
NRG_PRICE = await getNrgPrice();
if (!NRG_PRICE) return cb(('error,unable to get nrgPrice'), null);
let stablesFrom = await light.findStable3(opts.fromAddress);
//let stablesFrom = stable[0].amount + stable[0].amount_point / parseInt(1 + zero) - stable[0].fee - stable[0].fee_point / parseInt(1 + zero);
let stablesTo = new Decimal(opts.amount).add(new Decimal(0.0005*NRG_PRICE / 1000000000000000000)).toFixed();
if (new Decimal(stablesFrom).comparedTo(stablesTo) < 0) {
return cb("not enough spendable funds from " + opts.toAddress + " for " + (parseInt(opts.fee ? opts.fee : 0) + parseInt(opts.amount)));
}
let amount = (opts.amount + "").split('.')[0];
let amountP = (opts.amount + "").split('.')[1] ? (opts.amount + "").split('.')[1] : '';
// let amountPoint = amountP+zero.substring(-1,zero.length-amountP.length);
let amountstr = (amount+amountP).replace(/\b(0+)/gi,"")+zero.substring(-1,zero.length-amountP.length);
// let gas = new Decimal(constants.BASE_NRG).times(NRG_PRICE).toFixed();
try{
let info = await hashnethelper.getAccountInfo(opts.fromAddress);
let nonce = info.nonce;
let callData = opts.callData;
let gasPrice = NRG_PRICE;
let value = amountstr;
let gasLimit = constants.BASE_NRG;
//let gasLimit = 2000000;
let toAddress = opts.toAddress;
// let data ={
// nonce: utils.numberToBase64(nonce),
// //callData: utils.stringToBase64("3a93424a000000000000000000000000000000000000000000000000000000000000000"+callData),
// callData: utils.stringToBase64(utils.Hexstring2btye("3a93424a000000000000000000000000000000000000000000000000000000000000000"+callData)),
// gasPrice: utils.numberToBase64(gasPrice),
// value: utils.numberToBase64(value),
// gasLimit: utils.numberToBase64(gasLimit),
// toAddress: utils.stringToBase64(toAddress)
// }
let data ={
nonce: nonce.toString(),
//callData: utils.stringToBase64("3a93424a000000000000000000000000000000000000000000000000000000000000000"+callData),
callData: "3a93424a000000000000000000000000000000000000000000000000000000000000000"+callData,
gasPrice: gasPrice.toString(),
value: value.toString(),
gasLimit: gasLimit.toString(),
toAddress: toAddress.toString()
}
data = utils.stringToBase64(JSON.stringify(data));
let obj = {
fromAddress: opts.fromAddress,
timestamp: Math.round(Date.now()),
data: data,
//data: new Buffer(JSON.stringify(data)).toString("base64"),
vers: transationVersion,
pubkey: opts.pubkey,
type: 2
}
var xPrivKey = new Bitcore.HDPrivateKey.fromString(opts.xprivKey);
let buf_to_sign = objectHash.getUnitHashToSign(obj);
let pathSign = "m/44'/0'/0'/0/0";
let privKeyBuf = xPrivKey.derive(pathSign).privateKey.bn.toBuffer({size: 32});
let signature = ecdsaSig.sign(buf_to_sign, privKeyBuf);
obj.signature = signature;
cb(null, obj);
} catch (e) {
cb(e.toString());
}
}
/**
* 往共识网发送交易
* @param data
* @returns {*}
*/
let urlList = [];
async function sendTransactions(opts, cb){
try{
// if(urlList.length == 0){
// let result = await webHelper.httpPost(device.my_device_hashnetseed_url + '/v1/getlocalfullnodes', null, {pubkey: opts.pubkey});
// let localfullnodes = JSON.parse(JSON.parse(result).data);
// _.forEach(localfullnodes,function (res) {
// urlList.push(`${res.ip}:${res.httpPort}`)
// });
// }
// let localfullnode =urlList[Math.round(Math.random() * urlList.length)];
let localfullnode = config.URL.INVE_TRANSACTION_URL;
let message = JSON.stringify(opts);
let resultMessage = JSON.parse(await webHelper.httpPost(getUrl(localfullnode, '/v1/sendmsg'), null, buildData({message})));
if (resultMessage.code != 200) {
//如果发送失败,则马上返回到界面
cb(resultMessage.data, null);
}else {
console.log('opts: ',opts)
await inserTrans(opts)
cb(null,resultMessage)
}
}catch (e) {
cb(e.toString(),null)
}
}
/**
* 发往第三方服务器
* @param data
* @param cb
*/
async function sendTransactionToOtherServer(data, cb){
try{
let url = constants.payUrl;
let obj =
{
//localfullnode_list: data.localfullnode_list, //发送到intervalue的local full node
localfullnode_list: [config.URL.INVE_TRANSACTION_URL], //发送到intervalue的local full node
bizid: data.bizid, //商家号
paysign: data.paysign, //支付系统对本次支付交易的签名
orderid: data.orderid, //商家生成的订单号
paytransid : data.paytransid, //支付系统本次交易号
ptimestamp: data.ptimestamp, //支付系统生成的时间stamp
wallettransid: data.wallettransid, //就是下结构体中的signature
amount: data.amount,
paybody: {
message: data.paybody.message
} //intervalue要求的转帐数据体
}
let res = await webHelper.httpPost(url, header, obj);
res = JSON.parse(res);
if(res.errorcode =="0"){
await inserTrans(data.paybody.message);
cb(null, res);
}else {
cb(res.errormsg);
}
}catch (e) {
cb(e.toString())
}
}
let inserTrans = async (obj) => {
if(obj.hasOwnProperty("data")){
let b = JSON.parse(new Buffer(obj.data,"base64").toString());
// obj.amount = utils.base64ToNumber(b.value).toString();
// obj.fee = utils.base64ToNumber(b.gasLimit);
// obj.toAddress = utils.base64ToString(b.toAddress);
// obj.nrgPrice = utils.base64ToNumber(b.gasPrice)
obj.amount = b.value;
obj.fee = b.gasLimit;
obj.toAddress = b.toAddress;
obj.nrgPrice = b.gasPrice;
}
let amount = obj.amount;
let amountInt = parseInt(amount.replace(/"/g, '').substring(-1, amount.length - 18) ? amount.replace(/"/g, '').substring(-1, amount.length - 18) : 0);
let amountPoint = parseInt(amount.replace(/"/g, '').substring(amount.length - 18, amount.length) ? amount.replace(/"/g, '').substring(amount.length - 18, amount.length) : 0);
let NRG_PRICE = obj.nrgPrice;
let fee = (obj.fee * NRG_PRICE).toString();
let feeInt = parseInt(fee.replace(/"/g,'').substring(-1,fee.length-18) ? fee.replace(/"/g,'').substring(-1,fee.length-18) : 0);
let feePoint = parseInt(fee.replace(/"/g,'').substring(fee.length-18,fee.length) ? fee.replace(/"/g,'').substring(fee.length-18,fee.length) : 0);
let Base64 = require('./base64Code');
let note = obj.remark ? await Base64.decode(obj.remark) : '';
await mutex.lock(["write"], async function (unlock) {
try {
//更新数据库
await db.execute("INSERT INTO transactions (id,creation_date,amount,fee,addressFrom,addressTo,result,type,remark,amount_point,fee_point, multiHash,tranType) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)",
obj.signature, obj.timestamp, amountInt, feeInt, obj.fromAddress, obj.toAddress, "pending", obj.sendType ? obj.sendType : 0 ,note, amountPoint, feePoint,obj.order,obj.type);
//更新列表
obj.isStable = 1;
obj.isValid = 0;
light.refreshTranList(obj);
return '';
}
catch (e) {
console.log(e.toString());
return toString()
}
finally {
//解锁队列
await unlock();
}
});
}
//组装访问共识网的url
let getUrl = (localfullnode, suburl) => {
return 'http://' + localfullnode + suburl;
}
//组装往共识网发送数据的对象
let buildData = (data) => {
return JSON.parse(JSON.stringify(data));
}
module.exports = {
transactionMessage: transactionMessage,
sendTransactions: sendTransactions,
transactionContext: transactionContext,
sendTransactionToOtherServer: sendTransactionToOtherServer,
contractTransactionData: contractTransactionData
}