Skip to content

Commit 0c79108

Browse files
authored
Merge pull request #86 from BitGo/BTC-0.add-output-with-address
feat(wasm-utxo): add support for creating outputs with addresses
2 parents 10673e5 + f7d697d commit 0c79108

3 files changed

Lines changed: 88 additions & 9 deletions

File tree

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

Lines changed: 65 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,70 @@ 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
201232
*
202233
* @example
203234
* ```typescript
235+
* // Using script
204236
* const outputIndex = psbt.addOutput({
205237
* script: outputScript,
206238
* value: 50000n,
207239
* });
240+
*
241+
* // Using address
242+
* const outputIndex = psbt.addOutput({
243+
* address: "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4",
244+
* value: 50000n,
245+
* });
208246
* ```
209247
*/
210-
addOutput(options: AddOutputOptions): number {
211-
return this._wasm.add_output(options.script, options.value);
248+
addOutput(options: AddOutputOptions): number;
249+
addOutput(scriptOrOptions: Uint8Array | string | AddOutputOptions, value?: bigint): number {
250+
if (scriptOrOptions instanceof Uint8Array || typeof scriptOrOptions === "string") {
251+
if (value === undefined) {
252+
throw new Error("Value is required when passing a script or address");
253+
}
254+
if (scriptOrOptions instanceof Uint8Array) {
255+
return this._wasm.add_output(scriptOrOptions, value);
256+
}
257+
return this._wasm.add_output_with_address(scriptOrOptions, value);
258+
}
259+
260+
const options = scriptOrOptions;
261+
if ("script" in options) {
262+
return this._wasm.add_output(options.script, options.value);
263+
}
264+
if ("address" in options) {
265+
return this._wasm.add_output_with_address(options.address, options.value);
266+
}
267+
throw new Error("Invalid output options");
212268
}
213269

214270
/**

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)