Use Buffer notation in JSON
This commit is contained in:
parent
01c7ac39b6
commit
75f5e8f03c
2 changed files with 34 additions and 48 deletions
36
test/fixtures/psbt.json
vendored
36
test/fixtures/psbt.json
vendored
|
@ -174,37 +174,37 @@
|
|||
"psbt": "cHNidP8BAJoCAAAAAljoeiG1ba8MI76OcHBFbDNvfLqlyHV5JPVFiHuyq911AAAAAAD/////g40EJ9DsZQpoqka7CwmK6kQiwHGyyng1Kgd5WdB86h0BAAAAAP////8CcKrwCAAAAAAWABTYXCtx0AYLCcmIauuBXlCZHdoSTQDh9QUAAAAAFgAUAK6pouXw+HaliN9VRuh0LR2HAI8AAAAAAAAAAAA=",
|
||||
"inputData": [
|
||||
{
|
||||
"nonWitnessUtxo": "0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f618765000000",
|
||||
"redeemScript": "5221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae",
|
||||
"nonWitnessUtxo": "Buffer.from('0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f618765000000', 'hex')",
|
||||
"redeemScript": "Buffer.from('5221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae', 'hex')",
|
||||
"bip32Derivation": [
|
||||
{
|
||||
"masterFingerprint": "d90c6a4f",
|
||||
"pubkey": "029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f",
|
||||
"masterFingerprint": "Buffer.from('d90c6a4f', 'hex')",
|
||||
"pubkey": "Buffer.from('029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f', 'hex')",
|
||||
"path": "m/0'/0'/0'"
|
||||
},
|
||||
{
|
||||
"masterFingerprint": "d90c6a4f",
|
||||
"pubkey": "02dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d7",
|
||||
"masterFingerprint": "Buffer.from('d90c6a4f', 'hex')",
|
||||
"pubkey": "Buffer.from('02dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d7', 'hex')",
|
||||
"path": "m/0'/0'/1'"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"witnessUtxo": {
|
||||
"script": "a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e887",
|
||||
"script": "Buffer.from('a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e887', 'hex')",
|
||||
"value": 200000000
|
||||
},
|
||||
"redeemScript": "00208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b2028903",
|
||||
"witnessScript": "522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7352ae",
|
||||
"redeemScript": "Buffer.from('00208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b2028903', 'hex')",
|
||||
"witnessScript": "Buffer.from('522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7352ae', 'hex')",
|
||||
"bip32Derivation": [
|
||||
{
|
||||
"masterFingerprint": "d90c6a4f",
|
||||
"pubkey": "023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e73",
|
||||
"masterFingerprint": "Buffer.from('d90c6a4f', 'hex')",
|
||||
"pubkey": "Buffer.from('023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e73', 'hex')",
|
||||
"path": "m/0'/0'/3'"
|
||||
},
|
||||
{
|
||||
"masterFingerprint": "d90c6a4f",
|
||||
"pubkey": "03089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc",
|
||||
"masterFingerprint": "Buffer.from('d90c6a4f', 'hex')",
|
||||
"pubkey": "Buffer.from('03089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc', 'hex')",
|
||||
"path": "m/0'/0'/2'"
|
||||
}
|
||||
]
|
||||
|
@ -214,8 +214,8 @@
|
|||
{
|
||||
"bip32Derivation": [
|
||||
{
|
||||
"masterFingerprint": "d90c6a4f",
|
||||
"pubkey": "03a9a4c37f5996d3aa25dbac6b570af0650394492942460b354753ed9eeca58771",
|
||||
"masterFingerprint": "Buffer.from('d90c6a4f', 'hex')",
|
||||
"pubkey": "Buffer.from('03a9a4c37f5996d3aa25dbac6b570af0650394492942460b354753ed9eeca58771', 'hex')",
|
||||
"path": "m/0'/0'/4'"
|
||||
}
|
||||
]
|
||||
|
@ -223,8 +223,8 @@
|
|||
{
|
||||
"bip32Derivation": [
|
||||
{
|
||||
"masterFingerprint": "d90c6a4f",
|
||||
"pubkey": "027f6399757d2eff55a136ad02c684b1838b6556e5f1b6b34282a94b6b50051096",
|
||||
"masterFingerprint": "Buffer.from('d90c6a4f', 'hex')",
|
||||
"pubkey": "Buffer.from('027f6399757d2eff55a136ad02c684b1838b6556e5f1b6b34282a94b6b50051096', 'hex')",
|
||||
"path": "m/0'/0'/5'"
|
||||
}
|
||||
]
|
||||
|
@ -316,7 +316,7 @@
|
|||
{
|
||||
"description": "checks for hash and index",
|
||||
"inputData": {
|
||||
"hash": "000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f",
|
||||
"hash": "Buffer.from('000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f', 'hex')",
|
||||
"index": 2
|
||||
},
|
||||
"equals": "cHNidP8BADMCAAAAAQABAgMEBQYHCAkKCwwNDg8AAQIDBAUGBwgJCgsMDQ4PAgAAAAD/////AAAAAAAAAAA="
|
||||
|
|
46
test/psbt.js
46
test/psbt.js
|
@ -5,37 +5,23 @@ const ECPair = require('../src/ecpair')
|
|||
const Psbt = require('..').Psbt
|
||||
const NETWORKS = require('../src/networks')
|
||||
|
||||
const fixtures = require('./fixtures/psbt')
|
||||
const initBuffers = object => JSON.parse(JSON.stringify(object), (key, value) => {
|
||||
const regex = new RegExp(/^Buffer.from\(['"](.*)['"], ['"](.*)['"]\)$/)
|
||||
const result = regex.exec(value)
|
||||
if (!result) return value
|
||||
|
||||
const data = result[1]
|
||||
const encoding = result[2]
|
||||
|
||||
return Buffer.from(data, encoding)
|
||||
})
|
||||
|
||||
const fixtures = initBuffers(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)
|
||||
} else if (attr === 'hash') {
|
||||
if (
|
||||
typeof data === 'string' &&
|
||||
data.match(/^[0-9a-f]*$/i) &&
|
||||
data.length % 2 === 0
|
||||
) {
|
||||
data = b(data)
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
};
|
||||
|
||||
describe(`Psbt`, () => {
|
||||
describe('BIP174 Test Vectors', () => {
|
||||
fixtures.bip174.invalid.forEach(f => {
|
||||
|
@ -94,9 +80,9 @@ describe(`Psbt`, () => {
|
|||
adder = adder.bind(psbt)
|
||||
const arg = data[attr]
|
||||
if (Array.isArray(arg)) {
|
||||
arg.forEach(a => adder(i, initBuffers(attr, a)))
|
||||
arg.forEach(a => adder(i, a))
|
||||
} else {
|
||||
adder(i, initBuffers(attr, arg))
|
||||
adder(i, arg)
|
||||
if (attr === 'nonWitnessUtxo') {
|
||||
const first = psbt.inputs[i].nonWitnessUtxo
|
||||
psbt.__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[i] = undefined
|
||||
|
@ -320,7 +306,7 @@ 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])
|
||||
f.inputData[attr] = f.inputData[attr]
|
||||
}
|
||||
it(f.description, () => {
|
||||
const psbt = new Psbt()
|
||||
|
@ -349,7 +335,7 @@ describe(`Psbt`, () => {
|
|||
describe('addOutput', () => {
|
||||
fixtures.addOutput.checks.forEach(f => {
|
||||
for (const attr of Object.keys(f.outputData)) {
|
||||
f.outputData[attr] = initBuffers(attr, f.outputData[attr])
|
||||
f.outputData[attr] = f.outputData[attr]
|
||||
}
|
||||
it(f.description, () => {
|
||||
const psbt = new Psbt()
|
||||
|
|
Loading…
Add table
Reference in a new issue