@chironbuilder/ootle-sdk 0.1.14 → 0.1.16

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.
@@ -1,1365 +1,1389 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
- import { aggregateInputMasks as e, borEncodeTransaction as t, buildStealthInputsStatement as n, createStealthOutputWitness as r, encryptedDataDhKdfAead as i, generateKeypair as a, generateStealthBalanceProofSignature as o, generateStealthOutputsStatement as s, parseOotleAddress as c, sealTransaction as l, stealthDhSecret as ee, unblindOutput as te, validateBalanceProofSignature as ne, validateStealthTransfer as re } from "@tari-project/ootle-wasm";
5
- var u = /* @__PURE__ */ (function(e11) {
6
- return e11[e11.MainNet = 0] = "MainNet", e11[e11.StageNet = 1] = "StageNet", e11[e11.NextNet = 2] = "NextNet", e11[e11.LocalNet = 16] = "LocalNet", e11[e11.Igor = 36] = "Igor", e11[e11.Esmeralda = 38] = "Esmeralda", e11;
7
- })({}), d = class extends Error {
8
- constructor(e11, t2) {
9
- super(e11, t2), this.name = new.target.name;
10
- }
11
- }, ie = class extends d {
12
- constructor(e11, t2) {
13
- super(e11, t2);
14
- __publicField(this, "status");
15
- __publicField(this, "body");
16
- __publicField(this, "url");
17
- this.status = t2 == null ? void 0 : t2.status, this.body = t2 == null ? void 0 : t2.body, this.url = t2 == null ? void 0 : t2.url;
18
- }
19
- }, f = class extends d {
20
- constructor(e11, t2) {
21
- super(e11, t2);
22
- __publicField(this, "txId");
23
- __publicField(this, "reason");
24
- __publicField(this, "rejectReason");
25
- this.txId = t2.txId, this.reason = t2.reason, this.rejectReason = t2.rejectReason;
26
- }
27
- }, ae = class extends d {
28
- constructor(e11, t2) {
29
- super(e11, t2);
30
- __publicField(this, "txId");
31
- this.txId = t2.txId;
32
- }
33
- }, p = class extends d {
34
- }, m = class extends p {
35
- constructor(e11, t2) {
36
- super(e11, t2);
37
- __publicField(this, "address");
38
- this.address = t2.address;
39
- }
40
- }, h = class extends p {
41
- }, g = class extends d {
42
- }, _ = class extends d {
43
- constructor(e11, t2) {
44
- super(e11, t2);
45
- __publicField(this, "context");
46
- this.context = t2 == null ? void 0 : t2.context;
47
- }
48
- }, v = class extends d {
49
- }, oe = class extends d {
50
- };
51
- function se(e11, t2) {
52
- throw new v(`assertUnreachable${t2 ? ` (${t2})` : ""}: ${JSON.stringify(e11)}`);
53
- }
54
- function y(e11, t2, n2) {
55
- if (e11.length !== t2) throw new v(`${n2} must be ${t2} bytes, got ${e11.length}`);
56
- return e11;
57
- }
58
- function b(e11) {
59
- return Array.from(e11, (e12) => e12.toString(16).padStart(2, "0")).join("");
60
- }
61
- var ce = /^[0-9a-fA-F]*$/;
62
- function x(e11) {
63
- if (e11.length % 2 != 0) throw new v(`fromHexStr: hex string must have even length, got ${e11.length}`);
64
- if (!ce.test(e11)) throw new v("fromHexStr: hex string contains non-hex characters");
65
- let t2 = new Uint8Array(e11.length / 2);
66
- for (let n2 = 0; n2 < t2.length; n2++) t2[n2] = parseInt(e11.substring(n2 * 2, n2 * 2 + 2), 16);
67
- return t2;
68
- }
69
- var S = (1n << 64n) - 1n, le = 2n, C = 32, ue = C * 2, de = 32, fe = 128, pe = 129, me = 130, w = 131, he = 132, ge = 136, _e = 137, ve = 138, ye = 141, T = 0, be = 32, E = 64, xe = 96, D = 128, Se = 160, O = 192;
70
- function Ce(e11) {
71
- switch (typeof e11) {
72
- case "bigint":
73
- return k(e11);
74
- case "string":
75
- return Ee(e11);
76
- case "boolean":
77
- return Oe(e11);
78
- default:
79
- if (e11 instanceof Uint8Array) return De(e11);
80
- throw new v(`literalArg: cannot CBOR-encode value of type ${typeof e11}`);
81
- }
82
- }
83
- function k(e11) {
84
- if (e11 < 0n) throw new v(`amountLiteral: amount must be non-negative, got ${e11}`);
85
- if (e11 >> 128n != 0n) throw new v(`amountLiteral: amount overflows u128: ${e11}`);
86
- let t2 = [];
87
- return e11 <= S ? F(t2, T, e11) : we(t2, e11), I(t2);
88
- }
89
- function we(e11, t2) {
90
- let n2 = [], r2 = t2;
91
- for (; r2 > 0n; ) n2.unshift(Number(r2 & 255n)), r2 >>= 8n;
92
- F(e11, O, le), F(e11, E, BigInt(n2.length)), P(e11, n2);
93
- }
94
- function Te(e11) {
95
- let t2 = [];
96
- if (e11 >= 0n) {
97
- if (e11 > S) throw new v(`intLiteral: value exceeds the 64-bit CBOR integer range: ${e11}`);
98
- F(t2, T, e11);
99
- } else {
100
- let n2 = -1n - e11;
101
- if (n2 > S) throw new v(`intLiteral: value exceeds the 64-bit CBOR integer range: ${e11}`);
102
- F(t2, be, n2);
103
- }
104
- return I(t2);
105
- }
106
- function Ee(e11) {
107
- let t2 = [];
108
- return N(t2, e11), I(t2);
109
- }
110
- function De(e11) {
111
- let t2 = [];
112
- return F(t2, E, BigInt(e11.length)), P(t2, e11), I(t2);
113
- }
114
- function Oe(e11) {
115
- return { Literal: e11 ? "f5" : "f4" };
116
- }
117
- function A(e11) {
118
- return j(w, e11, "resource_");
119
- }
120
- function ke(e11) {
121
- return j(fe, e11, "component_");
122
- }
123
- function Ae(e11) {
124
- let t2 = e11 instanceof Map ? e11 : new Map(Object.entries(e11)), n2 = new TextEncoder(), r2 = [...t2.entries()].sort(([e12], [t3]) => Be(n2.encode(e12), n2.encode(t3))), i2 = [];
125
- F(i2, O, BigInt(pe)), F(i2, Se, BigInt(r2.length));
126
- for (let [e12, t3] of r2) N(i2, e12), N(i2, t3);
127
- return I(i2);
128
- }
129
- function je(e11) {
130
- return j(he, e11, "vault_");
131
- }
132
- function Me(e11) {
133
- return j(_e, e11, "template_");
134
- }
135
- function Ne(e11) {
136
- return j(ge, e11, "tombstone_");
137
- }
138
- function Pe(e11) {
139
- return j(ve, e11, "vnfp_");
140
- }
141
- function Fe(e11) {
142
- let t2 = typeof e11 == "string" ? x(e11) : e11;
143
- return y(t2, de, "publicKeyLiteral"), De(t2);
144
- }
145
- function Ie(e11) {
146
- let t2 = [];
147
- return F(t2, O, BigInt(me)), F(t2, D, 2n), M(t2, w, e11.resource_address, "resource_"), Re(t2, e11.id), I(t2);
148
- }
149
- function Le(e11) {
150
- let t2 = [];
151
- F(t2, O, BigInt(ye)), F(t2, D, 2n), M(t2, w, e11.resource_address, "resource_");
152
- let n2 = x(e11.id);
153
- return y(n2, C, "utxoAddressLiteral: id"), F(t2, E, BigInt(C)), P(t2, n2), I(t2);
154
- }
155
- function j(e11, t2, n2) {
156
- let r2 = [];
157
- return M(r2, e11, t2, n2), I(r2);
158
- }
159
- function M(e11, t2, n2, r2) {
160
- let i2 = n2.startsWith(r2) ? n2.slice(r2.length) : n2;
161
- if (i2.length !== ue) throw new v(`${r2}address must be 32 bytes (${ue} hex chars), got ${i2.length}`);
162
- F(e11, O, BigInt(t2)), F(e11, E, BigInt(C)), P(e11, x(i2));
163
- }
164
- function Re(e11, t2) {
165
- if (F(e11, D, 2n), "U256" in t2) {
166
- F(e11, T, 0n), F(e11, D, 1n);
167
- let n2 = x(t2.U256);
168
- y(n2, C, "NonFungibleId.U256"), F(e11, E, BigInt(C)), P(e11, n2);
169
- } else if ("String" in t2) F(e11, T, 1n), F(e11, D, 1n), N(e11, t2.String);
170
- else if ("Uint32" in t2) F(e11, T, 2n), F(e11, D, 1n), ze(e11, t2.Uint32, "NonFungibleId.Uint32", (1n << 32n) - 1n);
171
- else if ("Uint64" in t2) F(e11, T, 3n), F(e11, D, 1n), ze(e11, t2.Uint64, "NonFungibleId.Uint64", S);
172
- else throw new v(`nonFungibleAddressLiteral: unrecognised NonFungibleId variant: ${JSON.stringify(t2)}`);
173
- }
174
- function ze(e11, t2, n2, r2) {
175
- if (!Number.isInteger(t2) || t2 < 0) throw new v(`${n2} must be a non-negative integer, got ${t2}`);
176
- if (r2 > BigInt(2 ** 53 - 1) && t2 > 2 ** 53 - 1) throw new v(`${n2} value ${t2} exceeds Number.MAX_SAFE_INTEGER (2^53-1) and cannot be encoded without precision loss \u2014 the binding types this id as a JS number. Use a U256/String id for large values.`);
177
- let i2 = BigInt(t2);
178
- if (i2 > r2) throw new v(`${n2} value ${t2} exceeds its maximum of ${r2}`);
179
- F(e11, T, i2);
180
- }
181
- function N(e11, t2) {
182
- let n2 = new TextEncoder().encode(t2);
183
- F(e11, xe, BigInt(n2.length)), P(e11, n2);
184
- }
185
- function P(e11, t2) {
186
- let n2 = e11.length;
187
- e11.length = n2 + t2.length;
188
- for (let r2 = 0; r2 < t2.length; r2++) e11[n2 + r2] = t2[r2];
189
- }
190
- function Be(e11, t2) {
191
- let n2 = Math.min(e11.length, t2.length);
192
- for (let r2 = 0; r2 < n2; r2++) if (e11[r2] !== t2[r2]) return e11[r2] < t2[r2] ? -1 : 1;
193
- return e11.length - t2.length;
194
- }
195
- function F(e11, t2, n2) {
196
- n2 < 24n ? e11.push(t2 | Number(n2)) : n2 < 1n << 8n ? e11.push(t2 | 24, Number(n2)) : n2 < 1n << 16n ? e11.push(t2 | 25, Number(n2 >> 8n & 255n), Number(n2 & 255n)) : n2 < 1n << 32n ? e11.push(t2 | 26, Number(n2 >> 24n & 255n), Number(n2 >> 16n & 255n), Number(n2 >> 8n & 255n), Number(n2 & 255n)) : e11.push(t2 | 27, Number(n2 >> 56n & 255n), Number(n2 >> 48n & 255n), Number(n2 >> 40n & 255n), Number(n2 >> 32n & 255n), Number(n2 >> 24n & 255n), Number(n2 >> 16n & 255n), Number(n2 >> 8n & 255n), Number(n2 & 255n));
197
- }
198
- function I(e11) {
199
- return { Literal: b(Uint8Array.from(e11)) };
200
- }
201
- function L(e11) {
202
- return k(e11);
203
- }
204
- function Ve(e11) {
205
- if (e11 < 0n) throw new v(`microTariString: amount must be non-negative, got ${e11}`);
206
- return e11.toString();
207
- }
208
- function He(e11) {
209
- let t2 = e11.split(".");
210
- if (t2.length > 2) throw new v("Invalid workspace key format. Only one dot is allowed.");
211
- let n2 = t2[0];
212
- if (t2[1] === void 0) return {
213
- name: n2,
214
- offset: null
215
- };
216
- let r2 = Number.parseInt(t2[1], 10);
217
- if (Number.isNaN(r2) || r2 < 0 || String(r2) !== t2[1]) throw new v(`Invalid workspace key offset: "${t2[1]}"`);
218
- return {
219
- name: n2,
220
- offset: r2
221
- };
222
- }
223
- var Ue = 2160, We = 10;
224
- async function Ge(e11, t2 = 10) {
225
- if (!Number.isInteger(t2) || t2 < 1) throw new v(`resolveMaxEpoch: leadEpochs must be a positive integer, got ${t2}`);
226
- if (t2 > 2160) throw new v(`resolveMaxEpoch: leadEpochs ${t2} exceeds the network's maximum validity window of ${Ue} epochs`);
227
- return await e11.getCurrentEpoch() + t2;
228
- }
229
- var Ke = (1n << 64n) - 1n;
230
- function R(e11, t2) {
231
- if (!Number.isInteger(e11) || e11 < 0) throw new v(`${t2} must be a non-negative integer epoch, got ${e11}`);
232
- return e11;
233
- }
234
- function qe(e11) {
235
- return e11.startsWith("template_") ? e11.slice(9) : e11;
236
- }
237
- function Je(e11) {
238
- return typeof e11 != "object" || !e11 ? null : "PutLastInstructionOutputOnWorkspace" in e11 ? e11.PutLastInstructionOutputOnWorkspace.key : "TakeFromBucket" in e11 ? e11.TakeFromBucket.output_bucket : "AllocateAddress" in e11 ? e11.AllocateAddress.workspace_id : null;
239
- }
240
- var Ye = class {
241
- constructor() {
242
- __publicField(this, "nextId", 0);
243
- __publicField(this, "ids", /* @__PURE__ */ new Map());
244
- }
245
- insert(e11) {
246
- let t2 = this.nextId;
247
- return this.ids.set(e11, t2), this.nextId += 1, t2;
248
- }
249
- get(e11) {
250
- return this.ids.get(e11);
251
- }
252
- get next() {
253
- return this.nextId;
254
- }
255
- observeAllocated(e11) {
256
- e11 + 1 > this.nextId && (this.nextId = e11 + 1);
257
- }
258
- reset() {
259
- this.nextId = 0, this.ids.clear();
260
- }
261
- resetFrom(e11) {
262
- this.reset();
263
- for (let t2 of e11) {
264
- let e12 = Je(t2);
265
- e12 !== null && this.observeAllocated(e12);
266
- }
267
- }
268
- };
269
- function Xe(e11) {
270
- return new v(`No workspace variable named "${e11}" has been defined. Call builder.saveVar(${JSON.stringify(e11)}) on a preceding instruction whose output you want to reference.`);
271
- }
272
- var z = class e2 {
273
- constructor(e11, t2) {
274
- __publicField(this, "unsignedTransaction");
275
- __publicField(this, "workspaceIds");
276
- __publicField(this, "feeWorkspaceIds");
277
- this.unsignedTransaction = {
278
- network: e11,
279
- fee_instructions: [],
280
- instructions: [],
281
- inputs: [],
282
- min_epoch: null,
283
- max_epoch: R(t2, "maxEpoch"),
284
- dry_run: false,
285
- is_seal_signer_authorized: false,
286
- blobs: [],
287
- nonce: 0
288
- }, this.workspaceIds = new Ye(), this.feeWorkspaceIds = new Ye();
289
- }
290
- static new(t2, n2) {
291
- return new e2(t2, n2);
292
- }
293
- callFunction(e11, t2) {
294
- let n2 = this.resolveArgs(t2);
295
- return this.addInstruction({ CallFunction: {
296
- address: qe(e11.templateAddress),
297
- function: e11.functionName,
298
- args: n2
299
- } });
300
- }
301
- callMethod(e11, t2) {
302
- let n2;
303
- if (e11.componentAddress) n2 = { Address: e11.componentAddress };
304
- else if (e11.fromWorkspace) n2 = { Workspace: this.requireNamedId(e11.fromWorkspace) };
305
- else throw new v("callMethod requires either `componentAddress` or `fromWorkspace` to be set on the method definition. Use `fromWorkspace` to call a method on a component returned by a previous saveVar; use `componentAddress` for an on-chain component.");
306
- let r2 = this.resolveArgs(t2);
307
- return this.addInstruction({ CallMethod: {
308
- call: n2,
309
- method: e11.methodName,
310
- args: r2
311
- } });
312
- }
313
- createAccount(e11, t2) {
314
- let n2 = t2 ? this.getOffsetIdFromWorkspaceName(t2) : null;
315
- return this.addInstruction({ CreateAccount: {
316
- owner_public_key: e11,
317
- owner_rule: null,
318
- access_rules: null,
319
- bucket_workspace_id: n2
320
- } });
321
- }
322
- createProof(e11, t2) {
323
- return this.addInstruction({ CallMethod: {
324
- call: { Address: e11 },
325
- method: "create_proof_for_resource",
326
- args: [A(t2)]
327
- } });
328
- }
329
- claimBurn(e11, t2) {
330
- return this.addInstruction({ ClaimBurn: {
331
- claim: e11,
332
- output_data: t2
333
- } });
334
- }
335
- allocateAddress(e11, t2) {
336
- let n2 = this.addNamedId(t2);
337
- return this.addInstruction({ AllocateAddress: {
338
- allocatable_type: e11,
339
- workspace_id: n2
340
- } });
341
- }
342
- saveVar(e11) {
343
- let t2 = this.addNamedId(e11);
344
- return this.addInstruction({ PutLastInstructionOutputOnWorkspace: { key: t2 } });
345
- }
346
- feeTransactionPayFromComponent(e11, t2) {
347
- return this.addFeeInstruction({ CallMethod: {
348
- call: { Address: e11 },
349
- method: "pay_fee",
350
- args: [L(t2)]
351
- } });
352
- }
353
- feeTransactionPayFromComponentConfidential(e11, t2) {
354
- throw new v("feeTransactionPayFromComponentConfidential is not implemented: a ConfidentialWithdrawProof Literal must be tari_bor-CBOR-encoded, which the TS SDK does not yet support.");
355
- }
356
- dropAllProofsInWorkspace() {
357
- return this.addInstruction("DropAllProofsInWorkspace");
358
- }
359
- publishTemplate(e11, t2 = null) {
360
- let n2 = this.unsignedTransaction.blobs.length;
361
- return this.unsignedTransaction.blobs.push(e11), this.addInstruction({ PublishTemplate: {
362
- binary: n2,
363
- metadata_hash: t2
364
- } });
365
- }
366
- addInstruction(e11) {
367
- return this.observeAllocations(this.workspaceIds, [e11]), this.unsignedTransaction.instructions.push(e11), this;
368
- }
369
- addFeeInstruction(e11) {
370
- return this.observeAllocations(this.feeWorkspaceIds, [e11]), this.unsignedTransaction.fee_instructions.push(e11), this;
371
- }
372
- withInstructions(e11) {
373
- return this.observeAllocations(this.workspaceIds, e11), this.unsignedTransaction.instructions.push(...e11), this;
374
- }
375
- withFeeInstructions(e11) {
376
- return this.observeAllocations(this.feeWorkspaceIds, e11), this.unsignedTransaction.fee_instructions.push(...e11), this;
377
- }
378
- observeAllocations(e11, t2) {
379
- for (let n2 of t2) {
380
- let t3 = Je(n2);
381
- t3 !== null && e11.observeAllocated(t3);
382
- }
383
- }
384
- withFeeInstructionsBuilder(t2) {
385
- let n2 = t2(new e2(this.unsignedTransaction.network, this.unsignedTransaction.max_epoch));
386
- return this.unsignedTransaction.fee_instructions = n2.unsignedTransaction.instructions, this.feeWorkspaceIds.resetFrom(this.unsignedTransaction.fee_instructions), this;
387
- }
388
- addInput(e11) {
389
- return this.unsignedTransaction.inputs.push(e11), this;
390
- }
391
- withInputs(e11) {
392
- return this.unsignedTransaction.inputs.push(...e11), this;
393
- }
394
- withMinEpoch(e11) {
395
- return this.unsignedTransaction.min_epoch = R(e11, "minEpoch"), this;
396
- }
397
- withMaxEpoch(e11) {
398
- return this.unsignedTransaction.max_epoch = R(e11, "maxEpoch"), this;
399
- }
400
- withNonce(e11) {
401
- if (typeof e11 == "number" && !Number.isInteger(e11)) throw new v(`withNonce: nonce must be an integer, got ${e11}`);
402
- let t2 = typeof e11 == "bigint" ? e11 : BigInt(e11);
403
- if (t2 < 0n || t2 > Ke) throw new v(`withNonce: nonce must fit an unsigned 64-bit integer, got ${e11}`);
404
- return this.unsignedTransaction.nonce = t2 > BigInt(2 ** 53 - 1) ? t2 : Number(t2), this;
405
- }
406
- withUnsignedTransaction(e11) {
407
- return this.unsignedTransaction = {
408
- ...e11,
409
- instructions: [...e11.instructions],
410
- fee_instructions: [...e11.fee_instructions],
411
- inputs: [...e11.inputs],
412
- blobs: [...e11.blobs ?? []],
413
- nonce: e11.nonce ?? 0,
414
- max_epoch: R(e11.max_epoch, "withUnsignedTransaction: max_epoch")
415
- }, this.workspaceIds.resetFrom(this.unsignedTransaction.instructions), this.feeWorkspaceIds.resetFrom(this.unsignedTransaction.fee_instructions), this;
416
- }
417
- resolveWorkspaceOffsetId(e11) {
418
- return this.getOffsetIdFromWorkspaceName(e11);
419
- }
420
- buildUnsignedTransaction() {
421
- return {
422
- ...this.unsignedTransaction,
423
- instructions: [...this.unsignedTransaction.instructions],
424
- fee_instructions: [...this.unsignedTransaction.fee_instructions],
425
- inputs: [...this.unsignedTransaction.inputs],
426
- blobs: [...this.unsignedTransaction.blobs]
427
- };
428
- }
429
- addNamedId(e11) {
430
- return this.workspaceIds.insert(e11);
431
- }
432
- requireNamedId(e11) {
433
- let t2 = this.workspaceIds.get(e11);
434
- if (t2 === void 0) throw Xe(e11);
435
- return t2;
436
- }
437
- getOffsetIdFromWorkspaceName(e11) {
438
- let t2 = He(e11);
439
- return {
440
- id: this.requireNamedId(t2.name),
441
- offset: t2.offset
442
- };
443
- }
444
- resolveArgs(e11) {
445
- return e11.map((e12) => typeof e12 == "object" && e12 && "Workspace" in e12 && typeof e12.Workspace == "string" ? { Workspace: this.getOffsetIdFromWorkspaceName(e12.Workspace) } : e12);
446
- }
447
- }, Ze = {
448
- [u.LocalNet]: "http://localhost:12500",
449
- [u.Esmeralda]: "https://ootle-indexer-a.tari.com"
450
- };
451
- function Qe(e11) {
452
- let t2 = Ze[e11];
453
- if (t2 !== void 0) return t2;
454
- throw new v(`No default indexer URL is configured for ${u[e11]}. Pass an explicit URL via ProviderBuilder.withUrl(...) or IndexerProvider.connect({ url, network: Network.${u[e11]} }).`);
455
- }
456
- var $e = 132, et = 32;
457
- function* tt(e11) {
458
- let t2 = /* @__PURE__ */ new Set();
459
- for (let n2 of B(e11, $e)) n2.length === et * 2 && !t2.has(n2) && (t2.add(n2), yield `vault_${n2}`);
460
- }
461
- async function nt(e11, t2) {
462
- let n2 = (await e11.getSubstate(t2)).substate;
463
- return it(n2) ? Array.from(tt(n2.Component.body.state)) : [];
464
- }
465
- function* B(e11, t2) {
466
- if (Array.isArray(e11)) {
467
- for (let n3 of e11) yield* B(n3, t2);
468
- return;
469
- }
470
- if (typeof e11 != "object" || !e11) return;
471
- let n2 = e11, r2 = n2["@cbor"];
472
- if (typeof r2 == "string") {
473
- if (r2 === "tag") {
474
- let e12 = n2.value;
475
- if (n2.tag === t2) {
476
- let t3 = rt(e12);
477
- t3 !== null && (yield t3);
478
- }
479
- yield* B(e12, t2);
480
- } else if (r2 === "map") {
481
- let e12 = n2.entries;
482
- if (Array.isArray(e12)) for (let n3 of e12) Array.isArray(n3) && n3.length === 2 && (yield* B(n3[0], t2), yield* B(n3[1], t2));
483
- }
484
- return;
485
- }
486
- for (let e12 of Object.values(n2)) yield* B(e12, t2);
487
- }
488
- function rt(e11) {
489
- if (typeof e11 != "object" || !e11) return null;
490
- let t2 = e11;
491
- if (t2["@cbor"] !== "bytes") return null;
492
- let n2 = t2.hex;
493
- return typeof n2 == "string" ? n2 : null;
494
- }
495
- function it(e11) {
496
- return typeof e11 == "object" && !!e11 && "Component" in e11;
497
- }
498
- var at = "resource_0101010101010101010101010101010101010101010101010101010101010101", ot = "component_0102030000000000000000000000000000000000000000000000000000000000", st = "vault_0102030000000000000000000000000000000000000000000000000000000001", ct = "resource_0102030000000000000000000000000000000000000000000000000000000002", lt = "__ootleRawJson", ut = "stealth_revealed_input", dt = "stealth_revealed_change";
499
- function ft(e11, t2) {
500
- let n2 = e11.revealedInputBucket === null ? null : t2(e11.revealedInputBucket);
501
- return { StealthTransfer: {
502
- resource_address_ref: { Address: e11.resourceAddress },
503
- statement: V(e11.statement),
504
- revealed_input_bucket: n2
505
- } };
506
- }
507
- function V(e11) {
508
- return { [lt]: e11.toCompactJson() };
509
- }
510
- function H(e11) {
511
- return typeof e11 == "object" && !!e11 && "StealthTransfer" in e11;
512
- }
513
- function U(e11) {
514
- let t2 = [], n2 = (e12) => `__ootleRawJson:${e12}__`, r2 = JSON.stringify(e11, (e12, r3) => {
515
- if (typeof r3 == "bigint") return r3.toString();
516
- if (typeof r3 == "object" && r3 && "__ootleRawJson" in r3) {
517
- let e13 = n2(t2.length);
518
- return t2.push(r3[lt]), e13;
519
- }
520
- return r3;
521
- });
522
- for (let e12 = 0; e12 < t2.length; e12++) {
523
- let i2 = JSON.stringify(n2(e12)), a2 = r2.indexOf(i2);
524
- if (a2 < 0) throw new v(`serializeUnsignedTx: raw-JSON placeholder ${e12} was not emitted`);
525
- if (r2.indexOf(i2, a2 + i2.length) >= 0) throw new v(`serializeUnsignedTx: raw-JSON placeholder ${e12} collided with transaction data`);
526
- r2 = r2.slice(0, a2) + t2[e12] + r2.slice(a2 + i2.length);
527
- }
528
- return r2;
529
- }
530
- async function W(e11, t2) {
531
- let n2 = await e11.resolveInputs(t2.inputs);
532
- return {
533
- ...t2,
534
- inputs: n2
535
- };
536
- }
537
- function pt() {
538
- let { secret_key: e11, public_key: t2 } = a();
539
- return {
540
- secret_key: e11,
541
- public_key: t2
542
- };
543
- }
544
- function mt(e11, t2) {
545
- return {
546
- public_key: b(y(e11, 32, "publicKey")),
547
- signature: {
548
- public_nonce: b(y(t2.public_nonce, 32, "schnorr.public_nonce")),
549
- signature: b(y(t2.signature, 32, "schnorr.signature"))
550
- }
551
- };
552
- }
553
- async function G(e11, t2, n2) {
554
- let r2 = n2 ?? a(), i2 = y(r2.secret_key, 32, "sealKeypair.secret_key"), o2 = y(r2.public_key, 32, "sealKeypair.public_key"), s2 = [];
555
- for (let n3 of e11) {
556
- let e12 = await n3.signTransaction(t2, o2);
557
- s2.push(...e12);
558
- }
559
- let c2 = l(`{"transaction":${U(t2)},"signatures":${JSON.stringify(s2)}}`, i2);
560
- return {
561
- sealedJson: c2,
562
- transaction: JSON.parse(c2)
563
- };
564
- }
565
- function K(e11) {
566
- return t(e11.sealedJson);
567
- }
568
- async function ht(e11, t2) {
569
- return (await e11.submitTransaction(t2)).transaction_id;
570
- }
571
- function gt(e11) {
572
- var _a, _b;
573
- if (e11 === "Pending" || !("Finalized" in e11)) return null;
574
- let t2 = e11.Finalized, n2 = t2.final_decision;
575
- if (n2 === null) return null;
576
- if (n2 === "Commit") return { outcome: n2 };
577
- if (typeof n2 == "object" && n2 && "Abort" in n2) {
578
- let e12 = t2.abort_details ?? JSON.stringify(n2.Abort), r2 = (_b = (_a = t2.execution_result) == null ? void 0 : _a.finalize) == null ? void 0 : _b.result;
579
- return typeof r2 == "object" && r2 && "AcceptFeeRejectRest" in r2 ? {
580
- outcome: "FeeIntentCommit",
581
- reason: e12
582
- } : {
583
- outcome: "Reject",
584
- reason: e12
585
- };
586
- }
587
- throw new v(`Unexpected final_decision variant: ${JSON.stringify(n2)}`);
588
- }
589
- async function _t(e11, t2, n2) {
590
- let r2 = (n2 == null ? void 0 : n2.pollIntervalMs) ?? 500, i2 = (n2 == null ? void 0 : n2.timeoutMs) ?? 6e4, a2 = Date.now() + i2;
591
- for (; ; ) {
592
- let n3 = await e11.getTransactionResult(t2), o2 = n3.result;
593
- if (o2 !== "Pending" && "Finalized" in o2) {
594
- let { outcome: e12, reason: r3 } = gt(o2) ?? { outcome: "Reject" };
595
- if (e12 === "Reject") throw new f(`Transaction ${t2} was rejected: ${r3}`, {
596
- txId: t2,
597
- reason: r3 ?? ""
598
- });
599
- if (e12 === "FeeIntentCommit") throw new f(`Transaction ${t2} only committed fees (execution aborted): ${r3}`, {
600
- txId: t2,
601
- reason: `FeeIntentCommit: ${r3 ?? ""}`
602
- });
603
- return n3;
604
- }
605
- if (Date.now() >= a2) throw new ae(`Transaction ${t2} did not finalize within ${i2}ms`, { txId: t2 });
606
- await new Promise((e12) => setTimeout(e12, r2));
607
- }
608
- }
609
- async function vt(e11, t2, n2, r2) {
610
- let i2 = await W(e11, n2);
611
- return _t(e11, await ht(e11, K(await G(Array.isArray(t2) ? t2 : [t2], i2))), r2);
612
- }
613
- async function yt(e11, t2, n2, r2) {
614
- return vt(e11, t2, {
615
- ...n2,
616
- dry_run: true
617
- }, r2);
618
- }
619
- var bt = class {
620
- constructor(e11, t2) {
621
- __publicField(this, "builder");
622
- this.builder = z.new(e11, t2);
623
- }
624
- withInputs(e11) {
625
- return this.builder.withInputs(e11), this;
626
- }
627
- feeTransactionPayFromComponent(e11, t2) {
628
- return this.builder.feeTransactionPayFromComponent(e11, t2), this;
629
- }
630
- publicTransfer(e11, t2, n2, r2) {
631
- return this.builder.callMethod({
632
- componentAddress: e11,
633
- methodName: "withdraw"
634
- }, [A(t2), L(n2)]).saveVar("bucket").callMethod({
635
- componentAddress: r2,
636
- methodName: "deposit"
637
- }, [{ Workspace: "bucket" }]), this;
638
- }
639
- publishTemplate(e11, t2, n2) {
640
- return this.builder.publishTemplate(t2), n2 && this.builder.saveVar(n2), this;
641
- }
642
- build() {
643
- return this.builder.buildUnsignedTransaction();
644
- }
645
- }, xt = class {
646
- constructor(e11, t2, n2) {
647
- __publicField(this, "builder");
648
- __publicField(this, "faucetAddress");
649
- this.builder = z.new(e11, t2), this.faucetAddress = n2;
650
- }
651
- feeTransactionPayFromComponent(e11, t2) {
652
- return this.builder.feeTransactionPayFromComponent(e11, t2), this;
653
- }
654
- takeFaucetFunds(e11, t2) {
655
- return this.builder.callMethod({
656
- componentAddress: this.faucetAddress,
657
- methodName: "take_free_coins"
658
- }, [L(t2)]).saveVar("faucet_bucket").callMethod({
659
- componentAddress: e11,
660
- methodName: "deposit"
661
- }, [{ Workspace: "faucet_bucket" }]), this;
662
- }
663
- takeMaxFaucetFunds(e11) {
664
- return this.builder.callMethod({
665
- componentAddress: this.faucetAddress,
666
- methodName: "take_max_free_coins"
667
- }, []).saveVar("faucet_bucket").callMethod({
668
- componentAddress: e11,
669
- methodName: "deposit"
670
- }, [{ Workspace: "faucet_bucket" }]), this;
671
- }
672
- publishTemplate(e11, t2) {
673
- let n2 = t2 ?? "template";
674
- return this.builder.callFunction({
675
- templateAddress: e11,
676
- functionName: "new"
677
- }, []).saveVar(n2), this;
678
- }
679
- build() {
680
- return this.builder.buildUnsignedTransaction();
681
- }
682
- }, St = class {
683
- constructor() {
684
- __publicField(this, "keyProviders");
685
- __publicField(this, "defaultSignerAddress", null);
686
- this.keyProviders = /* @__PURE__ */ new Map();
687
- }
688
- registerKeyProvider(e11, t2) {
689
- return this.keyProviders.set(e11, t2), this;
690
- }
691
- setDefaultSigner(e11) {
692
- return this.requireKeyProvider(e11, "explicit"), this.defaultSignerAddress = e11, this;
693
- }
694
- async getAddress() {
695
- if (!this.defaultSignerAddress) throw new h("No default signer address set. Call setDefaultSigner() first.");
696
- return Promise.resolve(this.defaultSignerAddress);
697
- }
698
- async getPublicKey() {
699
- return this.getSignerOrThrow().getPublicKey();
700
- }
701
- async signTransaction(e11, t2) {
702
- return this.getSignerOrThrow().signTransaction(e11, t2);
703
- }
704
- async authorizeTransaction(e11, t2, n2) {
705
- return {
706
- signerAddress: e11,
707
- signatures: await this.requireKeyProvider(e11, "explicit").signTransaction(t2, n2)
708
- };
709
- }
710
- async authorizeTransactionAll(e11, t2) {
711
- return Promise.all([...this.keyProviders.keys()].map((n2) => this.authorizeTransaction(n2, e11, t2)));
712
- }
713
- getKeyProvider(e11) {
714
- return this.keyProviders.get(e11);
715
- }
716
- getSignerAddresses() {
717
- return [...this.keyProviders.keys()];
718
- }
719
- getSignerOrThrow() {
720
- if (!this.defaultSignerAddress) throw new h("No default signer address set. Call setDefaultSigner() first.");
721
- return this.requireKeyProvider(this.defaultSignerAddress, "default");
722
- }
723
- requireKeyProvider(e11, t2) {
724
- let n2 = this.keyProviders.get(e11);
725
- if (n2 === void 0) throw new m(t2 === "default" ? `No key provider registered for default address: ${e11}. Call wallet.registerKeyProvider(address, signer) for that address, or pick a different default with setDefaultSigner(otherAddress).` : `No key provider registered for address: ${e11}. Call wallet.registerKeyProvider(${JSON.stringify(e11)}, signer) before signing.`, { address: e11 });
726
- return n2;
727
- }
728
- }, Ct = class {
729
- constructor(e11, t2) {
730
- __publicField(this, "bytes");
731
- this.bytes = new Uint8Array(y(e11, 32, t2));
732
- }
733
- toBytes() {
734
- return new Uint8Array(this.bytes);
735
- }
736
- toHex() {
737
- return b(this.bytes);
738
- }
739
- }, q = class e3 extends Ct {
740
- constructor(e11) {
741
- super(e11, "Mask");
742
- }
743
- static zero() {
744
- return new e3(new Uint8Array(32));
745
- }
746
- static fromBytes(t2) {
747
- return new e3(t2);
748
- }
749
- static fromHex(t2) {
750
- return new e3(x(t2));
751
- }
752
- }, wt = class e4 {
753
- constructor(e11) {
754
- __publicField(this, "bytes");
755
- this.bytes = new Uint8Array(e11);
756
- }
757
- static fromHex(t2) {
758
- return new e4(x(t2));
759
- }
760
- toBytes() {
761
- return new Uint8Array(this.bytes);
762
- }
763
- toHex() {
764
- return b(this.bytes);
765
- }
766
- }, Tt = Object.freeze({ StealthPublicKey: Object.freeze({}) });
767
- function Et(e11) {
768
- if (e11.amount <= 0n) throw new v(`Output amount must be > 0, got ${e11.amount}`);
769
- return {
770
- destination: e11.destination,
771
- amount: e11.amount,
772
- resourceAddress: e11.resourceAddress,
773
- resourceViewKey: e11.resourceViewKey === void 0 ? void 0 : new Uint8Array(e11.resourceViewKey),
774
- memo: e11.memo,
775
- payTo: e11.payTo ?? Tt,
776
- minimumValuePromise: e11.minimumValuePromise ?? 0n
777
- };
778
- }
779
- function Dt(e11, t2) {
780
- if (e11.trim() === "") throw new v(`${t2} must be a non-empty decimal string`);
781
- return BigInt(e11);
782
- }
783
- function Ot(e11, t2) {
784
- let n2 = e11.trim();
785
- if (!n2.startsWith("{") || !n2.endsWith("}")) throw new v(`StealthTransferStatement.toCompactJson: ${t2} fragment is not a JSON object (got: ${e11.slice(0, 60)}...)`);
786
- }
787
- var J = class e5 extends Ct {
788
- constructor(e11) {
789
- super(e11, "StealthInput.commitment");
790
- }
791
- get commitment() {
792
- return this.toBytes();
793
- }
794
- toJSON() {
795
- return { commitment: this.toHex() };
796
- }
797
- static fromJSON(t2) {
798
- return new e5(x(t2.commitment));
799
- }
800
- }, Y = class e6 {
801
- constructor(e11, t2) {
802
- __publicField(this, "_publicNonce");
803
- __publicField(this, "_signature");
804
- this._publicNonce = new Uint8Array(y(e11, 32, "BalanceProofSignature.publicNonce")), this._signature = new Uint8Array(y(t2, 32, "BalanceProofSignature.signature"));
805
- }
806
- get publicNonce() {
807
- return new Uint8Array(this._publicNonce);
808
- }
809
- get signature() {
810
- return new Uint8Array(this._signature);
811
- }
812
- toJSON() {
813
- return {
814
- public_nonce: b(this._publicNonce),
815
- signature: b(this._signature)
816
- };
817
- }
818
- static fromJSON(t2) {
819
- return new e6(x(t2.public_nonce), x(t2.signature));
820
- }
821
- }, X = class e7 {
822
- constructor(e11, t2, n2) {
823
- __publicField(this, "inputs");
824
- __publicField(this, "revealedAmount");
825
- __publicField(this, "statementJson");
826
- this.inputs = e11, this.revealedAmount = t2, this.statementJson = n2;
827
- }
828
- static revealedOnly(t2) {
829
- return new e7([], t2);
830
- }
831
- toJSON() {
832
- return {
833
- inputs: this.inputs.map((e11) => e11.toJSON()),
834
- revealed_amount: Ve(this.revealedAmount)
835
- };
836
- }
837
- static fromJSON(t2) {
838
- return new e7(t2.inputs.map(J.fromJSON), Dt(t2.revealed_amount, "revealed_amount"));
839
- }
840
- }, Z = class e8 {
841
- constructor(e11) {
842
- __publicField(this, "statementJson");
843
- this.statementJson = e11;
844
- }
845
- parsed() {
846
- return JSON.parse(this.statementJson);
847
- }
848
- toJSON() {
849
- return this.statementJson;
850
- }
851
- static fromJSON(t2) {
852
- return new e8(t2);
853
- }
854
- }, Q = class e9 {
855
- constructor(e11, t2, n2) {
856
- __publicField(this, "inputsStatement");
857
- __publicField(this, "outputsStatement");
858
- __publicField(this, "balanceProof");
859
- this.inputsStatement = e11, this.outputsStatement = t2, this.balanceProof = n2;
860
- }
861
- toJSON() {
862
- let e11 = {
863
- inputs: this.inputsStatement.toJSON(),
864
- outputs: this.outputsStatement.toJSON()
865
- };
866
- return this.balanceProof !== void 0 && (e11.balance_proof = this.balanceProof.toJSON()), e11;
867
- }
868
- toCompactJson() {
869
- let e11 = this.inputsStatement.statementJson ?? JSON.stringify(this.inputsStatement.toJSON()), t2 = this.outputsStatement.statementJson;
870
- Ot(e11, "inputs_statement"), Ot(t2, "outputs_statement");
871
- let n2 = `{"inputs_statement":${e11},"outputs_statement":${t2},"covenant_claims":[]`;
872
- return this.balanceProof !== void 0 && (n2 += `,"balance_proof":${JSON.stringify(this.balanceProof.toJSON())}`), n2 + "}";
873
- }
874
- static fromJSON(t2) {
875
- return new e9(X.fromJSON(t2.inputs), Z.fromJSON(t2.outputs), t2.balance_proof === void 0 ? void 0 : Y.fromJSON(t2.balance_proof));
876
- }
877
- };
878
- function kt(e11, t2) {
879
- let n2 = new Uint8Array(e11.length * 32);
880
- for (let r2 = 0; r2 < e11.length; r2++) {
881
- let i2 = e11[r2];
882
- y(i2, 32, `${t2}[${r2}]`), n2.set(i2, r2 * 32);
883
- }
884
- return n2;
885
- }
886
- var At = class {
887
- constructor(e11 = u.LocalNet) {
888
- __publicField(this, "network");
889
- this.network = e11;
890
- }
891
- async generateOutputsStatement(e11, t2) {
892
- let n2 = s(`[${e11.map((e12) => this.outputWitness(e12)).join(",")}]`, t2);
893
- return y(n2.aggregated_output_mask, 32, "StealthOutputsResult.aggregated_output_mask"), {
894
- statement: Z.fromJSON(n2.statement_json),
895
- outputMask: q.fromBytes(n2.aggregated_output_mask)
896
- };
897
- }
898
- outputWitness(e11) {
899
- let t2 = c(e11.destination), n2 = jt(e11.payTo) ? null : JSON.stringify(e11.payTo), i2 = e11.memo === void 0 ? null : JSON.stringify(e11.memo), a2 = e11.resourceViewKey ?? null;
900
- return r(this.network, t2.owner_key, t2.view_key, e11.amount, e11.resourceAddress, a2, i2, n2, e11.minimumValuePromise);
901
- }
902
- async buildInputsStatement(e11, t2) {
903
- return new X(e11, t2, n(kt(e11.map((e12) => e12.commitment), "input commitment"), t2));
904
- }
905
- async generateBalanceProofSignature(e11, t2, n2, r2) {
906
- let i2 = o(e11.toBytes(), t2.toBytes(), n2, r2);
907
- return y(i2.public_nonce, 32, "balance proof public_nonce"), y(i2.signature, 32, "balance proof signature"), new Y(i2.public_nonce, i2.signature);
908
- }
909
- async validateBalanceProofSignature(e11, t2, n2) {
910
- return ne(e11.publicNonce, e11.signature, t2, n2);
911
- }
912
- async deriveAeadKey(e11, t2) {
913
- y(e11, 32, "deriveAeadKey privateKey"), y(t2, 32, "deriveAeadKey publicKey");
914
- let n2 = i(e11, t2);
915
- return y(n2, 32, "derived AEAD key"), n2;
916
- }
917
- async unblindOutput(e11, t2, n2, r2) {
918
- y(e11, 32, "unblindOutput commitment"), y(n2, 32, "unblindOutput aeadKey");
919
- let i2 = te(e11, t2, n2, r2);
920
- return {
921
- mask: q.fromBytes(i2.mask),
922
- value: i2.value,
923
- memo: i2.memo_json ?? void 0
924
- };
925
- }
926
- async aggregateInputMasks(t2) {
927
- let n2 = e(kt(t2.map((e11) => e11.toBytes()), "input mask"));
928
- return q.fromBytes(n2);
929
- }
930
- async stealthDhSecret(e11, t2, n2) {
931
- y(t2, 32, "stealthDhSecret ownerSecret"), y(n2, 32, "stealthDhSecret publicNonce");
932
- let r2 = ee(e11, t2, n2);
933
- return y(r2, 32, "stealth DH secret"), r2;
934
- }
935
- async validateTransfer(e11) {
936
- re(e11.toCompactJson(), null);
937
- }
938
- };
939
- function jt(e11) {
940
- let t2 = Object.keys(e11);
941
- if (t2.length !== 1 || t2[0] !== "StealthPublicKey") return false;
942
- let n2 = e11.StealthPublicKey;
943
- return typeof n2 == "object" && !!n2 && Object.keys(n2).length === 0;
944
- }
945
- var Mt = "utxo";
946
- function Nt(e11, t2) {
947
- return `${Mt}_${e11.startsWith("resource_") ? e11.slice(9) : e11}_${b(t2)}`;
948
- }
949
- var Pt = 64;
950
- function Ft(e11) {
951
- return typeof e11 == "object" && !!e11 && "Utxo" in e11;
952
- }
953
- function It(e11) {
954
- return e11.length > 0 && /^[0-9a-fA-F]+$/.test(e11);
955
- }
956
- function Lt(e11) {
957
- let t2 = e11.split("_");
958
- if (t2.length < 3 || t2[0] !== Mt) return null;
959
- let n2 = t2[t2.length - 1];
960
- return n2.length !== Pt || !It(n2) ? null : x(n2);
961
- }
962
- function Rt(e11, t2) {
963
- let n2 = e11.substate;
964
- if (!Ft(n2)) return null;
965
- let r2 = n2.Utxo;
966
- if (r2.output === null || r2.is_frozen) return null;
967
- let i2 = Lt(t2);
968
- return i2 === null ? null : {
969
- commitment: i2,
970
- body: r2.output.output
971
- };
972
- }
973
- var zt = 0, Bt = class {
974
- constructor(e11, t2, n2 = new At(e11.network())) {
975
- __publicField(this, "provider");
976
- __publicField(this, "crypto");
977
- __publicField(this, "builder");
978
- __publicField(this, "state");
979
- __publicField(this, "maxEpoch", null);
980
- __publicField(this, "prepared", false);
981
- __publicField(this, "revealedOutputBucketVar", null);
982
- this.provider = e11, this.crypto = n2, this.builder = z.new(e11.network(), zt), this.state = {
983
- resource: t2,
984
- revealedInput: null,
985
- inputsToSpend: /* @__PURE__ */ new Map(),
986
- outputs: [],
987
- revealedOutputAmount: 0n
988
- };
989
- }
990
- spendRevealedInput(e11, t2) {
991
- if (t2 <= 0n) throw new v(`spendRevealedInput amount must be > 0, got ${t2}`);
992
- let n2 = this.state.revealedInput;
993
- if (n2 !== null && n2.source !== e11) throw new v(`spendRevealedInput: all revealed input must come from one account (${n2.source} != ${e11})`);
994
- return this.state.revealedInput = {
995
- source: e11,
996
- amount: ((n2 == null ? void 0 : n2.amount) ?? 0n) + t2
997
- }, this;
998
- }
999
- toStealthOutput(e11) {
1000
- return this.state.outputs.push(e11), this;
1001
- }
1002
- toRevealedOutput(e11) {
1003
- if (e11 <= 0n) throw new v(`toRevealedOutput amount must be > 0, got ${e11}`);
1004
- return this.state.revealedOutputAmount += e11, this;
1005
- }
1006
- toRevealedOutputAsBucket(e11, t2) {
1007
- if (e11 <= 0n) throw new v(`toRevealedOutputAsBucket amount must be > 0, got ${e11}`);
1008
- if (this.revealedOutputBucketVar !== null && this.revealedOutputBucketVar !== t2) throw new v(`toRevealedOutputAsBucket: already routing revealed change to workspace "${this.revealedOutputBucketVar}" \u2014 call with the same name to accumulate, or use a single call`);
1009
- return this.revealedOutputBucketVar = t2, this.state.revealedOutputAmount += e11, this;
1010
- }
1011
- andThen(e11) {
1012
- return this.builder.withInstructions(e11), this;
1013
- }
1014
- payFeeFromRevealed(e11) {
1015
- if (this.state.revealedInput === null) throw new v("payFeeFromRevealed: call spendRevealedInput first to set the source account");
1016
- if (e11 <= 0n) throw new v(`payFeeFromRevealed amount must be > 0, got ${e11}`);
1017
- return this.builder.feeTransactionPayFromComponent(this.state.revealedInput.source, e11), this;
1018
- }
1019
- withMaxEpoch(e11) {
1020
- return this.builder.withMaxEpoch(e11), this.maxEpoch = e11, this;
1021
- }
1022
- withBuilder(e11) {
1023
- return this.builder = e11(this.builder), this;
1024
- }
1025
- spendStealthInput(e11, t2) {
1026
- let n2 = new J(t2), r2 = b(n2.commitment), i2 = this.state.inputsToSpend.get(r2);
1027
- if (i2 !== void 0) throw new v(`spendStealthInput: duplicate commitment ${r2} \u2014 each commitment is one UTXO and may be spent once (already added for owner ${i2.owner}). A commitment uniquely identifies a UTXO; adding it again (even under a different owner) would self-double-spend.`);
1028
- return this.state.inputsToSpend.set(r2, {
1029
- input: n2,
1030
- owner: e11
1031
- }), this;
1032
- }
1033
- async prepare() {
1034
- var _a;
1035
- if (this.prepared) throw new v("StealthTransfer.prepare: already prepared \u2014 create a new StealthTransfer to build again");
1036
- this.validate(), this.prepared = true;
1037
- let { statement: e11, outputMask: t2 } = await this.crypto.generateOutputsStatement(this.state.outputs, this.state.revealedOutputAmount), n2 = new Q(await this.crypto.buildInputsStatement([...this.state.inputsToSpend.values()].map((e12) => e12.input), ((_a = this.state.revealedInput) == null ? void 0 : _a.amount) ?? 0n), e11, void 0);
1038
- this.emitInstructions(n2), this.maxEpoch === null && this.builder.buildUnsignedTransaction().max_epoch === zt && this.builder.withMaxEpoch(await Ge(this.provider));
1039
- let r2 = /* @__PURE__ */ new Set();
1040
- if (this.state.revealedInput !== null) {
1041
- let e12 = this.state.revealedInput.source;
1042
- this.builder.addInput({
1043
- substate_id: e12,
1044
- version: null
1045
- }), r2.add(e12);
1046
- let t3 = await nt(this.provider, e12);
1047
- for (let e13 of t3) r2.has(e13) || (this.builder.addInput({
1048
- substate_id: e13,
1049
- version: null
1050
- }), r2.add(e13));
1051
- }
1052
- for (let { input: e12 } of this.state.inputsToSpend.values()) {
1053
- let t3 = Nt(this.state.resource, e12.commitment);
1054
- r2.has(t3) || (this.builder.addInput({
1055
- substate_id: t3,
1056
- version: null
1057
- }), r2.add(t3));
1058
- }
1059
- let i2 = await W(this.provider, this.builder.buildUnsignedTransaction()), a2 = [...this.state.inputsToSpend.values()], o2 = this.collectRequiredSigners(a2);
1060
- return {
1061
- unsignedTx: i2,
1062
- statement: n2,
1063
- outputMask: t2,
1064
- state: this.state,
1065
- requiredSigners: o2,
1066
- inputs: a2
1067
- };
1068
- }
1069
- collectRequiredSigners(e11) {
1070
- let t2 = [], n2 = /* @__PURE__ */ new Set();
1071
- this.state.revealedInput !== null && (t2.push(this.state.revealedInput.source), n2.add(this.state.revealedInput.source));
1072
- for (let { owner: r2 } of e11) n2.has(r2) || (t2.push(r2), n2.add(r2));
1073
- return t2;
1074
- }
1075
- validate() {
1076
- var _a;
1077
- let e11 = this.state.inputsToSpend.size > 0, t2 = ((_a = this.state.revealedInput) == null ? void 0 : _a.amount) ?? 0n;
1078
- if (!(t2 > 0n || e11)) throw new v("StealthTransfer.prepare: no inputs \u2014 call spendRevealedInput or spendStealthInput first");
1079
- if (this.state.outputs.length === 0 && this.state.revealedOutputAmount === 0n) throw new v("StealthTransfer.prepare: no outputs \u2014 call toStealthOutput or toRevealedOutput first");
1080
- if (this.state.outputs.length === 0) throw new v("StealthTransfer.prepare: at least one stealth output is required");
1081
- if (this.state.revealedOutputAmount > 0n && this.state.revealedInput === null) throw new v("StealthTransfer.prepare: revealed change requires a revealed source account to deposit into");
1082
- if (!e11) {
1083
- let e12 = this.state.outputs.reduce((e13, t3) => e13 + t3.amount, 0n), n2 = e12 + this.state.revealedOutputAmount, r2 = t2;
1084
- if (r2 !== n2) throw new v(`StealthTransfer.prepare: unbalanced transfer \u2014 revealed input ${r2} != stealth out ${e12} + revealed out ${this.state.revealedOutputAmount} (= ${n2})`);
1085
- }
1086
- }
1087
- emitInstructions(e11) {
1088
- let t2 = this.state.revealedInput, n2 = null;
1089
- t2 !== null && (this.builder.callMethod({
1090
- componentAddress: t2.source,
1091
- methodName: "withdraw"
1092
- }, [A(this.state.resource), k(t2.amount)]).saveVar(ut), n2 = ut), this.builder.addInstruction(ft({
1093
- resourceAddress: this.state.resource,
1094
- revealedInputBucket: n2,
1095
- statement: e11
1096
- }, (e12) => this.builder.resolveWorkspaceOffsetId(e12))), this.state.revealedOutputAmount > 0n && t2 !== null && (this.revealedOutputBucketVar === null ? this.builder.saveVar(dt).callMethod({
1097
- componentAddress: t2.source,
1098
- methodName: "deposit"
1099
- }, [{ Workspace: dt }]) : this.builder.saveVar(this.revealedOutputBucketVar));
1100
- }
1101
- };
1102
- function Vt(e11) {
1103
- if (Ht(e11) || e11.statementJson !== void 0) return e11.statementJson;
1104
- if (e11.inputs.length > 0) throw new _("inputs statement has confidential inputs but no WASM statementJson \u2014 call buildInputsStatement first", { context: "statementJsonFor" });
1105
- return JSON.stringify(e11.toJSON());
1106
- }
1107
- function Ht(e11) {
1108
- return !("inputs" in e11);
1109
- }
1110
- async function Ut(e11, t2, n2, r2, i2) {
1111
- let a2 = Vt(r2), o2 = Vt(i2);
1112
- return e11.generateBalanceProofSignature(t2, n2, a2, o2);
1113
- }
1114
- async function $(e11, t2, n2, r2) {
1115
- let i2 = await e11.deriveAeadKey(r2.viewSecret, r2.senderPublicNonce);
1116
- return e11.unblindOutput(t2, n2, i2, r2.skipMemo);
1117
- }
1118
- async function Wt(e11, t2, n2, r2) {
1119
- let i2 = Rt(n2, r2);
1120
- if (i2 === null) return null;
1121
- let a2 = x(i2.body.encrypted_data), o2 = x(i2.body.public_nonce);
1122
- try {
1123
- return await $(e11, i2.commitment, a2, {
1124
- senderPublicNonce: o2,
1125
- viewSecret: t2,
1126
- skipMemo: false
1127
- });
1128
- } catch {
1129
- return null;
1130
- }
1131
- }
1132
- async function Gt(e11, t2, n2) {
1133
- if (t2.length === 0) throw new v("generateOutputsStatement: at least one stealth output is required");
1134
- let { statement: r2, outputMask: i2 } = await e11.generateOutputsStatement(t2, n2), a2 = await e11.buildInputsStatement([], 0n);
1135
- return new Q(a2, r2, await Ut(e11, q.zero(), i2, a2, r2));
1136
- }
1137
- var Kt = class e10 {
1138
- constructor(e11, t2, n2, r2, i2) {
1139
- this.wallet = e11, this.spec = t2, this.crypto = n2, this.mustSignWithAccountKey = r2, this.viewSecret = i2;
1140
- }
1141
- static fromSpec(t2, n2, r2 = {}) {
1142
- return new e10(t2, n2, r2.crypto ?? new At(), r2.mustSignWithAccountKey ?? true, r2.viewSecret);
1143
- }
1144
- async prepare(e11) {
1145
- let t2 = await this.resolveStealthInputs(e11), n2 = t2.length === 0 ? q.zero() : await this.crypto.aggregateInputMasks(t2.map((e12) => e12.mask)), r2 = await Ut(this.crypto, n2, this.spec.outputMask, this.spec.statement.inputsStatement, this.spec.statement.outputsStatement), i2 = new Q(this.spec.statement.inputsStatement, this.spec.statement.outputsStatement, r2);
1146
- await this.crypto.validateTransfer(i2);
1147
- let a2 = Jt(this.spec.unsignedTx, i2), o2 = {
1148
- ...this.spec,
1149
- unsignedTx: a2,
1150
- statement: i2
1151
- };
1152
- return new qt({
1153
- wallet: this.wallet,
1154
- crypto: this.crypto,
1155
- spec: o2,
1156
- resolvedInputs: t2,
1157
- mustSignWithAccountKey: this.mustSignWithAccountKey,
1158
- sealKeypair: pt()
1159
- });
1160
- }
1161
- async resolveViewSecret() {
1162
- if (this.viewSecret !== void 0) return this.viewSecret;
1163
- let e11 = this.wallet.getKeyProvider(await this.safeDefaultAddress());
1164
- if ((e11 == null ? void 0 : e11.getViewSecret) !== void 0) try {
1165
- return await e11.getViewSecret();
1166
- } catch (e12) {
1167
- throw e12 instanceof p ? e12 : new p("WalletStealthAuthorizer: spending stealth inputs needs a view secret to unblind them, but the wallet's default signer could not provide one. Pass `fromSpec(wallet, spec, { viewSecret })` or use a SecretKeyWallet (created with a view key) as the default signer.", { cause: e12 });
1168
- }
1169
- throw new p("WalletStealthAuthorizer: spending stealth inputs needs a view secret to unblind them, but none was supplied and the wallet's default signer cannot provide one. Pass `fromSpec(wallet, spec, { viewSecret })` or use a SecretKeyWallet (created with a view key) as the default signer.");
1170
- }
1171
- async safeDefaultAddress() {
1172
- try {
1173
- return await this.wallet.getAddress();
1174
- } catch {
1175
- return "";
1176
- }
1177
- }
1178
- async resolveStealthInputs(e11) {
1179
- if (this.spec.inputs.length === 0) return [];
1180
- let t2 = await this.resolveViewSecret(), n2 = this.spec.state.resource, r2 = await Promise.allSettled(this.spec.inputs.map(async ({ input: r3, owner: i3 }) => {
1181
- let a2 = b(r3.commitment), o2 = await e11.getStealthUtxo(n2, r3.commitment);
1182
- if (o2 === null) throw new p(`WalletStealthAuthorizer.prepare: stealth input UTXO not found (spent or never created) for ${a2}`);
1183
- let s2 = Rt(o2, Nt(n2, r3.commitment));
1184
- if (s2 === null) throw new p(`WalletStealthAuthorizer.prepare: stealth input ${a2} is not a live, spendable UTXO`);
1185
- let c2 = x(s2.body.public_nonce), l2;
1186
- try {
1187
- l2 = await $(this.crypto, s2.commitment, x(s2.body.encrypted_data), {
1188
- senderPublicNonce: c2,
1189
- viewSecret: t2,
1190
- skipMemo: true
1191
- });
1192
- } catch (e12) {
1193
- throw new _(`WalletStealthAuthorizer.prepare: failed to decrypt stealth input with commitment ${b(r3.commitment)} (owner ${i3}). This usually means the supplied view secret does not own this UTXO. Pass the correct \`viewSecret\` via \`fromSpec(wallet, spec, { viewSecret })\`, or use the owning SecretKeyWallet (with its view key) as the default signer. Note: a single view secret is resolved for all inputs \u2014 spending UTXOs owned by distinct view keys in one transfer is not supported.`, {
1194
- cause: e12,
1195
- context: "unblindOutput"
1196
- });
1197
- }
1198
- return {
1199
- ownerAddr: i3,
1200
- input: r3,
1201
- mask: l2.mask,
1202
- publicNonce: c2
1203
- };
1204
- })), i2 = r2.find((e12) => e12.status === "rejected");
1205
- if (i2 !== void 0) throw i2.reason;
1206
- return r2.map((e12) => e12.value);
1207
- }
1208
- }, qt = class {
1209
- constructor(e11) {
1210
- __publicField(this, "wallet");
1211
- __publicField(this, "crypto");
1212
- __publicField(this, "spec");
1213
- __publicField(this, "resolvedInputs");
1214
- __publicField(this, "mustSignWithAccountKey");
1215
- __publicField(this, "sealKeypair");
1216
- __publicField(this, "extraSignatures", []);
1217
- __publicField(this, "cachedAuthorizations");
1218
- this.wallet = e11.wallet, this.crypto = e11.crypto, this.spec = e11.spec, this.resolvedInputs = e11.resolvedInputs, this.mustSignWithAccountKey = e11.mustSignWithAccountKey, this.sealKeypair = e11.sealKeypair;
1219
- }
1220
- getSealPublicKey() {
1221
- return this.sealKeypair.public_key;
1222
- }
1223
- getSpec() {
1224
- return this.spec;
1225
- }
1226
- async createAuthorizations() {
1227
- return this.cachedAuthorizations === void 0 && (this.cachedAuthorizations = await this.computeAuthorizations()), this.cachedAuthorizations;
1228
- }
1229
- async computeAuthorizations() {
1230
- if (this.resolvedInputs.length === 0) return [];
1231
- let e11 = U(this.spec.unsignedTx), t2 = this.sealKeypair.public_key, n2 = [];
1232
- for (let r2 of this.resolvedInputs) {
1233
- let i2 = this.wallet.getKeyProvider(r2.ownerAddr);
1234
- if (i2 === void 0) throw new m(`AuthorizedTransfer.createAuthorizations: no signer registered for stealth-input owner ${r2.ownerAddr}. Register it via wallet.registerKeyProvider(owner, signer).`, { address: r2.ownerAddr });
1235
- if (i2.addStealthSignature === void 0) throw new g(`AuthorizedTransfer.createAuthorizations: signer for ${r2.ownerAddr} cannot produce one-time stealth signatures. Use a SecretKeyWallet (which holds the owner secret).`);
1236
- let a2 = await i2.addStealthSignature(e11, r2.publicNonce, t2, { crypto: this.crypto });
1237
- n2.push({
1238
- signerAddress: r2.ownerAddr,
1239
- signatures: [a2]
1240
- });
1241
- }
1242
- return n2;
1243
- }
1244
- addSignature(e11) {
1245
- return this.extraSignatures.push(e11), this;
1246
- }
1247
- async seal() {
1248
- let e11 = (await this.createAuthorizations()).flatMap((e12) => e12.signatures), t2 = [];
1249
- this.mustSignWithAccountKey && t2.push(this.wallet);
1250
- let n2 = [...e11, ...this.extraSignatures];
1251
- return n2.length > 0 && t2.push(new Yt(n2)), K(await G(t2, this.spec.unsignedTx, this.sealKeypair));
1252
- }
1253
- };
1254
- function Jt(e11, t2) {
1255
- let n2 = e11.instructions.findIndex(H);
1256
- if (n2 < 0) throw new v("patchStealthStatement: expected exactly one StealthTransfer instruction, found 0 (not a stealth tx)");
1257
- if (e11.instructions.slice(n2 + 1).findIndex(H) >= 0) throw new v("patchStealthStatement: expected exactly one StealthTransfer instruction, found more than one (malformed)");
1258
- let r2 = { StealthTransfer: {
1259
- ...e11.instructions[n2].StealthTransfer,
1260
- statement: V(t2)
1261
- } }, i2 = [...e11.instructions];
1262
- return i2[n2] = r2, {
1263
- ...e11,
1264
- instructions: i2
1265
- };
1266
- }
1267
- var Yt = class {
1268
- constructor(e11) {
1269
- this.signatures = e11;
1270
- }
1271
- async getAddress() {
1272
- throw new g("StaticSignatureSigner has no address");
1273
- }
1274
- async getPublicKey() {
1275
- throw new g("StaticSignatureSigner has no public key");
1276
- }
1277
- async signTransaction(e11, t2) {
1278
- return this.signatures;
1279
- }
1280
- };
1281
- export {
1282
- bt as AccountInvokeBuilder,
1283
- qt as AuthorizedTransfer,
1284
- Y as BalanceProofSignature,
1285
- _ as CryptoBridgeError,
1286
- We as DEFAULT_TRANSACTION_VALIDITY_EPOCHS,
1287
- h as DefaultSignerNotSetError,
1288
- wt as EncryptedData,
1289
- xt as FaucetInvokeBuilder,
1290
- ie as IndexerClientError,
1291
- v as InvalidArgumentError,
1292
- m as KeyProviderNotFoundError,
1293
- Ue as MAX_TRANSACTION_VALIDITY_EPOCHS,
1294
- q as Mask,
1295
- u as Network,
1296
- d as OotleError,
1297
- St as OotleWallet,
1298
- oe as OperationCancelledError,
1299
- g as SignerError,
1300
- J as StealthInput,
1301
- X as StealthInputsStatement,
1302
- Z as StealthOutputsStatement,
1303
- Bt as StealthTransfer,
1304
- Q as StealthTransferStatement,
1305
- at as TARI_RESOURCE_ADDRESS,
1306
- z as TransactionBuilder,
1307
- f as TransactionRejectedError,
1308
- ae as TransactionTimeoutError,
1309
- p as WalletError,
1310
- Kt as WalletStealthAuthorizer,
1311
- At as WasmStealthCrypto,
1312
- ct as XTR_FAUCET_CLAIM_RESOURCE_ADDRESS,
1313
- ot as XTR_FAUCET_COMPONENT_ADDRESS,
1314
- st as XTR_FAUCET_VAULT_ADDRESS,
1315
- k as amountLiteral,
1316
- y as assertByteLength,
1317
- se as assertUnreachable,
1318
- Oe as boolLiteral,
1319
- mt as buildTransactionSignature,
1320
- De as bytesLiteral,
1321
- Ne as claimedOutputTombstoneAddressLiteral,
1322
- gt as classifyOutcome,
1323
- Lt as commitmentOf,
1324
- ke as componentAddressLiteral,
1325
- Et as createOutput,
1326
- $ as decryptInputData,
1327
- Wt as decryptOwnedUtxo,
1328
- Qe as defaultIndexerUrl,
1329
- x as fromHexStr,
1330
- Gt as generateOutputsStatement,
1331
- pt as generateSealKeypair,
1332
- nt as getVaultIdsForAccount,
1333
- Te as intLiteral,
1334
- H as isStealthTransferInstruction,
1335
- tt as iterVaultIdsInState,
1336
- Ce as literalArg,
1337
- Ae as metadataLiteral,
1338
- L as microTariLiteral,
1339
- Ve as microTariString,
1340
- Ie as nonFungibleAddressLiteral,
1341
- Rt as parseSubstateUtxo,
1342
- He as parseWorkspaceStringKey,
1343
- Jt as patchStealthStatement,
1344
- Fe as publicKeyLiteral,
1345
- Ge as resolveMaxEpoch,
1346
- W as resolveTransaction,
1347
- A as resourceAddressLiteral,
1348
- K as sealTransaction,
1349
- yt as sendDryRun,
1350
- vt as sendTransaction,
1351
- U as serializeUnsignedTx,
1352
- Ut as signBalanceProof,
1353
- G as signTransaction,
1354
- V as statementAsWire,
1355
- ft as stealthTransferInstruction,
1356
- Nt as stealthUtxoSubstateId,
1357
- Ee as stringLiteral,
1358
- ht as submitTransaction,
1359
- Me as templateAddressLiteral,
1360
- b as toHexStr,
1361
- Le as utxoAddressLiteral,
1362
- Pe as validatorFeePoolAddressLiteral,
1363
- je as vaultIdLiteral,
1364
- _t as watchTransaction
1365
- };
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+ import { aggregateInputMasks as e, borEncodeTransaction as t, buildStealthInputsStatement as n, createStealthOutputWitness as r, encryptedDataDhKdfAead as i, generateKeypair as a, generateStealthBalanceProofSignature as o, generateStealthOutputsStatement as s, parseOotleAddress as c, sealTransaction as l, stealthDhSecret as ee, unblindOutput as te, validateBalanceProofSignature as ne, validateStealthTransfer as re } from "@tari-project/ootle-wasm";
5
+ var u = /* @__PURE__ */ (function(e11) {
6
+ return e11[e11.MainNet = 0] = "MainNet", e11[e11.StageNet = 1] = "StageNet", e11[e11.NextNet = 2] = "NextNet", e11[e11.LocalNet = 16] = "LocalNet", e11[e11.Igor = 36] = "Igor", e11[e11.Esmeralda = 38] = "Esmeralda", e11;
7
+ })({}), d = class extends Error {
8
+ constructor(e11, t2) {
9
+ super(e11, t2), this.name = new.target.name;
10
+ }
11
+ }, ie = class extends d {
12
+ constructor(e11, t2) {
13
+ super(e11, t2);
14
+ __publicField(this, "status");
15
+ __publicField(this, "body");
16
+ __publicField(this, "url");
17
+ this.status = t2 == null ? void 0 : t2.status, this.body = t2 == null ? void 0 : t2.body, this.url = t2 == null ? void 0 : t2.url;
18
+ }
19
+ }, f = class extends d {
20
+ constructor(e11, t2) {
21
+ super(e11, t2);
22
+ __publicField(this, "txId");
23
+ __publicField(this, "reason");
24
+ __publicField(this, "rejectReason");
25
+ this.txId = t2.txId, this.reason = t2.reason, this.rejectReason = t2.rejectReason;
26
+ }
27
+ }, ae = class extends d {
28
+ constructor(e11, t2) {
29
+ super(e11, t2);
30
+ __publicField(this, "txId");
31
+ this.txId = t2.txId;
32
+ }
33
+ }, p = class extends d {
34
+ }, m = class extends p {
35
+ constructor(e11, t2) {
36
+ super(e11, t2);
37
+ __publicField(this, "address");
38
+ this.address = t2.address;
39
+ }
40
+ }, h = class extends p {
41
+ }, g = class extends d {
42
+ }, _ = class extends d {
43
+ constructor(e11, t2) {
44
+ super(e11, t2);
45
+ __publicField(this, "context");
46
+ this.context = t2 == null ? void 0 : t2.context;
47
+ }
48
+ }, v = class extends d {
49
+ }, oe = class extends d {
50
+ };
51
+ function se(e11, t2) {
52
+ throw new v(`assertUnreachable${t2 ? ` (${t2})` : ""}: ${JSON.stringify(e11)}`);
53
+ }
54
+ function y(e11, t2, n2) {
55
+ if (e11.length !== t2) throw new v(`${n2} must be ${t2} bytes, got ${e11.length}`);
56
+ return e11;
57
+ }
58
+ function b(e11) {
59
+ return Array.from(e11, (e12) => e12.toString(16).padStart(2, "0")).join("");
60
+ }
61
+ var ce = /^[0-9a-fA-F]*$/;
62
+ function x(e11) {
63
+ if (e11.length % 2 != 0) throw new v(`fromHexStr: hex string must have even length, got ${e11.length}`);
64
+ if (!ce.test(e11)) throw new v("fromHexStr: hex string contains non-hex characters");
65
+ let t2 = new Uint8Array(e11.length / 2);
66
+ for (let n2 = 0; n2 < t2.length; n2++) t2[n2] = parseInt(e11.substring(n2 * 2, n2 * 2 + 2), 16);
67
+ return t2;
68
+ }
69
+ var S = (1n << 64n) - 1n, le = 2n, C = 32, ue = C * 2, de = 32, fe = 128, pe = 129, me = 130, w = 131, he = 132, ge = 136, _e = 137, ve = 138, ye = 141, T = 0, be = 32, E = 64, xe = 96, D = 128, Se = 160, O = 192;
70
+ function Ce(e11) {
71
+ switch (typeof e11) {
72
+ case "bigint":
73
+ return k(e11);
74
+ case "string":
75
+ return Ee(e11);
76
+ case "boolean":
77
+ return De(e11);
78
+ default:
79
+ if (e11 instanceof Uint8Array) return A(e11);
80
+ throw new v(`literalArg: cannot CBOR-encode value of type ${typeof e11}`);
81
+ }
82
+ }
83
+ function k(e11) {
84
+ if (e11 < 0n) throw new v(`amountLiteral: amount must be non-negative, got ${e11}`);
85
+ if (e11 >> 128n != 0n) throw new v(`amountLiteral: amount overflows u128: ${e11}`);
86
+ let t2 = [];
87
+ return e11 <= S ? I(t2, T, e11) : we(t2, e11), L(t2);
88
+ }
89
+ function we(e11, t2) {
90
+ let n2 = [], r2 = t2;
91
+ for (; r2 > 0n; ) n2.unshift(Number(r2 & 255n)), r2 >>= 8n;
92
+ I(e11, O, le), I(e11, E, BigInt(n2.length)), F(e11, n2);
93
+ }
94
+ function Te(e11) {
95
+ let t2 = [];
96
+ if (e11 >= 0n) {
97
+ if (e11 > S) throw new v(`intLiteral: value exceeds the 64-bit CBOR integer range: ${e11}`);
98
+ I(t2, T, e11);
99
+ } else {
100
+ let n2 = -1n - e11;
101
+ if (n2 > S) throw new v(`intLiteral: value exceeds the 64-bit CBOR integer range: ${e11}`);
102
+ I(t2, be, n2);
103
+ }
104
+ return L(t2);
105
+ }
106
+ function Ee(e11) {
107
+ let t2 = [];
108
+ return P(t2, e11), L(t2);
109
+ }
110
+ function A(e11) {
111
+ let t2 = [];
112
+ return I(t2, E, BigInt(e11.length)), F(t2, e11), L(t2);
113
+ }
114
+ function De(e11) {
115
+ return { Literal: e11 ? "f5" : "f4" };
116
+ }
117
+ function j(e11) {
118
+ return M(w, e11, "resource_");
119
+ }
120
+ function Oe(e11) {
121
+ return M(fe, e11, "component_");
122
+ }
123
+ function ke(e11) {
124
+ let t2 = e11 instanceof Map ? e11 : new Map(Object.entries(e11)), n2 = new TextEncoder(), r2 = [...t2.entries()].sort(([e12], [t3]) => ze(n2.encode(e12), n2.encode(t3))), i2 = [];
125
+ I(i2, O, BigInt(pe)), I(i2, Se, BigInt(r2.length));
126
+ for (let [e12, t3] of r2) P(i2, e12), P(i2, t3);
127
+ return L(i2);
128
+ }
129
+ function Ae(e11) {
130
+ return M(he, e11, "vault_");
131
+ }
132
+ function je(e11) {
133
+ return M(_e, e11, "template_");
134
+ }
135
+ function Me(e11) {
136
+ return M(ge, e11, "tombstone_");
137
+ }
138
+ function Ne(e11) {
139
+ return M(ve, e11, "vnfp_");
140
+ }
141
+ function Pe(e11) {
142
+ let t2 = typeof e11 == "string" ? x(e11) : e11;
143
+ return y(t2, de, "publicKeyLiteral"), A(t2);
144
+ }
145
+ function Fe(e11) {
146
+ let t2 = [];
147
+ return I(t2, O, BigInt(me)), I(t2, D, 2n), N(t2, w, e11.resource_address, "resource_"), Le(t2, e11.id), L(t2);
148
+ }
149
+ function Ie(e11) {
150
+ let t2 = [];
151
+ I(t2, O, BigInt(ye)), I(t2, D, 2n), N(t2, w, e11.resource_address, "resource_");
152
+ let n2 = x(e11.id);
153
+ return y(n2, C, "utxoAddressLiteral: id"), I(t2, E, BigInt(C)), F(t2, n2), L(t2);
154
+ }
155
+ function M(e11, t2, n2) {
156
+ let r2 = [];
157
+ return N(r2, e11, t2, n2), L(r2);
158
+ }
159
+ function N(e11, t2, n2, r2) {
160
+ let i2 = n2.startsWith(r2) ? n2.slice(r2.length) : n2;
161
+ if (i2.length !== ue) throw new v(`${r2}address must be 32 bytes (${ue} hex chars), got ${i2.length}`);
162
+ I(e11, O, BigInt(t2)), I(e11, E, BigInt(C)), F(e11, x(i2));
163
+ }
164
+ function Le(e11, t2) {
165
+ if (I(e11, D, 2n), "U256" in t2) {
166
+ I(e11, T, 0n), I(e11, D, 1n);
167
+ let n2 = x(t2.U256);
168
+ y(n2, C, "NonFungibleId.U256"), I(e11, E, BigInt(C)), F(e11, n2);
169
+ } else if ("String" in t2) I(e11, T, 1n), I(e11, D, 1n), P(e11, t2.String);
170
+ else if ("Uint32" in t2) I(e11, T, 2n), I(e11, D, 1n), Re(e11, t2.Uint32, "NonFungibleId.Uint32", (1n << 32n) - 1n);
171
+ else if ("Uint64" in t2) I(e11, T, 3n), I(e11, D, 1n), Re(e11, t2.Uint64, "NonFungibleId.Uint64", S);
172
+ else throw new v(`nonFungibleAddressLiteral: unrecognised NonFungibleId variant: ${JSON.stringify(t2)}`);
173
+ }
174
+ function Re(e11, t2, n2, r2) {
175
+ if (!Number.isInteger(t2) || t2 < 0) throw new v(`${n2} must be a non-negative integer, got ${t2}`);
176
+ if (r2 > BigInt(2 ** 53 - 1) && t2 > 2 ** 53 - 1) throw new v(`${n2} value ${t2} exceeds Number.MAX_SAFE_INTEGER (2^53-1) and cannot be encoded without precision loss \u2014 the binding types this id as a JS number. Use a U256/String id for large values.`);
177
+ let i2 = BigInt(t2);
178
+ if (i2 > r2) throw new v(`${n2} value ${t2} exceeds its maximum of ${r2}`);
179
+ I(e11, T, i2);
180
+ }
181
+ function P(e11, t2) {
182
+ let n2 = new TextEncoder().encode(t2);
183
+ I(e11, xe, BigInt(n2.length)), F(e11, n2);
184
+ }
185
+ function F(e11, t2) {
186
+ let n2 = e11.length;
187
+ e11.length = n2 + t2.length;
188
+ for (let r2 = 0; r2 < t2.length; r2++) e11[n2 + r2] = t2[r2];
189
+ }
190
+ function ze(e11, t2) {
191
+ let n2 = Math.min(e11.length, t2.length);
192
+ for (let r2 = 0; r2 < n2; r2++) if (e11[r2] !== t2[r2]) return e11[r2] < t2[r2] ? -1 : 1;
193
+ return e11.length - t2.length;
194
+ }
195
+ function I(e11, t2, n2) {
196
+ n2 < 24n ? e11.push(t2 | Number(n2)) : n2 < 1n << 8n ? e11.push(t2 | 24, Number(n2)) : n2 < 1n << 16n ? e11.push(t2 | 25, Number(n2 >> 8n & 255n), Number(n2 & 255n)) : n2 < 1n << 32n ? e11.push(t2 | 26, Number(n2 >> 24n & 255n), Number(n2 >> 16n & 255n), Number(n2 >> 8n & 255n), Number(n2 & 255n)) : e11.push(t2 | 27, Number(n2 >> 56n & 255n), Number(n2 >> 48n & 255n), Number(n2 >> 40n & 255n), Number(n2 >> 32n & 255n), Number(n2 >> 24n & 255n), Number(n2 >> 16n & 255n), Number(n2 >> 8n & 255n), Number(n2 & 255n));
197
+ }
198
+ function L(e11) {
199
+ return { Literal: b(Uint8Array.from(e11)) };
200
+ }
201
+ function R(e11) {
202
+ return k(e11);
203
+ }
204
+ function Be(e11) {
205
+ if (e11 < 0n) throw new v(`microTariString: amount must be non-negative, got ${e11}`);
206
+ return e11.toString();
207
+ }
208
+ function Ve(e11) {
209
+ let t2 = e11.split(".");
210
+ if (t2.length > 2) throw new v("Invalid workspace key format. Only one dot is allowed.");
211
+ let n2 = t2[0];
212
+ if (t2[1] === void 0) return {
213
+ name: n2,
214
+ offset: null
215
+ };
216
+ let r2 = Number.parseInt(t2[1], 10);
217
+ if (Number.isNaN(r2) || r2 < 0 || String(r2) !== t2[1]) throw new v(`Invalid workspace key offset: "${t2[1]}"`);
218
+ return {
219
+ name: n2,
220
+ offset: r2
221
+ };
222
+ }
223
+ var He = 2160, Ue = 10;
224
+ async function We(e11, t2 = 10) {
225
+ if (!Number.isInteger(t2) || t2 < 1) throw new v(`resolveMaxEpoch: leadEpochs must be a positive integer, got ${t2}`);
226
+ if (t2 > 2160) throw new v(`resolveMaxEpoch: leadEpochs ${t2} exceeds the network's maximum validity window of ${He} epochs`);
227
+ return await e11.getCurrentEpoch() + t2;
228
+ }
229
+ var Ge = (1n << 64n) - 1n;
230
+ function z(e11, t2) {
231
+ if (!Number.isInteger(e11) || e11 < 0) throw new v(`${t2} must be a non-negative integer epoch, got ${e11}`);
232
+ return e11;
233
+ }
234
+ function Ke(e11) {
235
+ return e11.startsWith("template_") ? e11.slice(9) : e11;
236
+ }
237
+ function qe(e11) {
238
+ return typeof e11 != "object" || !e11 ? null : "PutLastInstructionOutputOnWorkspace" in e11 ? e11.PutLastInstructionOutputOnWorkspace.key : "TakeFromBucket" in e11 ? e11.TakeFromBucket.output_bucket : "AllocateAddress" in e11 ? e11.AllocateAddress.workspace_id : null;
239
+ }
240
+ var Je = class {
241
+ constructor() {
242
+ __publicField(this, "nextId", 0);
243
+ __publicField(this, "ids", /* @__PURE__ */ new Map());
244
+ }
245
+ insert(e11) {
246
+ let t2 = this.nextId;
247
+ return this.ids.set(e11, t2), this.nextId += 1, t2;
248
+ }
249
+ get(e11) {
250
+ return this.ids.get(e11);
251
+ }
252
+ get next() {
253
+ return this.nextId;
254
+ }
255
+ observeAllocated(e11) {
256
+ e11 + 1 > this.nextId && (this.nextId = e11 + 1);
257
+ }
258
+ reset() {
259
+ this.nextId = 0, this.ids.clear();
260
+ }
261
+ resetFrom(e11) {
262
+ this.reset();
263
+ for (let t2 of e11) {
264
+ let e12 = qe(t2);
265
+ e12 !== null && this.observeAllocated(e12);
266
+ }
267
+ }
268
+ };
269
+ function Ye(e11) {
270
+ return new v(`No workspace variable named "${e11}" has been defined. Call builder.saveVar(${JSON.stringify(e11)}) on a preceding instruction whose output you want to reference.`);
271
+ }
272
+ var B = class e2 {
273
+ constructor(e11, t2) {
274
+ __publicField(this, "unsignedTransaction");
275
+ __publicField(this, "workspaceIds");
276
+ __publicField(this, "feeWorkspaceIds");
277
+ this.unsignedTransaction = {
278
+ network: e11,
279
+ fee_instructions: [],
280
+ instructions: [],
281
+ inputs: [],
282
+ min_epoch: null,
283
+ max_epoch: z(t2, "maxEpoch"),
284
+ dry_run: false,
285
+ is_seal_signer_authorized: false,
286
+ blobs: [],
287
+ nonce: 0
288
+ }, this.workspaceIds = new Je(), this.feeWorkspaceIds = new Je();
289
+ }
290
+ static new(t2, n2) {
291
+ return new e2(t2, n2);
292
+ }
293
+ callFunction(e11, t2) {
294
+ let n2 = this.resolveArgs(t2);
295
+ return this.addInstruction({ CallFunction: {
296
+ address: Ke(e11.templateAddress),
297
+ function: e11.functionName,
298
+ args: n2
299
+ } });
300
+ }
301
+ callMethod(e11, t2) {
302
+ let n2;
303
+ if (e11.componentAddress) n2 = { Address: e11.componentAddress };
304
+ else if (e11.fromWorkspace) n2 = { Workspace: this.requireNamedId(e11.fromWorkspace) };
305
+ else throw new v("callMethod requires either `componentAddress` or `fromWorkspace` to be set on the method definition. Use `fromWorkspace` to call a method on a component returned by a previous saveVar; use `componentAddress` for an on-chain component.");
306
+ let r2 = this.resolveArgs(t2);
307
+ return this.addInstruction({ CallMethod: {
308
+ call: n2,
309
+ method: e11.methodName,
310
+ args: r2
311
+ } });
312
+ }
313
+ createAccount(e11, t2) {
314
+ let n2 = t2 ? this.getOffsetIdFromWorkspaceName(t2) : null;
315
+ return this.addInstruction({ CreateAccount: {
316
+ owner_public_key: e11,
317
+ owner_rule: null,
318
+ access_rules: null,
319
+ bucket_workspace_id: n2
320
+ } });
321
+ }
322
+ createProof(e11, t2) {
323
+ return this.addInstruction({ CallMethod: {
324
+ call: { Address: e11 },
325
+ method: "create_proof_for_resource",
326
+ args: [j(t2)]
327
+ } });
328
+ }
329
+ claimBurn(e11, t2) {
330
+ return this.addInstruction({ ClaimBurn: {
331
+ claim: e11,
332
+ output_data: t2
333
+ } });
334
+ }
335
+ allocateAddress(e11, t2) {
336
+ let n2 = this.addNamedId(t2);
337
+ return this.addInstruction({ AllocateAddress: {
338
+ allocatable_type: e11,
339
+ workspace_id: n2
340
+ } });
341
+ }
342
+ saveVar(e11) {
343
+ let t2 = this.addNamedId(e11);
344
+ return this.addInstruction({ PutLastInstructionOutputOnWorkspace: { key: t2 } });
345
+ }
346
+ feeTransactionPayFromComponent(e11, t2) {
347
+ return this.addFeeInstruction({ CallMethod: {
348
+ call: { Address: e11 },
349
+ method: "pay_fee",
350
+ args: [R(t2)]
351
+ } });
352
+ }
353
+ feeTransactionPayFromComponentConfidential(e11, t2) {
354
+ throw new v("feeTransactionPayFromComponentConfidential is not implemented: a ConfidentialWithdrawProof Literal must be tari_bor-CBOR-encoded, which the TS SDK does not yet support.");
355
+ }
356
+ dropAllProofsInWorkspace() {
357
+ return this.addInstruction("DropAllProofsInWorkspace");
358
+ }
359
+ publishTemplate(e11, t2 = null) {
360
+ let n2 = this.unsignedTransaction.blobs.length;
361
+ return this.unsignedTransaction.blobs.push(e11), this.addInstruction({ PublishTemplate: {
362
+ binary: n2,
363
+ metadata_hash: t2
364
+ } });
365
+ }
366
+ addInstruction(e11) {
367
+ return this.observeAllocations(this.workspaceIds, [e11]), this.unsignedTransaction.instructions.push(e11), this;
368
+ }
369
+ addFeeInstruction(e11) {
370
+ return this.observeAllocations(this.feeWorkspaceIds, [e11]), this.unsignedTransaction.fee_instructions.push(e11), this;
371
+ }
372
+ withInstructions(e11) {
373
+ return this.observeAllocations(this.workspaceIds, e11), this.unsignedTransaction.instructions.push(...e11), this;
374
+ }
375
+ withFeeInstructions(e11) {
376
+ return this.observeAllocations(this.feeWorkspaceIds, e11), this.unsignedTransaction.fee_instructions.push(...e11), this;
377
+ }
378
+ observeAllocations(e11, t2) {
379
+ for (let n2 of t2) {
380
+ let t3 = qe(n2);
381
+ t3 !== null && e11.observeAllocated(t3);
382
+ }
383
+ }
384
+ withFeeInstructionsBuilder(t2) {
385
+ let n2 = t2(new e2(this.unsignedTransaction.network, this.unsignedTransaction.max_epoch));
386
+ return this.unsignedTransaction.fee_instructions = n2.unsignedTransaction.instructions, this.feeWorkspaceIds.resetFrom(this.unsignedTransaction.fee_instructions), this;
387
+ }
388
+ addInput(e11) {
389
+ return this.unsignedTransaction.inputs.push(Xe(e11)), this;
390
+ }
391
+ withInputs(e11) {
392
+ return this.unsignedTransaction.inputs.push(...e11.map(Xe)), this;
393
+ }
394
+ withMinEpoch(e11) {
395
+ return this.unsignedTransaction.min_epoch = z(e11, "minEpoch"), this;
396
+ }
397
+ withMaxEpoch(e11) {
398
+ return this.unsignedTransaction.max_epoch = z(e11, "maxEpoch"), this;
399
+ }
400
+ withNonce(e11) {
401
+ if (typeof e11 == "number" && !Number.isInteger(e11)) throw new v(`withNonce: nonce must be an integer, got ${e11}`);
402
+ let t2 = typeof e11 == "bigint" ? e11 : BigInt(e11);
403
+ if (t2 < 0n || t2 > Ge) throw new v(`withNonce: nonce must fit an unsigned 64-bit integer, got ${e11}`);
404
+ return this.unsignedTransaction.nonce = t2 > BigInt(2 ** 53 - 1) ? t2 : Number(t2), this;
405
+ }
406
+ withUnsignedTransaction(e11) {
407
+ return this.unsignedTransaction = {
408
+ ...e11,
409
+ instructions: [...e11.instructions],
410
+ fee_instructions: [...e11.fee_instructions],
411
+ inputs: [...e11.inputs],
412
+ blobs: [...e11.blobs ?? []],
413
+ nonce: e11.nonce ?? 0,
414
+ max_epoch: z(e11.max_epoch, "withUnsignedTransaction: max_epoch")
415
+ }, this.workspaceIds.resetFrom(this.unsignedTransaction.instructions), this.feeWorkspaceIds.resetFrom(this.unsignedTransaction.fee_instructions), this;
416
+ }
417
+ resolveWorkspaceOffsetId(e11) {
418
+ return this.getOffsetIdFromWorkspaceName(e11);
419
+ }
420
+ buildUnsignedTransaction() {
421
+ return {
422
+ ...this.unsignedTransaction,
423
+ instructions: [...this.unsignedTransaction.instructions],
424
+ fee_instructions: [...this.unsignedTransaction.fee_instructions],
425
+ inputs: [...this.unsignedTransaction.inputs],
426
+ blobs: [...this.unsignedTransaction.blobs]
427
+ };
428
+ }
429
+ addNamedId(e11) {
430
+ return this.workspaceIds.insert(e11);
431
+ }
432
+ requireNamedId(e11) {
433
+ let t2 = this.workspaceIds.get(e11);
434
+ if (t2 === void 0) throw Ye(e11);
435
+ return t2;
436
+ }
437
+ getOffsetIdFromWorkspaceName(e11) {
438
+ let t2 = Ve(e11);
439
+ return {
440
+ id: this.requireNamedId(t2.name),
441
+ offset: t2.offset
442
+ };
443
+ }
444
+ resolveArgs(e11) {
445
+ return e11.map((e12) => typeof e12 == "object" && e12 && "Workspace" in e12 && typeof e12.Workspace == "string" ? { Workspace: this.getOffsetIdFromWorkspaceName(e12.Workspace) } : e12);
446
+ }
447
+ };
448
+ function Xe(e11) {
449
+ return {
450
+ substate_id: e11.substate_id,
451
+ version: e11.version ?? null,
452
+ is_write: "is_write" in e11 ? e11.is_write : true
453
+ };
454
+ }
455
+ var Ze = {
456
+ [u.LocalNet]: "http://localhost:12500",
457
+ [u.Esmeralda]: "https://ootle-indexer-a.tari.com"
458
+ };
459
+ function Qe(e11) {
460
+ let t2 = Ze[e11];
461
+ if (t2 !== void 0) return t2;
462
+ throw new v(`No default indexer URL is configured for ${u[e11]}. Pass an explicit URL via ProviderBuilder.withUrl(...) or IndexerProvider.connect({ url, network: Network.${u[e11]} }).`);
463
+ }
464
+ var $e = 132, et = 32;
465
+ function* tt(e11) {
466
+ let t2 = /* @__PURE__ */ new Set();
467
+ for (let n2 of V(e11, $e)) n2.length === et * 2 && !t2.has(n2) && (t2.add(n2), yield `vault_${n2}`);
468
+ }
469
+ async function nt(e11, t2) {
470
+ let n2 = (await e11.getSubstate(t2)).substate;
471
+ return it(n2) ? Array.from(tt(n2.Component.body.state)) : [];
472
+ }
473
+ function* V(e11, t2) {
474
+ if (Array.isArray(e11)) {
475
+ for (let n3 of e11) yield* V(n3, t2);
476
+ return;
477
+ }
478
+ if (typeof e11 != "object" || !e11) return;
479
+ let n2 = e11, r2 = n2["@cbor"];
480
+ if (typeof r2 == "string") {
481
+ if (r2 === "tag") {
482
+ let e12 = n2.value;
483
+ if (n2.tag === t2) {
484
+ let t3 = rt(e12);
485
+ t3 !== null && (yield t3);
486
+ }
487
+ yield* V(e12, t2);
488
+ } else if (r2 === "map") {
489
+ let e12 = n2.entries;
490
+ if (Array.isArray(e12)) for (let n3 of e12) Array.isArray(n3) && n3.length === 2 && (yield* V(n3[0], t2), yield* V(n3[1], t2));
491
+ }
492
+ return;
493
+ }
494
+ for (let e12 of Object.values(n2)) yield* V(e12, t2);
495
+ }
496
+ function rt(e11) {
497
+ if (typeof e11 != "object" || !e11) return null;
498
+ let t2 = e11;
499
+ if (t2["@cbor"] !== "bytes") return null;
500
+ let n2 = t2.hex;
501
+ return typeof n2 == "string" ? n2 : null;
502
+ }
503
+ function it(e11) {
504
+ return typeof e11 == "object" && !!e11 && "Component" in e11;
505
+ }
506
+ var at = "resource_0101010101010101010101010101010101010101010101010101010101010101", ot = "component_0102030000000000000000000000000000000000000000000000000000000000", st = "vault_0102030000000000000000000000000000000000000000000000000000000001", ct = "resource_0102030000000000000000000000000000000000000000000000000000000002", lt = "__ootleRawJson", ut = "stealth_revealed_input", dt = "stealth_revealed_change";
507
+ function ft(e11, t2) {
508
+ let n2 = e11.revealedInputBucket === null ? null : t2(e11.revealedInputBucket);
509
+ return { StealthTransfer: {
510
+ resource_address_ref: { Address: e11.resourceAddress },
511
+ statement: H(e11.statement),
512
+ revealed_input_bucket: n2
513
+ } };
514
+ }
515
+ function H(e11) {
516
+ return { [lt]: e11.toCompactJson() };
517
+ }
518
+ function U(e11) {
519
+ return typeof e11 == "object" && !!e11 && "StealthTransfer" in e11;
520
+ }
521
+ function W(e11) {
522
+ let t2 = [], n2 = (e12) => `__ootleRawJson:${e12}__`, r2 = JSON.stringify(e11, (e12, r3) => {
523
+ if (typeof r3 == "bigint") return r3.toString();
524
+ if (typeof r3 == "object" && r3 && "__ootleRawJson" in r3) {
525
+ let e13 = n2(t2.length);
526
+ return t2.push(r3[lt]), e13;
527
+ }
528
+ return r3;
529
+ });
530
+ for (let e12 = 0; e12 < t2.length; e12++) {
531
+ let i2 = JSON.stringify(n2(e12)), a2 = r2.indexOf(i2);
532
+ if (a2 < 0) throw new v(`serializeUnsignedTx: raw-JSON placeholder ${e12} was not emitted`);
533
+ if (r2.indexOf(i2, a2 + i2.length) >= 0) throw new v(`serializeUnsignedTx: raw-JSON placeholder ${e12} collided with transaction data`);
534
+ r2 = r2.slice(0, a2) + t2[e12] + r2.slice(a2 + i2.length);
535
+ }
536
+ return r2;
537
+ }
538
+ async function G(e11, t2) {
539
+ let n2 = new Map(t2.inputs.map((e12) => [JSON.stringify(e12.substate_id), e12.is_write])), r2 = (await e11.resolveInputs(t2.inputs.map(({ substate_id: e12, version: t3 }) => ({
540
+ substate_id: e12,
541
+ version: t3
542
+ })))).map((e12) => ({
543
+ ...e12,
544
+ is_write: n2.get(JSON.stringify(e12.substate_id)) ?? true
545
+ }));
546
+ return {
547
+ ...t2,
548
+ inputs: r2
549
+ };
550
+ }
551
+ function pt() {
552
+ let { secret_key: e11, public_key: t2 } = a();
553
+ return {
554
+ secret_key: e11,
555
+ public_key: t2
556
+ };
557
+ }
558
+ function mt(e11, t2) {
559
+ return {
560
+ public_key: b(y(e11, 32, "publicKey")),
561
+ signature: {
562
+ public_nonce: b(y(t2.public_nonce, 32, "schnorr.public_nonce")),
563
+ signature: b(y(t2.signature, 32, "schnorr.signature"))
564
+ }
565
+ };
566
+ }
567
+ async function K(e11, t2, n2) {
568
+ let r2 = n2 ?? a(), i2 = y(r2.secret_key, 32, "sealKeypair.secret_key"), o2 = y(r2.public_key, 32, "sealKeypair.public_key"), s2 = [];
569
+ for (let n3 of e11) {
570
+ let e12 = await n3.signTransaction(t2, o2);
571
+ s2.push(...e12);
572
+ }
573
+ let c2 = l(`{"transaction":${W(t2)},"signatures":${JSON.stringify(s2)}}`, i2);
574
+ return {
575
+ sealedJson: c2,
576
+ transaction: JSON.parse(c2)
577
+ };
578
+ }
579
+ function q(e11) {
580
+ return t(e11.sealedJson);
581
+ }
582
+ async function ht(e11, t2) {
583
+ return (await e11.submitTransaction(t2)).transaction_id;
584
+ }
585
+ function gt(e11) {
586
+ var _a, _b;
587
+ if (e11 === "Pending" || !("Finalized" in e11)) return null;
588
+ let t2 = e11.Finalized, n2 = t2.final_decision;
589
+ if (n2 === null) return null;
590
+ if (n2 === "Commit") return { outcome: n2 };
591
+ if (typeof n2 == "object" && n2 && "Abort" in n2) {
592
+ let e12 = t2.abort_details ?? JSON.stringify(n2.Abort), r2 = (_b = (_a = t2.execution_result) == null ? void 0 : _a.finalize) == null ? void 0 : _b.result;
593
+ return typeof r2 == "object" && r2 && "AcceptFeeRejectRest" in r2 ? {
594
+ outcome: "FeeIntentCommit",
595
+ reason: e12
596
+ } : {
597
+ outcome: "Reject",
598
+ reason: e12
599
+ };
600
+ }
601
+ throw new v(`Unexpected final_decision variant: ${JSON.stringify(n2)}`);
602
+ }
603
+ async function _t(e11, t2, n2) {
604
+ let r2 = (n2 == null ? void 0 : n2.pollIntervalMs) ?? 500, i2 = (n2 == null ? void 0 : n2.timeoutMs) ?? 6e4, a2 = Date.now() + i2;
605
+ for (; ; ) {
606
+ let n3 = await e11.getTransactionResult(t2), o2 = n3.result;
607
+ if (o2 !== "Pending" && "Finalized" in o2) {
608
+ let { outcome: e12, reason: r3 } = gt(o2) ?? { outcome: "Reject" };
609
+ if (e12 === "Reject") throw new f(`Transaction ${t2} was rejected: ${r3}`, {
610
+ txId: t2,
611
+ reason: r3 ?? ""
612
+ });
613
+ if (e12 === "FeeIntentCommit") throw new f(`Transaction ${t2} only committed fees (execution aborted): ${r3}`, {
614
+ txId: t2,
615
+ reason: `FeeIntentCommit: ${r3 ?? ""}`
616
+ });
617
+ return n3;
618
+ }
619
+ if (Date.now() >= a2) throw new ae(`Transaction ${t2} did not finalize within ${i2}ms`, { txId: t2 });
620
+ await new Promise((e12) => setTimeout(e12, r2));
621
+ }
622
+ }
623
+ async function vt(e11, t2, n2, r2) {
624
+ let i2 = await G(e11, n2);
625
+ return _t(e11, await ht(e11, q(await K(Array.isArray(t2) ? t2 : [t2], i2))), r2);
626
+ }
627
+ async function yt(e11, t2, n2, r2) {
628
+ return vt(e11, t2, {
629
+ ...n2,
630
+ dry_run: true
631
+ }, r2);
632
+ }
633
+ var bt = class {
634
+ constructor(e11, t2) {
635
+ __publicField(this, "builder");
636
+ this.builder = B.new(e11, t2);
637
+ }
638
+ withInputs(e11) {
639
+ return this.builder.withInputs(e11), this;
640
+ }
641
+ feeTransactionPayFromComponent(e11, t2) {
642
+ return this.builder.feeTransactionPayFromComponent(e11, t2), this;
643
+ }
644
+ publicTransfer(e11, t2, n2, r2) {
645
+ return this.builder.callMethod({
646
+ componentAddress: e11,
647
+ methodName: "withdraw"
648
+ }, [j(t2), R(n2)]).saveVar("bucket").callMethod({
649
+ componentAddress: r2,
650
+ methodName: "deposit"
651
+ }, [{ Workspace: "bucket" }]), this;
652
+ }
653
+ publishTemplate(e11, t2, n2) {
654
+ return this.builder.publishTemplate(t2), n2 && this.builder.saveVar(n2), this;
655
+ }
656
+ build() {
657
+ return this.builder.buildUnsignedTransaction();
658
+ }
659
+ }, xt = class {
660
+ constructor(e11, t2, n2) {
661
+ __publicField(this, "builder");
662
+ __publicField(this, "faucetAddress");
663
+ this.builder = B.new(e11, t2), this.faucetAddress = n2;
664
+ }
665
+ feeTransactionPayFromComponent(e11, t2) {
666
+ return this.builder.feeTransactionPayFromComponent(e11, t2), this;
667
+ }
668
+ takeFaucetFunds(e11, t2) {
669
+ return this.builder.callMethod({
670
+ componentAddress: this.faucetAddress,
671
+ methodName: "take_free_coins"
672
+ }, [R(t2)]).saveVar("faucet_bucket").callMethod({
673
+ componentAddress: e11,
674
+ methodName: "deposit"
675
+ }, [{ Workspace: "faucet_bucket" }]), this;
676
+ }
677
+ takeMaxFaucetFunds(e11) {
678
+ return this.builder.callMethod({
679
+ componentAddress: this.faucetAddress,
680
+ methodName: "take_max_free_coins"
681
+ }, []).saveVar("faucet_bucket").callMethod({
682
+ componentAddress: e11,
683
+ methodName: "deposit"
684
+ }, [{ Workspace: "faucet_bucket" }]), this;
685
+ }
686
+ publishTemplate(e11, t2) {
687
+ let n2 = t2 ?? "template";
688
+ return this.builder.callFunction({
689
+ templateAddress: e11,
690
+ functionName: "new"
691
+ }, []).saveVar(n2), this;
692
+ }
693
+ build() {
694
+ return this.builder.buildUnsignedTransaction();
695
+ }
696
+ }, St = class {
697
+ constructor() {
698
+ __publicField(this, "keyProviders");
699
+ __publicField(this, "defaultSignerAddress", null);
700
+ this.keyProviders = /* @__PURE__ */ new Map();
701
+ }
702
+ registerKeyProvider(e11, t2) {
703
+ return this.keyProviders.set(e11, t2), this;
704
+ }
705
+ setDefaultSigner(e11) {
706
+ return this.requireKeyProvider(e11, "explicit"), this.defaultSignerAddress = e11, this;
707
+ }
708
+ async getAddress() {
709
+ if (!this.defaultSignerAddress) throw new h("No default signer address set. Call setDefaultSigner() first.");
710
+ return Promise.resolve(this.defaultSignerAddress);
711
+ }
712
+ async getPublicKey() {
713
+ return this.getSignerOrThrow().getPublicKey();
714
+ }
715
+ async signTransaction(e11, t2) {
716
+ return this.getSignerOrThrow().signTransaction(e11, t2);
717
+ }
718
+ async authorizeTransaction(e11, t2, n2) {
719
+ return {
720
+ signerAddress: e11,
721
+ signatures: await this.requireKeyProvider(e11, "explicit").signTransaction(t2, n2)
722
+ };
723
+ }
724
+ async authorizeTransactionAll(e11, t2) {
725
+ return Promise.all([...this.keyProviders.keys()].map((n2) => this.authorizeTransaction(n2, e11, t2)));
726
+ }
727
+ getKeyProvider(e11) {
728
+ return this.keyProviders.get(e11);
729
+ }
730
+ getSignerAddresses() {
731
+ return [...this.keyProviders.keys()];
732
+ }
733
+ getSignerOrThrow() {
734
+ if (!this.defaultSignerAddress) throw new h("No default signer address set. Call setDefaultSigner() first.");
735
+ return this.requireKeyProvider(this.defaultSignerAddress, "default");
736
+ }
737
+ requireKeyProvider(e11, t2) {
738
+ let n2 = this.keyProviders.get(e11);
739
+ if (n2 === void 0) throw new m(t2 === "default" ? `No key provider registered for default address: ${e11}. Call wallet.registerKeyProvider(address, signer) for that address, or pick a different default with setDefaultSigner(otherAddress).` : `No key provider registered for address: ${e11}. Call wallet.registerKeyProvider(${JSON.stringify(e11)}, signer) before signing.`, { address: e11 });
740
+ return n2;
741
+ }
742
+ }, Ct = class {
743
+ constructor(e11, t2) {
744
+ __publicField(this, "bytes");
745
+ this.bytes = new Uint8Array(y(e11, 32, t2));
746
+ }
747
+ toBytes() {
748
+ return new Uint8Array(this.bytes);
749
+ }
750
+ toHex() {
751
+ return b(this.bytes);
752
+ }
753
+ }, J = class e3 extends Ct {
754
+ constructor(e11) {
755
+ super(e11, "Mask");
756
+ }
757
+ static zero() {
758
+ return new e3(new Uint8Array(32));
759
+ }
760
+ static fromBytes(t2) {
761
+ return new e3(t2);
762
+ }
763
+ static fromHex(t2) {
764
+ return new e3(x(t2));
765
+ }
766
+ }, wt = class e4 {
767
+ constructor(e11) {
768
+ __publicField(this, "bytes");
769
+ this.bytes = new Uint8Array(e11);
770
+ }
771
+ static fromHex(t2) {
772
+ return new e4(x(t2));
773
+ }
774
+ toBytes() {
775
+ return new Uint8Array(this.bytes);
776
+ }
777
+ toHex() {
778
+ return b(this.bytes);
779
+ }
780
+ }, Tt = Object.freeze({ StealthPublicKey: Object.freeze({}) });
781
+ function Et(e11) {
782
+ if (e11.amount <= 0n) throw new v(`Output amount must be > 0, got ${e11.amount}`);
783
+ return {
784
+ destination: e11.destination,
785
+ amount: e11.amount,
786
+ resourceAddress: e11.resourceAddress,
787
+ resourceViewKey: e11.resourceViewKey === void 0 ? void 0 : new Uint8Array(e11.resourceViewKey),
788
+ memo: e11.memo,
789
+ payTo: e11.payTo ?? Tt,
790
+ minimumValuePromise: e11.minimumValuePromise ?? 0n
791
+ };
792
+ }
793
+ function Dt(e11, t2) {
794
+ if (e11.trim() === "") throw new v(`${t2} must be a non-empty decimal string`);
795
+ return BigInt(e11);
796
+ }
797
+ function Ot(e11, t2) {
798
+ let n2 = e11.trim();
799
+ if (!n2.startsWith("{") || !n2.endsWith("}")) throw new v(`StealthTransferStatement.toCompactJson: ${t2} fragment is not a JSON object (got: ${e11.slice(0, 60)}...)`);
800
+ }
801
+ var Y = class e5 extends Ct {
802
+ constructor(e11) {
803
+ super(e11, "StealthInput.commitment");
804
+ }
805
+ get commitment() {
806
+ return this.toBytes();
807
+ }
808
+ toJSON() {
809
+ return { commitment: this.toHex() };
810
+ }
811
+ static fromJSON(t2) {
812
+ return new e5(x(t2.commitment));
813
+ }
814
+ }, X = class e6 {
815
+ constructor(e11, t2) {
816
+ __publicField(this, "_publicNonce");
817
+ __publicField(this, "_signature");
818
+ this._publicNonce = new Uint8Array(y(e11, 32, "BalanceProofSignature.publicNonce")), this._signature = new Uint8Array(y(t2, 32, "BalanceProofSignature.signature"));
819
+ }
820
+ get publicNonce() {
821
+ return new Uint8Array(this._publicNonce);
822
+ }
823
+ get signature() {
824
+ return new Uint8Array(this._signature);
825
+ }
826
+ toJSON() {
827
+ return {
828
+ public_nonce: b(this._publicNonce),
829
+ signature: b(this._signature)
830
+ };
831
+ }
832
+ static fromJSON(t2) {
833
+ return new e6(x(t2.public_nonce), x(t2.signature));
834
+ }
835
+ }, Z = class e7 {
836
+ constructor(e11, t2, n2) {
837
+ __publicField(this, "inputs");
838
+ __publicField(this, "revealedAmount");
839
+ __publicField(this, "statementJson");
840
+ this.inputs = e11, this.revealedAmount = t2, this.statementJson = n2;
841
+ }
842
+ static revealedOnly(t2) {
843
+ return new e7([], t2);
844
+ }
845
+ toJSON() {
846
+ return {
847
+ inputs: this.inputs.map((e11) => e11.toJSON()),
848
+ revealed_amount: Be(this.revealedAmount)
849
+ };
850
+ }
851
+ static fromJSON(t2) {
852
+ return new e7(t2.inputs.map(Y.fromJSON), Dt(t2.revealed_amount, "revealed_amount"));
853
+ }
854
+ }, Q = class e8 {
855
+ constructor(e11) {
856
+ __publicField(this, "statementJson");
857
+ this.statementJson = e11;
858
+ }
859
+ parsed() {
860
+ return JSON.parse(this.statementJson);
861
+ }
862
+ toJSON() {
863
+ return this.statementJson;
864
+ }
865
+ static fromJSON(t2) {
866
+ return new e8(t2);
867
+ }
868
+ }, $ = class e9 {
869
+ constructor(e11, t2, n2) {
870
+ __publicField(this, "inputsStatement");
871
+ __publicField(this, "outputsStatement");
872
+ __publicField(this, "balanceProof");
873
+ this.inputsStatement = e11, this.outputsStatement = t2, this.balanceProof = n2;
874
+ }
875
+ toJSON() {
876
+ let e11 = {
877
+ inputs: this.inputsStatement.toJSON(),
878
+ outputs: this.outputsStatement.toJSON()
879
+ };
880
+ return this.balanceProof !== void 0 && (e11.balance_proof = this.balanceProof.toJSON()), e11;
881
+ }
882
+ toCompactJson() {
883
+ let e11 = this.inputsStatement.statementJson ?? JSON.stringify(this.inputsStatement.toJSON()), t2 = this.outputsStatement.statementJson;
884
+ Ot(e11, "inputs_statement"), Ot(t2, "outputs_statement");
885
+ let n2 = `{"inputs_statement":${e11},"outputs_statement":${t2},"covenant_claims":[]`;
886
+ return this.balanceProof !== void 0 && (n2 += `,"balance_proof":${JSON.stringify(this.balanceProof.toJSON())}`), n2 + "}";
887
+ }
888
+ static fromJSON(t2) {
889
+ return new e9(Z.fromJSON(t2.inputs), Q.fromJSON(t2.outputs), t2.balance_proof === void 0 ? void 0 : X.fromJSON(t2.balance_proof));
890
+ }
891
+ };
892
+ function kt(e11, t2) {
893
+ let n2 = new Uint8Array(e11.length * 32);
894
+ for (let r2 = 0; r2 < e11.length; r2++) {
895
+ let i2 = e11[r2];
896
+ y(i2, 32, `${t2}[${r2}]`), n2.set(i2, r2 * 32);
897
+ }
898
+ return n2;
899
+ }
900
+ var At = class {
901
+ constructor(e11 = u.LocalNet) {
902
+ __publicField(this, "network");
903
+ this.network = e11;
904
+ }
905
+ async generateOutputsStatement(e11, t2, n2) {
906
+ let r2 = `[${e11.map((e12) => this.outputWitness(e12)).join(",")}]`;
907
+ if (t2 > 0n && n2 === void 0) throw new v("generateOutputsStatement: a revealed output needs a revealed receiver public key");
908
+ let i2 = t2 > 0n ? n2 : new Uint8Array();
909
+ t2 > 0n && y(i2, 32, "revealed receiver public key");
910
+ let a2 = s(r2, t2, i2);
911
+ return y(a2.aggregated_output_mask, 32, "StealthOutputsResult.aggregated_output_mask"), {
912
+ statement: Q.fromJSON(a2.statement_json),
913
+ outputMask: J.fromBytes(a2.aggregated_output_mask)
914
+ };
915
+ }
916
+ outputWitness(e11) {
917
+ let t2 = c(e11.destination), n2 = jt(e11.payTo) ? null : JSON.stringify(e11.payTo), i2 = e11.memo === void 0 ? null : JSON.stringify(e11.memo), a2 = e11.resourceViewKey ?? null;
918
+ return r(this.network, t2.owner_key, t2.view_key, e11.amount, e11.resourceAddress, a2, i2, n2, e11.minimumValuePromise);
919
+ }
920
+ async buildInputsStatement(e11, t2) {
921
+ return new Z(e11, t2, n(kt(e11.map((e12) => e12.commitment), "input commitment"), t2));
922
+ }
923
+ async generateBalanceProofSignature(e11, t2, n2, r2) {
924
+ let i2 = o(e11.toBytes(), t2.toBytes(), n2, r2, "[]");
925
+ return y(i2.public_nonce, 32, "balance proof public_nonce"), y(i2.signature, 32, "balance proof signature"), new X(i2.public_nonce, i2.signature);
926
+ }
927
+ async validateBalanceProofSignature(e11, t2, n2) {
928
+ return ne(e11.publicNonce, e11.signature, t2, n2, "[]");
929
+ }
930
+ async deriveAeadKey(e11, t2) {
931
+ y(e11, 32, "deriveAeadKey privateKey"), y(t2, 32, "deriveAeadKey publicKey");
932
+ let n2 = i(e11, t2);
933
+ return y(n2, 32, "derived AEAD key"), n2;
934
+ }
935
+ async unblindOutput(e11, t2, n2, r2) {
936
+ y(e11, 32, "unblindOutput commitment"), y(n2, 32, "unblindOutput aeadKey");
937
+ let i2 = te(e11, t2, n2, r2);
938
+ return {
939
+ mask: J.fromBytes(i2.mask),
940
+ value: i2.value,
941
+ memo: i2.memo_json ?? void 0
942
+ };
943
+ }
944
+ async aggregateInputMasks(t2) {
945
+ let n2 = e(kt(t2.map((e11) => e11.toBytes()), "input mask"));
946
+ return J.fromBytes(n2);
947
+ }
948
+ async stealthDhSecret(e11, t2, n2) {
949
+ y(t2, 32, "stealthDhSecret ownerSecret"), y(n2, 32, "stealthDhSecret publicNonce");
950
+ let r2 = ee(e11, t2, n2);
951
+ return y(r2, 32, "stealth DH secret"), r2;
952
+ }
953
+ async validateTransfer(e11) {
954
+ re(e11.toCompactJson(), null);
955
+ }
956
+ };
957
+ function jt(e11) {
958
+ let t2 = Object.keys(e11);
959
+ if (t2.length !== 1 || t2[0] !== "StealthPublicKey") return false;
960
+ let n2 = e11.StealthPublicKey;
961
+ return typeof n2 == "object" && !!n2 && Object.keys(n2).length === 0;
962
+ }
963
+ var Mt = "utxo";
964
+ function Nt(e11, t2) {
965
+ return `${Mt}_${e11.startsWith("resource_") ? e11.slice(9) : e11}_${b(t2)}`;
966
+ }
967
+ var Pt = 64;
968
+ function Ft(e11) {
969
+ return typeof e11 == "object" && !!e11 && "Utxo" in e11;
970
+ }
971
+ function It(e11) {
972
+ return e11.length > 0 && /^[0-9a-fA-F]+$/.test(e11);
973
+ }
974
+ function Lt(e11) {
975
+ let t2 = e11.split("_");
976
+ if (t2.length < 3 || t2[0] !== Mt) return null;
977
+ let n2 = t2[t2.length - 1];
978
+ return n2.length !== Pt || !It(n2) ? null : x(n2);
979
+ }
980
+ function Rt(e11, t2) {
981
+ let n2 = e11.substate;
982
+ if (!Ft(n2)) return null;
983
+ let r2 = n2.Utxo;
984
+ if (r2.output === null || r2.is_frozen) return null;
985
+ let i2 = Lt(t2);
986
+ return i2 === null ? null : {
987
+ commitment: i2,
988
+ body: r2.output.output
989
+ };
990
+ }
991
+ var zt = 0, Bt = class {
992
+ constructor(e11, t2, n2 = new At(e11.network())) {
993
+ __publicField(this, "provider");
994
+ __publicField(this, "crypto");
995
+ __publicField(this, "builder");
996
+ __publicField(this, "state");
997
+ __publicField(this, "maxEpoch", null);
998
+ __publicField(this, "prepared", false);
999
+ __publicField(this, "revealedOutputBucketVar", null);
1000
+ __publicField(this, "followUpInstructions", []);
1001
+ __publicField(this, "revealedReceiver", null);
1002
+ this.provider = e11, this.crypto = n2, this.builder = B.new(e11.network(), zt), this.state = {
1003
+ resource: t2,
1004
+ revealedInput: null,
1005
+ inputsToSpend: /* @__PURE__ */ new Map(),
1006
+ outputs: [],
1007
+ revealedOutputAmount: 0n
1008
+ };
1009
+ }
1010
+ spendRevealedInput(e11, t2) {
1011
+ if (t2 <= 0n) throw new v(`spendRevealedInput amount must be > 0, got ${t2}`);
1012
+ let n2 = this.state.revealedInput;
1013
+ if (n2 !== null && n2.source !== e11) throw new v(`spendRevealedInput: all revealed input must come from one account (${n2.source} != ${e11})`);
1014
+ return this.state.revealedInput = {
1015
+ source: e11,
1016
+ amount: ((n2 == null ? void 0 : n2.amount) ?? 0n) + t2
1017
+ }, this;
1018
+ }
1019
+ toStealthOutput(e11) {
1020
+ return this.state.outputs.push(e11), this;
1021
+ }
1022
+ toRevealedOutput(e11) {
1023
+ if (e11 <= 0n) throw new v(`toRevealedOutput amount must be > 0, got ${e11}`);
1024
+ return this.state.revealedOutputAmount += e11, this;
1025
+ }
1026
+ toRevealedOutputAsBucket(e11, t2) {
1027
+ if (e11 <= 0n) throw new v(`toRevealedOutputAsBucket amount must be > 0, got ${e11}`);
1028
+ return this.state.revealedOutputAmount += e11, this.revealedOutputBucketVar = t2, this;
1029
+ }
1030
+ withRevealedReceiver(e11) {
1031
+ if (e11.length !== 32) throw new v(`withRevealedReceiver: expected a 32-byte public key, got ${e11.length} bytes`);
1032
+ return this.revealedReceiver = e11, this;
1033
+ }
1034
+ andThen(e11) {
1035
+ return this.followUpInstructions.push(...e11), this;
1036
+ }
1037
+ payFeeFromRevealed(e11) {
1038
+ if (this.state.revealedInput === null) throw new v("payFeeFromRevealed: call spendRevealedInput first to set the source account");
1039
+ if (e11 <= 0n) throw new v(`payFeeFromRevealed amount must be > 0, got ${e11}`);
1040
+ return this.builder.feeTransactionPayFromComponent(this.state.revealedInput.source, e11), this;
1041
+ }
1042
+ withMaxEpoch(e11) {
1043
+ return this.builder.withMaxEpoch(e11), this.maxEpoch = e11, this;
1044
+ }
1045
+ withBuilder(e11) {
1046
+ return this.builder = e11(this.builder), this;
1047
+ }
1048
+ spendStealthInput(e11, t2) {
1049
+ let n2 = new Y(t2), r2 = b(n2.commitment), i2 = this.state.inputsToSpend.get(r2);
1050
+ if (i2 !== void 0) throw new v(`spendStealthInput: duplicate commitment ${r2} \u2014 each commitment is one UTXO and may be spent once (already added for owner ${i2.owner}). A commitment uniquely identifies a UTXO; adding it again (even under a different owner) would self-double-spend.`);
1051
+ return this.state.inputsToSpend.set(r2, {
1052
+ input: n2,
1053
+ owner: e11
1054
+ }), this;
1055
+ }
1056
+ async prepare() {
1057
+ var _a;
1058
+ if (this.prepared) throw new v("StealthTransfer.prepare: already prepared \u2014 create a new StealthTransfer to build again");
1059
+ this.validate(), this.prepared = true;
1060
+ let { statement: e11, outputMask: t2 } = await this.crypto.generateOutputsStatement(this.state.outputs, this.state.revealedOutputAmount, this.revealedReceiver ?? void 0), n2 = new $(await this.crypto.buildInputsStatement([...this.state.inputsToSpend.values()].map((e12) => e12.input), ((_a = this.state.revealedInput) == null ? void 0 : _a.amount) ?? 0n), e11, void 0);
1061
+ this.emitInstructions(n2), this.maxEpoch === null && this.builder.buildUnsignedTransaction().max_epoch === zt && this.builder.withMaxEpoch(await We(this.provider));
1062
+ let r2 = /* @__PURE__ */ new Set();
1063
+ if (this.state.revealedInput !== null) {
1064
+ let e12 = this.state.revealedInput.source;
1065
+ this.builder.addInput({
1066
+ substate_id: e12,
1067
+ version: null
1068
+ }), r2.add(e12);
1069
+ let t3 = await nt(this.provider, e12);
1070
+ for (let e13 of t3) r2.has(e13) || (this.builder.addInput({
1071
+ substate_id: e13,
1072
+ version: null
1073
+ }), r2.add(e13));
1074
+ }
1075
+ for (let { input: e12 } of this.state.inputsToSpend.values()) {
1076
+ let t3 = Nt(this.state.resource, e12.commitment);
1077
+ r2.has(t3) || (this.builder.addInput({
1078
+ substate_id: t3,
1079
+ version: null
1080
+ }), r2.add(t3));
1081
+ }
1082
+ let i2 = await G(this.provider, this.builder.buildUnsignedTransaction()), a2 = [...this.state.inputsToSpend.values()], o2 = this.collectRequiredSigners(a2);
1083
+ return {
1084
+ unsignedTx: i2,
1085
+ statement: n2,
1086
+ outputMask: t2,
1087
+ state: this.state,
1088
+ requiredSigners: o2,
1089
+ inputs: a2
1090
+ };
1091
+ }
1092
+ collectRequiredSigners(e11) {
1093
+ let t2 = [], n2 = /* @__PURE__ */ new Set();
1094
+ this.state.revealedInput !== null && (t2.push(this.state.revealedInput.source), n2.add(this.state.revealedInput.source));
1095
+ for (let { owner: r2 } of e11) n2.has(r2) || (t2.push(r2), n2.add(r2));
1096
+ return t2;
1097
+ }
1098
+ validate() {
1099
+ var _a;
1100
+ let e11 = this.state.inputsToSpend.size > 0, t2 = ((_a = this.state.revealedInput) == null ? void 0 : _a.amount) ?? 0n;
1101
+ if (!(t2 > 0n || e11)) throw new v("StealthTransfer.prepare: no inputs \u2014 call spendRevealedInput or spendStealthInput first");
1102
+ if (this.state.outputs.length === 0 && this.state.revealedOutputAmount === 0n) throw new v("StealthTransfer.prepare: no outputs \u2014 call toStealthOutput or toRevealedOutput first");
1103
+ if (this.state.outputs.length === 0) throw new v("StealthTransfer.prepare: at least one stealth output is required");
1104
+ if (this.state.revealedOutputAmount > 0n && this.revealedReceiver === null) throw new v("StealthTransfer.prepare: a revealed output needs withRevealedReceiver(publicKey) \u2014 the signing key allowed to take it");
1105
+ if (this.state.revealedOutputAmount > 0n && this.state.revealedInput === null && this.revealedOutputBucketVar === null) throw new v("StealthTransfer.prepare: revealed change requires a revealed source account to deposit into");
1106
+ if (!e11) {
1107
+ let e12 = this.state.outputs.reduce((e13, t3) => e13 + t3.amount, 0n), n2 = e12 + this.state.revealedOutputAmount, r2 = t2;
1108
+ if (r2 !== n2) throw new v(`StealthTransfer.prepare: unbalanced transfer \u2014 revealed input ${r2} != stealth out ${e12} + revealed out ${this.state.revealedOutputAmount} (= ${n2})`);
1109
+ }
1110
+ }
1111
+ emitInstructions(e11) {
1112
+ let t2 = this.state.revealedInput, n2 = null;
1113
+ t2 !== null && (this.builder.callMethod({
1114
+ componentAddress: t2.source,
1115
+ methodName: "withdraw"
1116
+ }, [j(this.state.resource), k(t2.amount)]).saveVar(ut), n2 = ut), this.builder.addInstruction(ft({
1117
+ resourceAddress: this.state.resource,
1118
+ revealedInputBucket: n2,
1119
+ statement: e11
1120
+ }, (e12) => this.builder.resolveWorkspaceOffsetId(e12))), this.state.revealedOutputAmount > 0n && this.revealedOutputBucketVar !== null ? this.builder.saveVar(this.revealedOutputBucketVar) : this.state.revealedOutputAmount > 0n && t2 !== null && this.builder.saveVar(dt).callMethod({
1121
+ componentAddress: t2.source,
1122
+ methodName: "deposit"
1123
+ }, [{ Workspace: dt }]), this.followUpInstructions.length > 0 && this.builder.withInstructions(this.followUpInstructions);
1124
+ }
1125
+ };
1126
+ function Vt(e11) {
1127
+ if (Ht(e11) || e11.statementJson !== void 0) return e11.statementJson;
1128
+ if (e11.inputs.length > 0) throw new _("inputs statement has confidential inputs but no WASM statementJson \u2014 call buildInputsStatement first", { context: "statementJsonFor" });
1129
+ return JSON.stringify(e11.toJSON());
1130
+ }
1131
+ function Ht(e11) {
1132
+ return !("inputs" in e11);
1133
+ }
1134
+ async function Ut(e11, t2, n2, r2, i2) {
1135
+ let a2 = Vt(r2), o2 = Vt(i2);
1136
+ return e11.generateBalanceProofSignature(t2, n2, a2, o2);
1137
+ }
1138
+ async function Wt(e11, t2, n2, r2) {
1139
+ let i2 = await e11.deriveAeadKey(r2.viewSecret, r2.senderPublicNonce);
1140
+ return e11.unblindOutput(t2, n2, i2, r2.skipMemo);
1141
+ }
1142
+ async function Gt(e11, t2, n2, r2) {
1143
+ let i2 = Rt(n2, r2);
1144
+ if (i2 === null) return null;
1145
+ let a2 = x(i2.body.encrypted_data), o2 = x(i2.body.public_nonce);
1146
+ try {
1147
+ return await Wt(e11, i2.commitment, a2, {
1148
+ senderPublicNonce: o2,
1149
+ viewSecret: t2,
1150
+ skipMemo: false
1151
+ });
1152
+ } catch {
1153
+ return null;
1154
+ }
1155
+ }
1156
+ async function Kt(e11, t2, n2, r2) {
1157
+ if (t2.length === 0) throw new v("generateOutputsStatement: at least one stealth output is required");
1158
+ let { statement: i2, outputMask: a2 } = await e11.generateOutputsStatement(t2, n2, r2), o2 = await e11.buildInputsStatement([], 0n);
1159
+ return new $(o2, i2, await Ut(e11, J.zero(), a2, o2, i2));
1160
+ }
1161
+ var qt = class e10 {
1162
+ constructor(e11, t2, n2, r2, i2) {
1163
+ this.wallet = e11, this.spec = t2, this.crypto = n2, this.mustSignWithAccountKey = r2, this.viewSecret = i2;
1164
+ }
1165
+ static fromSpec(t2, n2, r2 = {}) {
1166
+ return new e10(t2, n2, r2.crypto ?? new At(), r2.mustSignWithAccountKey ?? true, r2.viewSecret);
1167
+ }
1168
+ async prepare(e11) {
1169
+ let t2 = await this.resolveStealthInputs(e11), n2 = t2.length === 0 ? J.zero() : await this.crypto.aggregateInputMasks(t2.map((e12) => e12.mask)), r2 = await Ut(this.crypto, n2, this.spec.outputMask, this.spec.statement.inputsStatement, this.spec.statement.outputsStatement), i2 = new $(this.spec.statement.inputsStatement, this.spec.statement.outputsStatement, r2);
1170
+ await this.crypto.validateTransfer(i2);
1171
+ let a2 = Yt(this.spec.unsignedTx, i2), o2 = {
1172
+ ...this.spec,
1173
+ unsignedTx: a2,
1174
+ statement: i2
1175
+ };
1176
+ return new Jt({
1177
+ wallet: this.wallet,
1178
+ crypto: this.crypto,
1179
+ spec: o2,
1180
+ resolvedInputs: t2,
1181
+ mustSignWithAccountKey: this.mustSignWithAccountKey,
1182
+ sealKeypair: pt()
1183
+ });
1184
+ }
1185
+ async resolveViewSecret() {
1186
+ if (this.viewSecret !== void 0) return this.viewSecret;
1187
+ let e11 = this.wallet.getKeyProvider(await this.safeDefaultAddress());
1188
+ if ((e11 == null ? void 0 : e11.getViewSecret) !== void 0) try {
1189
+ return await e11.getViewSecret();
1190
+ } catch (e12) {
1191
+ throw e12 instanceof p ? e12 : new p("WalletStealthAuthorizer: spending stealth inputs needs a view secret to unblind them, but the wallet's default signer could not provide one. Pass `fromSpec(wallet, spec, { viewSecret })` or use a SecretKeyWallet (created with a view key) as the default signer.", { cause: e12 });
1192
+ }
1193
+ throw new p("WalletStealthAuthorizer: spending stealth inputs needs a view secret to unblind them, but none was supplied and the wallet's default signer cannot provide one. Pass `fromSpec(wallet, spec, { viewSecret })` or use a SecretKeyWallet (created with a view key) as the default signer.");
1194
+ }
1195
+ async safeDefaultAddress() {
1196
+ try {
1197
+ return await this.wallet.getAddress();
1198
+ } catch {
1199
+ return "";
1200
+ }
1201
+ }
1202
+ async resolveStealthInputs(e11) {
1203
+ if (this.spec.inputs.length === 0) return [];
1204
+ let t2 = await this.resolveViewSecret(), n2 = this.spec.state.resource, r2 = await Promise.allSettled(this.spec.inputs.map(async ({ input: r3, owner: i3 }) => {
1205
+ let a2 = b(r3.commitment), o2 = await e11.getStealthUtxo(n2, r3.commitment);
1206
+ if (o2 === null) throw new p(`WalletStealthAuthorizer.prepare: stealth input UTXO not found (spent or never created) for ${a2}`);
1207
+ let s2 = Rt(o2, Nt(n2, r3.commitment));
1208
+ if (s2 === null) throw new p(`WalletStealthAuthorizer.prepare: stealth input ${a2} is not a live, spendable UTXO`);
1209
+ let c2 = x(s2.body.public_nonce), l2;
1210
+ try {
1211
+ l2 = await Wt(this.crypto, s2.commitment, x(s2.body.encrypted_data), {
1212
+ senderPublicNonce: c2,
1213
+ viewSecret: t2,
1214
+ skipMemo: true
1215
+ });
1216
+ } catch (e12) {
1217
+ throw new _(`WalletStealthAuthorizer.prepare: failed to decrypt stealth input with commitment ${b(r3.commitment)} (owner ${i3}). This usually means the supplied view secret does not own this UTXO. Pass the correct \`viewSecret\` via \`fromSpec(wallet, spec, { viewSecret })\`, or use the owning SecretKeyWallet (with its view key) as the default signer. Note: a single view secret is resolved for all inputs \u2014 spending UTXOs owned by distinct view keys in one transfer is not supported.`, {
1218
+ cause: e12,
1219
+ context: "unblindOutput"
1220
+ });
1221
+ }
1222
+ return {
1223
+ ownerAddr: i3,
1224
+ input: r3,
1225
+ mask: l2.mask,
1226
+ publicNonce: c2
1227
+ };
1228
+ })), i2 = r2.find((e12) => e12.status === "rejected");
1229
+ if (i2 !== void 0) throw i2.reason;
1230
+ return r2.map((e12) => e12.value);
1231
+ }
1232
+ }, Jt = class {
1233
+ constructor(e11) {
1234
+ __publicField(this, "wallet");
1235
+ __publicField(this, "crypto");
1236
+ __publicField(this, "spec");
1237
+ __publicField(this, "resolvedInputs");
1238
+ __publicField(this, "mustSignWithAccountKey");
1239
+ __publicField(this, "sealKeypair");
1240
+ __publicField(this, "extraSignatures", []);
1241
+ __publicField(this, "cachedAuthorizations");
1242
+ this.wallet = e11.wallet, this.crypto = e11.crypto, this.spec = e11.spec, this.resolvedInputs = e11.resolvedInputs, this.mustSignWithAccountKey = e11.mustSignWithAccountKey, this.sealKeypair = e11.sealKeypair;
1243
+ }
1244
+ getSealPublicKey() {
1245
+ return this.sealKeypair.public_key;
1246
+ }
1247
+ getSpec() {
1248
+ return this.spec;
1249
+ }
1250
+ async createAuthorizations() {
1251
+ return this.cachedAuthorizations === void 0 && (this.cachedAuthorizations = await this.computeAuthorizations()), this.cachedAuthorizations;
1252
+ }
1253
+ async computeAuthorizations() {
1254
+ if (this.resolvedInputs.length === 0) return [];
1255
+ let e11 = W(this.spec.unsignedTx), t2 = this.sealKeypair.public_key, n2 = [];
1256
+ for (let r2 of this.resolvedInputs) {
1257
+ let i2 = this.wallet.getKeyProvider(r2.ownerAddr);
1258
+ if (i2 === void 0) throw new m(`AuthorizedTransfer.createAuthorizations: no signer registered for stealth-input owner ${r2.ownerAddr}. Register it via wallet.registerKeyProvider(owner, signer).`, { address: r2.ownerAddr });
1259
+ if (i2.addStealthSignature === void 0) throw new g(`AuthorizedTransfer.createAuthorizations: signer for ${r2.ownerAddr} cannot produce one-time stealth signatures. Use a SecretKeyWallet (which holds the owner secret).`);
1260
+ let a2 = await i2.addStealthSignature(e11, r2.publicNonce, t2, { crypto: this.crypto });
1261
+ n2.push({
1262
+ signerAddress: r2.ownerAddr,
1263
+ signatures: [a2]
1264
+ });
1265
+ }
1266
+ return n2;
1267
+ }
1268
+ addSignature(e11) {
1269
+ return this.extraSignatures.push(e11), this;
1270
+ }
1271
+ async seal() {
1272
+ let e11 = (await this.createAuthorizations()).flatMap((e12) => e12.signatures), t2 = [];
1273
+ this.mustSignWithAccountKey && t2.push(this.wallet);
1274
+ let n2 = [...e11, ...this.extraSignatures];
1275
+ return n2.length > 0 && t2.push(new Xt(n2)), q(await K(t2, this.spec.unsignedTx, this.sealKeypair));
1276
+ }
1277
+ };
1278
+ function Yt(e11, t2) {
1279
+ let n2 = e11.instructions.findIndex(U);
1280
+ if (n2 < 0) throw new v("patchStealthStatement: expected exactly one StealthTransfer instruction, found 0 (not a stealth tx)");
1281
+ if (e11.instructions.slice(n2 + 1).findIndex(U) >= 0) throw new v("patchStealthStatement: expected exactly one StealthTransfer instruction, found more than one (malformed)");
1282
+ let r2 = { StealthTransfer: {
1283
+ ...e11.instructions[n2].StealthTransfer,
1284
+ statement: H(t2)
1285
+ } }, i2 = [...e11.instructions];
1286
+ return i2[n2] = r2, {
1287
+ ...e11,
1288
+ instructions: i2
1289
+ };
1290
+ }
1291
+ var Xt = class {
1292
+ constructor(e11) {
1293
+ this.signatures = e11;
1294
+ }
1295
+ async getAddress() {
1296
+ throw new g("StaticSignatureSigner has no address");
1297
+ }
1298
+ async getPublicKey() {
1299
+ throw new g("StaticSignatureSigner has no public key");
1300
+ }
1301
+ async signTransaction(e11, t2) {
1302
+ return this.signatures;
1303
+ }
1304
+ };
1305
+ export {
1306
+ bt as AccountInvokeBuilder,
1307
+ Jt as AuthorizedTransfer,
1308
+ X as BalanceProofSignature,
1309
+ _ as CryptoBridgeError,
1310
+ Ue as DEFAULT_TRANSACTION_VALIDITY_EPOCHS,
1311
+ h as DefaultSignerNotSetError,
1312
+ wt as EncryptedData,
1313
+ xt as FaucetInvokeBuilder,
1314
+ ie as IndexerClientError,
1315
+ v as InvalidArgumentError,
1316
+ m as KeyProviderNotFoundError,
1317
+ He as MAX_TRANSACTION_VALIDITY_EPOCHS,
1318
+ J as Mask,
1319
+ u as Network,
1320
+ d as OotleError,
1321
+ St as OotleWallet,
1322
+ oe as OperationCancelledError,
1323
+ g as SignerError,
1324
+ Y as StealthInput,
1325
+ Z as StealthInputsStatement,
1326
+ Q as StealthOutputsStatement,
1327
+ Bt as StealthTransfer,
1328
+ $ as StealthTransferStatement,
1329
+ at as TARI_RESOURCE_ADDRESS,
1330
+ B as TransactionBuilder,
1331
+ f as TransactionRejectedError,
1332
+ ae as TransactionTimeoutError,
1333
+ p as WalletError,
1334
+ qt as WalletStealthAuthorizer,
1335
+ At as WasmStealthCrypto,
1336
+ ct as XTR_FAUCET_CLAIM_RESOURCE_ADDRESS,
1337
+ ot as XTR_FAUCET_COMPONENT_ADDRESS,
1338
+ st as XTR_FAUCET_VAULT_ADDRESS,
1339
+ k as amountLiteral,
1340
+ y as assertByteLength,
1341
+ se as assertUnreachable,
1342
+ De as boolLiteral,
1343
+ mt as buildTransactionSignature,
1344
+ A as bytesLiteral,
1345
+ Me as claimedOutputTombstoneAddressLiteral,
1346
+ gt as classifyOutcome,
1347
+ Lt as commitmentOf,
1348
+ Oe as componentAddressLiteral,
1349
+ Et as createOutput,
1350
+ Wt as decryptInputData,
1351
+ Gt as decryptOwnedUtxo,
1352
+ Qe as defaultIndexerUrl,
1353
+ x as fromHexStr,
1354
+ Kt as generateOutputsStatement,
1355
+ pt as generateSealKeypair,
1356
+ nt as getVaultIdsForAccount,
1357
+ Te as intLiteral,
1358
+ U as isStealthTransferInstruction,
1359
+ tt as iterVaultIdsInState,
1360
+ Ce as literalArg,
1361
+ ke as metadataLiteral,
1362
+ R as microTariLiteral,
1363
+ Be as microTariString,
1364
+ Fe as nonFungibleAddressLiteral,
1365
+ Rt as parseSubstateUtxo,
1366
+ Ve as parseWorkspaceStringKey,
1367
+ Yt as patchStealthStatement,
1368
+ Pe as publicKeyLiteral,
1369
+ We as resolveMaxEpoch,
1370
+ G as resolveTransaction,
1371
+ j as resourceAddressLiteral,
1372
+ q as sealTransaction,
1373
+ yt as sendDryRun,
1374
+ vt as sendTransaction,
1375
+ W as serializeUnsignedTx,
1376
+ Ut as signBalanceProof,
1377
+ K as signTransaction,
1378
+ H as statementAsWire,
1379
+ ft as stealthTransferInstruction,
1380
+ Nt as stealthUtxoSubstateId,
1381
+ Ee as stringLiteral,
1382
+ ht as submitTransaction,
1383
+ je as templateAddressLiteral,
1384
+ b as toHexStr,
1385
+ Ie as utxoAddressLiteral,
1386
+ Ne as validatorFeePoolAddressLiteral,
1387
+ Ae as vaultIdLiteral,
1388
+ _t as watchTransaction
1389
+ };