Skip to content

Commit 3d12b4a

Browse files
author
Hack.bg R&D
committed
feat(sdk): readd prototype Spend builder
1 parent f453841 commit 3d12b4a

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

src/sdk.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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. */
3199
export type Keypair = WasmKeypair;
32100
/** Load [Wasm] with default settings and create a [WasmKeypair]. */

src/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const keypair2 = keypair(new Uint8Array(Array(32).fill(9)));
1313
export default Test(import.meta, 'SimplicityHL', TestWasm(), TestOnLocalnet(), TestOnTestnet())
1414

1515
// Check that the API entrypoints are present on the WASM module:
16-
export function TestWasm() {
16+
export function TestWasm () {
1717
return Test('WASM', () => SimplicityHL.Wasm(),
1818
has('paramTypes', is('function')),
1919
has('witnessTypes', is('function')),

0 commit comments

Comments
 (0)