|
|
@ -6,6 +6,27 @@ const Psbt = require('..').Psbt |
|
|
|
|
|
|
|
const fixtures = require('./fixtures/psbt') |
|
|
|
|
|
|
|
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`, () => { |
|
|
|
describe('BIP174 Test Vectors', () => { |
|
|
|
fixtures.bip174.invalid.forEach(f => { |
|
|
@ -47,6 +68,36 @@ describe(`Psbt`, () => { |
|
|
|
assert.strictEqual(psbt.toBase64(), f.result) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
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) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
describe('signInput', () => { |
|
|
|