Skip to content
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

fix: Updating nonWitnessUtxo on input to undefined #63

Merged
merged 1 commit into from
Nov 8, 2023
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
5 changes: 3 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1967,11 +1967,12 @@ export class Transaction {
// however in case of nonWitnessUtxo it is better to expect string, since constructing this complex object will be difficult for user
if (typeof nonWitnessUtxo === 'string') nonWitnessUtxo = hex.decode(nonWitnessUtxo);
if (isBytes(nonWitnessUtxo)) nonWitnessUtxo = RawTx.decode(nonWitnessUtxo);
if (nonWitnessUtxo === undefined) nonWitnessUtxo = cur?.nonWitnessUtxo;
if (!('nonWitnessUtxo' in i) && nonWitnessUtxo === undefined)
nonWitnessUtxo = cur?.nonWitnessUtxo;
if (typeof txid === 'string') txid = hex.decode(txid);
if (txid === undefined) txid = cur?.txid;
let res: PSBTKeyMapKeys<typeof PSBTInput> = { ...cur, ...i, nonWitnessUtxo, txid };
if (res.nonWitnessUtxo === undefined) delete res.nonWitnessUtxo;
if (!('nonWitnessUtxo' in i) && res.nonWitnessUtxo === undefined) delete res.nonWitnessUtxo;
if (res.sequence === undefined) res.sequence = DEFAULT_SEQUENCE;
if (res.tapMerkleRoot === null) delete res.tapMerkleRoot;
res = mergeKeyMap(PSBTInput, res, cur, allowedFields);
Expand Down
21 changes: 21 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,27 @@ should('Transaction input/output', () => {
bip32Derivation: [bip2, bip1, bip3],
sequence: btc.DEFAULT_SEQUENCE,
});
// Remove nonWitnessUtxo
const nonWitnessUtxoHex =
'0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f618765000000';
const nonWitnessUtxoBinary = hex.decode(nonWitnessUtxoHex);
const nonWitnessUtxo = btc.RawTx.decode(nonWitnessUtxoBinary);
tx.updateInput(0, { index: 0, nonWitnessUtxo: nonWitnessUtxoHex });
deepStrictEqual(tx.inputs[0], {
txid: new Uint8Array(32),
index: 0,
bip32Derivation: [bip2, bip1, bip3],
sequence: btc.DEFAULT_SEQUENCE,
nonWitnessUtxo,
});
tx.updateInput(0, { nonWitnessUtxo: undefined });
deepStrictEqual(tx.inputs[0], {
txid: new Uint8Array(32),
index: 0,
bip32Derivation: [bip2, bip1, bip3],
sequence: btc.DEFAULT_SEQUENCE,
});
tx.updateInput(0, { index: 10 });
// Delete KV
tx.updateInput(0, { bip32Derivation: undefined });
deepStrictEqual(tx.inputs[0], {
Expand Down