Add tests
This commit is contained in:
parent
8d52ce1668
commit
5f26654802
3 changed files with 47 additions and 23 deletions
20
src/psbt.js
20
src/psbt.js
|
@ -10,6 +10,10 @@ const payments = require('./payments');
|
||||||
const bscript = require('./script');
|
const bscript = require('./script');
|
||||||
const transaction_1 = require('./transaction');
|
const transaction_1 = require('./transaction');
|
||||||
const varuint = require('varuint-bitcoin');
|
const varuint = require('varuint-bitcoin');
|
||||||
|
const DEFAULT_OPTS = {
|
||||||
|
network: networks_1.bitcoin,
|
||||||
|
maximumFeeRate: 5000,
|
||||||
|
};
|
||||||
class Psbt extends bip174_1.Psbt {
|
class Psbt extends bip174_1.Psbt {
|
||||||
constructor(opts = {}) {
|
constructor(opts = {}) {
|
||||||
super();
|
super();
|
||||||
|
@ -202,11 +206,11 @@ class Psbt extends bip174_1.Psbt {
|
||||||
const satoshis = feeRate * vsize;
|
const satoshis = feeRate * vsize;
|
||||||
if (feeRate >= this.opts.maximumFeeRate) {
|
if (feeRate >= this.opts.maximumFeeRate) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Warning: You are paying around ${satoshis / 1e8} in fees, which ` +
|
`Warning: You are paying around ${(satoshis / 1e8).toFixed(8)} in ` +
|
||||||
`is ${feeRate} satoshi per byte for a transaction with a VSize of ` +
|
`fees, which is ${feeRate} satoshi per byte for a transaction ` +
|
||||||
`${vsize} bytes (segwit counted as 0.25 byte per byte)\n` +
|
`with a VSize of ${vsize} bytes (segwit counted as 0.25 byte per ` +
|
||||||
`Use setMaximumFeeRate method to raise your threshold, or pass ` +
|
`byte). Use setMaximumFeeRate method to raise your threshold, or ` +
|
||||||
`true to the first arg of extractTransaction.`,
|
`pass true to the first arg of extractTransaction.`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -254,8 +258,6 @@ class Psbt extends bip174_1.Psbt {
|
||||||
const vout = this.__TX.ins[idx].index;
|
const vout = this.__TX.ins[idx].index;
|
||||||
const out = cache.__NON_WITNESS_UTXO_TX_CACHE[idx].outs[vout];
|
const out = cache.__NON_WITNESS_UTXO_TX_CACHE[idx].outs[vout];
|
||||||
inputAmount += out.value;
|
inputAmount += out.value;
|
||||||
} else {
|
|
||||||
throw new Error('Missing input value: index #' + idx);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.__EXTRACTED_TX = tx;
|
this.__EXTRACTED_TX = tx;
|
||||||
|
@ -341,10 +343,6 @@ class Psbt extends bip174_1.Psbt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.Psbt = Psbt;
|
exports.Psbt = Psbt;
|
||||||
const DEFAULT_OPTS = {
|
|
||||||
network: networks_1.bitcoin,
|
|
||||||
maximumFeeRate: 5000,
|
|
||||||
};
|
|
||||||
function addNonWitnessTxCache(cache, input, inputIndex) {
|
function addNonWitnessTxCache(cache, input, inputIndex) {
|
||||||
cache.__NON_WITNESS_UTXO_BUF_CACHE[inputIndex] = input.nonWitnessUtxo;
|
cache.__NON_WITNESS_UTXO_BUF_CACHE[inputIndex] = input.nonWitnessUtxo;
|
||||||
const tx = transaction_1.Transaction.fromBuffer(input.nonWitnessUtxo);
|
const tx = transaction_1.Transaction.fromBuffer(input.nonWitnessUtxo);
|
||||||
|
|
28
test/psbt.js
28
test/psbt.js
|
@ -97,6 +97,16 @@ describe(`Psbt`, () => {
|
||||||
arg.forEach(a => adder(i, initBuffers(attr, a)))
|
arg.forEach(a => adder(i, initBuffers(attr, a)))
|
||||||
} else {
|
} else {
|
||||||
adder(i, initBuffers(attr, arg))
|
adder(i, initBuffers(attr, arg))
|
||||||
|
if (attr === 'nonWitnessUtxo') {
|
||||||
|
const first = psbt.inputs[i].nonWitnessUtxo
|
||||||
|
psbt.__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[i] = undefined
|
||||||
|
const second = psbt.inputs[i].nonWitnessUtxo
|
||||||
|
psbt.inputs[i].nonWitnessUtxo = Buffer.from([1,2,3])
|
||||||
|
psbt.__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[i] = undefined
|
||||||
|
const third = psbt.inputs[i].nonWitnessUtxo
|
||||||
|
assert.ok(first.equals(second))
|
||||||
|
assert.ok(first.equals(third))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -140,6 +150,10 @@ describe(`Psbt`, () => {
|
||||||
it('Finalizes inputs and gives the expected PSBT', () => {
|
it('Finalizes inputs and gives the expected PSBT', () => {
|
||||||
const psbt = Psbt.fromBase64(f.psbt)
|
const psbt = Psbt.fromBase64(f.psbt)
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
psbt.getFeeRate()
|
||||||
|
}, new RegExp('PSBT must be finalized to calculate fee rate'))
|
||||||
|
|
||||||
psbt.finalizeAllInputs()
|
psbt.finalizeAllInputs()
|
||||||
|
|
||||||
assert.strictEqual(psbt.toBase64(), f.result)
|
assert.strictEqual(psbt.toBase64(), f.result)
|
||||||
|
@ -196,6 +210,11 @@ describe(`Psbt`, () => {
|
||||||
ECPair.fromWIF(f.shouldThrow.WIF),
|
ECPair.fromWIF(f.shouldThrow.WIF),
|
||||||
)
|
)
|
||||||
}, {message: f.shouldThrow.errorMessage})
|
}, {message: f.shouldThrow.errorMessage})
|
||||||
|
assert.rejects(async () => {
|
||||||
|
await psbtThatShouldThrow.signInputAsync(
|
||||||
|
f.shouldThrow.inputToCheck,
|
||||||
|
)
|
||||||
|
}, new RegExp('Need Signer to sign input'))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -218,6 +237,11 @@ describe(`Psbt`, () => {
|
||||||
ECPair.fromWIF(f.shouldThrow.WIF),
|
ECPair.fromWIF(f.shouldThrow.WIF),
|
||||||
)
|
)
|
||||||
}, {message: f.shouldThrow.errorMessage})
|
}, {message: f.shouldThrow.errorMessage})
|
||||||
|
assert.throws(() => {
|
||||||
|
psbtThatShouldThrow.signInput(
|
||||||
|
f.shouldThrow.inputToCheck,
|
||||||
|
)
|
||||||
|
}, new RegExp('Need Signer to sign input'))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -252,6 +276,9 @@ describe(`Psbt`, () => {
|
||||||
console.log(psbt.toBase64())
|
console.log(psbt.toBase64())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
assert.throws(() => {
|
||||||
|
psbt.addInput(f.inputData)
|
||||||
|
}, new RegExp('Duplicate input detected.'))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -307,6 +334,7 @@ describe(`Psbt`, () => {
|
||||||
index: 0
|
index: 0
|
||||||
});
|
});
|
||||||
|
|
||||||
|
assert.strictEqual(psbt.inputCount, 1)
|
||||||
assert.strictEqual(psbt.__TX.ins[0].sequence, 0xffffffff)
|
assert.strictEqual(psbt.__TX.ins[0].sequence, 0xffffffff)
|
||||||
psbt.setSequence(0, 0)
|
psbt.setSequence(0, 0)
|
||||||
assert.strictEqual(psbt.__TX.ins[0].sequence, 0)
|
assert.strictEqual(psbt.__TX.ins[0].sequence, 0)
|
||||||
|
|
|
@ -17,6 +17,11 @@ import * as bscript from './script';
|
||||||
import { Output, Transaction } from './transaction';
|
import { Output, Transaction } from './transaction';
|
||||||
const varuint = require('varuint-bitcoin');
|
const varuint = require('varuint-bitcoin');
|
||||||
|
|
||||||
|
const DEFAULT_OPTS: PsbtOpts = {
|
||||||
|
network: btcNetwork,
|
||||||
|
maximumFeeRate: 5000, // satoshi per byte
|
||||||
|
};
|
||||||
|
|
||||||
export class Psbt extends PsbtBase {
|
export class Psbt extends PsbtBase {
|
||||||
static fromTransaction<T extends typeof PsbtBase>(
|
static fromTransaction<T extends typeof PsbtBase>(
|
||||||
this: T,
|
this: T,
|
||||||
|
@ -250,11 +255,11 @@ export class Psbt extends PsbtBase {
|
||||||
const satoshis = feeRate * vsize;
|
const satoshis = feeRate * vsize;
|
||||||
if (feeRate >= this.opts.maximumFeeRate) {
|
if (feeRate >= this.opts.maximumFeeRate) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Warning: You are paying around ${satoshis / 1e8} in fees, which ` +
|
`Warning: You are paying around ${(satoshis / 1e8).toFixed(8)} in ` +
|
||||||
`is ${feeRate} satoshi per byte for a transaction with a VSize of ` +
|
`fees, which is ${feeRate} satoshi per byte for a transaction ` +
|
||||||
`${vsize} bytes (segwit counted as 0.25 byte per byte)\n` +
|
`with a VSize of ${vsize} bytes (segwit counted as 0.25 byte per ` +
|
||||||
`Use setMaximumFeeRate method to raise your threshold, or pass ` +
|
`byte). Use setMaximumFeeRate method to raise your threshold, or ` +
|
||||||
`true to the first arg of extractTransaction.`,
|
`pass true to the first arg of extractTransaction.`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -303,8 +308,6 @@ export class Psbt extends PsbtBase {
|
||||||
const vout = this.__TX.ins[idx].index;
|
const vout = this.__TX.ins[idx].index;
|
||||||
const out = cache.__NON_WITNESS_UTXO_TX_CACHE[idx].outs[vout] as Output;
|
const out = cache.__NON_WITNESS_UTXO_TX_CACHE[idx].outs[vout] as Output;
|
||||||
inputAmount += out.value;
|
inputAmount += out.value;
|
||||||
} else {
|
|
||||||
throw new Error('Missing input value: index #' + idx);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.__EXTRACTED_TX = tx;
|
this.__EXTRACTED_TX = tx;
|
||||||
|
@ -436,11 +439,6 @@ interface PsbtOpts {
|
||||||
maximumFeeRate: number;
|
maximumFeeRate: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_OPTS = {
|
|
||||||
network: btcNetwork,
|
|
||||||
maximumFeeRate: 5000, // satoshi per byte
|
|
||||||
};
|
|
||||||
|
|
||||||
function addNonWitnessTxCache(
|
function addNonWitnessTxCache(
|
||||||
cache: PsbtCache,
|
cache: PsbtCache,
|
||||||
input: PsbtInput,
|
input: PsbtInput,
|
||||||
|
|
Loading…
Add table
Reference in a new issue