@dvmkit/sdk 0.1.0-rc.5 → 0.1.0-rc.7
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/dist/{chunk-F2L6KIMD.js → chunk-5URG56JJ.js} +5 -143
- package/dist/{chunk-ZMZTZFRC.js → chunk-7AKBC4PW.js} +754 -150
- package/dist/chunk-ANFX5HEG.js +186 -0
- package/dist/{chunk-ATQIII6K.js → chunk-CMDI3ENK.js} +342 -176
- package/dist/{chunk-AB3L6B52.js → chunk-FJDCFHW5.js} +26 -12
- package/dist/{chunk-AAJNGQMC.js → chunk-JGGI65I3.js} +1 -1
- package/dist/{chunk-4KB3LTOS.js → chunk-KSQEFVFJ.js} +532 -685
- package/dist/{chunk-XYTSDAPH.js → chunk-LDTWX7JW.js} +1 -1
- package/dist/chunk-MKI6OVW4.js +194 -0
- package/dist/{chunk-AZBXSXQT.js → chunk-P4RUVDU7.js} +1 -0
- package/dist/chunk-TSWKITGR.js +772 -0
- package/dist/{credit-menu-ClA1JyYW.d.ts → credit-menu-CYkETVM4.d.ts} +22 -3
- package/dist/{fx-ptKVFOwq.d.ts → fx-Pf4Ey4f_.d.ts} +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +2 -2
- package/dist/internal/index.d.ts +4835 -24
- package/dist/internal/index.js +3731 -167
- package/dist/{job-store-DxFqDPYq.d.ts → job-store-BtCaLnvJ.d.ts} +23 -1
- package/dist/server/index.d.ts +5 -5
- package/dist/server/index.js +8 -21
- package/dist/tempo-lifecycle-SQL3KLEZ.js +13 -0
- package/dist/tempo-wallet-QOLEIPCV.js +31 -0
- package/dist/testing/index.d.ts +1 -1
- package/dist/{usd-Cha_j80I.d.ts → usd-D7fW2S7I.d.ts} +1 -1
- package/dist/{x402-3VQ64MCS.js → x402-FTG2GRAQ.js} +6 -4
- package/package.json +2 -2
|
@@ -0,0 +1,772 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CONFIG_AUDIT_FILE,
|
|
3
|
+
CONFIG_BACKUP_FILE,
|
|
4
|
+
CONFIG_FILE,
|
|
5
|
+
CONFIG_PINNED_FILE,
|
|
6
|
+
DvmError,
|
|
7
|
+
ensureConfigDir
|
|
8
|
+
} from "./chunk-MKI6OVW4.js";
|
|
9
|
+
|
|
10
|
+
// src/lib/evm-private-key.ts
|
|
11
|
+
import { secp256k1 } from "@noble/curves/secp256k1.js";
|
|
12
|
+
import { hexToBytes } from "@noble/hashes/utils.js";
|
|
13
|
+
var EVM_PRIVATE_KEY_RE = /^0x[0-9a-fA-F]{64}$/;
|
|
14
|
+
function isValidEvmPrivateKey(value) {
|
|
15
|
+
return EVM_PRIVATE_KEY_RE.test(value) && secp256k1.utils.isValidSecretKey(hexToBytes(value.slice(2)));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// src/lib/config-mutation.ts
|
|
19
|
+
import {
|
|
20
|
+
appendFileSync,
|
|
21
|
+
chmodSync,
|
|
22
|
+
existsSync,
|
|
23
|
+
readFileSync,
|
|
24
|
+
renameSync,
|
|
25
|
+
statSync,
|
|
26
|
+
unlinkSync,
|
|
27
|
+
writeFileSync
|
|
28
|
+
} from "fs";
|
|
29
|
+
var PROTECTED_CONFIG_FIELDS = ["nwcUri", "defaultIdentity", "tempo", "x402"];
|
|
30
|
+
var ProtectedFieldAuthorization = class {
|
|
31
|
+
/** Targets this authorization covers. A field also covers its own leaves. */
|
|
32
|
+
targets;
|
|
33
|
+
/** Prefer {@link authorizeProtectedRemoval}; this exists for it to call. */
|
|
34
|
+
constructor(targets) {
|
|
35
|
+
this.targets = [...targets];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* True when `target` is one this authorization names. Naming a field covers
|
|
39
|
+
* the leaves under it; naming a leaf covers only that leaf, so a command
|
|
40
|
+
* entitled to clear `tempo.apiKey` still cannot take the whole `tempo` block.
|
|
41
|
+
*/
|
|
42
|
+
covers(target) {
|
|
43
|
+
if (this.targets.includes(target)) return true;
|
|
44
|
+
const field = target.split(".")[0];
|
|
45
|
+
return target !== field && this.targets.includes(field);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
function authorizeProtectedRemoval(...targets) {
|
|
49
|
+
return new ProtectedFieldAuthorization(targets);
|
|
50
|
+
}
|
|
51
|
+
function planConfigReset(config) {
|
|
52
|
+
const present = Object.keys(config);
|
|
53
|
+
const resettable = resettableConfigFields();
|
|
54
|
+
const clearable = new Set(resettable);
|
|
55
|
+
return {
|
|
56
|
+
clear: resettable.filter((field) => present.includes(field)),
|
|
57
|
+
preserved: present.filter((field) => !clearable.has(field))
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function applyConfigPatch(base, patch) {
|
|
61
|
+
if (!isPlainObject(patch))
|
|
62
|
+
throw patchShapeError("A change must be an object with `set` and/or `unset`.");
|
|
63
|
+
if (patch.set !== void 0 && !isPlainObject(patch.set)) {
|
|
64
|
+
throw patchShapeError("A change's `set` must be an object of fields to write.");
|
|
65
|
+
}
|
|
66
|
+
if (patch.unset !== void 0 && !Array.isArray(patch.unset)) {
|
|
67
|
+
throw patchShapeError("A change's `unset` must be an array of field names.");
|
|
68
|
+
}
|
|
69
|
+
const set = patch.set ?? {};
|
|
70
|
+
const unset = patch.unset ?? [];
|
|
71
|
+
for (const [field, value] of Object.entries(set)) {
|
|
72
|
+
if (value !== void 0) continue;
|
|
73
|
+
throw new DvmError(
|
|
74
|
+
"config_patch_invalid",
|
|
75
|
+
`Config field '${field}' was declared with no value.`,
|
|
76
|
+
"Give the field a value, or name it in the change's `unset` list to remove it. An undefined value is how a field gets dropped without anyone deciding to drop it."
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
for (const field of unset) {
|
|
80
|
+
if (typeof field !== "string" || field.length === 0) {
|
|
81
|
+
throw patchShapeError("Every entry in a change's `unset` must be a field name.");
|
|
82
|
+
}
|
|
83
|
+
if (!Object.hasOwn(set, field)) continue;
|
|
84
|
+
throw new DvmError(
|
|
85
|
+
"config_patch_invalid",
|
|
86
|
+
`Config field '${field}' is both written and removed by the same change.`,
|
|
87
|
+
"Declare each field once: in `set` with its new value, or in `unset` to remove it."
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
const merged = { ...base, ...set };
|
|
91
|
+
const removing = new Set(unset);
|
|
92
|
+
return Object.fromEntries(
|
|
93
|
+
Object.entries(merged).filter(([field]) => !removing.has(field))
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
function deepFreezeConfig(config) {
|
|
97
|
+
freezeDeep(config);
|
|
98
|
+
return config;
|
|
99
|
+
}
|
|
100
|
+
function protectedRemovals(before, after) {
|
|
101
|
+
const previous = before;
|
|
102
|
+
const next = after;
|
|
103
|
+
const removed = [];
|
|
104
|
+
for (const field of PROTECTED_CONFIG_FIELDS) {
|
|
105
|
+
const held = previous[field];
|
|
106
|
+
if (!isPresentValue(held)) continue;
|
|
107
|
+
const kept = next[field];
|
|
108
|
+
if (!isPresentValue(kept)) {
|
|
109
|
+
removed.push(field);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const spec = PROTECTED_FIELDS[field];
|
|
113
|
+
if (spec.credentialLeaves.length === 0) continue;
|
|
114
|
+
if (!isPlainObject(held)) continue;
|
|
115
|
+
if (!isPlainObject(kept)) {
|
|
116
|
+
removed.push(field);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
for (const leaf of spec.credentialLeaves) {
|
|
120
|
+
if (!isPresentValue(held[leaf]) || isPresentValue(kept[leaf])) continue;
|
|
121
|
+
removed.push(`${field}.${leaf}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return removed;
|
|
125
|
+
}
|
|
126
|
+
function supersededProtectedSecrets(before, after) {
|
|
127
|
+
const previous = before;
|
|
128
|
+
const next = after;
|
|
129
|
+
const superseded = [];
|
|
130
|
+
for (const field of PROTECTED_CONFIG_FIELDS) {
|
|
131
|
+
const spec = PROTECTED_FIELDS[field];
|
|
132
|
+
if (!spec.secret) continue;
|
|
133
|
+
const held = previous[field];
|
|
134
|
+
const kept = next[field];
|
|
135
|
+
if (spec.credentialLeaves.length === 0) {
|
|
136
|
+
if (isPresentValue(held) && isPresentValue(kept) && held !== kept) superseded.push(field);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (!isPlainObject(held) || !isPlainObject(kept)) continue;
|
|
140
|
+
for (const leaf of spec.credentialLeaves) {
|
|
141
|
+
const was = held[leaf];
|
|
142
|
+
const now = kept[leaf];
|
|
143
|
+
if (!isPresentValue(was) || !isPresentValue(now) || was === now) continue;
|
|
144
|
+
superseded.push(`${field}.${leaf}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return superseded;
|
|
148
|
+
}
|
|
149
|
+
function changedConfigPaths(before, after) {
|
|
150
|
+
const found = /* @__PURE__ */ new Set();
|
|
151
|
+
collectChangedPaths(
|
|
152
|
+
before,
|
|
153
|
+
after,
|
|
154
|
+
CONFIG_PATH_SHAPE,
|
|
155
|
+
"",
|
|
156
|
+
found
|
|
157
|
+
);
|
|
158
|
+
return [...found].sort();
|
|
159
|
+
}
|
|
160
|
+
function assertValidConfig(config, fields) {
|
|
161
|
+
if (!isPlainObject(config)) {
|
|
162
|
+
throw new DvmError(
|
|
163
|
+
"config_invalid",
|
|
164
|
+
"The caller configuration is not a JSON object.",
|
|
165
|
+
`Expected an object at ${CONFIG_FILE}.${rollbackSuffix()}`
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
const record = config;
|
|
169
|
+
const scope = fields ? new Set(fields) : null;
|
|
170
|
+
for (const [field, kind] of Object.entries(CONFIG_FIELD_TYPES)) {
|
|
171
|
+
if (scope && !scope.has(field)) continue;
|
|
172
|
+
const value = record[field];
|
|
173
|
+
if (value === void 0) continue;
|
|
174
|
+
if (!matchesKind(value, kind)) throw invalidFieldError(field, kind);
|
|
175
|
+
}
|
|
176
|
+
for (const [path, kind] of Object.entries(CONFIG_LEAF_TYPES)) {
|
|
177
|
+
const [field, leaf] = path.split(".");
|
|
178
|
+
if (scope && !scope.has(field)) continue;
|
|
179
|
+
const owner = record[field];
|
|
180
|
+
if (!isPlainObject(owner)) continue;
|
|
181
|
+
const value = owner[leaf];
|
|
182
|
+
if (value === void 0 || !matchesKind(value, kind)) throw invalidFieldError(path, kind);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
function readRawConfig() {
|
|
186
|
+
if (!existsSync(CONFIG_FILE)) return null;
|
|
187
|
+
try {
|
|
188
|
+
return JSON.parse(readFileSync(CONFIG_FILE, "utf-8"));
|
|
189
|
+
} catch {
|
|
190
|
+
throw new DvmError(
|
|
191
|
+
"config_corrupt",
|
|
192
|
+
"Config file is corrupt.",
|
|
193
|
+
// Not "delete it": those bytes are the only copy on this machine of the
|
|
194
|
+
// NWC connection string and both private keys, however badly they parse.
|
|
195
|
+
`Run 'dvm config rollback' to restore a stored copy, or move ${CONFIG_FILE} aside yourself and run 'dvm init'.${rollbackSuffix()}`
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function writeConfigFile(config) {
|
|
200
|
+
writeJsonAtomic(CONFIG_FILE, config);
|
|
201
|
+
}
|
|
202
|
+
var CONFIG_SNAPSHOT_SLOTS = ["backup", "pinned"];
|
|
203
|
+
function configSnapshotPath(slot) {
|
|
204
|
+
return slot === "pinned" ? CONFIG_PINNED_FILE : CONFIG_BACKUP_FILE;
|
|
205
|
+
}
|
|
206
|
+
function describeConfigBackup(slot = "backup") {
|
|
207
|
+
const path = configSnapshotPath(slot);
|
|
208
|
+
try {
|
|
209
|
+
if (!existsSync(path)) return null;
|
|
210
|
+
const stat = statSync(path);
|
|
211
|
+
const posix = process.platform !== "win32";
|
|
212
|
+
const mode = stat.mode & 511;
|
|
213
|
+
return {
|
|
214
|
+
path,
|
|
215
|
+
slot,
|
|
216
|
+
bytes: stat.size,
|
|
217
|
+
modifiedAt: stat.mtime.toISOString(),
|
|
218
|
+
modeOctal: posix ? toOctal(mode) : null,
|
|
219
|
+
ownerOnly: !posix || (mode & 63) === 0
|
|
220
|
+
};
|
|
221
|
+
} catch {
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function readConfigBackup(slot = "backup") {
|
|
226
|
+
const path = configSnapshotPath(slot);
|
|
227
|
+
if (!existsSync(path)) return null;
|
|
228
|
+
try {
|
|
229
|
+
const parsed = JSON.parse(readFileSync(path, "utf-8"));
|
|
230
|
+
return isPlainObject(parsed) ? parsed : null;
|
|
231
|
+
} catch {
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
function retainConfigBackup(slot = "backup") {
|
|
236
|
+
if (!existsSync(CONFIG_FILE)) return false;
|
|
237
|
+
ensureConfigDir();
|
|
238
|
+
const path = configSnapshotPath(slot);
|
|
239
|
+
const tmp = `${path}.tmp`;
|
|
240
|
+
writeFileSync(tmp, readFileSync(CONFIG_FILE), { mode: 384 });
|
|
241
|
+
chmodSync(tmp, 384);
|
|
242
|
+
commitTmp(tmp, path);
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
function writeConfigBackup(config, slot = "backup") {
|
|
246
|
+
writeJsonAtomic(configSnapshotPath(slot), config);
|
|
247
|
+
}
|
|
248
|
+
function removeConfigBackup(slot = "backup") {
|
|
249
|
+
const path = configSnapshotPath(slot);
|
|
250
|
+
if (!existsSync(path)) return false;
|
|
251
|
+
unlinkSync(path);
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
function protectedTargetValue(config, target) {
|
|
255
|
+
const [field, named] = target.split(".");
|
|
256
|
+
const held = config[field];
|
|
257
|
+
const leaves = PROTECTED_FIELDS[field].credentialLeaves;
|
|
258
|
+
const leaf = named ?? (leaves.length > 0 ? leaves[0] : void 0);
|
|
259
|
+
if (leaf === void 0) return held;
|
|
260
|
+
return isPlainObject(held) ? held[leaf] : void 0;
|
|
261
|
+
}
|
|
262
|
+
function protectedValuesMatch(target, left, right) {
|
|
263
|
+
const normalized = comparableProtectedValue(target, left);
|
|
264
|
+
return normalized !== void 0 && normalized === comparableProtectedValue(target, right);
|
|
265
|
+
}
|
|
266
|
+
function isSecretProtectedTarget(target) {
|
|
267
|
+
return PROTECTED_FIELDS[target.split(".")[0]].secret;
|
|
268
|
+
}
|
|
269
|
+
function withoutProtectedTargets(config, targets) {
|
|
270
|
+
const dropping = /* @__PURE__ */ new Set();
|
|
271
|
+
for (const target of targets) {
|
|
272
|
+
const field = target.split(".")[0];
|
|
273
|
+
dropping.add(field);
|
|
274
|
+
for (const alias of PROTECTED_FIELDS[field].aliases) dropping.add(alias);
|
|
275
|
+
}
|
|
276
|
+
return Object.fromEntries(
|
|
277
|
+
Object.entries(config).filter(([field]) => !dropping.has(field))
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
function appendConfigAudit(record) {
|
|
281
|
+
try {
|
|
282
|
+
ensureConfigDir();
|
|
283
|
+
appendFileSync(CONFIG_AUDIT_FILE, JSON.stringify(record) + "\n", { mode: 384 });
|
|
284
|
+
chmodSync(CONFIG_AUDIT_FILE, 384);
|
|
285
|
+
} catch {
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
function protectedRemovalError(targets, reason) {
|
|
289
|
+
const named = targets.join(", ");
|
|
290
|
+
const one = targets.length === 1;
|
|
291
|
+
return new DvmError(
|
|
292
|
+
"config_protected_field",
|
|
293
|
+
`The '${reason}' change would remove ${named} from the local configuration.`,
|
|
294
|
+
`Nothing was written. ${named} ${one ? "is a credential or identity pointer" : "are credentials or identity pointers"} nothing else on this machine records, so only a command that names ${one ? "it" : "them"} explicitly may remove ${one ? "it" : "them"} \u2014 'dvm wallet disconnect', 'dvm wallet tempo-disconnect' and 'dvm wallet x402-disconnect' are those commands.`,
|
|
295
|
+
{ fields: targets }
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
var PROTECTED_FIELDS = {
|
|
299
|
+
nwcUri: { aliases: [], credentialLeaves: [], secret: true, hexCredential: false },
|
|
300
|
+
// A pointer at a key, not a key: worth protecting from removal, and never
|
|
301
|
+
// worth stripping out of a stored copy — that is recovery lost for nothing.
|
|
302
|
+
defaultIdentity: { aliases: [], credentialLeaves: [], secret: false, hexCredential: false },
|
|
303
|
+
// `mpp` is the pre-internal-review name; a caller who has not run a mutating command
|
|
304
|
+
// since the rename still has their private key under it.
|
|
305
|
+
tempo: { aliases: ["mpp"], credentialLeaves: ["apiKey"], secret: true, hexCredential: true },
|
|
306
|
+
x402: { aliases: [], credentialLeaves: ["privateKey"], secret: true, hexCredential: true }
|
|
307
|
+
};
|
|
308
|
+
var CONFIG_PATH_SHAPE = {
|
|
309
|
+
float: { statedBudget: {}, connectedAt: {}, alias: {}, methods: {} },
|
|
310
|
+
x402: { privateKey: {}, network: {} },
|
|
311
|
+
tempo: { method: {}, accountId: {}, apiKey: {} },
|
|
312
|
+
credit: {
|
|
313
|
+
targetJobs: {},
|
|
314
|
+
posture: {},
|
|
315
|
+
tier0Cap: {},
|
|
316
|
+
tier1Cap: {},
|
|
317
|
+
pocketTarget: {},
|
|
318
|
+
fundingRail: {},
|
|
319
|
+
perDvm: { "*": { targetJobs: {}, posture: {}, fundingRail: {}, dvmId: {}, builderPubkey: {} } },
|
|
320
|
+
trustOverrides: { "*": {} }
|
|
321
|
+
},
|
|
322
|
+
feedback: { posture: {} }
|
|
323
|
+
};
|
|
324
|
+
var CONFIG_FIELD_TYPES = {
|
|
325
|
+
nwcUri: "string",
|
|
326
|
+
float: "object",
|
|
327
|
+
defaultBudget: "string",
|
|
328
|
+
autoPayThreshold: "string",
|
|
329
|
+
defaultMaxPayments: "number",
|
|
330
|
+
cashuMints: "string[]",
|
|
331
|
+
x402: "object",
|
|
332
|
+
tempo: "object",
|
|
333
|
+
defaultRail: "string",
|
|
334
|
+
defaultIdentity: "string",
|
|
335
|
+
credit: "object",
|
|
336
|
+
feedback: "object"
|
|
337
|
+
};
|
|
338
|
+
var CONFIG_LEAF_TYPES = {
|
|
339
|
+
"float.connectedAt": "string",
|
|
340
|
+
"x402.privateKey": "string",
|
|
341
|
+
"tempo.method": "string"
|
|
342
|
+
};
|
|
343
|
+
function collectChangedPaths(before, after, shape, prefix, found) {
|
|
344
|
+
for (const key of /* @__PURE__ */ new Set([...Object.keys(before), ...Object.keys(after)])) {
|
|
345
|
+
const held = before[key];
|
|
346
|
+
const kept = after[key];
|
|
347
|
+
if (deepEqual(held, kept)) continue;
|
|
348
|
+
const known = shape !== void 0 && Object.hasOwn(shape, key);
|
|
349
|
+
const wildcard = !known && shape !== void 0 && Object.hasOwn(shape, "*");
|
|
350
|
+
const child = known ? shape[key] : wildcard ? shape["*"] : void 0;
|
|
351
|
+
const path = prefix ? `${prefix}.${wildcard ? "*" : key}` : key;
|
|
352
|
+
const descendable = child !== void 0 && Object.keys(child).length > 0 && (held === void 0 || isPlainObject(held)) && (kept === void 0 || isPlainObject(kept));
|
|
353
|
+
if (descendable) {
|
|
354
|
+
collectChangedPaths(
|
|
355
|
+
isPlainObject(held) ? held : {},
|
|
356
|
+
isPlainObject(kept) ? kept : {},
|
|
357
|
+
child,
|
|
358
|
+
path,
|
|
359
|
+
found
|
|
360
|
+
);
|
|
361
|
+
} else {
|
|
362
|
+
found.add(path);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
function resettableConfigFields() {
|
|
367
|
+
const guarded = new Set(PROTECTED_CONFIG_FIELDS);
|
|
368
|
+
return Object.keys(CONFIG_FIELD_TYPES).filter((field) => !guarded.has(field));
|
|
369
|
+
}
|
|
370
|
+
function invalidFieldError(path, kind) {
|
|
371
|
+
return new DvmError(
|
|
372
|
+
"config_invalid",
|
|
373
|
+
`Config field '${path}' must be ${describeKind(kind)}.`,
|
|
374
|
+
`Nothing was written. Correct the field in ${CONFIG_FILE}, or run 'dvm doctor' for the full picture.${rollbackSuffix()}`
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
function describeKind(kind) {
|
|
378
|
+
switch (kind) {
|
|
379
|
+
case "string":
|
|
380
|
+
return "a string";
|
|
381
|
+
case "number":
|
|
382
|
+
return "a number";
|
|
383
|
+
case "object":
|
|
384
|
+
return "an object";
|
|
385
|
+
case "string[]":
|
|
386
|
+
return "an array of strings";
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
function matchesKind(value, kind) {
|
|
390
|
+
switch (kind) {
|
|
391
|
+
case "string":
|
|
392
|
+
return typeof value === "string";
|
|
393
|
+
case "number":
|
|
394
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
395
|
+
case "object":
|
|
396
|
+
return isPlainObject(value);
|
|
397
|
+
case "string[]":
|
|
398
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
function rollbackSuffix() {
|
|
402
|
+
const stored = describeConfigBackup() ?? describeConfigBackup("pinned");
|
|
403
|
+
return stored === null ? "" : ` A stored copy of the previous configuration is at ${stored.path}.`;
|
|
404
|
+
}
|
|
405
|
+
function writeJsonAtomic(path, value) {
|
|
406
|
+
ensureConfigDir();
|
|
407
|
+
const tmp = `${path}.tmp`;
|
|
408
|
+
writeFileSync(tmp, JSON.stringify(value, null, 2) + "\n", { mode: 384 });
|
|
409
|
+
chmodSync(tmp, 384);
|
|
410
|
+
commitTmp(tmp, path);
|
|
411
|
+
}
|
|
412
|
+
function commitTmp(tmp, path) {
|
|
413
|
+
try {
|
|
414
|
+
renameSync(tmp, path);
|
|
415
|
+
} catch (err) {
|
|
416
|
+
try {
|
|
417
|
+
unlinkSync(tmp);
|
|
418
|
+
} catch {
|
|
419
|
+
}
|
|
420
|
+
throw err;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
function deepEqual(a, b) {
|
|
424
|
+
if (a === b) return true;
|
|
425
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
426
|
+
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false;
|
|
427
|
+
return a.every((item, index) => deepEqual(item, b[index]));
|
|
428
|
+
}
|
|
429
|
+
if (!isPlainObject(a) || !isPlainObject(b)) return false;
|
|
430
|
+
for (const key of /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)])) {
|
|
431
|
+
if (!deepEqual(a[key], b[key])) return false;
|
|
432
|
+
}
|
|
433
|
+
return true;
|
|
434
|
+
}
|
|
435
|
+
function isPresentValue(value) {
|
|
436
|
+
if (value === void 0 || value === null) return false;
|
|
437
|
+
return typeof value !== "string" || value.trim().length > 0;
|
|
438
|
+
}
|
|
439
|
+
function comparableProtectedValue(target, value) {
|
|
440
|
+
if (typeof value !== "string") return void 0;
|
|
441
|
+
const trimmed = value.trim();
|
|
442
|
+
if (trimmed.length === 0) return void 0;
|
|
443
|
+
const field = target.split(".")[0];
|
|
444
|
+
return PROTECTED_FIELDS[field].hexCredential ? trimmed.toLowerCase() : trimmed;
|
|
445
|
+
}
|
|
446
|
+
function patchShapeError(message) {
|
|
447
|
+
return new DvmError(
|
|
448
|
+
"config_patch_invalid",
|
|
449
|
+
message,
|
|
450
|
+
"A change declares `set` (fields to write) and `unset` (field names to remove). Anything else cannot be applied, and nothing was written."
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
function freezeDeep(value) {
|
|
454
|
+
if (typeof value !== "object" || value === null || Object.isFrozen(value)) return;
|
|
455
|
+
Object.freeze(value);
|
|
456
|
+
for (const nested of Object.values(value)) freezeDeep(nested);
|
|
457
|
+
}
|
|
458
|
+
function isPlainObject(value) {
|
|
459
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
460
|
+
}
|
|
461
|
+
function toOctal(mode) {
|
|
462
|
+
return "0" + mode.toString(8).padStart(3, "0");
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// src/lib/identity.ts
|
|
466
|
+
import lockfile from "proper-lockfile";
|
|
467
|
+
var CREDIT_POSTURES = ["suggest", "auto", "off"];
|
|
468
|
+
function isCreditPosture(value) {
|
|
469
|
+
return typeof value === "string" && CREDIT_POSTURES.includes(value);
|
|
470
|
+
}
|
|
471
|
+
var FEEDBACK_POSTURES = ["ask", "auto", "off"];
|
|
472
|
+
function isFeedbackPosture(value) {
|
|
473
|
+
return typeof value === "string" && FEEDBACK_POSTURES.includes(value);
|
|
474
|
+
}
|
|
475
|
+
function migrateCreditConfig(config) {
|
|
476
|
+
const credit = config.credit;
|
|
477
|
+
if (!credit) return config;
|
|
478
|
+
const migrateLayer = (layer) => {
|
|
479
|
+
const railed = migrateFundingRail(layer);
|
|
480
|
+
if (!("disabled" in railed)) return railed;
|
|
481
|
+
const { disabled, ...rest } = railed;
|
|
482
|
+
const migrated = { ...rest };
|
|
483
|
+
if (migrated.posture === void 0 && disabled === true) migrated.posture = "off";
|
|
484
|
+
return migrated;
|
|
485
|
+
};
|
|
486
|
+
const perDvm = credit.perDvm;
|
|
487
|
+
return {
|
|
488
|
+
...config,
|
|
489
|
+
credit: {
|
|
490
|
+
...migrateLayer(credit),
|
|
491
|
+
...perDvm ? {
|
|
492
|
+
perDvm: Object.fromEntries(
|
|
493
|
+
Object.entries(perDvm).map(([endpoint, per]) => [endpoint, migrateLayer(per)])
|
|
494
|
+
)
|
|
495
|
+
} : {}
|
|
496
|
+
}
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
function migrateFundingRail(layer) {
|
|
500
|
+
const loose = layer;
|
|
501
|
+
if (loose.fundingRail !== "mpp") return layer;
|
|
502
|
+
return { ...layer, fundingRail: "tempo" };
|
|
503
|
+
}
|
|
504
|
+
function migrateTempoWalletKey(config) {
|
|
505
|
+
const loose = config;
|
|
506
|
+
if (!loose.mpp || loose.tempo) return config;
|
|
507
|
+
const { mpp, ...rest } = loose;
|
|
508
|
+
return { ...rest, tempo: mpp };
|
|
509
|
+
}
|
|
510
|
+
function migrateConfig(config) {
|
|
511
|
+
return migrateCreditConfig(migrateTempoWalletKey(config));
|
|
512
|
+
}
|
|
513
|
+
function loadConfig() {
|
|
514
|
+
const raw = readRawConfig();
|
|
515
|
+
return raw === null ? null : migrateConfig(raw);
|
|
516
|
+
}
|
|
517
|
+
async function updateConfig(update) {
|
|
518
|
+
return withConfigLock(async () => {
|
|
519
|
+
const raw = readRawConfig();
|
|
520
|
+
const snapshot = raw === null ? {} : structuredClone(raw);
|
|
521
|
+
const base = raw === null ? null : migrateConfig(structuredClone(raw));
|
|
522
|
+
const handed = raw === null ? null : deepFreezeConfig(migrateConfig(structuredClone(raw)));
|
|
523
|
+
const next = applyConfigPatch(base ?? {}, await runPatch(update, handed));
|
|
524
|
+
const changed = changedConfigPaths(snapshot, next);
|
|
525
|
+
if (changed.length === 0) {
|
|
526
|
+
return {
|
|
527
|
+
config: base ?? {},
|
|
528
|
+
previous: base,
|
|
529
|
+
changed: [],
|
|
530
|
+
written: false,
|
|
531
|
+
backup: describeConfigBackup()
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
const removals = protectedRemovals(base ?? {}, next);
|
|
535
|
+
const unauthorized = removals.filter((target) => update.authorize?.covers(target) !== true);
|
|
536
|
+
if (unauthorized.length > 0) throw protectedRemovalError(unauthorized, update.reason);
|
|
537
|
+
assertValidConfig(
|
|
538
|
+
next,
|
|
539
|
+
changed.map((path) => path.split(".")[0])
|
|
540
|
+
);
|
|
541
|
+
const superseded = supersededProtectedSecrets(base ?? {}, next);
|
|
542
|
+
const stripped = [
|
|
543
|
+
.../* @__PURE__ */ new Set([...removals, ...superseded, ...update.sanitizeBackup ?? []])
|
|
544
|
+
].filter(isSecretProtectedTarget);
|
|
545
|
+
const sweeping = update.sweepSlots ?? CONFIG_SNAPSHOT_SLOTS;
|
|
546
|
+
const swept = [];
|
|
547
|
+
if (raw !== null) {
|
|
548
|
+
if (stripped.length > 0) {
|
|
549
|
+
writeConfigBackup(withoutProtectedTargets(raw, stripped));
|
|
550
|
+
swept.push("backup");
|
|
551
|
+
} else {
|
|
552
|
+
retainConfigBackup();
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
if (stripped.length > 0 && sweeping.includes("pinned")) {
|
|
556
|
+
if (sweepPinnedSnapshot(stripped, base ?? {}, update.reason)) swept.push("pinned");
|
|
557
|
+
}
|
|
558
|
+
const backup = describeConfigBackup();
|
|
559
|
+
writeConfigFile(next);
|
|
560
|
+
appendConfigAudit({
|
|
561
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
562
|
+
event: "update",
|
|
563
|
+
reason: update.reason,
|
|
564
|
+
fields: changed,
|
|
565
|
+
...removals.length > 0 ? { removed: removals } : {},
|
|
566
|
+
...superseded.length > 0 ? { superseded } : {},
|
|
567
|
+
...swept.length > 0 ? { sweptSlots: swept } : {},
|
|
568
|
+
backup: backup !== null,
|
|
569
|
+
pid: process.pid
|
|
570
|
+
});
|
|
571
|
+
return { config: next, previous: base, changed, written: true, backup };
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
async function pinConfigSnapshot(reason) {
|
|
575
|
+
return withConfigLock(() => {
|
|
576
|
+
if (describeConfigBackup("pinned") !== null) return null;
|
|
577
|
+
if (!retainConfigBackup("pinned")) return null;
|
|
578
|
+
const pinned = describeConfigBackup("pinned");
|
|
579
|
+
appendConfigAudit({
|
|
580
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
581
|
+
event: "pinned",
|
|
582
|
+
reason,
|
|
583
|
+
fields: [],
|
|
584
|
+
slot: "pinned",
|
|
585
|
+
backup: pinned !== null,
|
|
586
|
+
pid: process.pid
|
|
587
|
+
});
|
|
588
|
+
return pinned;
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
async function ensureConfig(seed = {}, reason = "init") {
|
|
592
|
+
return withConfigLock(() => {
|
|
593
|
+
const existing = readRawConfig();
|
|
594
|
+
if (existing !== null) return { config: migrateConfig(existing), created: false };
|
|
595
|
+
assertValidConfig(seed);
|
|
596
|
+
writeConfigFile(seed);
|
|
597
|
+
appendConfigAudit({
|
|
598
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
599
|
+
event: "create",
|
|
600
|
+
reason,
|
|
601
|
+
fields: changedConfigPaths({}, seed),
|
|
602
|
+
backup: describeConfigBackup() !== null,
|
|
603
|
+
pid: process.pid
|
|
604
|
+
});
|
|
605
|
+
return { config: seed, created: true };
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
function previewConfigRollback(source = "backup") {
|
|
609
|
+
const restored = readConfigBackup(source);
|
|
610
|
+
if (restored === null) return null;
|
|
611
|
+
let currentOnDisk = null;
|
|
612
|
+
try {
|
|
613
|
+
currentOnDisk = readRawConfig();
|
|
614
|
+
} catch {
|
|
615
|
+
}
|
|
616
|
+
return {
|
|
617
|
+
changed: changedConfigPaths(currentOnDisk ?? {}, restored),
|
|
618
|
+
removing: protectedRemovals(
|
|
619
|
+
currentOnDisk === null ? {} : migrateConfig(structuredClone(currentOnDisk)),
|
|
620
|
+
migrateConfig(structuredClone(restored))
|
|
621
|
+
)
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
async function rollbackConfig(reason = "config_rollback", source = "backup", authorize) {
|
|
625
|
+
return withConfigLock(() => {
|
|
626
|
+
const restored = readConfigBackup(source);
|
|
627
|
+
if (restored === null) {
|
|
628
|
+
const stored = describeConfigBackup(source);
|
|
629
|
+
throw stored === null ? new DvmError(
|
|
630
|
+
"config_no_rollback",
|
|
631
|
+
`No ${source === "pinned" ? "pinned snapshot" : "rollback copy"} of the configuration is stored.`,
|
|
632
|
+
`A rollback copy is written at ${configSnapshotPath(source)} before each change, so there is one to restore only after a change has been made on this machine.`
|
|
633
|
+
) : new DvmError(
|
|
634
|
+
"config_rollback_corrupt",
|
|
635
|
+
`The stored copy at ${configSnapshotPath(source)} is not readable as a configuration.`,
|
|
636
|
+
"Nothing was changed. Inspect that file yourself, or remove it and continue with the configuration you have."
|
|
637
|
+
);
|
|
638
|
+
}
|
|
639
|
+
assertValidConfig(restored);
|
|
640
|
+
let currentOnDisk = null;
|
|
641
|
+
try {
|
|
642
|
+
currentOnDisk = readRawConfig();
|
|
643
|
+
} catch {
|
|
644
|
+
}
|
|
645
|
+
const changed = changedConfigPaths(currentOnDisk ?? {}, restored);
|
|
646
|
+
const removed = protectedRemovals(
|
|
647
|
+
currentOnDisk === null ? {} : migrateConfig(structuredClone(currentOnDisk)),
|
|
648
|
+
migrateConfig(structuredClone(restored))
|
|
649
|
+
);
|
|
650
|
+
const unauthorized = removed.filter((target) => authorize?.covers(target) !== true);
|
|
651
|
+
if (unauthorized.length > 0) throw protectedRemovalError(unauthorized, reason);
|
|
652
|
+
if (!retainConfigBackup()) retireSlot(`${reason}:nothing_to_swap`, "backup");
|
|
653
|
+
if (source === "pinned") retireSlot(`${reason}:pin_restored`, "pinned");
|
|
654
|
+
writeConfigFile(restored);
|
|
655
|
+
appendConfigAudit({
|
|
656
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
657
|
+
event: "rollback",
|
|
658
|
+
reason,
|
|
659
|
+
fields: changed,
|
|
660
|
+
...removed.length > 0 ? { removed } : {},
|
|
661
|
+
source,
|
|
662
|
+
backup: describeConfigBackup() !== null,
|
|
663
|
+
pid: process.pid
|
|
664
|
+
});
|
|
665
|
+
return { config: restored, changed, removed };
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
async function retireConfigBackup(reason, slot = "backup") {
|
|
669
|
+
return withConfigLock(() => retireSlot(reason, slot));
|
|
670
|
+
}
|
|
671
|
+
async function withConfigLock(fn) {
|
|
672
|
+
ensureConfigDir();
|
|
673
|
+
let release;
|
|
674
|
+
try {
|
|
675
|
+
release = await lockfile.lock(CONFIG_FILE, {
|
|
676
|
+
lockfilePath: `${CONFIG_FILE}.lock`,
|
|
677
|
+
realpath: false,
|
|
678
|
+
stale: 3e4,
|
|
679
|
+
retries: { retries: 10, factor: 1, minTimeout: 100, maxTimeout: 100 }
|
|
680
|
+
});
|
|
681
|
+
} catch {
|
|
682
|
+
throw new DvmError(
|
|
683
|
+
"config_locked",
|
|
684
|
+
"Another dvm process is updating the local configuration.",
|
|
685
|
+
"Wait a moment and retry."
|
|
686
|
+
);
|
|
687
|
+
}
|
|
688
|
+
try {
|
|
689
|
+
return await fn();
|
|
690
|
+
} finally {
|
|
691
|
+
await release().catch(() => void 0);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
async function runPatch(update, current) {
|
|
695
|
+
try {
|
|
696
|
+
return await update.patch(current);
|
|
697
|
+
} catch (err) {
|
|
698
|
+
if (err instanceof TypeError && /read only|not extensible|frozen|cannot delete property|cannot add property/i.test(
|
|
699
|
+
err.message
|
|
700
|
+
)) {
|
|
701
|
+
throw new DvmError(
|
|
702
|
+
"config_patch_invalid",
|
|
703
|
+
`The '${update.reason}' change tried to modify the configuration it was handed.`,
|
|
704
|
+
"Nothing was written. A change declares what it wants through its `set` and `unset` lists; editing the current config in place bypasses the protected-field gate, so the object is frozen."
|
|
705
|
+
);
|
|
706
|
+
}
|
|
707
|
+
throw err;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
function sweepPinnedSnapshot(targets, departing, reason) {
|
|
711
|
+
if (describeConfigBackup("pinned") === null) return false;
|
|
712
|
+
const pinned = readConfigBackup("pinned");
|
|
713
|
+
if (pinned === null) {
|
|
714
|
+
return retireSlot(`${reason}:unreadable_pin`, "pinned");
|
|
715
|
+
}
|
|
716
|
+
const migrated = migrateConfig(structuredClone(pinned));
|
|
717
|
+
const matching = targets.filter(
|
|
718
|
+
(target) => protectedValuesMatch(
|
|
719
|
+
target,
|
|
720
|
+
protectedTargetValue(departing, target),
|
|
721
|
+
protectedTargetValue(migrated, target)
|
|
722
|
+
)
|
|
723
|
+
);
|
|
724
|
+
if (matching.length === 0) return false;
|
|
725
|
+
const sanitized = withoutProtectedTargets(pinned, matching);
|
|
726
|
+
const changed = changedConfigPaths(pinned, sanitized);
|
|
727
|
+
if (changed.length === 0) return false;
|
|
728
|
+
writeConfigBackup(sanitized, "pinned");
|
|
729
|
+
appendConfigAudit({
|
|
730
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
731
|
+
event: "backup_sanitized",
|
|
732
|
+
reason,
|
|
733
|
+
fields: changed,
|
|
734
|
+
removed: matching,
|
|
735
|
+
slot: "pinned",
|
|
736
|
+
backup: describeConfigBackup() !== null,
|
|
737
|
+
pid: process.pid
|
|
738
|
+
});
|
|
739
|
+
return true;
|
|
740
|
+
}
|
|
741
|
+
function retireSlot(reason, slot) {
|
|
742
|
+
if (!removeConfigBackup(slot)) return false;
|
|
743
|
+
appendConfigAudit({
|
|
744
|
+
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
745
|
+
event: "backup_retired",
|
|
746
|
+
reason,
|
|
747
|
+
fields: [],
|
|
748
|
+
slot,
|
|
749
|
+
backup: describeConfigBackup() !== null,
|
|
750
|
+
pid: process.pid
|
|
751
|
+
});
|
|
752
|
+
return true;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
export {
|
|
756
|
+
isValidEvmPrivateKey,
|
|
757
|
+
authorizeProtectedRemoval,
|
|
758
|
+
planConfigReset,
|
|
759
|
+
configSnapshotPath,
|
|
760
|
+
describeConfigBackup,
|
|
761
|
+
CREDIT_POSTURES,
|
|
762
|
+
isCreditPosture,
|
|
763
|
+
FEEDBACK_POSTURES,
|
|
764
|
+
isFeedbackPosture,
|
|
765
|
+
loadConfig,
|
|
766
|
+
updateConfig,
|
|
767
|
+
pinConfigSnapshot,
|
|
768
|
+
ensureConfig,
|
|
769
|
+
previewConfigRollback,
|
|
770
|
+
rollbackConfig,
|
|
771
|
+
retireConfigBackup
|
|
772
|
+
};
|