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