-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshadowWallet.js
314 lines (271 loc) · 8.47 KB
/
shadowWallet.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
/*jslint node: true */
"use strict";
var getSourceString = require('./string_utils').getSourceString;
var Bitcore = require('bitcore-lib');
var crypto = require('crypto');
var objectHash = require('./object_hash.js');
var ecdsaSig = require('./signature');
var signatureCode;
var signatureDetlCode;
var RANDOM;
/**
* 热钱包 生成授权签名-扫描地址
* @param address
* @param cb
* @returns
*/
exports.getSignatureCode = function(address,cb){
RANDOM = crypto.randomBytes(4).toString("hex");
console.log(RANDOM);
var db = require("./db");
db.query("select count(1) as t from my_addresses where address = ?",[address],function (rs) {
if(rs[0].t == 0) {
signatureCode =
{
name:"shadow",
type:"sign",
addr:address,
random:RANDOM
};
return cb(signatureCode);
}else {
return cb("wallet exists");
}
});
};
/**
* 冷钱包 进行授权签名
* @param signatureCode
* @param words
* @param cb
* @returns {*}
*/
exports.getSignatureDetlCode = function(signatureCode,xPrivkey, cb){
if(xPrivkey == null || xPrivkey == "") {
cb("xPrivkey could not be null~!");
return ;
}
var json;
switch(typeof signatureCode) {
case "string":
json = JSON.parse(signatureCode);
break;
case "object":
json = signatureCode;
break;
default:
cb(false);
break;
}
var sign_json = {
name:"shadow",
type:"sign",
addr:json.addr,
random:json.random
};
var buf_to_sign = crypto.createHash("sha256").update(getSourceString(sign_json), "utf8").digest();
var xPrivKey = new Bitcore.HDPrivateKey.fromString(xPrivkey);
var path = "m/44'/0'/0'/0/0";
var privateKey = xPrivKey.derive(path).privateKey.bn.toBuffer({size:32});
var sign_64 = ecdsaSig.sign(buf_to_sign, privateKey);
var path2 = "m/44'/0'/0'";
var privateKey2 = xPrivKey.derive(path2);
var xpubkey = Bitcore.HDPublicKey(privateKey2).xpubkey;
var pubkey = derivePubkey(xpubkey ,"m/0/0");
signatureDetlCode =
{
name:"shadow",
type:"signDetl",
signature:sign_64,
random:json.random,
expub:xpubkey +'',
addr:json.addr,
pubkey:pubkey
};
return cb(signatureDetlCode);
};
function derivePubkey(xPubKey, path) {
var hdPubKey = new Bitcore.HDPublicKey(xPubKey);
return hdPubKey.derive(path).publicKey.toBuffer().toString("base64");
}
/**
* 热钱包 生成热钱包
* @param signatureDetlCode
* @param cb
* @returns {*}
*/
exports.generateShadowWallet = function(signatureDetlCode,cb){
if(!RANDOM) {
return cb("random failed");
}
var json;
switch(typeof signatureDetlCode) {
case "string":
json = JSON.parse(signatureDetlCode);
break;
case "object":
json = signatureDetlCode;
break;
default:
cb(false);
break;
}
if(RANDOM != json.random) {
return cb("random failed");
}
var addr = json.addr;
var sign = json.signature;
var xpub = json.expub;
var pubkey = json.pubkey;
var sing_json = {
name:"shadow",
type:"sign",
addr:addr,
random:json.random
};
var result = {
'addr':addr,
'sign':sign,
'xpub':xpub,
'pubkey':pubkey
};
var buf_to_sign = crypto.createHash("sha256").update(getSourceString(sing_json), "utf8").digest();
var pub1 = ecdsaSig.recover(buf_to_sign,sign,1).toString("base64");
var pub2 = ecdsaSig.recover(buf_to_sign,sign,0).toString("base64");
var definition1 = ["sig",{"pubkey":pub1}];
var definition2 = ["sig",{"pubkey":pub2}];
var address1 = objectHash.getChash160(definition1);
var address2 = objectHash.getChash160(definition2);
if(address1 === addr || address2 == addr) {
RANDOM = '';
cb(result);
} else
cb("validation failed");
};
var light = require("./light");
/**
* 热钱包生成交易授权签名
* @param opts
* @param cb
* @returns {*}
*/
exports.getTradingUnit = function (opts ,cb) {
switch(typeof opts) {
case "string":
opts = JSON.parse(signatureDetlCode);
break;
case "object":
opts = opts;
break;
default:
cb(false);
break;
}
//判断发送方是否等于接收方,不允许发送给自己
if (opts.change_address == opts.to_address) {
return cb("to_address and from_address is same"
);
}
if (opts.change_address == opts.to_address) {
return cb("to_address and from_address is same");
}
if (typeof opts.amount !== 'number')
return cb('amount must be a number');
if (opts.amount < 0)
return cb('amount must be positive');
var isHot = opts.ishot;
var objectLength = require("./object_length.js");
var timestamp = Math.round(Date.now() / 1000);
var obj = { fromAddress: opts.change_address, toAddress: opts.to_address, amount: opts.amount+"", timestamp};
// obj.fee = ""+objectLength.getTotalPayloadSize(obj);
obj.fee = "0";
light.findStable2(opts.walletId ,function (stable) {
//TODO test
if (stable < (parseInt(obj.fee) + parseInt(obj.amount))) {
return cb("not enough spendable funds from " + obj.to_address + " for " + ((parseInt(obj.fee) + parseInt(obj.amount))/1000000));
}
var db = require("./db");
db.query("SELECT wallet, account, is_change, address_index,definition FROM my_addresses JOIN wallets USING(wallet) WHERE address=? ",[obj.fromAddress],function (row) {
var address;
if(row != null && row.length > 0) {
address = {
definition: JSON.parse(row[0].definition),
wallet: row[0].wallet,
account: row[0].account,
is_change: row[0].is_change,
address_index: row[0].address_index
};
obj.pubkey = address.definition[1].pubkey;
obj.type = 1;
var str = getSourceString(obj);
var authorized_signature = obj;
let h = crypto.createHash("md5");
h.update(str);
var md5 = h.digest("hex");
authorized_signature.md5 = md5;
authorized_signature.name = "isHot";
authorized_signature.type = "trading";
cb(authorized_signature);
}
});
});
};
/**
* 冷钱包进行签名
* @param opts
* @param words
* @param cb
* @returns {Promise<void>}
*/
exports.signTradingUnit = function (opts ,xPrivkey ,cb) {
if(xPrivkey == null || xPrivkey == "") {
cb("xPrivkey could not be null~!");
return ;
}
switch(typeof opts) {
case "string":
opts = JSON.parse(signatureDetlCode);
break;
case "object":
opts = opts;
break;
default:
cb(false);
break;
}
var type = opts.type;
var name = opts.name;
var md5 = opts.md5;
var obj = {fromAddress:opts.fromAddress,toAddress:opts.toAddress,amount:opts.amount,timestamp:opts.timestamp};
obj.fee = opts.fee;
obj.pubkey = opts.pubkey;
obj.type = 1;
var str = getSourceString(obj);
let h = crypto.createHash("md5");
h.update(str);
var result = h.digest("hex");
if( result != md5) {
return cb("validation failed");
}
var buf_to_sign = objectHash.getUnitHashToSign(obj);
//签名
// var mnemonic = new Mnemonic(words);
// var xPrivKey = mnemonic.toHDPrivateKey("");
var xPrivKey = new Bitcore.HDPrivateKey.fromString(xPrivkey);
var path = "m/44'/0'/0'/0/0";
var privateKey = xPrivKey.derive(path).privateKey.bn.toBuffer({size:32});
var signature = ecdsaSig.sign(buf_to_sign, privateKey);
var path2 = "m/44'/0'/0'";
var privateKey2 = xPrivKey.derive(path2);
var xpubkey = Bitcore.HDPublicKey(privateKey2).xpubkey;
var pubkey = derivePubkey(xpubkey ,"m/0/0");
var flag = ecdsaSig.verify(buf_to_sign,signature,pubkey);
opts.type = "sign";
opts.name = "isHot";
opts.signature = signature;
if(flag) {
cb(opts);
} else {
cb("signature failed");
}
};