@arkade-os/sdk 0.5.4 → 0.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/README.md +103 -17
  2. package/dist/adapters/expo.cjs +7 -6
  3. package/dist/adapters/expo.d.cts +4 -3
  4. package/dist/adapters/expo.d.ts +4 -3
  5. package/dist/adapters/expo.js +4 -3
  6. package/dist/{asyncStorageTaskQueue-DTBa8EH7.d.cts → asyncStorageTaskQueue-BbH-THYJ.d.cts} +1 -1
  7. package/dist/{asyncStorageTaskQueue-DHMIxY04.d.ts → asyncStorageTaskQueue-BlvkJyTa.d.ts} +1 -1
  8. package/dist/base-C-Y5t_0F.d.cts +359 -0
  9. package/dist/base-C-Y5t_0F.d.ts +359 -0
  10. package/dist/{chunk-AG4TFD6F.js → chunk-4I53THL6.js} +79 -884
  11. package/dist/chunk-4I53THL6.js.map +1 -0
  12. package/dist/{chunk-B6VNKGIH.cjs → chunk-5SYUEVJH.cjs} +10 -10
  13. package/dist/{chunk-B6VNKGIH.cjs.map → chunk-5SYUEVJH.cjs.map} +1 -1
  14. package/dist/{chunk-BTRCN4JJ.js → chunk-7GIKLFHC.js} +64 -4
  15. package/dist/chunk-7GIKLFHC.js.map +1 -0
  16. package/dist/{chunk-E6OOJEHL.cjs → chunk-AMH7Q3DF.cjs} +60 -934
  17. package/dist/chunk-AMH7Q3DF.cjs.map +1 -0
  18. package/dist/{chunk-6CUP6ZIF.js → chunk-BCA6D255.js} +17 -841
  19. package/dist/chunk-BCA6D255.js.map +1 -0
  20. package/dist/{chunk-TUOLZBNC.js → chunk-CE46RS6L.js} +5 -4
  21. package/dist/chunk-CE46RS6L.js.map +1 -0
  22. package/dist/chunk-DNFTHG73.js +829 -0
  23. package/dist/chunk-DNFTHG73.js.map +1 -0
  24. package/dist/chunk-FDJ734CH.js +882 -0
  25. package/dist/chunk-FDJ734CH.js.map +1 -0
  26. package/dist/chunk-FLPWNMX3.cjs +930 -0
  27. package/dist/chunk-FLPWNMX3.cjs.map +1 -0
  28. package/dist/{chunk-MWVAWFX6.js → chunk-FVVXCDNE.js} +4 -4
  29. package/dist/{chunk-MWVAWFX6.js.map → chunk-FVVXCDNE.js.map} +1 -1
  30. package/dist/{chunk-DNITZVNG.cjs → chunk-HQQ4NRXI.cjs} +18 -17
  31. package/dist/chunk-HQQ4NRXI.cjs.map +1 -0
  32. package/dist/chunk-JFJNB7ZH.cjs +884 -0
  33. package/dist/chunk-JFJNB7ZH.cjs.map +1 -0
  34. package/dist/{chunk-GHUUT54D.cjs → chunk-KCL5AVML.cjs} +89 -933
  35. package/dist/chunk-KCL5AVML.cjs.map +1 -0
  36. package/dist/{chunk-ZJHLLDBP.js → chunk-OO2KEK64.js} +2624 -1277
  37. package/dist/chunk-OO2KEK64.js.map +1 -0
  38. package/dist/{chunk-F3FAE7VJ.cjs → chunk-T254PXSN.cjs} +3115 -1752
  39. package/dist/chunk-T254PXSN.cjs.map +1 -0
  40. package/dist/chunk-V3ZLL5VY.js +380 -0
  41. package/dist/chunk-V3ZLL5VY.js.map +1 -0
  42. package/dist/chunk-X6ZI53DZ.cjs +383 -0
  43. package/dist/chunk-X6ZI53DZ.cjs.map +1 -0
  44. package/dist/{chunk-WXNSISXK.cjs → chunk-XWUCNMPN.cjs} +79 -18
  45. package/dist/chunk-XWUCNMPN.cjs.map +1 -0
  46. package/dist/compiler.cjs +50 -0
  47. package/dist/compiler.cjs.map +1 -0
  48. package/dist/compiler.d.cts +38 -0
  49. package/dist/compiler.d.ts +38 -0
  50. package/dist/compiler.js +47 -0
  51. package/dist/compiler.js.map +1 -0
  52. package/dist/contracts/handlers/index.cjs +11 -9
  53. package/dist/contracts/handlers/index.d.cts +6 -4
  54. package/dist/contracts/handlers/index.d.ts +6 -4
  55. package/dist/contracts/handlers/index.js +4 -2
  56. package/dist/{delegate-xrSBNybC.d.ts → delegate-gGkUnYpi.d.cts} +1 -1
  57. package/dist/{delegate-f4_t864Z.d.cts → delegate-iyDum4fw.d.ts} +1 -1
  58. package/dist/{index-BJDz15ms.d.ts → index-BWwVnJhU.d.cts} +5 -270
  59. package/dist/{index-BjiVGtp8.d.cts → index-Bx-iArDs.d.ts} +5 -270
  60. package/dist/{index-DRwe0UDN.d.ts → index-DCml4QJn.d.ts} +1593 -1711
  61. package/dist/{index-DRwe0UDN.d.cts → index-DkyzIzKL.d.cts} +1593 -1711
  62. package/dist/index.cjs +457 -378
  63. package/dist/index.d.cts +633 -378
  64. package/dist/index.d.ts +633 -378
  65. package/dist/index.js +7 -4
  66. package/dist/{wallet-jVBDQl6R.d.cts → keyringDescriptorProvider-CS4w4PWd.d.ts} +412 -361
  67. package/dist/{wallet-Oc3Wd5gv.d.ts → keyringDescriptorProvider-CmHXrIXd.d.cts} +412 -361
  68. package/dist/program-BARSNg3X.d.ts +286 -0
  69. package/dist/program-znrWkxE4.d.cts +286 -0
  70. package/dist/repositories/realm/index.cjs +71 -51
  71. package/dist/repositories/realm/index.cjs.map +1 -1
  72. package/dist/repositories/realm/index.d.cts +20 -9
  73. package/dist/repositories/realm/index.d.ts +20 -9
  74. package/dist/repositories/realm/index.js +42 -23
  75. package/dist/repositories/realm/index.js.map +1 -1
  76. package/dist/repositories/sqlite/index.cjs +81 -56
  77. package/dist/repositories/sqlite/index.cjs.map +1 -1
  78. package/dist/repositories/sqlite/index.d.cts +17 -8
  79. package/dist/repositories/sqlite/index.d.ts +17 -8
  80. package/dist/repositories/sqlite/index.js +55 -31
  81. package/dist/repositories/sqlite/index.js.map +1 -1
  82. package/dist/{taskRunner-CDlNeth0.d.ts → taskRunner-DgMLSk_O.d.ts} +1 -1
  83. package/dist/{taskRunner-BPPU-YHG.d.cts → taskRunner-buh6aFpG.d.cts} +1 -1
  84. package/dist/wallet/expo/background.cjs +17 -14
  85. package/dist/wallet/expo/background.cjs.map +1 -1
  86. package/dist/wallet/expo/background.d.cts +5 -4
  87. package/dist/wallet/expo/background.d.ts +5 -4
  88. package/dist/wallet/expo/background.js +9 -6
  89. package/dist/wallet/expo/background.js.map +1 -1
  90. package/dist/wallet/expo/index.cjs +29 -16
  91. package/dist/wallet/expo/index.cjs.map +1 -1
  92. package/dist/wallet/expo/index.d.cts +12 -7
  93. package/dist/wallet/expo/index.d.ts +12 -7
  94. package/dist/wallet/expo/index.js +19 -6
  95. package/dist/wallet/expo/index.js.map +1 -1
  96. package/dist/worker/expo/index.cjs +12 -9
  97. package/dist/worker/expo/index.cjs.map +1 -1
  98. package/dist/worker/expo/index.d.cts +6 -5
  99. package/dist/worker/expo/index.d.ts +6 -5
  100. package/dist/worker/expo/index.js +8 -5
  101. package/dist/worker/expo/index.js.map +1 -1
  102. package/package.json +16 -1
  103. package/dist/chunk-6CUP6ZIF.js.map +0 -1
  104. package/dist/chunk-AG4TFD6F.js.map +0 -1
  105. package/dist/chunk-BTRCN4JJ.js.map +0 -1
  106. package/dist/chunk-DNITZVNG.cjs.map +0 -1
  107. package/dist/chunk-E6OOJEHL.cjs.map +0 -1
  108. package/dist/chunk-F3FAE7VJ.cjs.map +0 -1
  109. package/dist/chunk-GHUUT54D.cjs.map +0 -1
  110. package/dist/chunk-TUOLZBNC.js.map +0 -1
  111. package/dist/chunk-WXNSISXK.cjs.map +0 -1
  112. package/dist/chunk-ZJHLLDBP.js.map +0 -1
@@ -0,0 +1,286 @@
1
+ import { V as VtxoScript, T as TapLeafScript } from './base-C-Y5t_0F.js';
2
+
3
+ /**
4
+ * Arkade Program model & compiler.
5
+ *
6
+ * A {@link Program} is the normalized SDK model: named functions, each
7
+ * split into a `tapscript` segment enforced
8
+ * on-chain and an optional `arkadeScript` segment emulated by the co-signing
9
+ * service. This module owns everything that is *pure data → script*:
10
+ *
11
+ * - the Program/segment/token types,
12
+ * - `$param` resolution ({@link resolveAsm}), witness and timelock resolution,
13
+ * - program validation ({@link validateProgram}) — every referenced constructor
14
+ * parameter must be declared and bound,
15
+ * - {@link ArkadeProgramScript} — a {@link VtxoScript} compiled from a
16
+ * program, its constructor args and the signer keys,
17
+ * - artifact JSON (de)serialization ({@link parseArtifact} /
18
+ * {@link stringifyArtifact}) and the string-keyed contract params used to
19
+ * persist a program contract through the `src/contracts` pipeline.
20
+ *
21
+ * Keeping this free of provider/transaction imports lets the generic
22
+ * `arkade` contract handler (src/contracts/handlers/arkade.ts) and the
23
+ * high-level `ArkadeContract` client share one compilation path.
24
+ *
25
+ * @module arkade/program
26
+ */
27
+
28
+ /** A token in an `asm` array: an opcode name, a number/bigint push, raw bytes, or a `$param` placeholder. */
29
+ type AsmToken = string | number | bigint | Uint8Array;
30
+ /** A constructor argument or resolved value. */
31
+ type ArkadeParamValue = Uint8Array | bigint | number;
32
+ /** A function call argument (function input). */
33
+ type ArkadeArgValue = Uint8Array | bigint | number;
34
+ /** Native scalar types and their compiler witness encodings. */
35
+ declare const ARKADE_TYPE_ENCODINGS: {
36
+ readonly pubkey: "raw";
37
+ readonly signature: "schnorr-64";
38
+ readonly bytes: "raw";
39
+ readonly bytes20: "raw-20";
40
+ readonly bytes32: "raw-32";
41
+ readonly asset: "raw-32";
42
+ readonly int: "scriptnum";
43
+ readonly bool: "scriptnum";
44
+ readonly AssetGroup: "scriptnum";
45
+ };
46
+ /** A native Arkade scalar type for a constructor parameter or call input. */
47
+ type ArkadeArgType = keyof typeof ARKADE_TYPE_ENCODINGS;
48
+ /**
49
+ * Maps an {@link ArkadeArgType} to its TypeScript argument type.
50
+ * @internal
51
+ */
52
+ interface ArkadeValueType {
53
+ bytes: Uint8Array;
54
+ pubkey: Uint8Array;
55
+ signature: Uint8Array;
56
+ bytes20: Uint8Array;
57
+ bytes32: Uint8Array;
58
+ asset: Uint8Array;
59
+ int: bigint | number;
60
+ bool: bigint | number;
61
+ AssetGroup: bigint | number;
62
+ }
63
+ /** A typed function input or constructor param: a name plus its {@link ArkadeArgType}. */
64
+ interface InputDef {
65
+ name: string;
66
+ type: ArkadeArgType;
67
+ }
68
+ /** A typed function input or constructor parameter. */
69
+ type InputRef = InputDef;
70
+ /** Constructor pubkey `$param` tweaked by `fn`'s covenant. */
71
+ interface TweakedSigner {
72
+ tweak: string;
73
+ fn: string;
74
+ }
75
+ /** A `"$param"` reference, literal x-only bytes, or a {@link TweakedSigner}. */
76
+ type SignerRef = string | Uint8Array | TweakedSigner;
77
+ /** A witness-stack item: a function-input name, a `"$param"`, a literal number/bigint (minimal script-num), or raw bytes. */
78
+ type WitnessRef = string | number | bigint | Uint8Array;
79
+ /** Bitcoin Script segment of a spending path — enforced on-chain. */
80
+ interface TapscriptSegment {
81
+ /**
82
+ * Required signers. A path with `arkadeScript` also inserts the emulator
83
+ * co-signer at `emulatorIndex` (last by default), unless `emulator` is `null`.
84
+ */
85
+ signers: SignerRef[];
86
+ /** Caller signature ABI; satisfied through PSBT signing, never ConditionWitness. */
87
+ signatures?: InputDef[];
88
+ /**
89
+ * Covenant named by `<EMULATOR_KEY:fn>`. `null` when this leaf belongs to a
90
+ * covenant but signs with a constructor-key tweak instead. Omitted on
91
+ * hand-written programs, which still append the co-signer.
92
+ */
93
+ emulator?: string | null;
94
+ /** Emulator's position in the full signer list; omitted to append it. */
95
+ emulatorIndex?: number;
96
+ /**
97
+ * Optional standard-opcode condition (e.g. a hashlock), encoded into a
98
+ * ConditionMultisig leaf — or ConditionCSVMultisig when combined with `csv`.
99
+ */
100
+ asm?: AsmToken[];
101
+ /**
102
+ * Relative timelock (CSV). The value is a literal or a `"$param"` reference
103
+ * resolved against the constructor args (same convention as {@link SignerRef}).
104
+ * `"blocks"` and `"seconds"` are counts; `"sequence"` is a raw BIP68
105
+ * sequence, which is what an `arkadec` CSV operand holds.
106
+ * Combinable with `asm` (condition + CSV); mutually exclusive with `cltv`.
107
+ */
108
+ csv?: {
109
+ type: "blocks" | "seconds" | "sequence";
110
+ value: bigint | string;
111
+ };
112
+ /**
113
+ * Absolute timelock (CLTV): a literal or a `"$param"` reference.
114
+ * Mutually exclusive with `csv`/`asm`.
115
+ */
116
+ cltv?: bigint | string;
117
+ /** Items satisfying the condition (e.g. an HTLC preimage); set via the ConditionWitness PSBT field. */
118
+ witness?: WitnessRef[];
119
+ }
120
+ /** Arkade Script segment of a spending path — emulated, bound via the key tweak. */
121
+ interface ArkadeSegment {
122
+ /** Raw Arkade opcodes with `$param` placeholders. */
123
+ asm: AsmToken[];
124
+ /** The arkade-script witness stack (e.g. an output index). */
125
+ witness?: WitnessRef[];
126
+ /**
127
+ * Where each statement's tokens in `asm` came from, read from the compiler's debug file.
128
+ * ponytail: in memory only; stringifyArtifact does not write it, so a restored contract has
129
+ * none. Persist it if apps need source positions after a restore.
130
+ */
131
+ sourceMap?: SourceRange[];
132
+ }
133
+ /** One statement's tokens in a covenant's `asm` and its place in the entry file. */
134
+ interface SourceRange {
135
+ /** Half-open `[start, end)` indexes into the covenant's `asm`. */
136
+ asm: [number, number];
137
+ /** 1-based `[line, column]` in the entry file, with UTF-16 columns. */
138
+ start: [number, number];
139
+ end: [number, number];
140
+ /** The `require()` message, when it has one. */
141
+ message?: string;
142
+ }
143
+ /** One named spending path. */
144
+ interface ArkadeFunction {
145
+ /** Compiler spend group, used to resolve named covenant commitments. */
146
+ group?: string;
147
+ /** Ordered typed call arguments; absent for nullary paths. */
148
+ inputs?: readonly InputDef[];
149
+ tapscript: TapscriptSegment;
150
+ /** Present for covenant paths; absent for pure-tapscript paths (cancel/exit). */
151
+ arkadeScript?: ArkadeSegment;
152
+ }
153
+ /** Supported normalized Program version. */
154
+ declare const SUPPORTED_PROGRAM_VERSION = 1;
155
+ /**
156
+ * A Program written for a version this SDK cannot read. Typed so a stored row in that format is
157
+ * reported as unsupported rather than broken: its script stays unknown until it is recovered with
158
+ * an SDK that reads that version, or re-created from fresh params.
159
+ */
160
+ declare class UnsupportedProgramVersionError extends Error {
161
+ readonly version: unknown;
162
+ readonly name = "UnsupportedProgramVersionError";
163
+ constructor(version: unknown, context: string);
164
+ }
165
+ /** An Arkade contract program (hand-written or compiler-emitted). */
166
+ interface Program {
167
+ version: number;
168
+ /**
169
+ * Optional program name — metadata only: round-tripped through the
170
+ * artifact JSON, never used in compilation or the taproot tree.
171
+ */
172
+ name?: string;
173
+ /** Ordered typed constructor parameters. */
174
+ params?: readonly InputDef[];
175
+ functions: Record<string, ArkadeFunction>;
176
+ }
177
+ /**
178
+ * Resolve an `asm` array to bytes: substitute `$param` placeholders from `bind`,
179
+ * pass everything else (opcode names, numbers, raw byte pushes) through to the
180
+ * Arkade script encoder. The SDK does not interpret opcodes — this is pure
181
+ * substitution + encoding. To embed a signer key (server, user, or any
182
+ * other), pass it as a `$param` in the constructor args. Compiler `<...>`
183
+ * placeholders are normalized by {@link programFromArtifact} before this step.
184
+ */
185
+ declare function resolveAsm(asm: AsmToken[], bind: Record<string, ArkadeParamValue>): Uint8Array;
186
+ /**
187
+ * Resolve one witness-stack item to bytes.
188
+ *
189
+ * - raw bytes pass through;
190
+ * - numbers/bigints encode as minimal script-nums;
191
+ * - `"$param"` strings resolve against the program's constructor args;
192
+ * - any other string is a function-input name and resolves against `callArgs`
193
+ * (throws when the input is unbound — e.g. outside a function call).
194
+ */
195
+ declare function witnessRefToBytes(ref: WitnessRef, callArgs: Record<string, ArkadeArgValue>, programArgs: Record<string, ArkadeParamValue>): Uint8Array;
196
+ /** Validate declared constructor values and reject undeclared references. */
197
+ declare function validateProgram(program: Program, args: Record<string, ArkadeParamValue>): void;
198
+ /** The signer keys a program is compiled against. */
199
+ interface ProgramKeys {
200
+ /** The Arkade Service signer key (x-only) — used for address derivation and collaborative-path detection, not for `$param` resolution. */
201
+ serverKey: Uint8Array;
202
+ /** The wallet's x-only key — identifies which inputs the wallet signs. */
203
+ userKey?: Uint8Array;
204
+ /** The co-signer (emulator) key — required only for covenant (`arkadeScript`) functions. */
205
+ emulatorKey?: Uint8Array;
206
+ }
207
+ /**
208
+ * A single spending path, fully resolved at compilation: its definition, the
209
+ * committed leaf body, the per-path arkade-script bytes (covenant paths only),
210
+ * and the {@link TapLeafScript} (control block) for spending it.
211
+ */
212
+ interface CompiledProgramFunction {
213
+ name: string;
214
+ def: ArkadeFunction;
215
+ /** The committed leaf script (body). */
216
+ leafScript: Uint8Array;
217
+ /** Resolved arkade-script bytes; undefined for pure-tapscript paths. */
218
+ arkadeScript?: Uint8Array;
219
+ /** Resolved signer keys (x-only), in the declared `signers` order. */
220
+ signerKeys: Uint8Array[];
221
+ /** Resolved once — the taproot leaf + control block for this path. */
222
+ tapLeafScript: TapLeafScript;
223
+ }
224
+ /**
225
+ * A {@link VtxoScript} compiled from an Arkade {@link Program}, its
226
+ * constructor args and the signer keys.
227
+ *
228
+ * This is the single compilation path shared by the high-level
229
+ * `ArkadeContract` client and the `"arkade"` contract handler: both produce
230
+ * byte-identical taproot trees for the same (program, args, keys), so a
231
+ * contract registered through the `src/contracts` pipeline re-derives the
232
+ * exact script it was created with.
233
+ */
234
+ declare class ArkadeProgramScript extends VtxoScript {
235
+ readonly program: Program;
236
+ readonly args: Record<string, ArkadeParamValue>;
237
+ readonly keys: ProgramKeys;
238
+ /** Spending paths in declaration order. */
239
+ readonly compiled: CompiledProgramFunction[];
240
+ constructor(program: Program, args: Record<string, ArkadeParamValue>, keys: ProgramKeys);
241
+ /** The compiled spending path with the given function name, if any. */
242
+ functionByName(name: string): CompiledProgramFunction | undefined;
243
+ }
244
+ /**
245
+ * Convert a Program JSON artifact into a {@link Program}. Byte values are
246
+ * encoded as `0x`-prefixed hex strings in `asm`/`witness`/`signers`; opcode names,
247
+ * `$param` placeholders and numbers pass through unchanged.
248
+ *
249
+ * This is *not* the JSON `arkadec` writes. That artifact lists spend groups in
250
+ * an array and uses `<param>` placeholders and raw leaf assembly; pass it to
251
+ * {@link programFromArtifact} instead.
252
+ */
253
+ declare function parseArtifact(artifact: {
254
+ version?: number;
255
+ name?: string;
256
+ params?: readonly InputDef[];
257
+ functions: unknown;
258
+ }): Program;
259
+ /**
260
+ * Serialize a {@link Program} to normalized Program JSON — the inverse of
261
+ * {@link parseArtifact}: bytes become `0x`-hex strings; bigint tokens become
262
+ * plain numbers (or, above `Number.MAX_SAFE_INTEGER`, `0x`-hex of their
263
+ * minimal script-num bytes, which the script encoder pushes identically);
264
+ * literal timelock values serialize as decimal strings while `"$param"`
265
+ * timelock references are emitted as-is.
266
+ */
267
+ declare function stringifyArtifact(program: Program): string;
268
+ /**
269
+ * Typed parameters of an `"arkade"` contract as persisted through the
270
+ * `src/contracts` pipeline. The program serializes as artifact JSON, args as
271
+ * a JSON map (`0x`-hex bytes, decimal-string bigints), keys as hex — so a
272
+ * stored contract row re-derives its script fully offline.
273
+ */
274
+ interface ArkadeContractParams {
275
+ program: Program;
276
+ args: Record<string, ArkadeParamValue>;
277
+ serverKey: Uint8Array;
278
+ userKey?: Uint8Array;
279
+ emulatorKey?: Uint8Array;
280
+ }
281
+ /** Serialize {@link ArkadeContractParams} to the string map `Contract.params` requires. */
282
+ declare function serializeArkadeContractParams(typed: ArkadeContractParams): Record<string, string>;
283
+ /** Parse the string map persisted in `Contract.params` back into typed params. */
284
+ declare function deserializeArkadeContractParams(params: Record<string, string>): ArkadeContractParams;
285
+
286
+ export { type ArkadeArgValue as A, type CompiledProgramFunction as C, type InputDef as I, type Program as P, type SourceRange as S, type TapscriptSegment as T, UnsupportedProgramVersionError as U, type WitnessRef as W, ArkadeProgramScript as a, type ProgramKeys as b, type ArkadeParamValue as c, type ArkadeFunction as d, type ArkadeValueType as e, type ArkadeArgType as f, type ArkadeContractParams as g, type ArkadeSegment as h, type AsmToken as i, type InputRef as j, SUPPORTED_PROGRAM_VERSION as k, type SignerRef as l, type TweakedSigner as m, deserializeArkadeContractParams as n, stringifyArtifact as o, parseArtifact as p, resolveAsm as r, serializeArkadeContractParams as s, validateProgram as v, witnessRefToBytes as w };
@@ -0,0 +1,286 @@
1
+ import { V as VtxoScript, T as TapLeafScript } from './base-C-Y5t_0F.cjs';
2
+
3
+ /**
4
+ * Arkade Program model & compiler.
5
+ *
6
+ * A {@link Program} is the normalized SDK model: named functions, each
7
+ * split into a `tapscript` segment enforced
8
+ * on-chain and an optional `arkadeScript` segment emulated by the co-signing
9
+ * service. This module owns everything that is *pure data → script*:
10
+ *
11
+ * - the Program/segment/token types,
12
+ * - `$param` resolution ({@link resolveAsm}), witness and timelock resolution,
13
+ * - program validation ({@link validateProgram}) — every referenced constructor
14
+ * parameter must be declared and bound,
15
+ * - {@link ArkadeProgramScript} — a {@link VtxoScript} compiled from a
16
+ * program, its constructor args and the signer keys,
17
+ * - artifact JSON (de)serialization ({@link parseArtifact} /
18
+ * {@link stringifyArtifact}) and the string-keyed contract params used to
19
+ * persist a program contract through the `src/contracts` pipeline.
20
+ *
21
+ * Keeping this free of provider/transaction imports lets the generic
22
+ * `arkade` contract handler (src/contracts/handlers/arkade.ts) and the
23
+ * high-level `ArkadeContract` client share one compilation path.
24
+ *
25
+ * @module arkade/program
26
+ */
27
+
28
+ /** A token in an `asm` array: an opcode name, a number/bigint push, raw bytes, or a `$param` placeholder. */
29
+ type AsmToken = string | number | bigint | Uint8Array;
30
+ /** A constructor argument or resolved value. */
31
+ type ArkadeParamValue = Uint8Array | bigint | number;
32
+ /** A function call argument (function input). */
33
+ type ArkadeArgValue = Uint8Array | bigint | number;
34
+ /** Native scalar types and their compiler witness encodings. */
35
+ declare const ARKADE_TYPE_ENCODINGS: {
36
+ readonly pubkey: "raw";
37
+ readonly signature: "schnorr-64";
38
+ readonly bytes: "raw";
39
+ readonly bytes20: "raw-20";
40
+ readonly bytes32: "raw-32";
41
+ readonly asset: "raw-32";
42
+ readonly int: "scriptnum";
43
+ readonly bool: "scriptnum";
44
+ readonly AssetGroup: "scriptnum";
45
+ };
46
+ /** A native Arkade scalar type for a constructor parameter or call input. */
47
+ type ArkadeArgType = keyof typeof ARKADE_TYPE_ENCODINGS;
48
+ /**
49
+ * Maps an {@link ArkadeArgType} to its TypeScript argument type.
50
+ * @internal
51
+ */
52
+ interface ArkadeValueType {
53
+ bytes: Uint8Array;
54
+ pubkey: Uint8Array;
55
+ signature: Uint8Array;
56
+ bytes20: Uint8Array;
57
+ bytes32: Uint8Array;
58
+ asset: Uint8Array;
59
+ int: bigint | number;
60
+ bool: bigint | number;
61
+ AssetGroup: bigint | number;
62
+ }
63
+ /** A typed function input or constructor param: a name plus its {@link ArkadeArgType}. */
64
+ interface InputDef {
65
+ name: string;
66
+ type: ArkadeArgType;
67
+ }
68
+ /** A typed function input or constructor parameter. */
69
+ type InputRef = InputDef;
70
+ /** Constructor pubkey `$param` tweaked by `fn`'s covenant. */
71
+ interface TweakedSigner {
72
+ tweak: string;
73
+ fn: string;
74
+ }
75
+ /** A `"$param"` reference, literal x-only bytes, or a {@link TweakedSigner}. */
76
+ type SignerRef = string | Uint8Array | TweakedSigner;
77
+ /** A witness-stack item: a function-input name, a `"$param"`, a literal number/bigint (minimal script-num), or raw bytes. */
78
+ type WitnessRef = string | number | bigint | Uint8Array;
79
+ /** Bitcoin Script segment of a spending path — enforced on-chain. */
80
+ interface TapscriptSegment {
81
+ /**
82
+ * Required signers. A path with `arkadeScript` also inserts the emulator
83
+ * co-signer at `emulatorIndex` (last by default), unless `emulator` is `null`.
84
+ */
85
+ signers: SignerRef[];
86
+ /** Caller signature ABI; satisfied through PSBT signing, never ConditionWitness. */
87
+ signatures?: InputDef[];
88
+ /**
89
+ * Covenant named by `<EMULATOR_KEY:fn>`. `null` when this leaf belongs to a
90
+ * covenant but signs with a constructor-key tweak instead. Omitted on
91
+ * hand-written programs, which still append the co-signer.
92
+ */
93
+ emulator?: string | null;
94
+ /** Emulator's position in the full signer list; omitted to append it. */
95
+ emulatorIndex?: number;
96
+ /**
97
+ * Optional standard-opcode condition (e.g. a hashlock), encoded into a
98
+ * ConditionMultisig leaf — or ConditionCSVMultisig when combined with `csv`.
99
+ */
100
+ asm?: AsmToken[];
101
+ /**
102
+ * Relative timelock (CSV). The value is a literal or a `"$param"` reference
103
+ * resolved against the constructor args (same convention as {@link SignerRef}).
104
+ * `"blocks"` and `"seconds"` are counts; `"sequence"` is a raw BIP68
105
+ * sequence, which is what an `arkadec` CSV operand holds.
106
+ * Combinable with `asm` (condition + CSV); mutually exclusive with `cltv`.
107
+ */
108
+ csv?: {
109
+ type: "blocks" | "seconds" | "sequence";
110
+ value: bigint | string;
111
+ };
112
+ /**
113
+ * Absolute timelock (CLTV): a literal or a `"$param"` reference.
114
+ * Mutually exclusive with `csv`/`asm`.
115
+ */
116
+ cltv?: bigint | string;
117
+ /** Items satisfying the condition (e.g. an HTLC preimage); set via the ConditionWitness PSBT field. */
118
+ witness?: WitnessRef[];
119
+ }
120
+ /** Arkade Script segment of a spending path — emulated, bound via the key tweak. */
121
+ interface ArkadeSegment {
122
+ /** Raw Arkade opcodes with `$param` placeholders. */
123
+ asm: AsmToken[];
124
+ /** The arkade-script witness stack (e.g. an output index). */
125
+ witness?: WitnessRef[];
126
+ /**
127
+ * Where each statement's tokens in `asm` came from, read from the compiler's debug file.
128
+ * ponytail: in memory only; stringifyArtifact does not write it, so a restored contract has
129
+ * none. Persist it if apps need source positions after a restore.
130
+ */
131
+ sourceMap?: SourceRange[];
132
+ }
133
+ /** One statement's tokens in a covenant's `asm` and its place in the entry file. */
134
+ interface SourceRange {
135
+ /** Half-open `[start, end)` indexes into the covenant's `asm`. */
136
+ asm: [number, number];
137
+ /** 1-based `[line, column]` in the entry file, with UTF-16 columns. */
138
+ start: [number, number];
139
+ end: [number, number];
140
+ /** The `require()` message, when it has one. */
141
+ message?: string;
142
+ }
143
+ /** One named spending path. */
144
+ interface ArkadeFunction {
145
+ /** Compiler spend group, used to resolve named covenant commitments. */
146
+ group?: string;
147
+ /** Ordered typed call arguments; absent for nullary paths. */
148
+ inputs?: readonly InputDef[];
149
+ tapscript: TapscriptSegment;
150
+ /** Present for covenant paths; absent for pure-tapscript paths (cancel/exit). */
151
+ arkadeScript?: ArkadeSegment;
152
+ }
153
+ /** Supported normalized Program version. */
154
+ declare const SUPPORTED_PROGRAM_VERSION = 1;
155
+ /**
156
+ * A Program written for a version this SDK cannot read. Typed so a stored row in that format is
157
+ * reported as unsupported rather than broken: its script stays unknown until it is recovered with
158
+ * an SDK that reads that version, or re-created from fresh params.
159
+ */
160
+ declare class UnsupportedProgramVersionError extends Error {
161
+ readonly version: unknown;
162
+ readonly name = "UnsupportedProgramVersionError";
163
+ constructor(version: unknown, context: string);
164
+ }
165
+ /** An Arkade contract program (hand-written or compiler-emitted). */
166
+ interface Program {
167
+ version: number;
168
+ /**
169
+ * Optional program name — metadata only: round-tripped through the
170
+ * artifact JSON, never used in compilation or the taproot tree.
171
+ */
172
+ name?: string;
173
+ /** Ordered typed constructor parameters. */
174
+ params?: readonly InputDef[];
175
+ functions: Record<string, ArkadeFunction>;
176
+ }
177
+ /**
178
+ * Resolve an `asm` array to bytes: substitute `$param` placeholders from `bind`,
179
+ * pass everything else (opcode names, numbers, raw byte pushes) through to the
180
+ * Arkade script encoder. The SDK does not interpret opcodes — this is pure
181
+ * substitution + encoding. To embed a signer key (server, user, or any
182
+ * other), pass it as a `$param` in the constructor args. Compiler `<...>`
183
+ * placeholders are normalized by {@link programFromArtifact} before this step.
184
+ */
185
+ declare function resolveAsm(asm: AsmToken[], bind: Record<string, ArkadeParamValue>): Uint8Array;
186
+ /**
187
+ * Resolve one witness-stack item to bytes.
188
+ *
189
+ * - raw bytes pass through;
190
+ * - numbers/bigints encode as minimal script-nums;
191
+ * - `"$param"` strings resolve against the program's constructor args;
192
+ * - any other string is a function-input name and resolves against `callArgs`
193
+ * (throws when the input is unbound — e.g. outside a function call).
194
+ */
195
+ declare function witnessRefToBytes(ref: WitnessRef, callArgs: Record<string, ArkadeArgValue>, programArgs: Record<string, ArkadeParamValue>): Uint8Array;
196
+ /** Validate declared constructor values and reject undeclared references. */
197
+ declare function validateProgram(program: Program, args: Record<string, ArkadeParamValue>): void;
198
+ /** The signer keys a program is compiled against. */
199
+ interface ProgramKeys {
200
+ /** The Arkade Service signer key (x-only) — used for address derivation and collaborative-path detection, not for `$param` resolution. */
201
+ serverKey: Uint8Array;
202
+ /** The wallet's x-only key — identifies which inputs the wallet signs. */
203
+ userKey?: Uint8Array;
204
+ /** The co-signer (emulator) key — required only for covenant (`arkadeScript`) functions. */
205
+ emulatorKey?: Uint8Array;
206
+ }
207
+ /**
208
+ * A single spending path, fully resolved at compilation: its definition, the
209
+ * committed leaf body, the per-path arkade-script bytes (covenant paths only),
210
+ * and the {@link TapLeafScript} (control block) for spending it.
211
+ */
212
+ interface CompiledProgramFunction {
213
+ name: string;
214
+ def: ArkadeFunction;
215
+ /** The committed leaf script (body). */
216
+ leafScript: Uint8Array;
217
+ /** Resolved arkade-script bytes; undefined for pure-tapscript paths. */
218
+ arkadeScript?: Uint8Array;
219
+ /** Resolved signer keys (x-only), in the declared `signers` order. */
220
+ signerKeys: Uint8Array[];
221
+ /** Resolved once — the taproot leaf + control block for this path. */
222
+ tapLeafScript: TapLeafScript;
223
+ }
224
+ /**
225
+ * A {@link VtxoScript} compiled from an Arkade {@link Program}, its
226
+ * constructor args and the signer keys.
227
+ *
228
+ * This is the single compilation path shared by the high-level
229
+ * `ArkadeContract` client and the `"arkade"` contract handler: both produce
230
+ * byte-identical taproot trees for the same (program, args, keys), so a
231
+ * contract registered through the `src/contracts` pipeline re-derives the
232
+ * exact script it was created with.
233
+ */
234
+ declare class ArkadeProgramScript extends VtxoScript {
235
+ readonly program: Program;
236
+ readonly args: Record<string, ArkadeParamValue>;
237
+ readonly keys: ProgramKeys;
238
+ /** Spending paths in declaration order. */
239
+ readonly compiled: CompiledProgramFunction[];
240
+ constructor(program: Program, args: Record<string, ArkadeParamValue>, keys: ProgramKeys);
241
+ /** The compiled spending path with the given function name, if any. */
242
+ functionByName(name: string): CompiledProgramFunction | undefined;
243
+ }
244
+ /**
245
+ * Convert a Program JSON artifact into a {@link Program}. Byte values are
246
+ * encoded as `0x`-prefixed hex strings in `asm`/`witness`/`signers`; opcode names,
247
+ * `$param` placeholders and numbers pass through unchanged.
248
+ *
249
+ * This is *not* the JSON `arkadec` writes. That artifact lists spend groups in
250
+ * an array and uses `<param>` placeholders and raw leaf assembly; pass it to
251
+ * {@link programFromArtifact} instead.
252
+ */
253
+ declare function parseArtifact(artifact: {
254
+ version?: number;
255
+ name?: string;
256
+ params?: readonly InputDef[];
257
+ functions: unknown;
258
+ }): Program;
259
+ /**
260
+ * Serialize a {@link Program} to normalized Program JSON — the inverse of
261
+ * {@link parseArtifact}: bytes become `0x`-hex strings; bigint tokens become
262
+ * plain numbers (or, above `Number.MAX_SAFE_INTEGER`, `0x`-hex of their
263
+ * minimal script-num bytes, which the script encoder pushes identically);
264
+ * literal timelock values serialize as decimal strings while `"$param"`
265
+ * timelock references are emitted as-is.
266
+ */
267
+ declare function stringifyArtifact(program: Program): string;
268
+ /**
269
+ * Typed parameters of an `"arkade"` contract as persisted through the
270
+ * `src/contracts` pipeline. The program serializes as artifact JSON, args as
271
+ * a JSON map (`0x`-hex bytes, decimal-string bigints), keys as hex — so a
272
+ * stored contract row re-derives its script fully offline.
273
+ */
274
+ interface ArkadeContractParams {
275
+ program: Program;
276
+ args: Record<string, ArkadeParamValue>;
277
+ serverKey: Uint8Array;
278
+ userKey?: Uint8Array;
279
+ emulatorKey?: Uint8Array;
280
+ }
281
+ /** Serialize {@link ArkadeContractParams} to the string map `Contract.params` requires. */
282
+ declare function serializeArkadeContractParams(typed: ArkadeContractParams): Record<string, string>;
283
+ /** Parse the string map persisted in `Contract.params` back into typed params. */
284
+ declare function deserializeArkadeContractParams(params: Record<string, string>): ArkadeContractParams;
285
+
286
+ export { type ArkadeArgValue as A, type CompiledProgramFunction as C, type InputDef as I, type Program as P, type SourceRange as S, type TapscriptSegment as T, UnsupportedProgramVersionError as U, type WitnessRef as W, ArkadeProgramScript as a, type ProgramKeys as b, type ArkadeParamValue as c, type ArkadeFunction as d, type ArkadeValueType as e, type ArkadeArgType as f, type ArkadeContractParams as g, type ArkadeSegment as h, type AsmToken as i, type InputRef as j, SUPPORTED_PROGRAM_VERSION as k, type SignerRef as l, type TweakedSigner as m, deserializeArkadeContractParams as n, stringifyArtifact as o, parseArtifact as p, resolveAsm as r, serializeArkadeContractParams as s, validateProgram as v, witnessRefToBytes as w };