bitcoinjs-lib
Advanced tools
Comparing version 5.1.4 to 5.1.5
@@ -0,1 +1,5 @@ | ||
# 5.1.5 | ||
__added__ | ||
- `Psbt` now has `getFee(): number` for use when all inputs are finalized. It returns the satoshi fee of the transaction. Calling getFee, getFeeRate, or extractTransaction will cache these values so if you call one after the other, the second call will return immediately. | ||
# 5.1.4 | ||
@@ -2,0 +6,0 @@ __changed__ |
{ | ||
"name": "bitcoinjs-lib", | ||
"version": "5.1.4", | ||
"version": "5.1.5", | ||
"description": "Client-side Bitcoin JavaScript library", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -156,2 +156,3 @@ 'use strict'; | ||
} | ||
c.__FEE = undefined; | ||
c.__FEE_RATE = undefined; | ||
@@ -175,2 +176,3 @@ c.__EXTRACTED_TX = undefined; | ||
this.data.addOutput(outputData); | ||
c.__FEE = undefined; | ||
c.__FEE_RATE = undefined; | ||
@@ -192,17 +194,12 @@ c.__EXTRACTED_TX = undefined; | ||
getFeeRate() { | ||
if (!this.data.inputs.every(isFinalized)) | ||
throw new Error('PSBT must be finalized to calculate fee rate'); | ||
const c = this.__CACHE; | ||
if (c.__FEE_RATE) return c.__FEE_RATE; | ||
let tx; | ||
let mustFinalize = true; | ||
if (c.__EXTRACTED_TX) { | ||
tx = c.__EXTRACTED_TX; | ||
mustFinalize = false; | ||
} else { | ||
tx = c.__TX.clone(); | ||
} | ||
inputFinalizeGetAmts(this.data.inputs, tx, c, mustFinalize); | ||
return c.__FEE_RATE; | ||
return getTxCacheValue( | ||
'__FEE_RATE', | ||
'fee rate', | ||
this.data.inputs, | ||
this.__CACHE, | ||
); | ||
} | ||
getFee() { | ||
return getTxCacheValue('__FEE', 'fee', this.data.inputs, this.__CACHE); | ||
} | ||
finalizeAllInputs() { | ||
@@ -730,2 +727,19 @@ utils_1.checkForInput(this.data.inputs, 0); // making sure we have at least one | ||
); | ||
function getTxCacheValue(key, name, inputs, c) { | ||
if (!inputs.every(isFinalized)) | ||
throw new Error(`PSBT must be finalized to calculate ${name}`); | ||
if (key === '__FEE_RATE' && c.__FEE_RATE) return c.__FEE_RATE; | ||
if (key === '__FEE' && c.__FEE) return c.__FEE; | ||
let tx; | ||
let mustFinalize = true; | ||
if (c.__EXTRACTED_TX) { | ||
tx = c.__EXTRACTED_TX; | ||
mustFinalize = false; | ||
} else { | ||
tx = c.__TX.clone(); | ||
} | ||
inputFinalizeGetAmts(inputs, tx, c, mustFinalize); | ||
if (key === '__FEE_RATE') return c.__FEE_RATE; | ||
else if (key === '__FEE') return c.__FEE; | ||
} | ||
function getFinalScripts( | ||
@@ -1131,2 +1145,3 @@ script, | ||
const bytes = tx.virtualSize(); | ||
cache.__FEE = fee; | ||
cache.__EXTRACTED_TX = tx; | ||
@@ -1133,0 +1148,0 @@ cache.__FEE_RATE = Math.floor(fee / bytes); |
@@ -60,2 +60,3 @@ /// <reference types="node" /> | ||
getFeeRate(): number; | ||
getFee(): number; | ||
finalizeAllInputs(): this; | ||
@@ -62,0 +63,0 @@ finalizeInput(inputIndex: number): this; |
213609
5570