@@ -282,6 +282,50 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
282
282
} ) ;
283
283
} ) ;
284
284
285
+ it ( 'can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2WPKH) input with nonWitnessUtxo' , async ( ) => {
286
+ const p2sh = createPayment ( 'p2sh-p2wpkh' ) ;
287
+ const inputData = await getInputData ( 5e4 , p2sh . payment , false , 'p2sh' ) ;
288
+ const inputData2 = await getInputData ( 5e4 , p2sh . payment , false , 'p2sh' ) ;
289
+ {
290
+ const {
291
+ hash,
292
+ index,
293
+ nonWitnessUtxo,
294
+ redeemScript,
295
+ } = inputData ;
296
+ assert . deepStrictEqual (
297
+ { hash, index, nonWitnessUtxo, redeemScript } ,
298
+ inputData ,
299
+ ) ;
300
+ }
301
+ const keyPair = p2sh . keys [ 0 ] ;
302
+ const outputData = {
303
+ script : p2sh . payment . output , // sending to myself for fun
304
+ value : 2e4 ,
305
+ } ;
306
+ const outputData2 = {
307
+ script : p2sh . payment . output , // sending to myself for fun
308
+ value : 7e4 ,
309
+ } ;
310
+
311
+ const tx = new bitcoin . Psbt ( )
312
+ . addInputs ( [ inputData , inputData2 ] )
313
+ . addOutputs ( [ outputData , outputData2 ] )
314
+ . signAllInputs ( keyPair )
315
+ . finalizeAllInputs ( )
316
+ . extractTransaction ( ) ;
317
+
318
+ // build and broadcast to the Bitcoin RegTest network
319
+ await regtestUtils . broadcast ( tx . toHex ( ) ) ;
320
+
321
+ await regtestUtils . verify ( {
322
+ txId : tx . getId ( ) ,
323
+ address : p2sh . payment . address ,
324
+ vout : 0 ,
325
+ value : 2e4 ,
326
+ } ) ;
327
+ } ) ;
328
+
285
329
it ( 'can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input' , async ( ) => {
286
330
// the only thing that changes is you don't give a redeemscript for input data
287
331
@@ -316,6 +360,40 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
316
360
} ) ;
317
361
} ) ;
318
362
363
+ it ( 'can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input with nonWitnessUtxo' , async ( ) => {
364
+ // the only thing that changes is you don't give a redeemscript for input data
365
+
366
+ const p2wpkh = createPayment ( 'p2wpkh' ) ;
367
+ const inputData = await getInputData ( 5e4 , p2wpkh . payment , false , 'noredeem' ) ;
368
+ {
369
+ const { hash, index, nonWitnessUtxo } = inputData ;
370
+ assert . deepStrictEqual ( { hash, index, nonWitnessUtxo } , inputData ) ;
371
+ }
372
+
373
+ const psbt = new bitcoin . Psbt ( { network : regtest } )
374
+ . addInput ( inputData )
375
+ . addOutput ( {
376
+ address : regtestUtils . RANDOM_ADDRESS ,
377
+ value : 2e4 ,
378
+ } )
379
+ . signInput ( 0 , p2wpkh . keys [ 0 ] ) ;
380
+
381
+ assert . strictEqual ( psbt . validateSignaturesOfInput ( 0 ) , true ) ;
382
+ psbt . finalizeAllInputs ( ) ;
383
+
384
+ const tx = psbt . extractTransaction ( ) ;
385
+
386
+ // build and broadcast to the Bitcoin RegTest network
387
+ await regtestUtils . broadcast ( tx . toHex ( ) ) ;
388
+
389
+ await regtestUtils . verify ( {
390
+ txId : tx . getId ( ) ,
391
+ address : regtestUtils . RANDOM_ADDRESS ,
392
+ vout : 0 ,
393
+ value : 2e4 ,
394
+ } ) ;
395
+ } ) ;
396
+
319
397
it ( 'can create (and broadcast via 3PBP) a Transaction, w/ a P2WSH(P2PK) input' , async ( ) => {
320
398
const p2wsh = createPayment ( 'p2wsh-p2pk' ) ;
321
399
const inputData = await getInputData ( 5e4 , p2wsh . payment , true , 'p2wsh' ) ;
@@ -356,6 +434,46 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
356
434
} ) ;
357
435
} ) ;
358
436
437
+ it ( 'can create (and broadcast via 3PBP) a Transaction, w/ a P2WSH(P2PK) input with nonWitnessUtxo' , async ( ) => {
438
+ const p2wsh = createPayment ( 'p2wsh-p2pk' ) ;
439
+ const inputData = await getInputData ( 5e4 , p2wsh . payment , false , 'p2wsh' ) ;
440
+ {
441
+ const {
442
+ hash,
443
+ index,
444
+ nonWitnessUtxo,
445
+ witnessScript, // NEW: A Buffer of the witnessScript
446
+ } = inputData ;
447
+ assert . deepStrictEqual (
448
+ { hash, index, nonWitnessUtxo, witnessScript } ,
449
+ inputData ,
450
+ ) ;
451
+ }
452
+
453
+ const psbt = new bitcoin . Psbt ( { network : regtest } )
454
+ . addInput ( inputData )
455
+ . addOutput ( {
456
+ address : regtestUtils . RANDOM_ADDRESS ,
457
+ value : 2e4 ,
458
+ } )
459
+ . signInput ( 0 , p2wsh . keys [ 0 ] ) ;
460
+
461
+ assert . strictEqual ( psbt . validateSignaturesOfInput ( 0 ) , true ) ;
462
+ psbt . finalizeAllInputs ( ) ;
463
+
464
+ const tx = psbt . extractTransaction ( ) ;
465
+
466
+ // build and broadcast to the Bitcoin RegTest network
467
+ await regtestUtils . broadcast ( tx . toHex ( ) ) ;
468
+
469
+ await regtestUtils . verify ( {
470
+ txId : tx . getId ( ) ,
471
+ address : regtestUtils . RANDOM_ADDRESS ,
472
+ vout : 0 ,
473
+ value : 2e4 ,
474
+ } ) ;
475
+ } ) ;
476
+
359
477
it ( 'can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2WSH(P2MS(3 of 4))) (SegWit multisig) input' , async ( ) => {
360
478
const p2sh = createPayment ( 'p2sh-p2wsh-p2ms(3 of 4)' ) ;
361
479
const inputData = await getInputData ( 5e4 , p2sh . payment , true , 'p2sh-p2wsh' ) ;
@@ -406,6 +524,56 @@ describe('bitcoinjs-lib (transactions with psbt)', () => {
406
524
} ) ;
407
525
} ) ;
408
526
527
+ it ( 'can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2WSH(P2MS(3 of 4))) (SegWit multisig) input with nonWitnessUtxo' , async ( ) => {
528
+ const p2sh = createPayment ( 'p2sh-p2wsh-p2ms(3 of 4)' ) ;
529
+ const inputData = await getInputData ( 5e4 , p2sh . payment , false , 'p2sh-p2wsh' ) ;
530
+ {
531
+ const {
532
+ hash,
533
+ index,
534
+ nonWitnessUtxo,
535
+ redeemScript,
536
+ witnessScript,
537
+ } = inputData ;
538
+ assert . deepStrictEqual (
539
+ { hash, index, nonWitnessUtxo, redeemScript, witnessScript } ,
540
+ inputData ,
541
+ ) ;
542
+ }
543
+
544
+ const psbt = new bitcoin . Psbt ( { network : regtest } )
545
+ . addInput ( inputData )
546
+ . addOutput ( {
547
+ address : regtestUtils . RANDOM_ADDRESS ,
548
+ value : 2e4 ,
549
+ } )
550
+ . signInput ( 0 , p2sh . keys [ 0 ] )
551
+ . signInput ( 0 , p2sh . keys [ 2 ] )
552
+ . signInput ( 0 , p2sh . keys [ 3 ] ) ;
553
+
554
+ assert . strictEqual ( psbt . validateSignaturesOfInput ( 0 ) , true ) ;
555
+ assert . strictEqual (
556
+ psbt . validateSignaturesOfInput ( 0 , p2sh . keys [ 3 ] . publicKey ) ,
557
+ true ,
558
+ ) ;
559
+ assert . throws ( ( ) => {
560
+ psbt . validateSignaturesOfInput ( 0 , p2sh . keys [ 1 ] . publicKey ) ;
561
+ } , new RegExp ( 'No signatures for this pubkey' ) ) ;
562
+ psbt . finalizeAllInputs ( ) ;
563
+
564
+ const tx = psbt . extractTransaction ( ) ;
565
+
566
+ // build and broadcast to the Bitcoin RegTest network
567
+ await regtestUtils . broadcast ( tx . toHex ( ) ) ;
568
+
569
+ await regtestUtils . verify ( {
570
+ txId : tx . getId ( ) ,
571
+ address : regtestUtils . RANDOM_ADDRESS ,
572
+ vout : 0 ,
573
+ value : 2e4 ,
574
+ } ) ;
575
+ } ) ;
576
+
409
577
it ( 'can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input using HD' , async ( ) => {
410
578
const hdRoot = bip32 . fromSeed ( rng ( 64 ) ) ;
411
579
const masterFingerprint = hdRoot . fingerprint ;
0 commit comments