|
1 | 1 | import * as assert from 'assert';
|
2 | 2 | import { describe, it } from 'mocha';
|
3 | 3 |
|
4 |
| -import { bip32, ECPair, networks as NETWORKS, Psbt } from '..'; |
| 4 | +import { bip32, ECPair, networks as NETWORKS, Psbt, payments } from '..'; |
5 | 5 |
|
6 | 6 | import * as preFixtures from './fixtures/psbt.json';
|
7 | 7 |
|
@@ -542,6 +542,143 @@ describe(`Psbt`, () => {
|
542 | 542 | });
|
543 | 543 | });
|
544 | 544 |
|
| 545 | + describe('inputHasPubkey', () => { |
| 546 | + it('should throw', () => { |
| 547 | + const psbt = new Psbt(); |
| 548 | + psbt.addInput({ |
| 549 | + hash: |
| 550 | + '0000000000000000000000000000000000000000000000000000000000000000', |
| 551 | + index: 0, |
| 552 | + }); |
| 553 | + |
| 554 | + assert.throws(() => { |
| 555 | + psbt.inputHasPubkey(0, Buffer.from([])); |
| 556 | + }, new RegExp("Can't find pubkey in input without Utxo data")); |
| 557 | + |
| 558 | + psbt.updateInput(0, { |
| 559 | + witnessUtxo: { |
| 560 | + value: 1337, |
| 561 | + script: payments.p2sh({ |
| 562 | + redeem: { output: Buffer.from([0x51]) }, |
| 563 | + }).output!, |
| 564 | + }, |
| 565 | + }); |
| 566 | + |
| 567 | + assert.throws(() => { |
| 568 | + psbt.inputHasPubkey(0, Buffer.from([])); |
| 569 | + }, new RegExp('Incomplete script information')); |
| 570 | + |
| 571 | + delete psbt.data.inputs[0].witnessUtxo; |
| 572 | + |
| 573 | + psbt.updateInput(0, { |
| 574 | + witnessUtxo: { |
| 575 | + value: 1337, |
| 576 | + script: payments.p2wsh({ |
| 577 | + redeem: { output: Buffer.from([0x51]) }, |
| 578 | + }).output!, |
| 579 | + }, |
| 580 | + }); |
| 581 | + |
| 582 | + assert.throws(() => { |
| 583 | + psbt.inputHasPubkey(0, Buffer.from([])); |
| 584 | + }, new RegExp('Incomplete script information')); |
| 585 | + |
| 586 | + delete psbt.data.inputs[0].witnessUtxo; |
| 587 | + |
| 588 | + psbt.updateInput(0, { |
| 589 | + witnessUtxo: { |
| 590 | + value: 1337, |
| 591 | + script: payments.p2sh({ |
| 592 | + redeem: payments.p2wsh({ |
| 593 | + redeem: { output: Buffer.from([0x51]) }, |
| 594 | + }), |
| 595 | + }).output!, |
| 596 | + }, |
| 597 | + redeemScript: payments.p2wsh({ |
| 598 | + redeem: { output: Buffer.from([0x51]) }, |
| 599 | + }).output!, |
| 600 | + }); |
| 601 | + |
| 602 | + assert.throws(() => { |
| 603 | + psbt.inputHasPubkey(0, Buffer.from([])); |
| 604 | + }, new RegExp('Incomplete script information')); |
| 605 | + |
| 606 | + psbt.updateInput(0, { |
| 607 | + witnessScript: Buffer.from([0x51]), |
| 608 | + }); |
| 609 | + |
| 610 | + assert.doesNotThrow(() => { |
| 611 | + psbt.inputHasPubkey(0, Buffer.from([0x51])); |
| 612 | + }); |
| 613 | + }); |
| 614 | + }); |
| 615 | + |
| 616 | + describe('outputHasPubkey', () => { |
| 617 | + it('should throw', () => { |
| 618 | + const psbt = new Psbt(); |
| 619 | + psbt |
| 620 | + .addInput({ |
| 621 | + hash: |
| 622 | + '0000000000000000000000000000000000000000000000000000000000000000', |
| 623 | + index: 0, |
| 624 | + }) |
| 625 | + .addOutput({ |
| 626 | + script: payments.p2sh({ |
| 627 | + redeem: { output: Buffer.from([0x51]) }, |
| 628 | + }).output!, |
| 629 | + value: 1337, |
| 630 | + }); |
| 631 | + |
| 632 | + assert.throws(() => { |
| 633 | + psbt.outputHasPubkey(0, Buffer.from([])); |
| 634 | + }, new RegExp('Incomplete script information')); |
| 635 | + |
| 636 | + (psbt as any).__CACHE.__TX.outs[0].script = payments.p2wsh({ |
| 637 | + redeem: { output: Buffer.from([0x51]) }, |
| 638 | + }).output!; |
| 639 | + |
| 640 | + assert.throws(() => { |
| 641 | + psbt.outputHasPubkey(0, Buffer.from([])); |
| 642 | + }, new RegExp('Incomplete script information')); |
| 643 | + |
| 644 | + (psbt as any).__CACHE.__TX.outs[0].script = payments.p2sh({ |
| 645 | + redeem: payments.p2wsh({ |
| 646 | + redeem: { output: Buffer.from([0x51]) }, |
| 647 | + }), |
| 648 | + }).output!; |
| 649 | + |
| 650 | + psbt.updateOutput(0, { |
| 651 | + redeemScript: payments.p2wsh({ |
| 652 | + redeem: { output: Buffer.from([0x51]) }, |
| 653 | + }).output!, |
| 654 | + }); |
| 655 | + |
| 656 | + assert.throws(() => { |
| 657 | + psbt.outputHasPubkey(0, Buffer.from([])); |
| 658 | + }, new RegExp('Incomplete script information')); |
| 659 | + |
| 660 | + delete psbt.data.outputs[0].redeemScript; |
| 661 | + |
| 662 | + psbt.updateOutput(0, { |
| 663 | + witnessScript: Buffer.from([0x51]), |
| 664 | + }); |
| 665 | + |
| 666 | + assert.throws(() => { |
| 667 | + psbt.outputHasPubkey(0, Buffer.from([])); |
| 668 | + }, new RegExp('Incomplete script information')); |
| 669 | + |
| 670 | + psbt.updateOutput(0, { |
| 671 | + redeemScript: payments.p2wsh({ |
| 672 | + redeem: { output: Buffer.from([0x51]) }, |
| 673 | + }).output!, |
| 674 | + }); |
| 675 | + |
| 676 | + assert.doesNotThrow(() => { |
| 677 | + psbt.outputHasPubkey(0, Buffer.from([0x51])); |
| 678 | + }); |
| 679 | + }); |
| 680 | + }); |
| 681 | + |
545 | 682 | describe('clone', () => {
|
546 | 683 | it('Should clone a psbt exactly with no reference', () => {
|
547 | 684 | const f = fixtures.clone;
|
@@ -643,6 +780,8 @@ describe(`Psbt`, () => {
|
643 | 780 | assert.throws(() => {
|
644 | 781 | psbt.setVersion(3);
|
645 | 782 | }, new RegExp('Can not modify transaction, signatures exist.'));
|
| 783 | + assert.strictEqual(psbt.inputHasPubkey(0, alice.publicKey), true); |
| 784 | + assert.strictEqual(psbt.outputHasPubkey(0, alice.publicKey), false); |
646 | 785 | assert.strictEqual(
|
647 | 786 | psbt.extractTransaction().toHex(),
|
648 | 787 | '02000000013ebc8203037dda39d482bf41ff3be955996c50d9d4f7cfc3d2097a694a7' +
|
|
0 commit comments