@@ -13,43 +13,6 @@ import type {
1313
1414/** Instance of SimplicityHL WASM module. */
1515export type Wasm = InitOutput ;
16-
17- /** WASM instance of keypair. */
18- export type Keypair = WasmKeypair ;
19-
20- /** SimplicityHL compiler instance configured for specific chain. */
21- export type Compiler = WasmCompiler ;
22-
23- /** Collection of SimplicityHL program argument types (template parameters or witness values). */
24- export type ArgTypes = Record < string , string > ;
25-
26- /** Collection of SimplicityHL program arguments with values (template parameters or witness values). */
27- export type Args = Record < string , Arg > ;
28-
29- /** SimplicityHL program argument (template parameter or witness value). */
30- export type Arg = { type : string , value : unknown } ;
31-
32- /** SimplicityHL program compiler for specific chain and parameters. */
33- export type Program = WasmProgram & {
34- rpcCommit : Fn ,
35- rpcRedeem : Fn ,
36- } ;
37-
38- /** Parameters for commit transaction. */
39- export type Call = { previous : unknown , amount : Num , fee : Num } ;
40-
41- /** Parameters for commit transaction. */
42- export type Commit = Call & { from : string } ;
43-
44- /** Parameters for redeem transaction. */
45- export type Redeem = Call & { recipient : string , witness ?: Args } ;
46-
47- /** Connection to Elements RPC for sending and signing transactions. */
48- export type Connect = ( Log & Partial < Pick < Bitcoin , 'rpc' | 'rest' > > ) & {
49- send ? ( hex : Uint8Array ) : Fn . Async < unknown >
50- sign ? ( hex : Uint8Array ) : Fn . Async < Uint8Array >
51- } ;
52-
5316/** Load SimplicityHL WASM module. */
5417export async function Wasm ( {
5518 wasm = process . env [ 'FADROMA_SIMF_WASM' ] || import . meta. resolve ( '../pkg/fadroma_simf_bg.wasm' ) as string | URL | object ,
@@ -64,27 +27,63 @@ export async function Wasm ({
6427 return wrap ;
6528}
6629
30+ /** WASM instance of keypair. */
31+ export type Keypair = WasmKeypair ;
6732/** Load [Wasm] with default settings and create a [WasmKeypair]. */
6833export async function Keypair ( secret : Uint8Array ) {
6934 const { keypair } = await Wasm ( ) ;
7035 return keypair ( secret )
7136}
7237
73- /** Wrap a [Keypair] as a Signer. */
74- export async function Signer ( secret : Uint8Array ) {
75- const { pubECDSA } = await import ( 'npm:@scure/btc-signer/utils.js' ) ;
76- return {
77- keypair : await Keypair ( secret ) ,
78- pubEcdsa : pubECDSA ( secret ) ,
79- }
80- }
81-
38+ /** SimplicityHL compiler instance configured for specific chain. */
39+ export type Compiler = WasmCompiler ;
8240/** Load [Wasm] with default settings and create a [Compiler]. */
8341export async function Compiler ( ...args : Parameters < Wasm [ 'compiler' ] > ) {
8442 const { compiler } = await Wasm ( ) ;
8543 return compiler ( ...args )
8644}
8745
46+ /** Collection of SimplicityHL program argument types (template parameters or witness values). */
47+ export type ArgTypes = Record < string , string > ;
48+
49+ /** Collection of SimplicityHL program arguments with values (template parameters or witness values). */
50+ export type Args = Record < string , Arg > ;
51+
52+ /** SimplicityHL program argument (template parameter or witness value). */
53+ export type Arg = { type : string , value : unknown } ;
54+ /** Format a `{ type, value }` pair as used to pass `param` and `witness` values. */
55+ export function Arg ( type : string , value ?: unknown ) {
56+ return { type, value }
57+ }
58+ /** SimplicityHL program argument constructors.
59+ *
60+ * TODO: Fully cover https://github.com/BlockstreamResearch/SimplicityHL/blob/master/src/types.rs#L815 */
61+ export namespace Arg {
62+ /** Literal 256-bit number. */
63+ export const U256 = defU256 ( 'U256' ) ;
64+ /** SimplicityHL signature field (32 bytes). */
65+ export const Signature = defU256 ( 'Signature' ) ;
66+ /** SimplicityHL public key field (32 bytes). */
67+ export const Pubkey = defU256 ( 'Pubkey' ) ;
68+ /** SimplicityHL message field (32 bytes). */
69+ export const Message = defU256 ( 'Message' ) ;
70+
71+ /** Name-tagged 32-byte array, equivalent to U256. */
72+ function defU256 ( type : string ) {
73+ /** Construct the given kind of U256-like. */
74+ return Fn . Name ( type , function defineU256 (
75+ value : Uint8Array < ArrayBufferLike > = new Uint8Array ( new Array ( 32 ) . fill ( 0 ) )
76+ ) {
77+ return { type, value : '0x' + Base16 . encode ( value ) }
78+ } ) ;
79+ }
80+ }
81+
82+ /** SimplicityHL program compiler for specific chain and parameters. */
83+ export type Program = WasmProgram & {
84+ rpcCommit : Fn ,
85+ rpcRedeem : Fn ,
86+ } ;
8887/** Load WASM with default settings, create [Compiler], and compile program.
8988 *
9089 * Example:
@@ -123,60 +122,8 @@ export async function Program (source: string, {
123122 const program = compiler . compile ( source , { args, chain } ) ;
124123 // Inspect WASM program descriptor, receiving the P2TR.
125124 const fields = ( program as unknown as { toJSON ( ) : Program } ) . toJSON ( ) ;
126- // Deserialize args (FIXME? do this on the rust side)
127- if ( typeof fields . args === 'string' ) fields . args = JSON . parse ( fields . args as unknown as string ) ;
128125 // Manually attach properties and methods:
129- return Object . assign ( program , fields , {
130- async rpcCommit ( {
131- rest = missing ( 'rest' ) , rpc = missing ( 'rpc' ) , ...options
132- } : Connect & Commit ) {
133- const tx = program . commitTx ( options ) ;
134- return await rest . tx ( await rpc ! . sendrawtransaction ( tx . hex ) ) ;
135- } ,
136- async rpcRedeem ( {
137- rest = missing ( 'rest' ) , rpc = missing ( 'rpc' ) , ...options
138- } : Connect & Redeem ) {
139- const tx = program . redeemTx ( options ) ;
140- return await rest . tx ( await rpc . sendrawtransaction ( tx . hex ) ) ;
141- }
142- } ) ;
143- }
144-
145- /** Format a `{ type, value }` pair as used to pass `param` and `witness` values. */
146- export function Arg ( type : string , value ?: unknown ) {
147- return { type, value }
126+ return Object . assign ( program , fields ) ;
148127}
149128
150- /** SimplicityHL program argument constructors.
151- *
152- * TODO: Fully cover https://github.com/BlockstreamResearch/SimplicityHL/blob/master/src/types.rs#L815 */
153- export namespace Arg {
154- /** Literal 256-bit number. */
155- export const U256 = defU256 ( 'U256' ) ;
156- /** SimplicityHL signature field (32 bytes). */
157- export const Signature = defU256 ( 'Signature' ) ;
158- /** SimplicityHL public key field (32 bytes). */
159- export const Pubkey = defU256 ( 'Pubkey' ) ;
160- /** SimplicityHL message field (32 bytes). */
161- export const Message = defU256 ( 'Message' ) ;
162-
163- /** Name-tagged 32-byte array, equivalent to U256. */
164- function defU256 ( type : string ) {
165- /** Construct the given kind of U256-like. */
166- return Fn . Name ( type , function defineU256 (
167- value : Uint8Array < ArrayBufferLike > = new Uint8Array ( new Array ( 32 ) . fill ( 0 ) )
168- ) {
169- return { type, value : '0x' + Base16 . encode ( value ) }
170- } ) ;
171- }
172- }
173-
174- /** Partially signed transaction from SimplicityHL WASM module. */
175- export interface Transaction {
176- input : unknown [ ]
177- output : unknown [ ]
178- version : unknown
179- lock_time : { block : number } | { seconds : number }
180- } ;
181-
182129export type * from './pkg/fadroma_simf.d.ts' ;
0 commit comments