Skip to content

feat(psbt): use bigint values #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
],
"dependencies": {
"bech32": "^2.0.0",
"bip174": "^2.0.1",
"bip174": "git+https://github.com/brandonblack/bip174#c2ca5fa9ccdb6244b9617b1d78d77c82c2403185",
"bs58check": "^2.1.2",
"create-hash": "^1.1.0",
"fastpriorityqueue": "^0.7.1",
Expand Down
10 changes: 5 additions & 5 deletions src/psbt.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface PsbtTxInput extends TransactionInput {
}
export interface TransactionOutput {
script: Buffer;
value: number;
value: bigint;
}
export interface PsbtTxOutput extends TransactionOutput {
address: string | undefined;
Expand Down Expand Up @@ -76,9 +76,9 @@ export declare class Psbt {
addInput(inputData: PsbtInputExtended): this;
addOutputs(outputDatas: PsbtOutputExtended[]): this;
addOutput(outputData: PsbtOutputExtended): this;
extractTransaction(disableFeeCheck?: boolean): Transaction;
extractTransaction(disableFeeCheck?: boolean): Transaction<bigint>;
getFeeRate(): number;
getFee(): number;
getFee(): bigint;
finalizeAllInputs(): this;
finalizeInput(inputIndex: number, finalScriptsFunc?: FinalScriptsFunc): this;
getInputType(inputIndex: number): AllScriptType;
Expand Down Expand Up @@ -116,11 +116,11 @@ interface PsbtInputExtended extends PsbtInput, TransactionInput {
declare type PsbtOutputExtended = PsbtOutputExtendedAddress | PsbtOutputExtendedScript;
interface PsbtOutputExtendedAddress extends PsbtOutput {
address: string;
value: number;
value: bigint;
}
interface PsbtOutputExtendedScript extends PsbtOutput {
script: Buffer;
value: number;
value: bigint;
}
interface HDSignerBase {
/**
Expand Down
34 changes: 19 additions & 15 deletions src/psbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,8 @@ class Psbt {
return tx;
}
getFeeRate() {
return getTxCacheValue(
'__FEE_RATE',
'fee rate',
this.data.inputs,
this.__CACHE,
return Number(
getTxCacheValue('__FEE_RATE', 'fee rate', this.data.inputs, this.__CACHE),
);
}
getFee() {
Expand Down Expand Up @@ -623,7 +620,7 @@ const transactionFromBuffer = buffer => new PsbtTransaction(buffer);
*/
class PsbtTransaction {
constructor(buffer = Buffer.from([2, 0, 0, 0, 0, 0, 0, 0, 0, 0])) {
this.tx = transaction_1.Transaction.fromBuffer(buffer);
this.tx = transaction_1.Transaction.fromBuffer(buffer, undefined, 'bigint');
checkTxEmpty(this.tx);
Object.defineProperty(this, 'tx', {
enumerable: false,
Expand Down Expand Up @@ -656,7 +653,7 @@ class PsbtTransaction {
output.script === undefined ||
output.value === undefined ||
!Buffer.isBuffer(output.script) ||
typeof output.value !== 'number'
typeof output.value !== 'bigint'
) {
throw new Error('Error adding output.');
}
Expand Down Expand Up @@ -737,12 +734,15 @@ function check32Bit(num) {
}
}
function checkFees(psbt, cache, opts) {
const feeRate = cache.__FEE_RATE || psbt.getFeeRate();
const feeRate = Number(cache.__FEE_RATE) || psbt.getFeeRate();
const vsize = cache.__EXTRACTED_TX.virtualSize();
const satoshis = feeRate * vsize;
if (feeRate >= opts.maximumFeeRate) {
const satoshis = BigInt(feeRate) * BigInt(vsize);
if (Number(feeRate) >= opts.maximumFeeRate) {
const satoshisPerCoin = BigInt(1e8);
const coinString = (satoshis / satoshisPerCoin).toString();
const satsString = (satoshis % satoshisPerCoin).toString().padStart(8, '0');
throw new Error(
`Warning: You are paying around ${(satoshis / 1e8).toFixed(8)} in ` +
`Warning: You are paying around ${coinString}.${satsString} in ` +
`fees, which is ${feeRate} satoshi per byte for a transaction ` +
`with a VSize of ${vsize} bytes (segwit counted as 0.25 byte per ` +
`byte). Use setMaximumFeeRate method to raise your threshold, or ` +
Expand Down Expand Up @@ -1208,7 +1208,11 @@ function witnessStackToScriptWitness(witness) {
}
function addNonWitnessTxCache(cache, input, inputIndex) {
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,
undefined,
'bigint',
);
cache.__NON_WITNESS_UTXO_TX_CACHE[inputIndex] = tx;
const self = cache;
const selfIndex = inputIndex;
Expand All @@ -1232,7 +1236,7 @@ function addNonWitnessTxCache(cache, input, inputIndex) {
});
}
function inputFinalizeGetAmts(inputs, tx, cache, mustFinalize) {
let inputAmount = 0;
let inputAmount = BigInt(0);
inputs.forEach((input, idx) => {
if (mustFinalize && input.finalScriptSig)
tx.ins[idx].script = input.finalScriptSig;
Expand All @@ -1250,15 +1254,15 @@ function inputFinalizeGetAmts(inputs, tx, cache, mustFinalize) {
inputAmount += out.value;
}
});
const outputAmount = tx.outs.reduce((total, o) => total + o.value, 0);
const outputAmount = tx.outs.reduce((total, o) => total + o.value, BigInt(0));
const fee = inputAmount - outputAmount;
if (fee < 0) {
throw new Error('Outputs are spending more than Inputs');
}
const bytes = tx.virtualSize();
cache.__FEE = fee;
cache.__EXTRACTED_TX = tx;
cache.__FEE_RATE = Math.floor(fee / bytes);
cache.__FEE_RATE = fee / BigInt(bytes);
}
function nonWitnessUtxoTxFromCache(cache, input, inputIndex) {
const c = cache.__NON_WITNESS_UTXO_TX_CACHE;
Expand Down
Loading