forked from p2pderivatives/cfd-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfdjs_util.js
340 lines (326 loc) · 10.2 KB
/
cfdjs_util.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
338
339
340
const cfdjs = require('./index');
/**
* Parse bip32 path.
* @param {number} path The bip32 path.
* @return {array} The array of the bip32 number.
*/
function parseBip32Path(path) {
let targetPath = path;
if (targetPath.startsWith('m/')) {
targetPath = targetPath.substring(2);
}
if (targetPath === '') {
throw new Error('empty BIP 32 path.');
}
if (targetPath.startsWith('/')) {
throw new Error('Slash cannot be used at the beginning of BIP32 path.');
}
const items = targetPath.split('/');
if (items.length > 10) {
throw new Error('Out of Range. Number of BIP 32 derivations to perform is up to 10.');
}
const hardendedTargets = ['\'', 'h', 'H'];
const length = items.length;
if (length === 0) {
throw new Error('Out of Range. Number of BIP 32 derivations to perform is empty.');
}
const array = [];
for (let idx = 0; idx < length; ++idx) {
let isFind = false;
for (let hIdx = 0; hIdx < hardendedTargets.length; ++hIdx) {
const hKey = hardendedTargets[hIdx];
const item = items[idx].split(hKey);
if (item.length > 1) {
const num = Number(item[0]);
if ((num === Number.NaN) || (item[1] !== '') || (item.length.length > 2)) {
throw new Error(`Illegal path format. [${item[0]},${item[1]}]`);
}
// const value = 0x80000000 | num;
const value = 2147483648 + num;
array.push(value);
isFind = true;
break;
}
}
if (!isFind) {
const num = Number(items[idx]);
if (num === Number.NaN) throw new Error(`Illegal path format. [${items[idx]}]`);
array.push(num);
}
}
return array;
}
/**
* Check child extkey.
* @param {string} rootExtkey The root extkey.
* @param {string} rootBip32Path The root bip32 path.
* @param {string} childExtkey The child extkey.
* @param {string} childBip32Path The child bip32 path.
* @return {boolean} The match child extkey.
*/
exports.HasChildExtkey = function(
rootExtkey, rootBip32Path, childExtkey, childBip32Path) {
const rootExtKeyInfo = cfdjs.GetExtkeyInfo({
extkey: rootExtkey,
});
const childExtKeyInfo = cfdjs.GetExtkeyInfo({
extkey: childExtkey,
});
if (rootExtKeyInfo.version !== childExtKeyInfo.version) {
throw new Error('extkey unmatch version type');
}
const rootPathArray = (!rootBip32Path) ? [] : parseBip32Path(rootBip32Path);
const childPathArray = parseBip32Path(childBip32Path);
const rootEndNum = rootPathArray.length;
const childEndNum = childPathArray.length;
if (rootEndNum >= childEndNum) {
throw new Error('bip32Path unmatch length');
}
for (const idx in rootPathArray) {
if (rootPathArray[idx] !== childPathArray[idx]) {
throw new Error('bip32Path unmatch childPath');
}
}
const childPath = [];
for (let subIndex = rootEndNum; subIndex < childEndNum; ++subIndex) {
childPath.push(childPathArray[subIndex]);
}
if (rootExtKeyInfo.depth + childPath.length !== childExtKeyInfo.depth) {
throw new Error('extkey unmatch depth');
}
if (rootEndNum > 0) {
if (rootExtKeyInfo.childNumber !== rootPathArray[rootEndNum - 1]) {
throw new Error('rootExtkey unmatch childNumber');
}
}
if (childExtKeyInfo.childNumber !== childPathArray[childEndNum - 1]) {
throw new Error('childExtkey unmatch childNumber');
}
let netType = 'mainnet';
let keyType = 'extPubkey';
if (rootExtKeyInfo.version === '0488ade4') {
// privkey, mainnet
keyType = 'extPrivkey';
} else if (rootExtKeyInfo.version === '04358394') {
// privkey, testnet
netType = 'testnet';
keyType = 'extPrivkey';
} else if (rootExtKeyInfo.version === '0488b21e') {
// pubkey, mainnet
} else if (rootExtKeyInfo.version === '043587cf') {
// pubkey, testnet
netType = 'testnet';
}
const deriveResult = cfdjs.CreateExtkeyFromParentPath({
extkey: rootExtkey,
network: netType,
extkeyType: keyType,
childNumberArray: childPath,
});
const deriveExtkey = deriveResult.extkey;
return deriveExtkey === childExtkey;
};
/**
* Check child pubkeys.
* @param {string} rootExtkey The root extkey.
* @param {string} rootBip32Path The root bip32 path.
* @param {array} childPubkeyArray The child pubkey array.
* @param {string} childBip32Path The child bip32 path.
* @return {boolean} The match child extkey.
*/
exports.HasChildPubkeyArray = function(
rootExtkey, rootBip32Path, childPubkeyArray, childBip32Path) {
if (!childPubkeyArray) {
throw new Error('childPubkeyArray empty.');
}
const rootExtKeyInfo = cfdjs.GetExtkeyInfo({
extkey: rootExtkey,
});
const rootPathArray = (rootBip32Path === '') ? [] : parseBip32Path(rootBip32Path);
const childPathArray = parseBip32Path(childBip32Path);
const rootEndNum = rootPathArray.length;
const childEndNum = childPathArray.length;
if (rootEndNum >= childEndNum) {
throw new Error('bip32Path unmatch length');
}
for (const idx in rootPathArray) {
if (rootPathArray[idx] !== childPathArray[idx]) {
throw new Error('bip32Path unmatch childPath');
}
}
const childPath = [];
for (let subIndex = rootEndNum; subIndex < childEndNum; ++subIndex) {
childPath.push(childPathArray[subIndex]);
}
if (rootEndNum > 0) {
if (rootExtKeyInfo.childNumber !== rootPathArray[rootEndNum - 1]) {
throw new Error('rootExtkey unmatch childNumber');
}
}
let netType = 'mainnet';
let keyType = 'extPubkey';
if (rootExtKeyInfo.version === '0488ade4') {
// privkey, mainnet
keyType = 'extPrivkey';
} else if (rootExtKeyInfo.version === '04358394') {
// privkey, testnet
netType = 'testnet';
keyType = 'extPrivkey';
} else if (rootExtKeyInfo.version === '0488b21e') {
// pubkey, mainnet
} else if (rootExtKeyInfo.version === '043587cf') {
// pubkey, testnet
netType = 'testnet';
}
const deriveResult = cfdjs.CreateExtkeyFromParentPath({
extkey: rootExtkey,
network: netType,
extkeyType: keyType,
childNumberArray: childPath,
});
const derivePubkeyResult = cfdjs.GetPubkeyFromExtkey({
extkey: deriveResult.extkey,
network: netType,
});
const derivePubkey = derivePubkeyResult.pubkey;
for (const pubkey of childPubkeyArray) {
if (derivePubkey === pubkey) {
return true;
}
}
return false;
};
/**
* Check child pubkey.
* @param {string} rootExtkey The root extkey.
* @param {string} rootBip32Path The root bip32 path.
* @param {string} childPubkey The child pubkey.
* @param {string} childBip32Path The child bip32 path.
* @return {boolean} The match child extkey.
*/
exports.HasChildPubkey = function(
rootExtkey, rootBip32Path, childPubkey, childBip32Path) {
return exports.HasChildPubkeyArray(
rootExtkey, rootBip32Path, [childPubkey], childBip32Path);
};
/**
* Get pubkey from pubkey or extkey.
* @param {string} key The key string.
* @return {string} The pubkey.
*/
exports.GetPubkeyFromKey = function(key) {
if (key.length === 66) {
return key;
} else {
// xpub or xpriv
const extKeyInfo = cfdjs.GetExtkeyInfo({
extkey: key,
});
let netType = 'testnet';
if (extKeyInfo.version === '0488ade4' || extKeyInfo.version === '0488b21e') {
netType = 'mainnet';
}
const derivePubkeyResult = cfdjs.GetPubkeyFromExtkey({
extkey: key,
network: netType,
});
return derivePubkeyResult.pubkey;
}
};
/**
* Get pubkey array from descriptor.
* @param {string} outputDescriptor The output descriptor.
* @param {string} networkType The network type.
* @return {array} The pubkey array.
*/
exports.GetPubkeyArrayFromDescriptor = function(
outputDescriptor, networkType = 'mainnet') {
let isElements = false;
const nettype = networkType.toLowerCase();
if (nettype.indexOf('liquid') >= 0 || nettype.indexOf('elements') >= 0) {
isElements = true;
}
const desc = cfdjs.ParseDescriptor({
descriptor: outputDescriptor,
network: nettype,
bip32DerivationPath: '',
isElements: isElements,
});
if (desc.hashType.indexOf('pkh') >= 0) {
// pkh
const endScript = desc.scripts[desc.scripts.length - 1];
if (endScript.key) {
return [exports.GetPubkeyFromKey(endScript.key)];
}
} else if (desc.scripts) {
// sh
const endScript = desc.scripts[desc.scripts.length - 1];
if (endScript.keys) {
const pubkeys = [];
for (const key of endScript.keys) {
pubkeys.push(exports.GetPubkeyFromKey(key.key));
}
return pubkeys;
}
}
throw new Error('descriptor fail. empty pubkey.');
};
/**
* Set multisig scriptsig.
* @param {string} txHex The transaction hex.
* @param {string} txid The utxo txid.
* @param {number} vout The utxo vout.
* @param {string} scriptsig The Scriptsig.
* @param {string} hashType The hash type.
* @param {boolean} isElements The elements flag.
* @return {cfdjs.AddMultisigSignResponse} The multisig sign response.
*/
exports.SetMultisigScriptSig = function(
txHex, txid, vout, scriptsig, hashType, isElements = true) {
const scriptResult = cfdjs.ParseScript({
script: scriptsig,
});
const scriptsigItems = scriptResult.scriptItems;
let redeemScript = '';
let witnessScript = '';
if (hashType === 'p2sh') {
redeemScript = scriptsigItems[scriptsigItems.length - 1];
} else if (hashType === 'p2wsh') {
witnessScript = scriptsigItems[scriptsigItems.length - 1];
} else if (hashType === 'p2sh-p2wsh') {
witnessScript = scriptsigItems[scriptsigItems.length - 1];
const addrResult = cfdjs.CreateAddress({
'keyData': {
hex: witnessScript,
type: 'redeem_script',
},
'network': (isElements) ? 'liquidv1' : 'mainnet',
'isElements': isElements,
'hashType': hashType,
});
redeemScript = addrResult.redeemScript;
} else {
throw Error(`ERROR: Invalid argument hashType(${hashType}).`);
}
const signParams = [];
for (let i = 1; i < scriptsigItems.length - 1; ++i) {
const signature = scriptsigItems[i];
signParams.push({
hex: signature,
type: 'sign',
derEncode: false,
});
}
return cfdjs.AddMultisigSign({
tx: txHex,
txin: {
txid: txid,
vout: vout,
isWitness: (hashType !== 'p2sh'),
signParams: signParams,
redeemScript: redeemScript,
witnessScript: witnessScript,
hashType: hashType,
},
});
};