Skip to content

Commit db14c7f

Browse files
OttoAllmendingerllm-git
andcommitted
feat(wasm-utxo): refactor parsed transaction type definitions
Update the ParsedInput/Output types to use stronger typing (non-optional address fields) and more explicit null values instead of undefined. Also add test coverage for OP_RETURN outputs in transaction parsing. Issue: BTC-2652 Co-authored-by: llm-git <[email protected]>
1 parent 1bd0c04 commit db14c7f

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

packages/wasm-utxo/js/fixedScriptWallet.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ type ReplayProtection =
5757
export type ScriptId = { chain: number; index: number };
5858

5959
export type ParsedInput = {
60-
address?: string;
60+
address: string;
6161
script: Uint8Array;
6262
value: bigint;
63-
scriptId: ScriptId | undefined;
63+
scriptId: ScriptId | null;
6464
};
6565

6666
export type ParsedOutput = {
67-
address?: string;
67+
address: string | null;
6868
script: Uint8Array;
6969
value: bigint;
70-
scriptId?: ScriptId;
70+
scriptId: ScriptId | null;
7171
};
7272

7373
export type ParsedTransaction = {

packages/wasm-utxo/src/fixed_script_wallet/bitgo_psbt/psbt_wallet_input.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ pub struct ScriptId {
280280
/// Parsed input from a PSBT transaction
281281
#[derive(Debug, Clone)]
282282
pub struct ParsedInput {
283-
pub address: Option<String>,
283+
pub address: String,
284284
pub script: Vec<u8>,
285285
pub value: u64,
286286
pub script_id: Option<ScriptId>,
@@ -333,7 +333,7 @@ impl ParsedInput {
333333
output_script.as_script(),
334334
network,
335335
)
336-
.ok();
336+
.map_err(ParseInputError::Address)?;
337337

338338
Ok(Self {
339339
address,

packages/wasm-utxo/test/fixedScript/parseTransactionWithWalletKeys.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,19 @@ describe("parseTransactionWithWalletKeys", function () {
9797
const internalOutputs = parsed.outputs.filter((o) => o.scriptId);
9898

9999
// Count external outputs (scriptId is null or undefined)
100-
const externalOutputs = parsed.outputs.filter((o) => !o.scriptId);
100+
const externalOutputs = parsed.outputs.filter((o) => o.scriptId === null);
101+
102+
assert.ok(externalOutputs.every((o) => o.address || o.script));
103+
const nonAddressOutputs = externalOutputs.filter((o) => o.address === null);
104+
assert.strictEqual(nonAddressOutputs.length, 1);
105+
const [opReturnOutput] = nonAddressOutputs;
106+
const expectedOpReturn = utxolib.payments.embed({
107+
data: [Buffer.from("setec astronomy")],
108+
}).output;
109+
assert.strictEqual(
110+
Buffer.from(opReturnOutput.script).toString("hex"),
111+
expectedOpReturn.toString("hex"),
112+
);
101113

102114
// Fixtures now have 3 external outputs
103115
assert.ok(internalOutputs.length > 0, "Should have internal outputs (have scriptId)");

0 commit comments

Comments
 (0)