Skip to content

Commit

Permalink
Merge pull request #5066 from BitGo/BTC-1582.op-return
Browse files Browse the repository at this point in the history
test(utxo-lib): Add check in utxo-lib that we can handle OP_RETURN
  • Loading branch information
davidkaplanbitgo authored Oct 25, 2024
2 parents 32a4c0e + a137dd7 commit 3c01812
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 6 additions & 2 deletions modules/utxo-lib/src/testutil/psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ export type Input = {
/**
* Set isInternalAddress=true for internal output address
*/
// Make script: string as instead of scriptType or address
export type Output = {
value: bigint;
isInternalAddress?: boolean;
} & ({ scriptType: OutputScriptType } | { address: string });
} & ({ scriptType: OutputScriptType } | { address: string } | { script: string });

/**
* array of supported input script types.
Expand Down Expand Up @@ -207,9 +208,12 @@ export function constructPsbt(
i,
output.value
);
} else if (output.address) {
} else if ('address' in output) {
const { address, value } = output;
psbt.addOutput({ script: toOutputScript(address, network), value });
} else if ('script' in output) {
const { script, value } = output;
psbt.addOutput({ script: Buffer.from(script, 'hex'), value });
}
});

Expand Down
26 changes: 26 additions & 0 deletions modules/utxo-lib/test/bitgo/psbt/PsbtOutputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,31 @@ describe('psbt internal and wallet outputs', function () {
(e: any) => e.message === 'Input Bip32Derivation can not be found'
);
});

it('PSBT woth an OP_RETURN output', function () {
const opReturnScript =
'6a4c505341542b01045bde60b7d0e6b758ca5dd8c61d377a2c5f1af51ec1a9e209f5ea0036c8c2f41078a3cebee57d8a47d501041f5e0e66b17576a914c4b8ae927ff2b9ce218e20bf06d425d6b68424fd88ac';
const psbt = testutil.constructPsbt(
[{ scriptType: 'p2wsh', value: BigInt(value) }],
[
{
address: externalAddress,
value: BigInt(value - fee),
},
{
script: opReturnScript,
value: BigInt(0),
},
],
network,
rootWalletKeys,
'unsigned'
);
const tx = psbt.getUnsignedTx();
assert.strictEqual(tx.outs.length, 2);
const out = tx.outs[1];
assert.strictEqual(out.value, BigInt(0));
assert.strictEqual(out.script.toString('hex'), opReturnScript);
});
});
});

0 comments on commit 3c01812

Please sign in to comment.