Skip to content
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
74 changes: 65 additions & 9 deletions packages/wasm-utxo/js/fixedScriptWallet/BitGoPsbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,17 @@ export type AddInputOptions = {
prevTx?: Uint8Array;
};

export type AddOutputOptions = {
/** Output script (scriptPubKey) */
script: Uint8Array;
/** Value in satoshis */
value: bigint;
};
export type AddOutputOptions =
| {
script: Uint8Array;
/** Value in satoshis */
value: bigint;
}
| {
address: string;
/** Value in satoshis */
value: bigint;
};

/** Key identifier for signing ("user", "backup", or "bitgo") */
export type SignerKey = "user" | "backup" | "bitgo";
Expand Down Expand Up @@ -196,19 +201,70 @@ export class BitGoPsbt {
/**
* Add an output to the PSBT
*
* @param options - Output options (script, value)
* @param script - The output script (scriptPubKey)
* @param value - Value in satoshis
* @returns The index of the newly added output
*
* @example
* ```typescript
* const outputIndex = psbt.addOutput(outputScript, 50000n);
* ```
*/
addOutput(script: Uint8Array, value: bigint): number;
/**
* Add an output to the PSBT by address
*
* @param address - The destination address
* @param value - Value in satoshis
* @returns The index of the newly added output
*
* @example
* ```typescript
* const outputIndex = psbt.addOutput("bc1q...", 50000n);
* ```
*/
addOutput(address: string, value: bigint): number;
/**
* Add an output to the PSBT
*
* @param options - Output options (script or address, and value)
* @returns The index of the newly added output
*
* @example
* ```typescript
* // Using script
* const outputIndex = psbt.addOutput({
* script: outputScript,
* value: 50000n,
* });
*
* // Using address
* const outputIndex = psbt.addOutput({
* address: "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4",
* value: 50000n,
* });
* ```
*/
addOutput(options: AddOutputOptions): number {
return this._wasm.add_output(options.script, options.value);
addOutput(options: AddOutputOptions): number;
addOutput(scriptOrOptions: Uint8Array | string | AddOutputOptions, value?: bigint): number {
if (scriptOrOptions instanceof Uint8Array || typeof scriptOrOptions === "string") {
if (value === undefined) {
throw new Error("Value is required when passing a script or address");
}
if (scriptOrOptions instanceof Uint8Array) {
return this._wasm.add_output(scriptOrOptions, value);
}
return this._wasm.add_output_with_address(scriptOrOptions, value);
}

const options = scriptOrOptions;
if ("script" in options) {
return this._wasm.add_output(options.script, options.value);
}
if ("address" in options) {
return this._wasm.add_output_with_address(options.address, options.value);
}
throw new Error("Invalid output options");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,13 @@ impl BitGoPsbt {
psbt.outputs.len() - 1
}

pub fn add_output_with_address(&mut self, address: &str, value: u64) -> Result<usize, String> {
let script =
crate::address::networks::to_output_script_with_network(address, self.network())
.map_err(|e| e.to_string())?;
Ok(self.add_output(script, value))
}

/// Add a wallet input with full PSBT metadata
///
/// This is a higher-level method that adds an input and populates all required
Expand Down
16 changes: 16 additions & 0 deletions packages/wasm-utxo/src/wasm/fixed_script_wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,22 @@ impl BitGoPsbt {
Ok(self.psbt.add_output(script, value))
}

/// Add an output to the PSBT by address
///
/// # Arguments
/// * `address` - The destination address
/// * `value` - The value in satoshis
///
/// # Returns
/// The index of the newly added output
pub fn add_output_with_address(
&mut self,
address: &str,
value: u64,
) -> Result<usize, WasmUtxoError> {
Ok(self.psbt.add_output_with_address(address, value)?)
}

/// Add a wallet input with full PSBT metadata
///
/// This is a higher-level method that adds an input and populates all required
Expand Down