-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utxo-lib): parse basic info from the psbt
TICKET: BTC-637
- Loading branch information
1 parent
92af608
commit 2742e1b
Showing
3 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { UtxoPsbt } from './UtxoPsbt'; | ||
import { createTransactionFromBuffer } from './transaction'; | ||
|
||
export function getTransactionAmountsFromPsbt(psbt: UtxoPsbt): { | ||
inputCount: number; | ||
outputCount: number; | ||
inputAmount: bigint; | ||
outputAmount: bigint; | ||
fee: bigint; | ||
} { | ||
const inputCount = psbt.data.inputs.length; | ||
const outputCount = psbt.data.outputs.length; | ||
const txInputs = psbt.txInputs; | ||
const txOutputs = psbt.txOutputs; | ||
const inputAmount = psbt.data.inputs.reduce((acc, input, inputIndex) => { | ||
if (input.witnessUtxo) { | ||
return acc + BigInt(input.witnessUtxo.value); | ||
} else if (input.nonWitnessUtxo) { | ||
const tx = createTransactionFromBuffer(input.nonWitnessUtxo, psbt.network, { amountType: 'bigint' }); | ||
return acc + tx.outs[txInputs[inputIndex].index].value; | ||
} else { | ||
throw new Error('missing witnessUtxo and nonWitnessUtxo'); | ||
} | ||
}, BigInt(0)); | ||
const outputAmount = psbt.data.outputs.reduce( | ||
(acc, output, outputIndex) => acc + txOutputs[outputIndex].value, | ||
BigInt(0) | ||
); | ||
const fee = inputAmount - outputAmount; | ||
return { | ||
inputCount, | ||
outputCount, | ||
inputAmount, | ||
outputAmount, | ||
fee, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters