@@ -27,6 +27,74 @@ export async function Wasm ({
2727 return wrap ;
2828}
2929
30+ /** Spend transaction builder.
31+ *
32+ * TODO: Support multiple outputs, multiple inputs, multiple assets, in that order.
33+ * This will happen by extending the `sendSigner` Rust implementation. */
34+ export interface Spend {
35+ /** Transaction asset. */
36+ readonly asset : string ;
37+ /** Set the transaction fee. */
38+ fee ( amount : Num ) : this;
39+ /** Add a P2WPKH input with signer. */
40+ input ( utxo : Btc . Utxo , signer : Signer ) : this;
41+ /** Add a SimplicityHL/Taproot input with witness .*/
42+ input ( utxo : Btc . Utxo , program : Program , witness : Fn ) : this;
43+ /** Add a transaction output. */
44+ output ( address : string , amount : Num ) : this;
45+ /** Broadcast the signed transaction. */
46+ broadcast ( chain : Chain ) : Promise < Btc . TxInfo > ;
47+ }
48+
49+ /** Start building a spend transaction. */
50+ export function Spend ( asset : string ) : Spend {
51+ let utxo = null ;
52+ let signer = null ;
53+ let program = null ;
54+ let witness = null ;
55+ let address = null ;
56+ let amount = null ;
57+ let fee = null ;
58+ const spend = {
59+ fee ( x : Num ) {
60+ fee = x ;
61+ return spend ;
62+ } ,
63+ input ( x : Btc . Utxo , ...args : unknown [ ] ) {
64+ utxo = x ;
65+ if ( args . length === 1 ) {
66+ signer = args [ 0 ] ;
67+ } else if ( args . length === 2 ) {
68+ if ( typeof program !== 'object' ) {
69+ throw new Error ( '.input(utxo, program <- must be object, ...' )
70+ }
71+ program = args [ 0 ] ;
72+ if ( typeof witness !== 'function' ) {
73+ throw new Error ( '.input(utxo, program, witness <- must be function' )
74+ }
75+ witness = args [ 1 ] ;
76+ } else {
77+ throw new Error ( 'use .input(utxo, signer) or .input(utxo, program, witness)' )
78+ }
79+ return spend ;
80+ } ,
81+ output ( x : string , y : num ) {
82+ address = x ;
83+ amount = y ;
84+ return spend ;
85+ } ,
86+ async broadcast ( chain : Btc ) {
87+ if ( ! utxo ) throw new Error ( 'no input specified' )
88+ if ( ! address || ! amount ) throw new Error ( 'no output specified' )
89+ if ( ! fee ) throw new Error ( 'no fee specified' )
90+ const { sendSigned } = await Wasm ( ) ;
91+ const { hex } = await sendSigned ( signer , { } ) ;
92+ return await chain . broadcast ( hex ) ;
93+ }
94+ } ;
95+ return spend ;
96+ }
97+
3098/** WASM instance of keypair. */
3199export type Keypair = WasmKeypair ;
32100/** Load [Wasm] with default settings and create a [WasmKeypair]. */
0 commit comments