Add some tests and an input duplicate checker
This commit is contained in:
parent
93e1661c6c
commit
8d52ce1668
5 changed files with 228 additions and 36 deletions
31
test/fixtures/psbt.json
vendored
31
test/fixtures/psbt.json
vendored
|
@ -304,6 +304,37 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
"addInput": {
|
||||
"checks": [
|
||||
{
|
||||
"description": "checks for hash and index",
|
||||
"inputData": {
|
||||
"hash": 42
|
||||
},
|
||||
"exception": "Error adding input."
|
||||
},
|
||||
{
|
||||
"description": "checks for hash and index",
|
||||
"inputData": {
|
||||
"hash": "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f",
|
||||
"index": 2
|
||||
},
|
||||
"equals": "cHNidP8BADMCAAAAAQABAgMEBQYHCAkKCwwNDg8AAQIDBAUGBwgJCgsMDQ4PAgAAAAD/////AAAAAAAAAAA="
|
||||
}
|
||||
]
|
||||
},
|
||||
"addOutput": {
|
||||
"checks": [
|
||||
{
|
||||
"description": "checks for hash and index",
|
||||
"outputData": {
|
||||
"address": "1P2NFEBp32V2arRwZNww6tgXEV58FG94mr",
|
||||
"value": "xyz"
|
||||
},
|
||||
"exception": "Error adding output."
|
||||
}
|
||||
]
|
||||
},
|
||||
"signInput": {
|
||||
"checks": [
|
||||
{
|
||||
|
|
118
test/psbt.js
118
test/psbt.js
|
@ -21,8 +21,16 @@ const initBuffers = (attr, data) => {
|
|||
} else if (attr === 'bip32Derivation') {
|
||||
data.masterFingerprint = b(data.masterFingerprint)
|
||||
data.pubkey = b(data.pubkey)
|
||||
} else if (attr === 'witnessUtxo') {
|
||||
} else if (attr === 'witnessUtxo') {
|
||||
data.script = b(data.script)
|
||||
} else if (attr === 'hash') {
|
||||
if (
|
||||
typeof data === 'string' &&
|
||||
data.match(/^[0-9a-f]*$/i) &&
|
||||
data.length % 2 === 0
|
||||
) {
|
||||
data = b(data)
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
|
@ -140,11 +148,55 @@ describe(`Psbt`, () => {
|
|||
|
||||
fixtures.bip174.extractor.forEach(f => {
|
||||
it('Extracts the expected transaction from a PSBT', () => {
|
||||
const psbt = Psbt.fromBase64(f.psbt)
|
||||
const psbt1 = Psbt.fromBase64(f.psbt)
|
||||
const transaction1 = psbt1.extractTransaction(true).toHex()
|
||||
|
||||
const transaction = psbt.extractTransaction().toHex()
|
||||
const psbt2 = Psbt.fromBase64(f.psbt)
|
||||
const transaction2 = psbt2.extractTransaction().toHex()
|
||||
|
||||
assert.strictEqual(transaction, f.transaction)
|
||||
assert.strictEqual(transaction1, transaction2)
|
||||
assert.strictEqual(transaction1, f.transaction)
|
||||
|
||||
const psbt3 = Psbt.fromBase64(f.psbt)
|
||||
delete psbt3.inputs[0].finalScriptSig
|
||||
delete psbt3.inputs[0].finalScriptWitness
|
||||
assert.throws(() => {
|
||||
psbt3.extractTransaction()
|
||||
}, new RegExp('Not finalized'))
|
||||
|
||||
const psbt4 = Psbt.fromBase64(f.psbt)
|
||||
psbt4.setMaximumFeeRate(1)
|
||||
assert.throws(() => {
|
||||
psbt4.extractTransaction()
|
||||
}, new RegExp('Warning: You are paying around [\\d.]+ in fees'))
|
||||
|
||||
const psbt5 = Psbt.fromBase64(f.psbt)
|
||||
psbt5.extractTransaction(true)
|
||||
const fr1 = psbt5.getFeeRate()
|
||||
const fr2 = psbt5.getFeeRate()
|
||||
assert.strictEqual(fr1, fr2)
|
||||
})
|
||||
})
|
||||
|
||||
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),
|
||||
)
|
||||
})
|
||||
|
||||
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt)
|
||||
assert.rejects(async () => {
|
||||
await psbtThatShouldThrow.signInputAsync(
|
||||
f.shouldThrow.inputToCheck,
|
||||
ECPair.fromWIF(f.shouldThrow.WIF),
|
||||
)
|
||||
}, {message: f.shouldThrow.errorMessage})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -179,6 +231,54 @@ describe(`Psbt`, () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe('addInput', () => {
|
||||
fixtures.addInput.checks.forEach(f => {
|
||||
for (const attr of Object.keys(f.inputData)) {
|
||||
f.inputData[attr] = initBuffers(attr, f.inputData[attr])
|
||||
}
|
||||
it(f.description, () => {
|
||||
const psbt = new Psbt()
|
||||
|
||||
if (f.exception) {
|
||||
assert.throws(() => {
|
||||
psbt.addInput(f.inputData)
|
||||
}, new RegExp(f.exception))
|
||||
} else {
|
||||
assert.doesNotThrow(() => {
|
||||
psbt.addInput(f.inputData)
|
||||
if (f.equals) {
|
||||
assert.strictEqual(psbt.toBase64(), f.equals)
|
||||
} else {
|
||||
console.log(psbt.toBase64())
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('addOutput', () => {
|
||||
fixtures.addOutput.checks.forEach(f => {
|
||||
for (const attr of Object.keys(f.outputData)) {
|
||||
f.outputData[attr] = initBuffers(attr, f.outputData[attr])
|
||||
}
|
||||
it(f.description, () => {
|
||||
const psbt = new Psbt()
|
||||
|
||||
if (f.exception) {
|
||||
assert.throws(() => {
|
||||
psbt.addOutput(f.outputData)
|
||||
}, new RegExp(f.exception))
|
||||
} else {
|
||||
assert.doesNotThrow(() => {
|
||||
psbt.addOutput(f.outputData)
|
||||
console.log(psbt.toBase64())
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('setVersion', () => {
|
||||
it('Sets the version value of the unsigned transaction', () => {
|
||||
const psbt = new Psbt()
|
||||
|
@ -225,6 +325,16 @@ describe(`Psbt`, () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe('setMaximumFeeRate', () => {
|
||||
it('Sets the maximumFeeRate value', () => {
|
||||
const psbt = new Psbt()
|
||||
|
||||
assert.strictEqual(psbt.opts.maximumFeeRate, 5000)
|
||||
psbt.setMaximumFeeRate(6000)
|
||||
assert.strictEqual(psbt.opts.maximumFeeRate, 6000)
|
||||
})
|
||||
})
|
||||
|
||||
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]));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue