@miden-sdk/miden-sdk 0.14.11 → 0.15.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/dist/mt/{Cargo-CZopJ--X.js → Cargo-smJQCGjz.js} +1111 -768
  2. package/dist/mt/Cargo-smJQCGjz.js.map +1 -0
  3. package/dist/mt/api-types.d.ts +122 -33
  4. package/dist/mt/assets/miden_client_web.wasm +0 -0
  5. package/dist/mt/crates/miden_client_web.d.ts +376 -303
  6. package/dist/mt/docs-entry.d.ts +3 -0
  7. package/dist/mt/eager.js +7 -4
  8. package/dist/mt/eager.js.map +1 -1
  9. package/dist/mt/index.d.ts +103 -10
  10. package/dist/mt/index.js +669 -312
  11. package/dist/mt/index.js.map +1 -1
  12. package/dist/mt/wasm.js +1 -1
  13. package/dist/mt/workerHelpers.js +1 -1
  14. package/dist/mt/workers/{Cargo-CZopJ--X-SsyOTzpb.js → Cargo-smJQCGjz-q4GYXDiD.js} +1111 -768
  15. package/dist/mt/workers/Cargo-smJQCGjz-q4GYXDiD.js.map +1 -0
  16. package/dist/mt/workers/assets/miden_client_web.wasm +0 -0
  17. package/dist/mt/workers/web-client-methods-worker.js +1140 -792
  18. package/dist/mt/workers/web-client-methods-worker.js.map +1 -1
  19. package/dist/mt/workers/web-client-methods-worker.module.js +23 -19
  20. package/dist/mt/workers/web-client-methods-worker.module.js.map +1 -1
  21. package/dist/mt/workers/workerHelpers.js +1 -1
  22. package/dist/st/{Cargo-DC6jSekr.js → Cargo-CG4XszZo.js} +1105 -763
  23. package/dist/st/Cargo-CG4XszZo.js.map +1 -0
  24. package/dist/st/api-types.d.ts +122 -33
  25. package/dist/st/assets/miden_client_web.wasm +0 -0
  26. package/dist/st/crates/miden_client_web.d.ts +376 -303
  27. package/dist/st/docs-entry.d.ts +3 -0
  28. package/dist/st/eager.js +7 -4
  29. package/dist/st/eager.js.map +1 -1
  30. package/dist/st/index.d.ts +103 -10
  31. package/dist/st/index.js +669 -312
  32. package/dist/st/index.js.map +1 -1
  33. package/dist/st/wasm.js +1 -1
  34. package/dist/st/workers/{Cargo-DC6jSekr-BG7C7m56.js → Cargo-CG4XszZo-S7EHAZSa.js} +1105 -763
  35. package/dist/st/workers/Cargo-CG4XszZo-S7EHAZSa.js.map +1 -0
  36. package/dist/st/workers/assets/miden_client_web.wasm +0 -0
  37. package/dist/st/workers/web-client-methods-worker.js +1134 -787
  38. package/dist/st/workers/web-client-methods-worker.js.map +1 -1
  39. package/dist/st/workers/web-client-methods-worker.module.js +23 -19
  40. package/dist/st/workers/web-client-methods-worker.module.js.map +1 -1
  41. package/js/client.js +491 -0
  42. package/js/node/client-factory.js +117 -0
  43. package/js/node/loader.js +138 -0
  44. package/js/node/napi-compat.js +253 -0
  45. package/js/node-index.js +194 -0
  46. package/js/resources/accounts.js +222 -0
  47. package/js/resources/compiler.js +74 -0
  48. package/js/resources/keystore.js +54 -0
  49. package/js/resources/notes.js +124 -0
  50. package/js/resources/settings.js +30 -0
  51. package/js/resources/tags.js +31 -0
  52. package/js/resources/transactions.js +667 -0
  53. package/js/standalone.js +109 -0
  54. package/js/utils.js +232 -0
  55. package/package.json +17 -2
  56. package/dist/mt/Cargo-CZopJ--X.js.map +0 -1
  57. package/dist/mt/workers/Cargo-CZopJ--X-SsyOTzpb.js.map +0 -1
  58. package/dist/st/Cargo-DC6jSekr.js.map +0 -1
  59. package/dist/st/workers/Cargo-DC6jSekr-BG7C7m56.js.map +0 -1
@@ -0,0 +1,222 @@
1
+ import {
2
+ resolveAccountRef,
3
+ resolveStorageMode,
4
+ resolveAuthScheme,
5
+ resolveAccountMutability,
6
+ hashSeed,
7
+ } from "../utils.js";
8
+
9
+ export class AccountsResource {
10
+ #inner;
11
+ #getWasm;
12
+ #client;
13
+
14
+ constructor(inner, getWasm, client) {
15
+ this.#inner = inner;
16
+ this.#getWasm = getWasm;
17
+ this.#client = client;
18
+ }
19
+
20
+ async create(opts) {
21
+ this.#client.assertNotTerminated();
22
+ const wasm = await this.#getWasm();
23
+
24
+ const type = opts?.type;
25
+
26
+ if (
27
+ type === 0 ||
28
+ type === 1 ||
29
+ type === "FungibleFaucet" ||
30
+ type === "NonFungibleFaucet"
31
+ ) {
32
+ const storageMode = resolveStorageMode(opts.storage ?? "public", wasm);
33
+ const authScheme = resolveAuthScheme(opts.auth, wasm);
34
+ return await this.#inner.newFaucet(
35
+ storageMode,
36
+ type === 1 || type === "NonFungibleFaucet",
37
+ opts.name ?? opts.symbol,
38
+ opts.symbol,
39
+ opts.decimals,
40
+ BigInt(opts.maxSupply),
41
+ authScheme
42
+ );
43
+ } else if (
44
+ type === "ImmutableContract" ||
45
+ type === "MutableContract" ||
46
+ opts?.components // Contracts are distinguished from wallets by having components
47
+ ) {
48
+ return await this.#createContract(opts, wasm);
49
+ } else {
50
+ // Default: wallet (mutable or immutable based on type)
51
+ const mutable = resolveAccountMutability(opts?.type);
52
+ const storageMode = resolveStorageMode(opts?.storage ?? "private", wasm);
53
+ const authScheme = resolveAuthScheme(opts?.auth, wasm);
54
+ const seed = opts?.seed ? await hashSeed(opts.seed) : undefined;
55
+ return await this.#inner.newWallet(
56
+ storageMode,
57
+ mutable,
58
+ authScheme,
59
+ seed
60
+ );
61
+ }
62
+ }
63
+
64
+ async #createContract(opts, wasm) {
65
+ if (!opts.seed)
66
+ throw new Error("Contract creation requires a 'seed' (Uint8Array)");
67
+ if (!opts.auth)
68
+ throw new Error("Contract creation requires an 'auth' (AuthSecretKey)");
69
+
70
+ // The 0.15 protocol has no code-mutability distinction, so the `type`
71
+ // ("ImmutableContract" / "MutableContract") only steers routing here; the
72
+ // account's on-chain visibility is set entirely by `storageMode`.
73
+ const storageMode = resolveStorageMode(opts.storage ?? "public", wasm);
74
+ const authComponent =
75
+ wasm.AccountComponent.createAuthComponentFromSecretKey(opts.auth);
76
+
77
+ // Schema commitment from `build()` is not a substitute for contract code; require explicit
78
+ // `components` so auth-only contracts are rejected at this layer.
79
+ const components = opts.components ?? [];
80
+ if (components.length === 0) {
81
+ throw new Error(
82
+ "Contract accounts require at least one non-auth procedure: pass at least one entry in `components`."
83
+ );
84
+ }
85
+
86
+ let builder = new wasm.AccountBuilder(opts.seed)
87
+ .storageMode(storageMode)
88
+ .withAuthComponent(authComponent);
89
+
90
+ for (const component of components) {
91
+ builder = builder.withComponent(component);
92
+ }
93
+
94
+ const built = builder.build();
95
+ const account = built.account;
96
+
97
+ await this.#inner.newAccountWithSecretKey(account, opts.auth);
98
+ return account;
99
+ }
100
+
101
+ async insert({ account, overwrite = false }) {
102
+ this.#client.assertNotTerminated();
103
+ await this.#inner.newAccount(account, overwrite);
104
+ }
105
+
106
+ async getOrImport(ref) {
107
+ this.#client.assertNotTerminated();
108
+ return (await this.get(ref)) ?? (await this.import(ref));
109
+ }
110
+
111
+ async get(ref) {
112
+ this.#client.assertNotTerminated();
113
+ const wasm = await this.#getWasm();
114
+ const id = resolveAccountRef(ref, wasm);
115
+ const account = await this.#inner.getAccount(id);
116
+ return account ?? null;
117
+ }
118
+
119
+ async list() {
120
+ this.#client.assertNotTerminated();
121
+ return await this.#inner.getAccounts();
122
+ }
123
+
124
+ async getDetails(ref) {
125
+ this.#client.assertNotTerminated();
126
+ const wasm = await this.#getWasm();
127
+ const id = resolveAccountRef(ref, wasm);
128
+ const account = await this.#inner.getAccount(id);
129
+ if (!account) {
130
+ throw new Error(`Account not found: ${id.toString()}`);
131
+ }
132
+ const keys = this.#inner.keystore
133
+ ? await this.#inner.keystore.getCommitments(id)
134
+ : await this.#inner.getPublicKeyCommitmentsOfAccount(id);
135
+ return {
136
+ account,
137
+ vault: account.vault(),
138
+ storage: account.storage(),
139
+ code: account.code() ?? null,
140
+ keys,
141
+ };
142
+ }
143
+
144
+ async getBalance(accountRef, tokenRef) {
145
+ this.#client.assertNotTerminated();
146
+ const wasm = await this.#getWasm();
147
+ const accountId = resolveAccountRef(accountRef, wasm);
148
+ const faucetId = resolveAccountRef(tokenRef, wasm);
149
+ const reader = await this.#inner.accountReader(accountId);
150
+ return await reader.getBalance(faucetId);
151
+ }
152
+
153
+ async import(input) {
154
+ this.#client.assertNotTerminated();
155
+ const wasm = await this.#getWasm();
156
+
157
+ // Early exit for string, Account, and AccountHeader types before property
158
+ // checks, preventing misrouting if a WASM object ever gains a .file or .seed
159
+ // property. Bare AccountId (no .id() method) falls through to the fallback.
160
+ if (typeof input === "string" || typeof input.id === "function") {
161
+ const id = resolveAccountRef(input, wasm);
162
+ await this.#inner.importAccountById(id);
163
+ return await this.#inner.getAccount(id);
164
+ }
165
+
166
+ if (input.file) {
167
+ // Extract accountId before importAccountFile — WASM consumes the
168
+ // AccountFile by value, invalidating the JS wrapper after the call.
169
+ const accountId =
170
+ typeof input.file.accountId === "function"
171
+ ? input.file.accountId()
172
+ : null;
173
+ await this.#inner.importAccountFile(input.file);
174
+ if (accountId) {
175
+ return await this.#inner.getAccount(accountId);
176
+ }
177
+ throw new Error(
178
+ "Could not determine account ID from AccountFile. " +
179
+ "Ensure the file contains a valid account."
180
+ );
181
+ }
182
+
183
+ if (input.seed) {
184
+ // Import public account from seed
185
+ const authScheme = resolveAuthScheme(input.auth, wasm);
186
+ const mutable = resolveAccountMutability(input.type);
187
+ return await this.#inner.importPublicAccountFromSeed(
188
+ input.seed,
189
+ mutable,
190
+ authScheme
191
+ );
192
+ }
193
+
194
+ // Fallback: treat as AccountRef (string, AccountId, Account, AccountHeader)
195
+ const id = resolveAccountRef(input, wasm);
196
+ await this.#inner.importAccountById(id);
197
+ return await this.#inner.getAccount(id);
198
+ }
199
+
200
+ async export(ref) {
201
+ this.#client.assertNotTerminated();
202
+ const wasm = await this.#getWasm();
203
+ const id = resolveAccountRef(ref, wasm);
204
+ return await this.#inner.exportAccountFile(id);
205
+ }
206
+
207
+ async addAddress(ref, addr) {
208
+ this.#client.assertNotTerminated();
209
+ const wasm = await this.#getWasm();
210
+ const id = resolveAccountRef(ref, wasm);
211
+ const address = wasm.Address.fromBech32(addr);
212
+ await this.#inner.insertAccountAddress(id, address);
213
+ }
214
+
215
+ async removeAddress(ref, addr) {
216
+ this.#client.assertNotTerminated();
217
+ const wasm = await this.#getWasm();
218
+ const id = resolveAccountRef(ref, wasm);
219
+ const address = wasm.Address.fromBech32(addr);
220
+ await this.#inner.removeAccountAddress(id, address);
221
+ }
222
+ }
@@ -0,0 +1,74 @@
1
+ export class CompilerResource {
2
+ #inner;
3
+ #getWasm;
4
+ #client;
5
+
6
+ constructor(inner, getWasm, client = null) {
7
+ this.#inner = inner;
8
+ this.#getWasm = getWasm;
9
+ this.#client = client;
10
+ }
11
+
12
+ /**
13
+ * Compiles MASM code + slots into an AccountComponent ready for accounts.create().
14
+ *
15
+ * @param {{ code: string, slots: StorageSlot[], supportAllTypes?: boolean }} opts
16
+ * @returns {Promise<AccountComponent>}
17
+ */
18
+ async component({ code, slots = [], supportAllTypes = true }) {
19
+ this.#client?.assertNotTerminated();
20
+ const wasm = await this.#getWasm();
21
+ const builder = await this.#inner.createCodeBuilder();
22
+ const compiled = builder.compileAccountComponentCode(code);
23
+ const component = wasm.AccountComponent.compile(compiled, slots);
24
+ return supportAllTypes ? component.withSupportsAllTypes() : component;
25
+ }
26
+
27
+ /**
28
+ * Compiles a transaction script, optionally linking named libraries inline.
29
+ *
30
+ * @param {{ code: string, libraries?: Array<{ namespace: string, code: string, linking?: "dynamic" | "static" }> }} opts
31
+ * @returns {Promise<TransactionScript>}
32
+ */
33
+ async txScript({ code, libraries = [] }) {
34
+ this.#client?.assertNotTerminated();
35
+ // Ensure WASM is initialized (result unused — only #inner needs it)
36
+ await this.#getWasm();
37
+ const builder = await this.#inner.createCodeBuilder();
38
+ linkLibraries(builder, libraries);
39
+ return builder.compileTxScript(code);
40
+ }
41
+
42
+ /**
43
+ * Compiles a note script, optionally linking named libraries inline.
44
+ *
45
+ * @param {{ code: string, libraries?: Array<{ namespace: string, code: string, linking?: "dynamic" | "static" }> }} opts
46
+ * @returns {Promise<NoteScript>}
47
+ */
48
+ async noteScript({ code, libraries = [] }) {
49
+ this.#client?.assertNotTerminated();
50
+ await this.#getWasm();
51
+ const builder = await this.#inner.createCodeBuilder();
52
+ linkLibraries(builder, libraries);
53
+ return builder.compileNoteScript(code);
54
+ }
55
+ }
56
+
57
+ // Builds and links each library entry against `builder`. Inline
58
+ // `{ namespace, code, linking? }` entries are built via `buildLibrary` and
59
+ // linked according to `linking` (defaulting to dynamic, matching tutorial
60
+ // behavior). Pre-built library objects are linked dynamically.
61
+ function linkLibraries(builder, libraries) {
62
+ for (const lib of libraries) {
63
+ if (lib && typeof lib.namespace === "string") {
64
+ const built = builder.buildLibrary(lib.namespace, lib.code);
65
+ if (lib.linking === "static") {
66
+ builder.linkStaticLibrary(built);
67
+ } else {
68
+ builder.linkDynamicLibrary(built);
69
+ }
70
+ } else {
71
+ builder.linkDynamicLibrary(lib);
72
+ }
73
+ }
74
+ }
@@ -0,0 +1,54 @@
1
+ export class KeystoreResource {
2
+ #inner;
3
+ #client;
4
+
5
+ constructor(inner, client) {
6
+ this.#inner = inner;
7
+ this.#client = client;
8
+ }
9
+
10
+ async insert(accountId, secretKey) {
11
+ this.#client.assertNotTerminated();
12
+ if (this.#inner.keystore) {
13
+ return await this.#inner.keystore.insert(accountId, secretKey);
14
+ }
15
+ return await this.#inner.addAccountSecretKeyToWebStore(
16
+ accountId,
17
+ secretKey
18
+ );
19
+ }
20
+
21
+ async get(pubKeyCommitment) {
22
+ this.#client.assertNotTerminated();
23
+ if (this.#inner.keystore) {
24
+ return await this.#inner.keystore.get(pubKeyCommitment);
25
+ }
26
+ return await this.#inner.getAccountAuthByPubKeyCommitment(pubKeyCommitment);
27
+ }
28
+
29
+ async remove(pubKeyCommitment) {
30
+ this.#client.assertNotTerminated();
31
+ if (this.#inner.keystore) {
32
+ return await this.#inner.keystore.remove(pubKeyCommitment);
33
+ }
34
+ throw new Error("remove() is not supported on this platform");
35
+ }
36
+
37
+ async getCommitments(accountId) {
38
+ this.#client.assertNotTerminated();
39
+ if (this.#inner.keystore) {
40
+ return await this.#inner.keystore.getCommitments(accountId);
41
+ }
42
+ return await this.#inner.getPublicKeyCommitmentsOfAccount(accountId);
43
+ }
44
+
45
+ async getAccountId(pubKeyCommitment) {
46
+ this.#client.assertNotTerminated();
47
+ if (this.#inner.keystore) {
48
+ return await this.#inner.keystore.getAccountId(pubKeyCommitment);
49
+ }
50
+ const account =
51
+ await this.#inner.getAccountByKeyCommitment(pubKeyCommitment);
52
+ return account ? account.id() : undefined;
53
+ }
54
+ }
@@ -0,0 +1,124 @@
1
+ import {
2
+ resolveAccountRef,
3
+ resolveAddress,
4
+ resolveNoteIdHex,
5
+ } from "../utils.js";
6
+
7
+ export class NotesResource {
8
+ #inner;
9
+ #getWasm;
10
+ #client;
11
+
12
+ constructor(inner, getWasm, client) {
13
+ this.#inner = inner;
14
+ this.#getWasm = getWasm;
15
+ this.#client = client;
16
+ }
17
+
18
+ async list(query) {
19
+ this.#client.assertNotTerminated();
20
+ const wasm = await this.#getWasm();
21
+ const filter = buildNoteFilter(query, wasm);
22
+ return await this.#inner.getInputNotes(filter);
23
+ }
24
+
25
+ async get(noteId) {
26
+ this.#client.assertNotTerminated();
27
+ const result = await this.#inner.getInputNote(resolveNoteIdHex(noteId));
28
+ return result ?? null;
29
+ }
30
+
31
+ async listSent(query) {
32
+ this.#client.assertNotTerminated();
33
+ const wasm = await this.#getWasm();
34
+ const filter = buildNoteFilter(query, wasm);
35
+ return await this.#inner.getOutputNotes(filter);
36
+ }
37
+
38
+ async listAvailable(opts) {
39
+ this.#client.assertNotTerminated();
40
+ const wasm = await this.#getWasm();
41
+ const accountId = resolveAccountRef(opts.account, wasm);
42
+ const consumable = await this.#inner.getConsumableNotes(accountId);
43
+ return consumable.map((c) => c.inputNoteRecord());
44
+ }
45
+
46
+ async import(noteFile) {
47
+ this.#client.assertNotTerminated();
48
+ return await this.#inner.importNoteFile(noteFile);
49
+ }
50
+
51
+ async export(noteId, opts) {
52
+ this.#client.assertNotTerminated();
53
+ const wasm = await this.#getWasm();
54
+ const format = opts?.format ?? wasm.NoteExportFormat.Full;
55
+ return await this.#inner.exportNoteFile(resolveNoteIdHex(noteId), format);
56
+ }
57
+
58
+ async fetchPrivate(opts) {
59
+ this.#client.assertNotTerminated();
60
+ if (opts?.mode === "all") {
61
+ await this.#inner.fetchAllPrivateNotes();
62
+ } else {
63
+ await this.#inner.fetchPrivateNotes();
64
+ }
65
+ }
66
+
67
+ async sendPrivate(opts) {
68
+ this.#client.assertNotTerminated();
69
+ const wasm = await this.#getWasm();
70
+
71
+ let note;
72
+ const input = opts.note;
73
+ // Check if input is a Note object (has .id() and .assets() but not .toNote())
74
+ if (
75
+ input &&
76
+ typeof input === "object" &&
77
+ typeof input.id === "function" &&
78
+ typeof input.assets === "function" &&
79
+ typeof input.toNote !== "function"
80
+ ) {
81
+ note = input;
82
+ } else {
83
+ const noteHex = resolveNoteIdHex(input);
84
+ const noteRecord = await this.#inner.getInputNote(noteHex);
85
+ if (!noteRecord) {
86
+ throw new Error(`Note not found: ${noteHex}`);
87
+ }
88
+ note = noteRecord.toNote();
89
+ }
90
+
91
+ const address = resolveAddress(opts.to, wasm);
92
+ await this.#inner.sendPrivateNote(note, address);
93
+ }
94
+ }
95
+
96
+ function buildNoteFilter(query, wasm) {
97
+ if (!query) {
98
+ return new wasm.NoteFilter(wasm.NoteFilterTypes.All, undefined);
99
+ }
100
+
101
+ if (query.ids) {
102
+ const noteIds = query.ids.map((id) =>
103
+ wasm.NoteId.fromHex(resolveNoteIdHex(id))
104
+ );
105
+ return new wasm.NoteFilter(wasm.NoteFilterTypes.List, noteIds);
106
+ }
107
+
108
+ if (query.status) {
109
+ const statusMap = {
110
+ consumed: wasm.NoteFilterTypes.Consumed,
111
+ committed: wasm.NoteFilterTypes.Committed,
112
+ expected: wasm.NoteFilterTypes.Expected,
113
+ processing: wasm.NoteFilterTypes.Processing,
114
+ unverified: wasm.NoteFilterTypes.Unverified,
115
+ };
116
+ const filterType = statusMap[query.status];
117
+ if (filterType === undefined) {
118
+ throw new Error(`Unknown note status: ${query.status}`);
119
+ }
120
+ return new wasm.NoteFilter(filterType, undefined);
121
+ }
122
+
123
+ return new wasm.NoteFilter(wasm.NoteFilterTypes.All, undefined);
124
+ }
@@ -0,0 +1,30 @@
1
+ export class SettingsResource {
2
+ #inner;
3
+ #client;
4
+
5
+ constructor(inner, _getWasm, client) {
6
+ this.#inner = inner;
7
+ this.#client = client;
8
+ }
9
+
10
+ async get(key) {
11
+ this.#client.assertNotTerminated();
12
+ const value = await this.#inner.getSetting(key);
13
+ return value === undefined ? null : value;
14
+ }
15
+
16
+ async set(key, value) {
17
+ this.#client.assertNotTerminated();
18
+ await this.#inner.setSetting(key, value);
19
+ }
20
+
21
+ async remove(key) {
22
+ this.#client.assertNotTerminated();
23
+ await this.#inner.removeSetting(key);
24
+ }
25
+
26
+ async listKeys() {
27
+ this.#client.assertNotTerminated();
28
+ return await this.#inner.listSettingKeys();
29
+ }
30
+ }
@@ -0,0 +1,31 @@
1
+ export class TagsResource {
2
+ #inner;
3
+ #client;
4
+
5
+ constructor(inner, getWasm, client) {
6
+ this.#inner = inner;
7
+ this.#client = client;
8
+ }
9
+
10
+ async add(tag) {
11
+ this.#client.assertNotTerminated();
12
+ await this.#inner.addTag(String(tag));
13
+ }
14
+
15
+ async remove(tag) {
16
+ this.#client.assertNotTerminated();
17
+ await this.#inner.removeTag(String(tag));
18
+ }
19
+
20
+ async list() {
21
+ this.#client.assertNotTerminated();
22
+ const tags = await this.#inner.listTags();
23
+ return Array.from(tags).map((t) => {
24
+ const n = Number(t);
25
+ if (Number.isNaN(n)) {
26
+ throw new Error(`Invalid tag value: ${t}`);
27
+ }
28
+ return n;
29
+ });
30
+ }
31
+ }