-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcreate.js
650 lines (558 loc) · 19.9 KB
/
create.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
let bitcoin = require("bitcoinjs-lib");
bitcoin.bigi = require('bigi');
bitcoin.Buffer = require('safe-buffer').Buffer;
let bip38 = require('bip38');
bip38.wifEnc = require('wif');
let bip65 = require('bip65');
let bip39 = require('bip39');
let b58 = require('bs58check');
function createP2PKH(networkInput){
let NETWORK = networkInput === "testnet" ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
let wif = bitcoin.ECPair.makeRandom({network: NETWORK}).toWIF();
let keyPair = bitcoin.ECPair.fromWIF(wif, NETWORK);
let newaddy = keyPair.getAddress();
return {
pk: wif,
addr: newaddy
};
}
function createP2WPKH(networkInput){
let NETWORK = networkInput === "testnet" ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
let wif = bitcoin.ECPair.makeRandom({network: NETWORK}).toWIF();
let keyPair = bitcoin.ECPair.fromWIF(wif, NETWORK);
let pubKey = keyPair.getPublicKeyBuffer();
let scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey));
let newaddy = bitcoin.address.fromOutputScript(scriptPubKey, NETWORK);
return {
pk: wif,
addr: newaddy
};
}
function createP2SHP2WPKH(networkInput){
let NETWORK = networkInput === "testnet" ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
let wif = bitcoin.ECPair.makeRandom({network: NETWORK}).toWIF();
let keyPair = bitcoin.ECPair.fromWIF(wif, NETWORK);
let pubKey = keyPair.getPublicKeyBuffer();
let pubKeyHash = bitcoin.crypto.hash160(pubKey);
let redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash);
let redeemScriptHash = bitcoin.crypto.hash160(redeemScript);
let scriptPubKey = bitcoin.script.scriptHash.output.encode(redeemScriptHash);
let newaddy = bitcoin.address.fromOutputScript(scriptPubKey, NETWORK);
return {
pk: wif,
addr: newaddy
};
}
function getNewAddress(networkInput){
let NETWORK = networkInput === "testnet" ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
let wif = bitcoin.ECPair.makeRandom({network: NETWORK}).toWIF();
let keyPair = bitcoin.ECPair.fromWIF(wif, NETWORK);
//p2pkh
let p2pkhAddr = keyPair.getAddress();
//native witness
let pubKey = keyPair.getPublicKeyBuffer();
let scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey));
let p2wpkhAddr = bitcoin.address.fromOutputScript(scriptPubKey, NETWORK);
//p2sh witness
let pubKeyHash = bitcoin.crypto.hash160(pubKey);
let redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash);
let redeemScriptHex = redeemScript.toString('hex');
let redeemScriptHash = bitcoin.crypto.hash160(redeemScript);
let scriptPubKey2 = bitcoin.script.scriptHash.output.encode(redeemScriptHash);
let p2shp2wpkhAddr = bitcoin.address.fromOutputScript(scriptPubKey2, NETWORK);
return {
pk: wif,
p2pkh: p2pkhAddr,
p2wpkh: p2wpkhAddr,
p2shp2wpkh: p2shp2wpkhAddr,
redeemScript: redeemScriptHex
};
}
function bip38Encrypt(key, phrase, networkInput){
let NETWORK = networkInput === "testnet" ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
let keyPair = bitcoin.ECPair.fromWIF(key, NETWORK);
//p2pkh
let p2pkhAddr = keyPair.getAddress();
//native witness
let pubKey = keyPair.getPublicKeyBuffer();
let scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey));
let p2wpkhAddr = bitcoin.address.fromOutputScript(scriptPubKey, NETWORK);
//p2sh witness
let pubKeyHash = bitcoin.crypto.hash160(pubKey);
let redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash);
let redeemScriptHex = redeemScript.toString('hex');
let redeemScriptHash = bitcoin.crypto.hash160(redeemScript);
let scriptPubKey2 = bitcoin.script.scriptHash.output.encode(redeemScriptHash);
let p2shp2wpkhAddr = bitcoin.address.fromOutputScript(scriptPubKey2, NETWORK);
if(phrase != null){
let decoded = bip38.wifEnc.decode(key);
let encryptedKey = bip38.encrypt(decoded.privateKey, decoded.compressed, phrase);
return {
pk: encryptedKey,
p2pkh: p2pkhAddr,
p2wpkh: p2wpkhAddr,
p2shp2wpkh: p2shp2wpkhAddr,
redeemScript: redeemScriptHex
};
} else {
let encryptedKey = "missing passphrase parameter";
return {
pk: encryptedKey,
p2pkh: p2pkhAddr,
p2wpkh: p2wpkhAddr,
p2shp2wpkh: p2shp2wpkhAddr,
redeemScript: redeemScriptHex
};
}
}
function bip38Decrypt(encryptedKey, phrase){
let decryptedKey = bip38.decrypt(encryptedKey, phrase);
let decryptFinish = bip38.wifEnc.encode(0x80, decryptedKey.privateKey, decryptedKey.compressed);
return {
pk: decryptFinish
};
}
function getDetails(inputWIF, networkInput){
let NETWORK = networkInput === "testnet" ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
let wif = inputWIF;
let keyPair = bitcoin.ECPair.fromWIF(wif, NETWORK);
//p2pkh
let p2pkhAddr = keyPair.getAddress();
//native witness
let pubKey = keyPair.getPublicKeyBuffer();
let pubKeyHex = pubKey.toString('hex');
let scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey));
let p2wpkhAddr = bitcoin.address.fromOutputScript(scriptPubKey, NETWORK);
//p2sh witness
let pubKeyHash = bitcoin.crypto.hash160(pubKey);
let redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash);
let redeemScriptHex = redeemScript.toString('hex');
let redeemScriptHash = bitcoin.crypto.hash160(redeemScript);
let scriptPubKey2 = bitcoin.script.scriptHash.output.encode(redeemScriptHash);
let p2shp2wpkhAddr = bitcoin.address.fromOutputScript(scriptPubKey2, NETWORK);
return {
pk: wif,
p2pkh: p2pkhAddr,
p2wpkh: p2wpkhAddr,
p2shp2wpkh: p2shp2wpkhAddr,
redeemScript: redeemScriptHex,
publicKey: pubKeyHex
};
}
function pubToAddress(publicKey, networkInput){
let NETWORK = networkInput === "testnet" ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
let pubKeyBuffer = new Buffer(publicKey, 'hex');
let publicKeyHash = bitcoin.crypto.hash160(pubKeyBuffer);
//p2pkh
let address = bitcoin.address.toBase58Check(publicKeyHash, NETWORK.pubKeyHash);
//p2wpkh
let scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(publicKeyHash));
let p2wpkhAddr = bitcoin.address.fromOutputScript(scriptPubKey, NETWORK);
//p2sh-p2wpkh
let redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(publicKeyHash);
let redeemScriptHex = redeemScript.toString('hex');
let redeemScriptHash = bitcoin.crypto.hash160(redeemScript);
let scriptPubKey2 = bitcoin.script.scriptHash.output.encode(redeemScriptHash);
let p2shp2wpkhAddr = bitcoin.address.fromOutputScript(scriptPubKey2, NETWORK);
return {
p2pkh: address,
p2wpkh: p2wpkhAddr,
p2shp2wpkh: p2shp2wpkhAddr,
redeemScript: redeemScriptHex
}
}
function validateAddress(address){
try {
bitcoin.address.toOutputScript(address)
return true
} catch (e) {
return false
}
}
function sha256(shaInput, iterations){
z = shaInput;
for(i=0;i<iterations;i++){
let data = z;
z = bitcoin.crypto.sha256(data).toString('hex');
}
return z;
}
function createTransaction(typei, txidi, outni, outputi, amounti, wifi, changeout, changeamt, inputvalue, networkInput){
let NETWORK = networkInput === "testnet" ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
//typei indicate input type by using 1st character of address type spending from
//examples
//createTransaction("1", "34eceJ... > spends from p2pkh
//createTransaction("3", "34eceJ... > spending from p2sh-p2wpkh
//createTransaction("b", "34eceJ... > spending from p2wpk(bech32)
//
//spending from segwit addresses (p2sh or native) you must specify inputvalue, this is the full value of the unspent output being spent which becomes the input in the new transaction
//examples
//createTransaction("3", "34eceJ..", 0, "1P5Ef7FsaD1KsJNSTUcACceEWN9vsUe3eN", 350000, "L1RLQhjuGoQ37QP4jfaEFTHMuEUeh4JdUDkx32xeafhnpzRMDMXD", null, null, 4000000)
if(typei=="1"){
//legacy address starts with a 1
//create transaction
let txb = new bitcoin.TransactionBuilder(NETWORK);
let txid = txidi;
let outn = outni;
//input
txb.addInput(txid, outn);
//output
txb.addOutput(outputi, amounti);
//check for change output
if(validateAddress(changeout)){
txb.addOutput(changeout, changeamt);
}
//sign transaction
let WIF = wifi;
let keypairSpend = bitcoin.ECPair.fromWIF(WIF, NETWORK);
txb.sign(0,keypairSpend);
//buidl transaction
let tx = txb.build();
let txhex = tx.toHex();
return {
signedtx: txhex
}
} else if(typei=="3"){
//p2sh segwit, starts with a 3
//create transaction
//create transaction
let txb = new bitcoin.TransactionBuilder(NETWORK);
let txid = txidi;
let outn = outni;
//need scriptPubKey for adding input
let WIF = wifi; //private key of p2sh-p2wpkh output
let keypair = bitcoin.ECPair.fromWIF(WIF, NETWORK);
let pubKey = keypair.getPublicKeyBuffer();
let pubKeyHash = bitcoin.crypto.hash160(pubKey);
let redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash);
let redeemScriptHash = bitcoin.crypto.hash160(redeemScript);
let scriptPubkey = bitcoin.script.scriptHash.output.encode(redeemScriptHash);
//add input
txb.addInput(txid, outn, null, scriptPubkey);
//output
txb.addOutput(outputi, amounti);
//check for change output
if(validateAddress(changeout)){
txb.addOutput(changeout, changeamt);
}
//sign transaction
txb.sign(0, keypair, redeemScript, null, inputvalue);
//buidl transaction
let tx = txb.build();
let txhex = tx.toHex();
return {
signedtx: txhex
}
} else if(typei=="b"){
//bech32 native segwit
//create transaction
//create transaction
let txb = new bitcoin.TransactionBuilder(NETWORK);
let txid = txidi;
let outn = outni;
//need scriptPubKey for adding input
let WIF = wifi; //private key of p2sh-p2wpkh output
let keypair = bitcoin.ECPair.fromWIF(WIF, NETWORK);
let scriptPubkey = bitcoin.script.witnessPubKeyHash.output.encode(
bitcoin.crypto.hash160(
keypair.getPublicKeyBuffer()
)
);
//add input
txb.addInput(txid, outn, null, scriptPubkey);
//output
txb.addOutput(outputi, amounti);
//check for change output
if(validateAddress(changeout)){
txb.addOutput(changeout, changeamt);
}
//sign transaction
txb.sign(0, keypair, null, null, inputvalue);
//buidl transaction
let tx = txb.build();
let txhex = tx.toHex();
return {
signedtx: txhex
}
} else {
return {
signedtx: "invalid type 1st argument must be string '1','3',or'b' depending on address type spending from"
}
}
}
function createFrom(srcInput, networkInput){
let NETWORK = networkInput === "testnet" ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
let hashInput = srcInput;
let hash = bitcoin.crypto.sha256(bitcoin.Buffer.from(hashInput));
let d = bitcoin.bigi.fromBuffer(hash);
let keyPair = new bitcoin.ECPair(d);
//p2pkh
let wif = keyPair.toWIF();
let p2pkhAddr = keyPair.getAddress();
//native witness
let pubKey = keyPair.getPublicKeyBuffer();
let pubKeyHex = pubKey.toString('hex');
let scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey));
let p2wpkhAddr = bitcoin.address.fromOutputScript(scriptPubKey, NETWORK);
//p2sh witness
let pubKeyHash = bitcoin.crypto.hash160(pubKey);
let redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash);
let redeemScriptHex = redeemScript.toString('hex');
let redeemScriptHash = bitcoin.crypto.hash160(redeemScript);
let scriptPubKey2 = bitcoin.script.scriptHash.output.encode(redeemScriptHash);
let p2shp2wpkhAddr = bitcoin.address.fromOutputScript(scriptPubKey2, NETWORK);
return {
pk: wif,
p2pkh: p2pkhAddr,
p2wpkh: p2wpkhAddr,
p2shp2wpkh: p2shp2wpkhAddr,
redeemScript: redeemScriptHex,
publicKey: pubKeyHex
};
}
function fromXpub(xpub, acctNumber, keyindex){
var type = xpub.substring(0,1);
if(type==="x"){
var addr = bitcoin.HDNode.fromBase58(xpub).derivePath(acctNumber+"/"+keyindex).getAddress();
} else if(type==="y"){
var ypubconv = convertXpub(xpub,"xpub");
var wallet = bitcoin.HDNode.fromBase58(ypubconv).derivePath(acctNumber+"/"+keyindex);
//segwit p2sh
var pubKey = wallet.keyPair.getPublicKeyBuffer();
var pubKeyHash = bitcoin.crypto.hash160(pubKey);
var redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash);
var redeemScriptHash = bitcoin.crypto.hash160(redeemScript);
var scriptPubKey2 = bitcoin.script.scriptHash.output.encode(redeemScriptHash);
var addr = bitcoin.address.fromOutputScript(scriptPubKey2);
} else if(type==="z"){
var zpubconv = convertXpub(xpub,"xpub");
var wallet = bitcoin.HDNode.fromBase58(zpubconv).derivePath(acctNumber+"/"+keyindex);
//native witness
var pubKey = wallet.keyPair.getPublicKeyBuffer();
var pubKeyHex = pubKey.toString('hex');
var scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey));
var addr = bitcoin.address.fromOutputScript(scriptPubKey);
}
return{
addr
};
}
function xprvToWIF(xprv, change, index){
var inputExt = convertXpub(xprv,"xprv");
let wif = bitcoin.HDNode.fromBase58(inputExt).derivePath(change+"/"+index).keyPair.toWIF();
return{
wif
}
}
function fromHDSeed(seed, deriv, account, change, index){
let root = bitcoin.HDNode.fromSeedHex(seed);
let acct = root.derivePath("m/"+deriv+"'/0'/"+account+"'");
let xpub = acct.neutered().toBase58();
let pair = acct.derivePath(change+"/"+index).keyPair;
var wifkey = pair.toWIF();
if(deriv===44){
var address = pair.getAddress();
} else if(deriv===49){
var pubKey = pair.getPublicKeyBuffer();
var pubKeyHash = bitcoin.crypto.hash160(pubKey);
var redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash);
var redeemScriptHash = bitcoin.crypto.hash160(redeemScript);
var scriptPubKey2 = bitcoin.script.scriptHash.output.encode(redeemScriptHash);
var address = bitcoin.address.fromOutputScript(scriptPubKey2);
} else if(deriv===84){
var pubKey = pair.getPublicKeyBuffer();
var pubKeyHex = pubKey.toString('hex');
var scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey));
var address = bitcoin.address.fromOutputScript(scriptPubKey);
}
return{
addr: address,
pk: wifkey
}
}
function seedToXpub(seed, deriv, account){
let root = bitcoin.HDNode.fromSeedHex(seed);
let acct = root.derivePath("m/"+deriv+"'/0'/"+account+"'");
var xpub = acct.neutered().toBase58();
var outpub = xpub;
if(deriv==84){
var zpub = convertXpub(xpub,"zpub");
outpub = zpub;
} else if (deriv==49)
{
var ypub = convertXpub(xpub,"ypub");
outpub = ypub;
}
return outpub;
}
function seedToXprv(seed, deriv, account){
var root = bitcoin.HDNode.fromSeedHex(seed);
var acct = root.derivePath("m/"+deriv+"'/0'/"+account+"'");
var xprv = acct.toBase58();
var outprv = xprv;
if(deriv==84){
var zprv = convertXpub(xprv,"zprv");
outprv = zprv;
} else if (deriv==49)
{
var yprv = convertXpub(xprv,"yprv");
outprv = yprv;
}
return outprv;
}
function convertXpub(xpub,target){
//source script https://github.com/jlopp/xpub-converter/blob/master/js/xpubConvert.js
const prefixes = new Map(
[
['xpub', '0488b21e'],
['ypub', '049d7cb2'],
['Ypub', '0295b43f'],
['zpub', '04b24746'],
['Zpub', '02aa7ed3'],
['tpub', '043587cf'],
['upub', '044a5262'],
['Upub', '024289ef'],
['vpub', '045f1cf6'],
['Vpub', '02575483'],
['xprv', '0488ade4'],
['yprv', '049d7878'],
['zprv', '04b2430c']
]
);
xpub = xpub.trim();
try {
var data = b58.decode(xpub);
data = data.slice(4);
data = Buffer.concat([Buffer.from(prefixes.get(target),'hex'), data]);
return b58.encode(data);
} catch (err) {
return "Invalid extended public key! Please double check that you didn't accidentally paste extra data.";
}
}
function newMnemonic(){
let words = bip39.generateMnemonic();
return{
words
}
}
function verifyMnemonic(phrase){
let validMnemonic = bip39.validateMnemonic(phrase);
return{
validMnemonic
}
}
function mnemonic2SeedHex(mnemonicInput){
seedHex = bip39.mnemonicToSeedSync(mnemonicInput).toString('hex')
return{
seedHex
}
}
function entropy2Mnemonic(entropySrc){
words = bip39.entropyToMnemonic(entropySrc);
return{
words
}
}
function multisig(pubKey1, pubKey2, pubKey3, networkInput){
let NETWORK = networkInput === "testnet" ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
let pubKeys = [
pubKey1,
pubKey2,
pubKey3
].map(function (hex) { return Buffer.from(hex, 'hex') })
let redeemScript = bitcoin.script.multisig.output.encode(2, pubKeys); // 2 of 3
let scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript, NETWORK));
let address = bitcoin.address.fromOutputScript(scriptPubKey, NETWORK);
let redeemHex = redeemScript.toString('hex');
return{
addr: address,
redeemScript: redeemHex
}
}
function multisigRandom(m,n,networkInput){
let NETWORK = networkInput === "testnet" ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
if(m>n){
return "your first parameter must be less than your second parameter";
}
let wifList = [];
let pubList = [];
for(var i=0;i<n;i++){
let wif = bitcoin.ECPair.makeRandom({network: NETWORK}).toWIF();
wifList.push(wif);
let keyPair = bitcoin.ECPair.fromWIF(wif, NETWORK);
let pubKey = keyPair.getPublicKeyBuffer();
let pubKeyHex = pubKey.toString('hex');
pubList.push(pubKeyHex);
}
let pubKeys = pubList.map(function (hex) { return Buffer.from(hex, 'hex') })
let redeemScript = bitcoin.script.multisig.output.encode(m, pubKeys); // 2 of 3
let scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript, NETWORK));
let address = bitcoin.address.fromOutputScript(scriptPubKey, NETWORK);
let redeemHex = redeemScript.toString('hex');
let wifListToString = wifList.join();
return{
addr: address,
redeemScript: redeemHex,
privateKeys: wifListToString
}
}
function cltv(privateKey, locktime, networkInput){
let NETWORK = networkInput === "testnet" ? bitcoin.networks.testnet : bitcoin.networks.bitcoin;
function cltvCheckSigOutput (ecPair, lockTimeInput) {
return bitcoin.script.compile([
bitcoin.opcodes.OP_IF,
bitcoin.script.number.encode(lockTimeInput),
bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY,
bitcoin.opcodes.OP_DROP,
bitcoin.opcodes.OP_ELSE,
bitcoin.opcodes.OP_NOP,
bitcoin.opcodes.OP_ENDIF,
ecPair.publicKey,
bitcoin.opcodes.OP_CHECKSIG
])
}
function utcNow () {
return Math.floor(Date.now() / 1000)
}
let ecPairInput = bitcoin.ECPair.fromWIF(privateKey, NETWORK);
let lockTimeInput = bip65.encode({ utc: utcNow() + locktime });
let redeemScript = cltvCheckSigOutput(ecPairInput, lockTimeInput);
let redeemScriptHash160 = bitcoin.crypto.hash160(redeemScript);
let redeemScriptHex = redeemScript.toString("hex");
let scriptPubKey = bitcoin.script.scriptHash.output.encode(redeemScriptHash160);
let address = bitcoin.address.fromOutputScript(scriptPubKey, NETWORK);
return {
addr: address,
redeemScript: redeemScriptHex
}
}
module.exports = {
createP2PKH,
createP2WPKH,
createP2SHP2WPKH,
getNewAddress,
getDetails,
validateAddress,
createTransaction,
createFrom,
bip38Encrypt,
bip38Decrypt,
fromXpub,
xprvToWIF,
fromHDSeed,
seedToXpub,
seedToXprv,
convertXpub,
newMnemonic,
verifyMnemonic,
mnemonic2SeedHex,
entropy2Mnemonic,
multisig,
multisigRandom,
cltv,
pubToAddress,
sha256,
bitcoin
}
//binding functions to buidl
//browserify create.js --standalone buidl > buidl.js