diff --git a/test/psbt.js b/test/psbt.js index ecb78ab..cd45c1e 100644 --- a/test/psbt.js +++ b/test/psbt.js @@ -154,6 +154,20 @@ describe(`Psbt`, () => { psbt.getFeeRate() }, new RegExp('PSBT must be finalized to calculate fee rate')) + const pubkey = Buffer.from( + '029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f', + 'hex', + ) + assert.strictEqual(psbt.validateSignatures(0), true) + assert.strictEqual(psbt.validateSignatures(0, pubkey), true) + assert.throws(() => { + pubkey[32] = 42 + psbt.validateSignatures(0, pubkey) + }, new RegExp('No signatures for this pubkey')) + assert.throws(() => { + psbt.validateSignatures(42) + }, new RegExp('No signatures to validate')) + psbt.finalizeAllInputs() assert.strictEqual(psbt.toBase64(), f.result) @@ -246,6 +260,54 @@ describe(`Psbt`, () => { }) }) + describe('signAsync', () => { + fixtures.signInput.checks.forEach(f => { + if (f.description === 'checks the input exists') return + it(f.description, async () => { + const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt) + assert.doesNotReject(async () => { + await psbtThatShouldsign.signAsync( + ECPair.fromWIF(f.shouldSign.WIF), + ) + }) + + const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt) + assert.rejects(async () => { + await psbtThatShouldThrow.signAsync( + ECPair.fromWIF(f.shouldThrow.WIF), + ) + }, new RegExp('No inputs were signed')) + assert.rejects(async () => { + await psbtThatShouldThrow.signAsync() + }, new RegExp('Need Signer to sign input')) + }) + }) + }) + + describe('sign', () => { + fixtures.signInput.checks.forEach(f => { + if (f.description === 'checks the input exists') return + it(f.description, () => { + const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt) + assert.doesNotThrow(() => { + psbtThatShouldsign.sign( + ECPair.fromWIF(f.shouldSign.WIF), + ) + }) + + const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt) + assert.throws(() => { + psbtThatShouldThrow.sign( + ECPair.fromWIF(f.shouldThrow.WIF), + ) + }, new RegExp('No inputs were signed')) + assert.throws(() => { + psbtThatShouldThrow.sign() + }, new RegExp('Need Signer to sign input')) + }) + }) + }) + describe('fromTransaction', () => { fixtures.fromTransaction.forEach(f => { it('Creates the expected PSBT from a transaction buffer', () => { @@ -363,6 +425,44 @@ describe(`Psbt`, () => { }) }) + describe('create 1-to-1 transaction', () => { + const alice = ECPair.fromWIF('L2uPYXe17xSTqbCjZvL2DsyXPCbXspvcu5mHLDYUgzdUbZGSKrSr') + const psbt = new Psbt() + psbt.addInput({ + hash: '7d067b4a697a09d2c3cff7d4d9506c9955e93bff41bf82d439da7d030382bc3e', + index: 0, + nonWitnessUtxo: Buffer.from( + '0200000001f9f34e95b9d5c8abcd20fc5bd4a825d1517be62f0f775e5f36da944d9' + + '452e550000000006b483045022100c86e9a111afc90f64b4904bd609e9eaed80d48' + + 'ca17c162b1aca0a788ac3526f002207bb79b60d4fc6526329bf18a77135dc566020' + + '9e761da46e1c2f1152ec013215801210211755115eabf846720f5cb18f248666fec' + + '631e5e1e66009ce3710ceea5b1ad13ffffffff01905f0100000000001976a9148bb' + + 'c95d2709c71607c60ee3f097c1217482f518d88ac00000000', + 'hex', + ), + sighashType: 1, + }) + psbt.addOutput({ + address: '1KRMKfeZcmosxALVYESdPNez1AP1mEtywp', + value: 80000 + }) + psbt.signInput(0, alice) + assert.throws(() => { + psbt.setVersion(3) + }, new RegExp('Can not modify transaction, signatures exist.')) + psbt.validateSignatures(0) + psbt.finalizeAllInputs() + assert.strictEqual( + psbt.extractTransaction().toHex(), + '02000000013ebc8203037dda39d482bf41ff3be955996c50d9d4f7cfc3d2097a694a7' + + 'b067d000000006b483045022100931b6db94aed25d5486884d83fc37160f37f3368c0' + + 'd7f48c757112abefec983802205fda64cff98c849577026eb2ce916a50ea70626a766' + + '9f8596dd89b720a26b4d501210365db9da3f8a260078a7e8f8b708a1161468fb2323f' + + 'fda5ec16b261ec1056f455ffffffff0180380100000000001976a914ca0d36044e0dc' + + '08a22724efa6f6a07b0ec4c79aa88ac00000000', + ) + }) + describe('Method return types', () => { it('fromTransaction returns Psbt type (not base class)', () => { const psbt = Psbt.fromTransaction(Buffer.from([2,0,0,0,0,0,0,0,0,0]));