bitcoinjs-lib/test/psbt.js

163 lines
4.8 KiB
JavaScript
Raw Normal View History

2019-06-28 12:17:18 +02:00
const { describe, it } = require('mocha')
const assert = require('assert')
const ECPair = require('../src/ecpair')
const Psbt = require('..').Psbt
2019-07-04 10:32:16 +02:00
const NETWORKS = require('../src/networks')
2019-06-28 11:55:00 +02:00
const fixtures = require('./fixtures/psbt')
2019-07-04 09:53:51 +02:00
const upperCaseFirstLetter = str => str.replace(/^./, s => s.toUpperCase())
const b = hex => Buffer.from(hex, 'hex');
const initBuffers = (attr, data) => {
if ([
'nonWitnessUtxo',
'redeemScript',
'witnessScript'
].includes(attr)) {
data = b(data)
} else if (attr === 'bip32Derivation') {
data.masterFingerprint = b(data.masterFingerprint)
data.pubkey = b(data.pubkey)
} else if (attr === 'witnessUtxo') {
data.script = b(data.script)
}
return data
};
describe(`Psbt`, () => {
2019-07-03 11:40:37 +02:00
describe('BIP174 Test Vectors', () => {
fixtures.bip174.invalid.forEach(f => {
it(`Invalid: ${f.description}`, () => {
2019-07-03 11:40:37 +02:00
assert.throws(() => {
Psbt.fromBase64(f.psbt)
}, {message: f.errorMessage})
})
})
2019-07-03 12:02:02 +02:00
fixtures.bip174.valid.forEach(f => {
it(`Valid: ${f.description}`, () => {
2019-07-03 12:02:02 +02:00
assert.doesNotThrow(() => {
Psbt.fromBase64(f.psbt)
2019-07-03 12:02:02 +02:00
})
})
})
2019-07-03 12:38:09 +02:00
fixtures.bip174.failSignChecks.forEach(f => {
const keyPair = ECPair.makeRandom()
it(`Fails Signer checks: ${f.description}`, () => {
const psbt = Psbt.fromBase64(f.psbt)
assert.throws(() => {
psbt.signInput(f.inputToCheck, keyPair)
}, {message: f.errorMessage})
})
})
2019-07-04 08:47:18 +02:00
fixtures.bip174.creator.forEach(f => {
it('Creates expected PSBT', () => {
const psbt = new Psbt()
for (const input of f.inputs) {
psbt.addInput(input)
}
for (const output of f.outputs) {
const script = Buffer.from(output.script, 'hex');
psbt.addOutput({...output, script})
}
assert.strictEqual(psbt.toBase64(), f.result)
})
})
2019-07-04 09:53:51 +02:00
fixtures.bip174.updater.forEach(f => {
it('Updates PSBT to the expected result', () => {
const psbt = Psbt.fromBase64(f.psbt)
for (const inputOrOutput of ['input', 'output']) {
const fixtureData = f[`${inputOrOutput}Data`]
if (fixtureData) {
for (const [i, data] of fixtureData.entries()) {
const attrs = Object.keys(data)
for (const attr of attrs) {
const upperAttr = upperCaseFirstLetter(attr)
let adder = psbt[`add${upperAttr}To${upperCaseFirstLetter(inputOrOutput)}`]
if (adder !== undefined) {
adder = adder.bind(psbt)
const arg = data[attr]
if (Array.isArray(arg)) {
arg.forEach(a => adder(i, initBuffers(attr, a)))
} else {
adder(i, initBuffers(attr, arg))
}
}
}
}
}
}
assert.strictEqual(psbt.toBase64(), f.result)
})
})
2019-07-03 11:40:37 +02:00
})
2019-07-04 10:32:16 +02:00
fixtures.bip174.signer.forEach(f => {
it('Signs PSBT to the expected result', () => {
const psbt = Psbt.fromBase64(f.psbt)
f.keys.forEach(({inputToSign, WIF}) => {
const keyPair = ECPair.fromWIF(WIF, NETWORKS.testnet);
psbt.signInput(inputToSign, keyPair);
})
assert.strictEqual(psbt.toBase64(), f.result)
})
})
2019-07-04 11:11:19 +02:00
fixtures.bip174.combiner.forEach(f => {
it('Combines two PSBTs to the expected result', () => {
const psbts = f.psbts.map(psbt => Psbt.fromBase64(psbt))
psbts[0].combine(psbts[1])
// Produces a different Base64 string due to implemetation specific key-value ordering.
// That means this test will fail:
// assert.strictEqual(psbts[0].toBase64(), f.result)
// However, if we compare the actual PSBT properties we can see they are logically identical:
assert.deepStrictEqual(psbts[0], Psbt.fromBase64(f.result))
})
})
2019-07-04 11:16:19 +02:00
fixtures.bip174.finalizer.forEach(f => {
it('Finalizes inputs and gives the expected PSBT', () => {
const psbt = Psbt.fromBase64(f.psbt)
psbt.finalizeAllInputs()
assert.strictEqual(psbt.toBase64(), f.result)
})
})
describe('signInput', () => {
2019-06-28 11:55:00 +02:00
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),
)
2019-06-28 11:55:00 +02:00
})
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt)
assert.throws(() => {
psbtThatShouldThrow.signInput(
f.shouldThrow.inputToCheck,
ECPair.fromWIF(f.shouldThrow.WIF),
)
2019-06-28 11:55:00 +02:00
}, {message: f.shouldThrow.errorMessage})
})
})
})
})