@sundaeswap/sprinkles 0.1.1

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 (69) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +260 -0
  3. package/dist/cjs/Sprinkle/__tests__/bigint-reviver.test.js +40 -0
  4. package/dist/cjs/Sprinkle/__tests__/bigint-reviver.test.js.map +1 -0
  5. package/dist/cjs/Sprinkle/__tests__/encryption.test.js +267 -0
  6. package/dist/cjs/Sprinkle/__tests__/encryption.test.js.map +1 -0
  7. package/dist/cjs/Sprinkle/__tests__/enhancements.test.js +147 -0
  8. package/dist/cjs/Sprinkle/__tests__/enhancements.test.js.map +1 -0
  9. package/dist/cjs/Sprinkle/__tests__/extract-message.test.js +60 -0
  10. package/dist/cjs/Sprinkle/__tests__/extract-message.test.js.map +1 -0
  11. package/dist/cjs/Sprinkle/__tests__/fill-in-struct.test.js +131 -0
  12. package/dist/cjs/Sprinkle/__tests__/fill-in-struct.test.js.map +1 -0
  13. package/dist/cjs/Sprinkle/__tests__/schemas.test.js +184 -0
  14. package/dist/cjs/Sprinkle/__tests__/schemas.test.js.map +1 -0
  15. package/dist/cjs/Sprinkle/__tests__/settings-persistence.test.js +199 -0
  16. package/dist/cjs/Sprinkle/__tests__/settings-persistence.test.js.map +1 -0
  17. package/dist/cjs/Sprinkle/__tests__/show-menu.test.js +108 -0
  18. package/dist/cjs/Sprinkle/__tests__/show-menu.test.js.map +1 -0
  19. package/dist/cjs/Sprinkle/__tests__/test-helpers.js +16 -0
  20. package/dist/cjs/Sprinkle/__tests__/test-helpers.js.map +1 -0
  21. package/dist/cjs/Sprinkle/__tests__/tx-dialog.test.js +271 -0
  22. package/dist/cjs/Sprinkle/__tests__/tx-dialog.test.js.map +1 -0
  23. package/dist/cjs/Sprinkle/index.js +954 -0
  24. package/dist/cjs/Sprinkle/index.js.map +1 -0
  25. package/dist/cjs/index.js +17 -0
  26. package/dist/cjs/index.js.map +1 -0
  27. package/dist/cjs/package.json +1 -0
  28. package/dist/esm/Sprinkle/__tests__/bigint-reviver.test.js +38 -0
  29. package/dist/esm/Sprinkle/__tests__/bigint-reviver.test.js.map +1 -0
  30. package/dist/esm/Sprinkle/__tests__/encryption.test.js +264 -0
  31. package/dist/esm/Sprinkle/__tests__/encryption.test.js.map +1 -0
  32. package/dist/esm/Sprinkle/__tests__/enhancements.test.js +145 -0
  33. package/dist/esm/Sprinkle/__tests__/enhancements.test.js.map +1 -0
  34. package/dist/esm/Sprinkle/__tests__/extract-message.test.js +58 -0
  35. package/dist/esm/Sprinkle/__tests__/extract-message.test.js.map +1 -0
  36. package/dist/esm/Sprinkle/__tests__/fill-in-struct.test.js +130 -0
  37. package/dist/esm/Sprinkle/__tests__/fill-in-struct.test.js.map +1 -0
  38. package/dist/esm/Sprinkle/__tests__/schemas.test.js +182 -0
  39. package/dist/esm/Sprinkle/__tests__/schemas.test.js.map +1 -0
  40. package/dist/esm/Sprinkle/__tests__/settings-persistence.test.js +196 -0
  41. package/dist/esm/Sprinkle/__tests__/settings-persistence.test.js.map +1 -0
  42. package/dist/esm/Sprinkle/__tests__/show-menu.test.js +106 -0
  43. package/dist/esm/Sprinkle/__tests__/show-menu.test.js.map +1 -0
  44. package/dist/esm/Sprinkle/__tests__/test-helpers.js +10 -0
  45. package/dist/esm/Sprinkle/__tests__/test-helpers.js.map +1 -0
  46. package/dist/esm/Sprinkle/__tests__/tx-dialog.test.js +269 -0
  47. package/dist/esm/Sprinkle/__tests__/tx-dialog.test.js.map +1 -0
  48. package/dist/esm/Sprinkle/index.js +928 -0
  49. package/dist/esm/Sprinkle/index.js.map +1 -0
  50. package/dist/esm/index.js +2 -0
  51. package/dist/esm/index.js.map +1 -0
  52. package/dist/types/Sprinkle/index.d.ts +205 -0
  53. package/dist/types/Sprinkle/index.d.ts.map +1 -0
  54. package/dist/types/index.d.ts +2 -0
  55. package/dist/types/index.d.ts.map +1 -0
  56. package/dist/types/tsconfig.build.tsbuildinfo +1 -0
  57. package/package.json +85 -0
  58. package/src/Sprinkle/__tests__/bigint-reviver.test.ts +49 -0
  59. package/src/Sprinkle/__tests__/encryption.test.ts +266 -0
  60. package/src/Sprinkle/__tests__/enhancements.test.ts +154 -0
  61. package/src/Sprinkle/__tests__/extract-message.test.ts +60 -0
  62. package/src/Sprinkle/__tests__/fill-in-struct.test.ts +159 -0
  63. package/src/Sprinkle/__tests__/schemas.test.ts +215 -0
  64. package/src/Sprinkle/__tests__/settings-persistence.test.ts +181 -0
  65. package/src/Sprinkle/__tests__/show-menu.test.ts +123 -0
  66. package/src/Sprinkle/__tests__/test-helpers.ts +14 -0
  67. package/src/Sprinkle/__tests__/tx-dialog.test.ts +293 -0
  68. package/src/Sprinkle/index.ts +1215 -0
  69. package/src/index.ts +1 -0
@@ -0,0 +1,928 @@
1
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
2
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
3
+ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
4
+ import { Blockfrost } from "@blaze-cardano/query";
5
+ import { Blaze, ColdWallet, Core, HotWallet } from "@blaze-cardano/sdk";
6
+ import { CborSet, VkeyWitness } from "@blaze-cardano/core";
7
+ import { confirm, input, password, search, select } from "@inquirer/prompts";
8
+ import { Kind, Type, OptionalKind } from "@sinclair/typebox";
9
+ import * as fs from "fs";
10
+ import * as path from "path";
11
+ export * from "@sinclair/typebox";
12
+ const isOptional = t => t[OptionalKind] === "Optional";
13
+ const isImport = t => t[Kind] === "Import";
14
+ const isArray = t => t[Kind] === "Array";
15
+ const isBigInt = t => t[Kind] === "BigInt";
16
+ // const isBoolean = (t: TSchema): t is TBoolean => t[Kind] === "Boolean";
17
+ const isLiteral = t => t[Kind] === "Literal";
18
+ // const isNumber = (t: TSchema): t is TNumber => t[Kind] === "Number";
19
+ const isObject = t => t[Kind] === "Object";
20
+ // const isRecord = (t: TSchema): t is TRecord => t[Kind] === "Record";
21
+ const isRef = t => t[Kind] === "Ref";
22
+ const isString = t => t[Kind] === "String";
23
+ const isThis = t => t[Kind] === "This";
24
+ // const isTuple = (t: TSchema): t is TTuple => t[Kind] === "Tuple";
25
+ const isUnion = t => t[Kind] === "Union";
26
+ // const isAny = (t: TSchema): t is TAny => t[Kind] === "Any";
27
+
28
+ const isSensitive = t => isString(t) && t.sensitive === true;
29
+ export const NetworkSchema = Type.Union([Type.Literal("mainnet"), Type.Literal("preview"), Type.Literal("preprod")]);
30
+ export const MultisigScriptModule = Type.Module({
31
+ MultisigScript: Type.Union([Type.Object({
32
+ Signature: Type.Object({
33
+ key_hash: Type.String()
34
+ }, {
35
+ ctor: 0n
36
+ })
37
+ }), Type.Object({
38
+ AllOf: Type.Object({
39
+ scripts: Type.Array(Type.Ref("MultisigScript"))
40
+ }, {
41
+ ctor: 1n
42
+ })
43
+ }), Type.Object({
44
+ AnyOf: Type.Object({
45
+ scripts: Type.Array(Type.Ref("MultisigScript"))
46
+ }, {
47
+ ctor: 2n
48
+ })
49
+ }), Type.Object({
50
+ AtLeast: Type.Object({
51
+ required: Type.BigInt(),
52
+ scripts: Type.Array(Type.Ref("MultisigScript"))
53
+ }, {
54
+ ctor: 3n
55
+ })
56
+ }), Type.Object({
57
+ Before: Type.Object({
58
+ time: Type.BigInt()
59
+ }, {
60
+ ctor: 4n
61
+ })
62
+ }), Type.Object({
63
+ After: Type.Object({
64
+ time: Type.BigInt()
65
+ }, {
66
+ ctor: 5n
67
+ })
68
+ }), Type.Object({
69
+ Script: Type.Object({
70
+ script_hash: Type.String()
71
+ }, {
72
+ ctor: 6n
73
+ })
74
+ })])
75
+ });
76
+ export const MultisigScript = MultisigScriptModule.Import("MultisigScript");
77
+ export const ProviderSettingsSchema = Type.Union([Type.Object({
78
+ type: Type.Literal("blockfrost"),
79
+ projectId: Type.String({
80
+ minLength: 1,
81
+ title: "Blockfrost Project ID"
82
+ })
83
+ }), Type.Object({
84
+ type: Type.Literal("maestro"),
85
+ apiKey: Type.String({
86
+ minLength: 1,
87
+ title: "Maestro API Key"
88
+ })
89
+ })]);
90
+ export const WalletSettingsSchema = Type.Union([Type.Object({
91
+ type: Type.Literal("hot"),
92
+ privateKey: Type.String({
93
+ minLength: 1,
94
+ title: "Hot Wallet Private Key"
95
+ })
96
+ }), Type.Object({
97
+ type: Type.Literal("cold"),
98
+ address: Type.String({
99
+ minLength: 1,
100
+ title: "Cold Wallet Address"
101
+ })
102
+ })]);
103
+ export class Sprinkle {
104
+ constructor(type, storagePath, options) {
105
+ _defineProperty(this, "storagePath", void 0);
106
+ _defineProperty(this, "settings", {});
107
+ _defineProperty(this, "type", void 0);
108
+ _defineProperty(this, "defaults", {});
109
+ _defineProperty(this, "options", void 0);
110
+ _defineProperty(this, "profileId", "");
111
+ _defineProperty(this, "profileMeta", {
112
+ name: "",
113
+ createdAt: "",
114
+ updatedAt: ""
115
+ });
116
+ this.type = type;
117
+ this.storagePath = storagePath;
118
+ this.options = options ?? {};
119
+ }
120
+
121
+ // --- Profile path helpers ---
122
+
123
+ static sanitizeProfileId(name) {
124
+ return name.toLowerCase().replace(/[^a-z0-9\s-]/g, "").replace(/[\s]+/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "") || "profile";
125
+ }
126
+ static profilesDir(storagePath) {
127
+ return path.join(storagePath, "profiles");
128
+ }
129
+ static profilePath(storagePath, id) {
130
+ if (!id || /[\/\\]/.test(id) || id.includes("..")) {
131
+ throw new Error(`Invalid profile ID: "${id}"`);
132
+ }
133
+ return path.join(storagePath, "profiles", `${id}.json`);
134
+ }
135
+ static activeProfilePath(storagePath) {
136
+ return path.join(storagePath, "active-profile");
137
+ }
138
+ static findAvailableId(profilesDir, baseId) {
139
+ let candidate = baseId;
140
+ let counter = 2;
141
+ while (fs.existsSync(path.join(profilesDir, `${candidate}.json`))) {
142
+ candidate = `${baseId}-${counter}`;
143
+ counter++;
144
+ }
145
+ return candidate;
146
+ }
147
+
148
+ // --- Profile I/O ---
149
+
150
+ scanProfiles() {
151
+ const dir = Sprinkle.profilesDir(this.storagePath);
152
+ if (!fs.existsSync(dir)) return [];
153
+ const files = fs.readdirSync(dir).filter(f => f.endsWith(".json"));
154
+ const entries = [];
155
+ for (const file of files) {
156
+ try {
157
+ const content = fs.readFileSync(path.join(dir, file), "utf-8");
158
+ const parsed = JSON.parse(content, Sprinkle.bigIntReviver);
159
+ if (parsed.meta?.name) {
160
+ entries.push({
161
+ id: file.replace(/\.json$/, ""),
162
+ meta: parsed.meta
163
+ });
164
+ }
165
+ } catch {
166
+ // Skip malformed profile files
167
+ }
168
+ }
169
+ return entries;
170
+ }
171
+ async loadProfile(id) {
172
+ const filePath = Sprinkle.profilePath(this.storagePath, id);
173
+ const content = fs.readFileSync(filePath, "utf-8");
174
+ const parsed = JSON.parse(content, Sprinkle.bigIntReviver);
175
+ this.profileId = id;
176
+ this.profileMeta = parsed.meta;
177
+ this.settings = await this.decryptSettings(parsed.settings);
178
+ this.defaults = parsed.defaults ?? {};
179
+ // Update active profile pointer
180
+ fs.writeFileSync(Sprinkle.activeProfilePath(this.storagePath), id, "utf-8");
181
+ }
182
+ saveProfile() {
183
+ if (!this.profileId) {
184
+ throw new Error("Cannot save profile: no profile is loaded. Call loadProfile() or create a profile first.");
185
+ }
186
+ const filePath = Sprinkle.profilePath(this.storagePath, this.profileId);
187
+ const dir = path.dirname(filePath);
188
+ if (!fs.existsSync(dir)) {
189
+ fs.mkdirSync(dir, {
190
+ recursive: true
191
+ });
192
+ }
193
+ this.profileMeta.updatedAt = new Date().toISOString();
194
+ const settingsToSave = this.encryptSettings(this.settings);
195
+ const jsonContent = JSON.stringify({
196
+ meta: this.profileMeta,
197
+ settings: settingsToSave,
198
+ defaults: this.defaults
199
+ }, Sprinkle.bigIntReplacer, 2);
200
+ fs.writeFileSync(filePath, jsonContent, "utf-8");
201
+ }
202
+ async promptProfileMeta(defaultName, defaultDescription) {
203
+ const name = await input({
204
+ message: "Profile name:",
205
+ default: defaultName,
206
+ validate: v => v.trim().length > 0 ? true : "Name cannot be empty"
207
+ });
208
+ const description = await input({
209
+ message: "Profile description (optional):",
210
+ default: defaultDescription ?? ""
211
+ });
212
+ return {
213
+ name,
214
+ description: description || undefined
215
+ };
216
+ }
217
+ async createProfile() {
218
+ const {
219
+ name,
220
+ description
221
+ } = await this.promptProfileMeta();
222
+ const profilesDir = Sprinkle.profilesDir(this.storagePath);
223
+ const id = Sprinkle.findAvailableId(profilesDir, Sprinkle.sanitizeProfileId(name));
224
+ const now = new Date().toISOString();
225
+ this.profileId = id;
226
+ this.profileMeta = {
227
+ name,
228
+ description,
229
+ createdAt: now,
230
+ updatedAt: now
231
+ };
232
+ this.defaults = {};
233
+ this.settings = await this.FillInStruct(this.type);
234
+ this.saveProfile();
235
+ fs.writeFileSync(Sprinkle.activeProfilePath(this.storagePath), id, "utf-8");
236
+ }
237
+ async migrateIfNeeded() {
238
+ const profilesDir = Sprinkle.profilesDir(this.storagePath);
239
+ const legacyPath = Sprinkle.SettingsPath(this.storagePath);
240
+ if (!fs.existsSync(profilesDir)) {
241
+ if (fs.existsSync(legacyPath)) {
242
+ // Migrate legacy settings.json -> profiles/default.json
243
+ const content = fs.readFileSync(legacyPath, "utf-8");
244
+ const parsed = JSON.parse(content, Sprinkle.bigIntReviver);
245
+ const now = new Date().toISOString();
246
+ const profileData = {
247
+ meta: {
248
+ name: "Default",
249
+ createdAt: now,
250
+ updatedAt: now
251
+ },
252
+ settings: parsed.settings,
253
+ defaults: parsed.defaults ?? {}
254
+ };
255
+ fs.mkdirSync(profilesDir, {
256
+ recursive: true
257
+ });
258
+ fs.writeFileSync(Sprinkle.profilePath(this.storagePath, "default"), JSON.stringify(profileData, Sprinkle.bigIntReplacer, 2), "utf-8");
259
+ fs.writeFileSync(Sprinkle.activeProfilePath(this.storagePath), "default", "utf-8");
260
+ // Backup legacy file
261
+ fs.renameSync(legacyPath, `${legacyPath}.bak`);
262
+ } else {
263
+ fs.mkdirSync(profilesDir, {
264
+ recursive: true
265
+ });
266
+ }
267
+ }
268
+ }
269
+ async selectOrCreateProfile() {
270
+ const profiles = this.scanProfiles();
271
+ if (profiles.length === 0) {
272
+ await this.createProfile();
273
+ return;
274
+ }
275
+ if (profiles.length === 1) {
276
+ await this.loadProfile(profiles[0].id);
277
+ return;
278
+ }
279
+
280
+ // Multiple profiles: check active-profile pointer
281
+ const pointerPath = Sprinkle.activeProfilePath(this.storagePath);
282
+ if (fs.existsSync(pointerPath)) {
283
+ const activeId = fs.readFileSync(pointerPath, "utf-8").trim();
284
+ if (profiles.some(p => p.id === activeId)) {
285
+ await this.loadProfile(activeId);
286
+ return;
287
+ }
288
+ }
289
+
290
+ // No valid pointer - show selector
291
+ await this.selectProfile(profiles);
292
+ }
293
+ async selectProfile(profiles) {
294
+ const available = profiles ?? this.scanProfiles();
295
+ const selection = await select({
296
+ message: "Select a profile:",
297
+ choices: available.map(p => ({
298
+ name: p.meta.description ? `${p.meta.name} - ${p.meta.description}` : p.meta.name,
299
+ value: p.id
300
+ }))
301
+ });
302
+ await this.loadProfile(selection);
303
+ }
304
+
305
+ // --- Profile management (CRUD) ---
306
+
307
+ async duplicateProfile() {
308
+ const {
309
+ name,
310
+ description
311
+ } = await this.promptProfileMeta(`${this.profileMeta.name} (copy)`, this.profileMeta.description);
312
+ const profilesDir = Sprinkle.profilesDir(this.storagePath);
313
+ const id = Sprinkle.findAvailableId(profilesDir, Sprinkle.sanitizeProfileId(name));
314
+ const now = new Date().toISOString();
315
+ // Write duplicate with current settings
316
+ const settingsToSave = this.encryptSettings(this.settings);
317
+ const jsonContent = JSON.stringify({
318
+ meta: {
319
+ name,
320
+ description,
321
+ createdAt: now,
322
+ updatedAt: now
323
+ },
324
+ settings: settingsToSave,
325
+ defaults: this.defaults
326
+ }, Sprinkle.bigIntReplacer, 2);
327
+ fs.writeFileSync(path.join(profilesDir, `${id}.json`), jsonContent, "utf-8");
328
+ console.log(`Profile "${name}" created as a copy.`);
329
+ }
330
+ async renameProfile() {
331
+ const {
332
+ name,
333
+ description
334
+ } = await this.promptProfileMeta(this.profileMeta.name, this.profileMeta.description);
335
+ const newId = Sprinkle.sanitizeProfileId(name);
336
+ const oldId = this.profileId;
337
+ this.profileMeta.name = name;
338
+ this.profileMeta.description = description;
339
+ if (newId !== oldId) {
340
+ const newPath = Sprinkle.profilePath(this.storagePath, newId);
341
+ if (fs.existsSync(newPath)) {
342
+ console.log(`A profile with ID "${newId}" already exists. Choose a different name.`);
343
+ return;
344
+ }
345
+ // Save to new path, remove old file
346
+ this.profileId = newId;
347
+ this.saveProfile();
348
+ fs.unlinkSync(Sprinkle.profilePath(this.storagePath, oldId));
349
+ fs.writeFileSync(Sprinkle.activeProfilePath(this.storagePath), newId, "utf-8");
350
+ } else {
351
+ this.saveProfile();
352
+ }
353
+ console.log(`Profile renamed to "${name}".`);
354
+ }
355
+ async deleteProfile() {
356
+ const profiles = this.scanProfiles();
357
+ const others = profiles.filter(p => p.id !== this.profileId);
358
+ if (others.length === 0) {
359
+ console.log("Cannot delete the only profile.");
360
+ return;
361
+ }
362
+ const toDelete = await select({
363
+ message: "Select a profile to delete:",
364
+ choices: [...others.map(p => ({
365
+ name: p.meta.description ? `${p.meta.name} - ${p.meta.description}` : p.meta.name,
366
+ value: p.id
367
+ })), {
368
+ name: "Cancel",
369
+ value: "__cancel__"
370
+ }]
371
+ });
372
+ if (toDelete === "__cancel__") return;
373
+ const profileToDelete = others.find(p => p.id === toDelete);
374
+ const confirmed = await confirm({
375
+ message: `Delete profile "${profileToDelete?.meta.name ?? toDelete}"? This cannot be undone.`,
376
+ default: false
377
+ });
378
+ if (confirmed) {
379
+ fs.unlinkSync(Sprinkle.profilePath(this.storagePath, toDelete));
380
+ console.log(`Profile "${profileToDelete?.meta.name ?? toDelete}" deleted.`);
381
+ }
382
+ }
383
+
384
+ // --- Menu ---
385
+
386
+ async showMenu(menu) {
387
+ return this._showMenu(menu, true);
388
+ }
389
+ async _showMenu(menu, main) {
390
+ if (menu.beforeShow) {
391
+ await menu.beforeShow(this);
392
+ }
393
+ const choices = menu.items.map((item, index) => {
394
+ if ("action" in item) {
395
+ return {
396
+ name: item.title,
397
+ value: index
398
+ };
399
+ } else {
400
+ return {
401
+ name: `${item.title} Submenu`,
402
+ value: index
403
+ };
404
+ }
405
+ });
406
+ if (!main) {
407
+ choices.push({
408
+ name: "Back",
409
+ value: -1
410
+ });
411
+ } else {
412
+ choices.push({
413
+ name: "Settings & Profiles",
414
+ value: -5
415
+ });
416
+ choices.push({
417
+ name: "Exit",
418
+ value: -1
419
+ });
420
+ }
421
+ const selection = await select({
422
+ message: "Select an option:",
423
+ choices: choices
424
+ });
425
+ if (selection === -5) {
426
+ const settingsMenu = {
427
+ title: "Settings & Profiles",
428
+ items: [{
429
+ title: "Edit settings",
430
+ action: async () => {
431
+ this.settings = await this.EditStruct(this.type, this.settings);
432
+ this.saveSettings();
433
+ }
434
+ }, {
435
+ title: "Switch profile",
436
+ action: async () => {
437
+ this.saveSettings();
438
+ const profiles = this.scanProfiles();
439
+ if (profiles.length <= 1) {
440
+ console.log("No other profiles to switch to. Create a new one first.");
441
+ } else {
442
+ await this.selectProfile(profiles);
443
+ }
444
+ }
445
+ }, {
446
+ title: "Create new profile",
447
+ action: async () => {
448
+ await this.createProfile();
449
+ }
450
+ }, {
451
+ title: "Duplicate current profile",
452
+ action: async () => {
453
+ await this.duplicateProfile();
454
+ }
455
+ }, {
456
+ title: "Rename current profile",
457
+ action: async () => {
458
+ await this.renameProfile();
459
+ }
460
+ }, {
461
+ title: "Delete a profile",
462
+ action: async () => {
463
+ await this.deleteProfile();
464
+ }
465
+ }]
466
+ };
467
+ await this._showMenu(settingsMenu, false);
468
+ await this._showMenu(menu, main);
469
+ return;
470
+ }
471
+ if (selection === -1) {
472
+ return;
473
+ }
474
+ const selectedItem = menu.items[selection];
475
+ if ("action" in selectedItem) {
476
+ const result = await selectedItem.action(this);
477
+ if (result instanceof Sprinkle) {
478
+ this.settings = result.settings;
479
+ this.saveSettings();
480
+ }
481
+ await this._showMenu(menu, main);
482
+ } else {
483
+ await this._showMenu(selectedItem, false);
484
+ await this._showMenu(menu, main);
485
+ }
486
+ return;
487
+ }
488
+ static async New(type, storagePath, options) {
489
+ const sprinkle = new Sprinkle(type, storagePath, options);
490
+ await sprinkle.migrateIfNeeded();
491
+ await sprinkle.selectOrCreateProfile();
492
+ sprinkle.saveSettings();
493
+ return sprinkle;
494
+ }
495
+ static async GetProvider(network, settings) {
496
+ switch (settings.type) {
497
+ case "blockfrost":
498
+ return new Blockfrost({
499
+ network: `cardano-${network}`,
500
+ projectId: settings.projectId
501
+ });
502
+ case "maestro":
503
+ // Dynamic import - Maestro may or may not be exported depending on @blaze-cardano/query version
504
+ const queryModule = await import("@blaze-cardano/query");
505
+ if (!queryModule.Maestro) {
506
+ throw new Error("Maestro is not available in the installed version of @blaze-cardano/query. Please install a version that includes Maestro support.");
507
+ }
508
+ return new queryModule.Maestro({
509
+ network: network,
510
+ apiKey: settings.apiKey
511
+ });
512
+ default:
513
+ throw new Error("Invalid provider type");
514
+ }
515
+ }
516
+ static async GetWallet(settings, provider) {
517
+ switch (settings.type) {
518
+ case "hot":
519
+ return HotWallet.fromMasterkey(Core.Bip32PrivateKeyHex(settings.privateKey), provider, provider.network);
520
+ case "cold":
521
+ return new ColdWallet(Core.Address.fromBech32(settings.address), provider.network, provider);
522
+ default:
523
+ throw new Error("Invalid wallet type");
524
+ }
525
+ }
526
+ static async GetBlaze(network, providerSettings, walletSettings) {
527
+ const provider = await Sprinkle.GetProvider(network, providerSettings);
528
+ const wallet = await Sprinkle.GetWallet(walletSettings, provider);
529
+ return Blaze.from(provider, wallet);
530
+ }
531
+ static async SearchSelect(opts) {
532
+ return search(opts);
533
+ }
534
+ static SettingsPath(storagePath) {
535
+ return `${storagePath}${path.sep}settings.json`;
536
+ }
537
+ async LoadSettings(type, storagePath) {
538
+ try {
539
+ // Check if the settings file exists
540
+ if (fs.existsSync(storagePath)) {
541
+ // Read and parse the JSON file
542
+ const fileContent = fs.readFileSync(storagePath, "utf-8");
543
+ const parsed = JSON.parse(fileContent, Sprinkle.bigIntReviver);
544
+ // Convert string representations back to BigInt where needed
545
+ this.settings = await this.decryptSettings(parsed.settings);
546
+ this.defaults = parsed.defaults;
547
+ } else {
548
+ this.defaults = {};
549
+ this.settings = await this.FillInStruct(type);
550
+ }
551
+ } catch (error) {
552
+ throw new Error(`Error loading settings from ${storagePath}: ${error}`);
553
+ }
554
+ }
555
+ static bigIntReviver(key, value) {
556
+ if (typeof value === "string" && /^\d+n$/.test(value)) {
557
+ return BigInt(value.slice(0, -1));
558
+ }
559
+ return value;
560
+ }
561
+ static bigIntReplacer(_key, value) {
562
+ return typeof value === "bigint" ? `${value.toString()}n` : value;
563
+ }
564
+ static collectSensitivePaths(type, prefix = "") {
565
+ const paths = [];
566
+ if (isObject(type)) {
567
+ const fields = type["properties"];
568
+ for (const [field, fieldType] of Object.entries(fields)) {
569
+ const fieldPath = prefix ? `${prefix}.${field}` : field;
570
+ if (isSensitive(fieldType)) {
571
+ paths.push(fieldPath);
572
+ }
573
+ paths.push(...Sprinkle.collectSensitivePaths(fieldType, fieldPath));
574
+ }
575
+ }
576
+ if (isUnion(type)) {
577
+ for (const variant of type.anyOf) {
578
+ paths.push(...Sprinkle.collectSensitivePaths(variant, prefix));
579
+ }
580
+ }
581
+ return paths;
582
+ }
583
+ static getNestedValue(obj, path) {
584
+ return path.split(".").reduce((o, k) => o?.[k], obj);
585
+ }
586
+ static setNestedValue(obj, path, value) {
587
+ const keys = path.split(".");
588
+ const last = keys.pop();
589
+ const parent = keys.reduce((o, k) => o?.[k], obj);
590
+ if (parent && typeof parent === "object") {
591
+ parent[last] = value;
592
+ }
593
+ }
594
+ encryptSettings(settings) {
595
+ if (!this.options.encryption) return settings;
596
+ const clone = JSON.parse(JSON.stringify(settings, Sprinkle.bigIntReplacer), Sprinkle.bigIntReviver);
597
+ const sensitivePaths = Sprinkle.collectSensitivePaths(this.type);
598
+ for (const p of sensitivePaths) {
599
+ const value = Sprinkle.getNestedValue(clone, p);
600
+ if (typeof value === "string" && value.length > 0) {
601
+ Sprinkle.setNestedValue(clone, p, this.options.encryption.encrypt(value));
602
+ }
603
+ }
604
+ return clone;
605
+ }
606
+ async decryptSettings(settings) {
607
+ if (!this.options.encryption) return settings;
608
+ const clone = JSON.parse(JSON.stringify(settings, Sprinkle.bigIntReplacer), Sprinkle.bigIntReviver);
609
+ const sensitivePaths = Sprinkle.collectSensitivePaths(this.type);
610
+ for (const p of sensitivePaths) {
611
+ const value = Sprinkle.getNestedValue(clone, p);
612
+ if (typeof value === "string" && value.length > 0) {
613
+ Sprinkle.setNestedValue(clone, p, await this.options.encryption.decrypt(value));
614
+ }
615
+ }
616
+ return clone;
617
+ }
618
+ saveSettings() {
619
+ this.saveProfile();
620
+ }
621
+ async TxDialog(blaze, tx, opts) {
622
+ const txCbor = tx.toCbor();
623
+ let expanded = false;
624
+ const showDialog = async () => {
625
+ if (expanded) {
626
+ console.log("Transaction CBOR:", txCbor);
627
+ } else {
628
+ console.log("Transaction CBOR:", `${String(txCbor).slice(0, 50)}...`);
629
+ }
630
+ const menuItems = [];
631
+ if (!expanded) {
632
+ menuItems.push({
633
+ title: "Expand CBOR",
634
+ action: async () => {
635
+ expanded = true;
636
+ await showDialog();
637
+ }
638
+ });
639
+ }
640
+ menuItems.push({
641
+ title: "Copy CBOR to clipboard",
642
+ action: async () => {
643
+ try {
644
+ const {
645
+ default: clipboard
646
+ } = await import("clipboardy");
647
+ clipboard.writeSync(String(txCbor));
648
+ console.log("Transaction CBOR copied to clipboard.");
649
+ } catch (e) {
650
+ console.log("Failed to copy to clipboard, expanding instead.");
651
+ expanded = true;
652
+ await showDialog();
653
+ }
654
+ }
655
+ });
656
+ if (blaze.wallet instanceof HotWallet) {
657
+ menuItems.push({
658
+ title: "Sign and submit transaction",
659
+ action: async () => {
660
+ if (opts?.beforeSign) {
661
+ await opts.beforeSign();
662
+ }
663
+
664
+ // Detect if stake key signature is required
665
+ let needsStakeKey = false;
666
+ try {
667
+ const wallet = blaze.wallet;
668
+ const addresses = await wallet.getUsedAddresses();
669
+ const userAddress = addresses[0];
670
+ if (userAddress) {
671
+ const stakeCredential = userAddress.asBase()?.getStakeCredential();
672
+ const stakeKeyHash = stakeCredential?.hash?.toString();
673
+ if (stakeKeyHash) {
674
+ const requiredSigners = tx.body().requiredSigners();
675
+ if (requiredSigners) {
676
+ const signerArray = Array.from(requiredSigners.values());
677
+ needsStakeKey = signerArray.some(signer => signer.toString() === stakeKeyHash);
678
+ }
679
+ const certs = tx.body().certs();
680
+ const hasCertificates = certs && certs.size() > 0;
681
+ const withdrawals = tx.body().withdrawals();
682
+ const hasWithdrawals = withdrawals && withdrawals.size > 0;
683
+ if (hasCertificates || hasWithdrawals) {
684
+ needsStakeKey = true;
685
+ }
686
+ }
687
+ }
688
+ if (needsStakeKey) {
689
+ console.log("Transaction requires stake key signature.");
690
+ } else {
691
+ console.log("Transaction requires payment key signature only.");
692
+ }
693
+ } catch (error) {
694
+ console.warn("Could not determine stake key requirement, signing with payment key only.");
695
+ console.warn(`Error: ${error.message}`);
696
+ }
697
+ let signedTx;
698
+ if (needsStakeKey) {
699
+ const signed = await blaze.wallet.signTransaction(tx, true, true);
700
+ const ws = tx.witnessSet();
701
+ const vkeys = ws.vkeys()?.toCore() ?? [];
702
+ const signedKeys = signed.vkeys();
703
+ if (!signedKeys) {
704
+ throw new Error("signTransaction: no signed keys in wallet witness response");
705
+ }
706
+ if (signedKeys.toCore().some(([vkey]) => vkeys.some(([key2]) => vkey === key2))) {
707
+ throw new Error("signTransaction: some keys were already signed");
708
+ }
709
+ ws.setVkeys(CborSet.fromCore([...signedKeys.toCore(), ...vkeys], VkeyWitness.fromCore));
710
+ tx.setWitnessSet(ws);
711
+ signedTx = tx;
712
+ } else {
713
+ signedTx = await blaze.signTransaction(tx);
714
+ }
715
+ const txId = await blaze.submitTransaction(signedTx);
716
+ console.log(`Transaction submitted: ${txId}`);
717
+ }
718
+ });
719
+ }
720
+ const txMenu = {
721
+ title: "Transaction Menu",
722
+ items: menuItems
723
+ };
724
+ await this._showMenu(txMenu, false);
725
+ };
726
+ await showDialog();
727
+ }
728
+ async EditStruct(type, current) {
729
+ return this._editStruct(type, ["root"], current);
730
+ }
731
+ async _editStruct(type, path, current) {
732
+ if (isObject(type)) {
733
+ const obj = {};
734
+ const fields = type["properties"];
735
+ const menuItems = [];
736
+ const currentRecord = current;
737
+ for (const [field, fieldType] of Object.entries(fields)) {
738
+ if (current && field in currentRecord) {
739
+ obj[field] = currentRecord[field];
740
+ }
741
+ const menuTitle = Sprinkle.ExtractMessage(fieldType, `Edit ${field} at ${path.join(".")}`);
742
+ if (isOptional(fieldType) && current && currentRecord[field] !== undefined) {
743
+ menuItems.push({
744
+ title: `Clear ${field}`,
745
+ action: async sprinkle => {
746
+ obj[field] = undefined;
747
+ return sprinkle;
748
+ }
749
+ });
750
+ }
751
+ menuItems.push({
752
+ title: menuTitle,
753
+ action: async sprinkle => {
754
+ const fieldValue = await sprinkle._editStruct(fieldType, path.concat([field]), current && field in currentRecord ? currentRecord[field] : undefined);
755
+ obj[field] = fieldValue;
756
+ return sprinkle;
757
+ }
758
+ });
759
+ }
760
+ const editMenu = {
761
+ title: "Test",
762
+ items: menuItems
763
+ };
764
+ await this._showMenu(editMenu, false);
765
+ return obj;
766
+ }
767
+ return this._fillInStruct(type, path, {}, current);
768
+ }
769
+ async FillInStruct(type, def) {
770
+ return this._fillInStruct(type, ["root"], {}, def);
771
+ }
772
+ async _fillInStruct(type, path, defs, def) {
773
+ if ("$ref" in type) {
774
+ defs = {
775
+ ...defs,
776
+ ...type["$defs"]
777
+ };
778
+ const resolvedType = defs[type["$ref"]];
779
+ if (!resolvedType) {
780
+ throw new Error(`Could not resolve type ${type["$ref"]} at ${path.join(".")}`);
781
+ }
782
+ return this._fillInStruct(resolvedType, path, defs, def);
783
+ }
784
+ if (isOptional(type)) {
785
+ const shouldSet = await select({
786
+ message: Sprinkle.ExtractMessage(type, `Set value for ${path.join(".")}?`),
787
+ choices: [{
788
+ name: "Yes",
789
+ value: true
790
+ }, {
791
+ name: "Skip",
792
+ value: false
793
+ }],
794
+ default: def !== undefined
795
+ });
796
+ if (!shouldSet) {
797
+ return undefined;
798
+ }
799
+ // Unwrap the optional and fill the inner type
800
+ const innerType = {
801
+ ...type
802
+ };
803
+ delete innerType[OptionalKind];
804
+ return this._fillInStruct(innerType, path, defs, def);
805
+ }
806
+ if (isUnion(type)) {
807
+ const choices = [];
808
+ const resolved = this.resolveType(type, path, defs);
809
+ for (const variant of resolved.anyOf) {
810
+ choices.push({
811
+ name: Sprinkle.ExtractMessage(variant, `${variant}`),
812
+ value: variant
813
+ });
814
+ }
815
+ const selection = await select({
816
+ message: Sprinkle.ExtractMessage(resolved, `Enter a choice for ${path.join(".")}`),
817
+ choices: choices,
818
+ default: def ? `${def}` : undefined
819
+ });
820
+ return this._fillInStruct(selection, path, defs);
821
+ }
822
+ if (isString(type)) {
823
+ const defaultString = def ? def : this.defaults["string"];
824
+ const message = Sprinkle.ExtractMessage(type, `Enter a string for ${path.join(".")}`);
825
+ let answer;
826
+ if (isSensitive(type)) {
827
+ answer = await password({
828
+ message
829
+ });
830
+ } else {
831
+ answer = await input({
832
+ message,
833
+ default: defaultString
834
+ });
835
+ this.defaults["string"] = answer;
836
+ }
837
+ return answer;
838
+ }
839
+ if (isBigInt(type)) {
840
+ const answer = await input({
841
+ message: Sprinkle.ExtractMessage(type, `Enter a bigint for ${path.join(".")}`),
842
+ default: def ? def.toString() : undefined,
843
+ validate: s => {
844
+ try {
845
+ BigInt(s);
846
+ return true;
847
+ } catch {
848
+ return "Please enter a valid bigint.";
849
+ }
850
+ }
851
+ });
852
+ return BigInt(answer);
853
+ }
854
+ if (isLiteral(type)) {
855
+ return type.const;
856
+ }
857
+ if (isObject(type)) {
858
+ const obj = {};
859
+ const fields = type["properties"];
860
+ for (const [field, fieldType] of Object.entries(fields)) {
861
+ const fieldValue = await this._fillInStruct(fieldType, path.concat([field]), defs, def ? def[field] : undefined);
862
+ obj[field] = fieldValue;
863
+ }
864
+ return obj;
865
+ }
866
+
867
+ //TODO: support starting with default values for arrays and allow removal of items
868
+ if (isArray(type)) {
869
+ const arr = [];
870
+ const itemType = type.items;
871
+ let addMore = true;
872
+ while (addMore) {
873
+ const itemValue = await this._fillInStruct(itemType, path.concat([`[${arr.length}]`]), defs);
874
+ arr.push(itemValue);
875
+ const continueAnswer = await select({
876
+ message: `Add another item to ${path.join(".")}?`,
877
+ choices: [{
878
+ name: "Yes",
879
+ value: true
880
+ }, {
881
+ name: "No",
882
+ value: false
883
+ }]
884
+ });
885
+ addMore = continueAnswer;
886
+ }
887
+ return arr;
888
+ }
889
+ throw new Error(`Unable to fill in struct for type at path ${path.join(".")}`);
890
+ }
891
+ resolveType(type, path, defs) {
892
+ if (isRef(type) || isThis(type) || isImport(type)) {
893
+ defs = {
894
+ ...defs,
895
+ ...type.$defs
896
+ };
897
+ const realType = defs[type.$ref];
898
+ if (!realType) {
899
+ throw new Error(`Invalid type at ${path.join(".")}: Unrecognized reference ${type.$ref}`);
900
+ }
901
+ return this.resolveType(realType, path, defs);
902
+ }
903
+ return type;
904
+ }
905
+ static ExtractMessage(type, def) {
906
+ if ("title" in type) {
907
+ return type.title;
908
+ }
909
+ if ("description" in type) {
910
+ return type.description;
911
+ }
912
+ if (isLiteral(type)) {
913
+ return `${type.const}`;
914
+ }
915
+ if (isObject(type)) {
916
+ const fields = type["properties"];
917
+ if ("type" in fields) {
918
+ return Sprinkle.ExtractMessage(fields["type"], def);
919
+ }
920
+ const fieldNames = Object.keys(fields);
921
+ if (fieldNames.length === 1) {
922
+ return fieldNames[0];
923
+ }
924
+ }
925
+ return def;
926
+ }
927
+ }
928
+ //# sourceMappingURL=index.js.map