From f25938d3ca8abf8fcbac2074f81b629661bd1e03 Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Thu, 11 Jul 2019 16:24:35 +0700 Subject: [PATCH] Test signing a non-whitelisted sighashtype --- test/fixtures/psbt.json | 9 +++ test/psbt.js | 160 +++++++++++++++++++++++----------------- 2 files changed, 101 insertions(+), 68 deletions(-) diff --git a/test/fixtures/psbt.json b/test/fixtures/psbt.json index fbad821..1a37031 100644 --- a/test/fixtures/psbt.json +++ b/test/fixtures/psbt.json @@ -427,6 +427,15 @@ "inputToCheck": 0, "WIF": "KxnAnQh6UJBxLF8Weup77yn8tWhLHhDhnXeyJuzmmcZA5aRdMJni" } + }, + { + "description": "allows signing non-whitelisted sighashtype when explicitly passed in", + "shouldSign": { + "psbt": "cHNidP8BADMBAAAAAYaq+PdOUY2PnV9kZKa82XlqrPByOqwH2TRg2+LQdqs2AAAAAAD/////AAAAAAAAAQEgAOH1BQAAAAAXqRSTNeWHqa9INvSnQ120SZeJc+6JSocBAwSBAAAAAQQWABQvLKRyDqYsPYImhD3eURpDGL10RwAA", + "sighashTypes": [129], + "inputToCheck": 0, + "WIF": "KxnAnQh6UJBxLF8Weup77yn8tWhLHhDhnXeyJuzmmcZA5aRdMJni" + } } ] }, diff --git a/test/psbt.js b/test/psbt.js index 1804e67..8ed7b51 100644 --- a/test/psbt.js +++ b/test/psbt.js @@ -167,26 +167,32 @@ describe(`Psbt`, () => { describe('signInputAsync', () => { fixtures.signInput.checks.forEach(f => { it(f.description, async () => { - const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt) - assert.doesNotReject(async () => { - await psbtThatShouldsign.signInputAsync( - f.shouldSign.inputToCheck, - ECPair.fromWIF(f.shouldSign.WIF), - ) - }) + if (f.shouldSign) { + const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt) + assert.doesNotReject(async () => { + await psbtThatShouldsign.signInputAsync( + f.shouldSign.inputToCheck, + ECPair.fromWIF(f.shouldSign.WIF), + f.shouldSign.sighashTypes || undefined, + ) + }) + } - const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt) - assert.rejects(async () => { - await psbtThatShouldThrow.signInputAsync( - f.shouldThrow.inputToCheck, - ECPair.fromWIF(f.shouldThrow.WIF), - ) - }, new RegExp(f.shouldThrow.errorMessage)) - assert.rejects(async () => { - await psbtThatShouldThrow.signInputAsync( - f.shouldThrow.inputToCheck, - ) - }, new RegExp('Need Signer to sign input')) + if (f.shouldThrow) { + const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt) + assert.rejects(async () => { + await psbtThatShouldThrow.signInputAsync( + f.shouldThrow.inputToCheck, + ECPair.fromWIF(f.shouldThrow.WIF), + f.shouldThrow.sighashTypes || undefined, + ) + }, new RegExp(f.shouldThrow.errorMessage)) + assert.rejects(async () => { + await psbtThatShouldThrow.signInputAsync( + f.shouldThrow.inputToCheck, + ) + }, new RegExp('Need Signer to sign input')) + } }) }) }) @@ -194,26 +200,32 @@ describe(`Psbt`, () => { describe('signInput', () => { fixtures.signInput.checks.forEach(f => { it(f.description, () => { - const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt) - assert.doesNotThrow(() => { - psbtThatShouldsign.signInput( - f.shouldSign.inputToCheck, - ECPair.fromWIF(f.shouldSign.WIF), - ) - }) + if (f.shouldSign) { + const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt) + assert.doesNotThrow(() => { + psbtThatShouldsign.signInput( + f.shouldSign.inputToCheck, + ECPair.fromWIF(f.shouldSign.WIF), + f.shouldSign.sighashTypes || undefined, + ) + }) + } - const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt) - assert.throws(() => { - psbtThatShouldThrow.signInput( - f.shouldThrow.inputToCheck, - ECPair.fromWIF(f.shouldThrow.WIF), - ) - }, new RegExp(f.shouldThrow.errorMessage)) - assert.throws(() => { - psbtThatShouldThrow.signInput( - f.shouldThrow.inputToCheck, - ) - }, new RegExp('Need Signer to sign input')) + if (f.shouldThrow) { + const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt) + assert.throws(() => { + psbtThatShouldThrow.signInput( + f.shouldThrow.inputToCheck, + ECPair.fromWIF(f.shouldThrow.WIF), + f.shouldThrow.sighashTypes || undefined, + ) + }, new RegExp(f.shouldThrow.errorMessage)) + assert.throws(() => { + psbtThatShouldThrow.signInput( + f.shouldThrow.inputToCheck, + ) + }, new RegExp('Need Signer to sign input')) + } }) }) }) @@ -222,22 +234,28 @@ describe(`Psbt`, () => { 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), - ) - }) + if (f.shouldSign) { + const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt) + assert.doesNotReject(async () => { + await psbtThatShouldsign.signAsync( + ECPair.fromWIF(f.shouldSign.WIF), + f.shouldSign.sighashTypes || undefined, + ) + }) + } - 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')) + if (f.shouldThrow) { + const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt) + assert.rejects(async () => { + await psbtThatShouldThrow.signAsync( + ECPair.fromWIF(f.shouldThrow.WIF), + f.shouldThrow.sighashTypes || undefined, + ) + }, new RegExp('No inputs were signed')) + assert.rejects(async () => { + await psbtThatShouldThrow.signAsync() + }, new RegExp('Need Signer to sign input')) + } }) }) }) @@ -246,22 +264,28 @@ describe(`Psbt`, () => { 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), - ) - }) + if (f.shouldSign) { + const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt) + assert.doesNotThrow(() => { + psbtThatShouldsign.sign( + ECPair.fromWIF(f.shouldSign.WIF), + f.shouldSign.sighashTypes || undefined, + ) + }) + } - 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')) + if (f.shouldThrow) { + const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt) + assert.throws(() => { + psbtThatShouldThrow.sign( + ECPair.fromWIF(f.shouldThrow.WIF), + f.shouldThrow.sighashTypes || undefined, + ) + }, new RegExp('No inputs were signed')) + assert.throws(() => { + psbtThatShouldThrow.sign() + }, new RegExp('Need Signer to sign input')) + } }) }) })