Skip to content

Commit 023a3d7

Browse files
OttoAllmendingerllm-git
andcommitted
feat(wasm-utxo): add support for creating outputs with addresses
Added the ability to create transaction outputs using addresses instead of raw scripts. This provides a more convenient API for users who don't want to deal with script creation. - Added `add_output_with_address` method in Rust - Extended TypeScript `addOutput` method with overloads for: - Direct script/value pairs - Address/value pairs - Legacy options object (now deprecated) Issue: BTC-0 Co-authored-by: llm-git <llm-git@ttll.de>
1 parent ca862f7 commit 023a3d7

3 files changed

Lines changed: 89 additions & 9 deletions

File tree

packages/wasm-utxo/js/fixedScriptWallet/BitGoPsbt.ts

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,17 @@ export type AddInputOptions = {
7070
prevTx?: Uint8Array;
7171
};
7272

73-
export type AddOutputOptions = {
74-
/** Output script (scriptPubKey) */
75-
script: Uint8Array;
76-
/** Value in satoshis */
77-
value: bigint;
78-
};
73+
export type AddOutputOptions =
74+
| {
75+
script: Uint8Array;
76+
/** Value in satoshis */
77+
value: bigint;
78+
}
79+
| {
80+
address: string;
81+
/** Value in satoshis */
82+
value: bigint;
83+
};
7984

8085
/** Key identifier for signing ("user", "backup", or "bitgo") */
8186
export type SignerKey = "user" | "backup" | "bitgo";
@@ -196,19 +201,71 @@ export class BitGoPsbt {
196201
/**
197202
* Add an output to the PSBT
198203
*
199-
* @param options - Output options (script, value)
204+
* @param script - The output script (scriptPubKey)
205+
* @param value - Value in satoshis
206+
* @returns The index of the newly added output
207+
*
208+
* @example
209+
* ```typescript
210+
* const outputIndex = psbt.addOutput(outputScript, 50000n);
211+
* ```
212+
*/
213+
addOutput(script: Uint8Array, value: bigint): number;
214+
/**
215+
* Add an output to the PSBT by address
216+
*
217+
* @param address - The destination address
218+
* @param value - Value in satoshis
219+
* @returns The index of the newly added output
220+
*
221+
* @example
222+
* ```typescript
223+
* const outputIndex = psbt.addOutput("bc1q...", 50000n);
224+
* ```
225+
*/
226+
addOutput(address: string, value: bigint): number;
227+
/**
228+
* Add an output to the PSBT
229+
*
230+
* @param options - Output options (script or address, and value)
200231
* @returns The index of the newly added output
232+
* @deprecated Use `addOutput(script | address, value)` instead
201233
*
202234
* @example
203235
* ```typescript
236+
* // Using script
204237
* const outputIndex = psbt.addOutput({
205238
* script: outputScript,
206239
* value: 50000n,
207240
* });
241+
*
242+
* // Using address
243+
* const outputIndex = psbt.addOutput({
244+
* address: "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4",
245+
* value: 50000n,
246+
* });
208247
* ```
209248
*/
210-
addOutput(options: AddOutputOptions): number {
211-
return this._wasm.add_output(options.script, options.value);
249+
addOutput(options: AddOutputOptions): number;
250+
addOutput(scriptOrOptions: Uint8Array | string | AddOutputOptions, value?: bigint): number {
251+
if (scriptOrOptions instanceof Uint8Array || typeof scriptOrOptions === "string") {
252+
if (value === undefined) {
253+
throw new Error("Value is required when passing a script or address");
254+
}
255+
if (scriptOrOptions instanceof Uint8Array) {
256+
return this._wasm.add_output(scriptOrOptions, value);
257+
}
258+
return this._wasm.add_output_with_address(scriptOrOptions, value);
259+
}
260+
261+
const options = scriptOrOptions;
262+
if ("script" in options) {
263+
return this._wasm.add_output(options.script, options.value);
264+
}
265+
if ("address" in options) {
266+
return this._wasm.add_output_with_address(options.address, options.value);
267+
}
268+
throw new Error("Invalid output options");
212269
}
213270

214271
/**

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,13 @@ impl BitGoPsbt {
726726
psbt.outputs.len() - 1
727727
}
728728

729+
pub fn add_output_with_address(&mut self, address: &str, value: u64) -> Result<usize, String> {
730+
let script =
731+
crate::address::networks::to_output_script_with_network(address, self.network())
732+
.map_err(|e| e.to_string())?;
733+
Ok(self.add_output(script, value))
734+
}
735+
729736
/// Add a wallet input with full PSBT metadata
730737
///
731738
/// This is a higher-level method that adds an input and populates all required

packages/wasm-utxo/src/wasm/fixed_script_wallet/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,22 @@ impl BitGoPsbt {
281281
Ok(self.psbt.add_output(script, value))
282282
}
283283

284+
/// Add an output to the PSBT by address
285+
///
286+
/// # Arguments
287+
/// * `address` - The destination address
288+
/// * `value` - The value in satoshis
289+
///
290+
/// # Returns
291+
/// The index of the newly added output
292+
pub fn add_output_with_address(
293+
&mut self,
294+
address: &str,
295+
value: u64,
296+
) -> Result<usize, WasmUtxoError> {
297+
Ok(self.psbt.add_output_with_address(address, value)?)
298+
}
299+
284300
/// Add a wallet input with full PSBT metadata
285301
///
286302
/// This is a higher-level method that adds an input and populates all required

0 commit comments

Comments
 (0)