Skip to content

Commit f453841

Browse files
author
Hack.bg R&D
committed
chore(sdk): remove redundant definitions
1 parent 226d184 commit f453841

File tree

2 files changed

+59
-107
lines changed

2 files changed

+59
-107
lines changed

src/sdk.ts

Lines changed: 46 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,6 @@ import type {
1313

1414
/** Instance of SimplicityHL WASM module. */
1515
export 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. */
5417
export 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]. */
6833
export 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]. */
8341
export 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-
182129
export type * from './pkg/fadroma_simf.d.ts';

src/test.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import { deepStrictEqual as equal, rejects, throws, ok } from 'node:assert';
33
import { Base16, Fn, Test, Run, sleep } from '../../../library/index.ts';
44
import Btc, { Rpc, LiquidTestnet, ElementsRegtest } from '../../Bitcoin/index.ts';
5-
import { Signer, Program, Wasm, Arg, Args } from './sdk.ts';
5+
import * as SimplicityHL from './sdk.ts';
66
const { is, has } = Test;
7-
const { sendSigned, keypair } = await Wasm();
7+
const { sendSigned, keypair } = await SimplicityHL.Wasm();
88

99
const keypair1 = keypair(new Uint8Array(Array(32).fill(8)));
1010
const keypair2 = keypair(new Uint8Array(Array(32).fill(9)));
@@ -14,7 +14,7 @@ export default Test(import.meta, 'SimplicityHL', TestWasm(), TestOnLocalnet(), T
1414

1515
// Check that the API entrypoints are present on the WASM module:
1616
export function TestWasm() {
17-
return Test('WASM', () => Wasm(),
17+
return Test('WASM', () => SimplicityHL.Wasm(),
1818
has('paramTypes', is('function')),
1919
has('witnessTypes', is('function')),
2020
has('compiler', is('function')),
@@ -80,10 +80,10 @@ export function TestOnLocalnet () {
8080
argTypes: { PK: "u256" },
8181
witTypes: { SIG: "[u8; 64]" },
8282
provideArgs: () => ({
83-
PK: Arg.Pubkey(keypair1.xOnlyPublicKey())
83+
PK: SimplicityHL.Arg.Pubkey(keypair1.xOnlyPublicKey())
8484
}),
8585
provideWits: (sighash: Uint8Array<ArrayBufferLike>) => ({
86-
SIG: Arg.Signature(keypair1.signSchnorr(sighash)),
86+
SIG: SimplicityHL.Arg.Signature(keypair1.signSchnorr(sighash)),
8787
}),
8888
fee: 2.7e-7, })),
8989
// Shutdown the localnet.
@@ -176,7 +176,7 @@ function TestProgram (name: string, src: string, {
176176
argTypes = {} as Record<string, string>,
177177
witTypes = {} as Record<string, string>,
178178
/** Function that provides parameter data. */
179-
provideArgs = null as null|Fn.Returns<Fn.Async<Args>>,
179+
provideArgs = null as null|Fn.Returns<Fn.Async<SimplicityHL.Args>>,
180180
/** Function that provides witness data. */
181181
provideWits = null as null|Fn<[Uint8Array<ArrayBufferLike>], Fn.Async<object>>,
182182
} = {}) {
@@ -194,14 +194,19 @@ function TestProgram (name: string, src: string, {
194194
const args = provideArgs ? await provideArgs() : undefined;
195195

196196
// Ok, compile this program for this chain with these arguments.
197-
const prog = await Program(src, { chain: ID, genesis, args });
197+
const prog = await SimplicityHL.Program(src, { chain: chain.ID, genesis, args });
198198

199199
// Check against expected program address, if provided.
200200
if (p2tr) equal(prog.p2tr, p2tr);
201201

202202
// Fund program from deployer:
203203
// TODO: Use sendSigned
204-
const id = await rpc.sendtoaddress(p2tr, String(1));
204+
const commitAmount = 1_00000000n;
205+
const commitTxid = await SimplicityHL.Spend()
206+
.input(chain.findUtxo(keypair1, commitAmount).keypair1)
207+
.output(p2tr, commitAmount)
208+
.fee(fee)
209+
.broadcast(chain);
205210

206211
// Create local spender wallet and import it to RPC:
207212
const recipient = P2WPKH(keypair1.publicKey()).address;

0 commit comments

Comments
 (0)