-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
150 lines (129 loc) · 4.2 KB
/
utils.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
'use strict';
var Bitcore = require('bitcore-lib');
var Mnemonic = require('bitcore-mnemonic');
var objectHash = require('./object_hash.js');
var ecdsaSig = require('./signature.js');
var Decimal = require('decimal.js');
//let words = 'shield salmon sport horse cool hole pool panda embark wrap fancy equip'
//let words = 'silent lava disease visa liquid champion finish curtain alarm spy picnic become'//商户
//let address = '6GTHF7OFAGTZ6HS6KHLD44HDUC6XCJMG'//商户
//let words = 'file rough blade expand shield glimpse fabric screen simple subway rail arrest'//用户1
//let address = '5DRZKXVSZ2JEDQ6VCWB4PUMKO4IETLJD'//用户1
// let words = 'special script goat oil monitor loan ring else joke divorce anchor draw'//用户2
//let address = '7UJN4AEMWVYXGYB6KKFFNXV3DVTTNRO4'//用户2
function derivePubkey(xPubKey, path) {
var hdPubKey = new Bitcore.HDPublicKey(xPubKey);
return hdPubKey.derive(path).publicKey.toBuffer().toString("base64");
}
function getprivKey(words) {
var m = new Mnemonic(words);
console.log(m.toString());
var xprivKey = m.toHDPrivateKey().xprivkey; //主私钥
var xpubkey = m.toHDPrivateKey().xpubkey; //主公钥
var xPrivKey = new Bitcore.HDPrivateKey.fromString(xprivKey);
var path2 = "m/44'/0'/0'";
var privateKey2 = xPrivKey.derive(path2);
var xPubkey = Bitcore.HDPublicKey(privateKey2).xpubkey;
var path = "m/0/0";
var pubkey = derivePubkey(xPubkey, path); //扩展公钥,用于验证签名
var arrDefinition = ["sig", {"pubkey": pubkey}];
var address = objectHash.getChash160(arrDefinition);
var obj = {
xprivKey: xprivKey,
xpubkey: xpubkey,
pubkey: pubkey,
address: address
};
return obj;
}
function getPubkey(xprivKey) {
var xPrivKey = new Bitcore.HDPrivateKey.fromString(xprivKey);
var path2 = "m/44'/0'/0'";
var privateKey2 = xPrivKey.derive(path2);
var xPubkey = Bitcore.HDPublicKey(privateKey2).xpubkey;
var path = "m/0/0";
function derivePubkey(xPubKey, path) {
var hdPubKey = new Bitcore.HDPublicKey(xPubKey);
return hdPubKey.derive(path).publicKey.toBuffer().toString("base64");
}
var pubkey = derivePubkey(xPubkey, path); //扩展公钥,用于验证签名
return pubkey;
}
function signature(opts, xprivKey) {
var buf_to_sign = objectHash.getUnitHashToSign(opts);
var xPrivKey = new Bitcore.HDPrivateKey.fromString(xprivKey);
//获取签名的私钥
var pathSign = "m/44'/0'/0'/0/0";
var privKeyBuf = xPrivKey.derive(pathSign).privateKey.bn.toBuffer({size: 32});
let signature = ecdsaSig.sign(buf_to_sign, privKeyBuf);
//对签名进行验证
return signature;
}
/**
* 字符串转base64
* @param data
* @returns {string}
*/
let stringToBase64 = (data) => {
return new Buffer(data).toString("base64")
}
/**
* 数字转base64
* @param data
* @returns {*}
*/
let numberToBase64 = (data) => {
let n = new Decimal(data);
let k = n.toNumber().toString(16);
k = k.length % 2 == 1 ? "0" + k : k;
return Buffer.from(k, 'hex').toString("base64")
}
/**
* base转字符串
* @param data
*/
let base64ToString = (data) => {
return Buffer.from(data, "base64").toString();
}
/**
* base64转数字
* @param data
*/
let base64ToNumber = (data) => {
let b = Buffer.from(data, "base64").toString("hex");
return new Decimal(parseInt(b, 16)).sub('0').toFixed();
}
let Hexstring2btye = (str)=> {
let pos = 0;
let len = str.length;
if (len % 2 != 0) {
return null;
}
len /= 2;
let hexA = new Array();
for (let i = 0; i < len; i++) {
let s = str.substr(pos, 2);
let v = parseInt(s, 16);
hexA.push(v);
pos += 2;
}
return hexA;
}
let arrayUnique = (arr, name) => {
var hash = {};
return arr.reduce(function (item, next) {
hash[next[name]] ? '' : hash[next[name]] = true && item.push(next);
return item;
}, []);
}
module.exports = {
getprivKey: getprivKey,
getPubkey: getPubkey,
stringToBase64: stringToBase64,
numberToBase64: numberToBase64,
base64ToString: base64ToString,
base64ToNumber: base64ToNumber,
signature: signature,
Hexstring2btye: Hexstring2btye,
arrayUnique:arrayUnique
};