@ipab/cli 0.1.0-rc.4 → 0.1.0-rc.6
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/README.md +88 -16
- package/dist/app-templates.js +1 -1
- package/dist/app-templates.js.map +1 -1
- package/dist/builtins/skill.ipab.car +0 -0
- package/dist/builtins/template.node.car +0 -0
- package/dist/builtins/template.web-app.car +0 -0
- package/dist/cli.d.ts +6 -2
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +656 -278
- package/dist/cli.js.map +1 -1
- package/dist/config-secret.d.ts +1 -1
- package/dist/config-secret.d.ts.map +1 -1
- package/dist/config-secret.js +2 -73
- package/dist/config-secret.js.map +1 -1
- package/dist/config.d.ts +1 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +12 -1
- package/dist/config.js.map +1 -1
- package/dist/content-provider.d.ts +1 -1
- package/dist/content-provider.d.ts.map +1 -1
- package/dist/content-provider.js +4 -3
- package/dist/content-provider.js.map +1 -1
- package/dist/context-types.d.ts +12 -0
- package/dist/context-types.d.ts.map +1 -0
- package/dist/context-types.js +2 -0
- package/dist/context-types.js.map +1 -0
- package/dist/context.d.ts +6 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +24 -9
- package/dist/context.js.map +1 -1
- package/dist/doctor.d.ts +29 -0
- package/dist/doctor.d.ts.map +1 -0
- package/dist/doctor.js +576 -0
- package/dist/doctor.js.map +1 -0
- package/dist/identity.d.ts +1 -1
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +9 -9
- package/dist/identity.js.map +1 -1
- package/dist/ipc.d.ts +3 -2
- package/dist/ipc.d.ts.map +1 -1
- package/dist/ipc.js +33 -9
- package/dist/ipc.js.map +1 -1
- package/dist/kubo-install.d.ts +26 -0
- package/dist/kubo-install.d.ts.map +1 -0
- package/dist/kubo-install.js +724 -0
- package/dist/kubo-install.js.map +1 -0
- package/dist/kubo-peering.d.ts +11 -0
- package/dist/kubo-peering.d.ts.map +1 -0
- package/dist/kubo-peering.js +93 -0
- package/dist/kubo-peering.js.map +1 -0
- package/dist/kubo-peers.d.ts +12 -0
- package/dist/kubo-peers.d.ts.map +1 -0
- package/dist/kubo-peers.js +118 -0
- package/dist/kubo-peers.js.map +1 -0
- package/dist/kubo-release.d.ts +22 -0
- package/dist/kubo-release.d.ts.map +1 -0
- package/dist/kubo-release.js +176 -0
- package/dist/kubo-release.js.map +1 -0
- package/dist/kubo.d.ts +12 -4
- package/dist/kubo.d.ts.map +1 -1
- package/dist/kubo.js +218 -259
- package/dist/kubo.js.map +1 -1
- package/dist/pins.d.ts +15 -17
- package/dist/pins.d.ts.map +1 -1
- package/dist/pins.js +144 -106
- package/dist/pins.js.map +1 -1
- package/dist/publish-source.d.ts +3 -0
- package/dist/publish-source.d.ts.map +1 -0
- package/dist/publish-source.js +85 -0
- package/dist/publish-source.js.map +1 -0
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +4 -2
- package/dist/runtime.js.map +1 -1
- package/dist/state-storage-windows.d.ts +2 -0
- package/dist/state-storage-windows.d.ts.map +1 -0
- package/dist/state-storage-windows.js +114 -0
- package/dist/state-storage-windows.js.map +1 -0
- package/dist/state-storage.d.ts +5 -0
- package/dist/state-storage.d.ts.map +1 -0
- package/dist/state-storage.js +330 -0
- package/dist/state-storage.js.map +1 -0
- package/dist/state.d.ts +42 -8
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +254 -98
- package/dist/state.js.map +1 -1
- package/dist/swarm-envelope.d.ts +1 -0
- package/dist/swarm-envelope.d.ts.map +1 -1
- package/dist/swarm-envelope.js +12 -0
- package/dist/swarm-envelope.js.map +1 -1
- package/dist/swarm-files.d.ts +1 -1
- package/dist/swarm-files.d.ts.map +1 -1
- package/dist/swarm-files.js +4 -1
- package/dist/swarm-files.js.map +1 -1
- package/dist/swarm.d.ts +70 -11
- package/dist/swarm.d.ts.map +1 -1
- package/dist/swarm.js +1033 -167
- package/dist/swarm.js.map +1 -1
- package/package.json +4 -4
- package/src/app-templates.ts +1 -1
- package/src/cli.ts +827 -356
- package/src/config-secret.ts +2 -84
- package/src/config.ts +14 -1
- package/src/content-provider.ts +4 -2
- package/src/context-types.ts +11 -0
- package/src/context.ts +39 -9
- package/src/doctor.ts +902 -0
- package/src/identity.ts +10 -10
- package/src/ipc.ts +49 -8
- package/src/kubo-install.ts +936 -0
- package/src/kubo-peering.ts +126 -0
- package/src/kubo-peers.ts +145 -0
- package/src/kubo-release.ts +230 -0
- package/src/kubo.ts +302 -336
- package/src/pins.ts +188 -126
- package/src/publish-source.ts +103 -0
- package/src/runtime.ts +10 -2
- package/src/state-storage-windows.ts +134 -0
- package/src/state-storage.ts +375 -0
- package/src/state.ts +410 -118
- package/src/swarm-envelope.ts +14 -0
- package/src/swarm-files.ts +4 -0
- package/src/swarm.ts +1482 -207
- package/dist/setup.d.ts +0 -7
- package/dist/setup.d.ts.map +0 -1
- package/dist/setup.js +0 -22
- package/dist/setup.js.map +0 -1
- package/src/setup.ts +0 -21
package/src/state.ts
CHANGED
|
@@ -1,28 +1,53 @@
|
|
|
1
|
-
import { mkdirSync } from "node:fs";
|
|
2
1
|
import { randomUUID } from "node:crypto";
|
|
3
|
-
import {
|
|
2
|
+
import { resolve } from "node:path";
|
|
4
3
|
import { DatabaseSync } from "node:sqlite";
|
|
5
4
|
import { CID } from "multiformats/cid";
|
|
6
5
|
import type { StoreConfiguration } from "@ipab/ipfs-store";
|
|
7
6
|
import {
|
|
7
|
+
rejectLegacySwarmCid,
|
|
8
8
|
validateUserConfig,
|
|
9
9
|
swarmUrl,
|
|
10
10
|
type ConfigKey,
|
|
11
11
|
type ConfigValue,
|
|
12
12
|
} from "./config.js";
|
|
13
13
|
import { HostError, object, syntax } from "./errors.js";
|
|
14
|
-
import {
|
|
14
|
+
import { openStateStorage } from "./state-storage.js";
|
|
15
15
|
|
|
16
16
|
export interface Association {
|
|
17
17
|
schemaCid: string;
|
|
18
18
|
appCid: string;
|
|
19
19
|
argv: string[];
|
|
20
20
|
}
|
|
21
|
+
export function createAssociation(
|
|
22
|
+
schemaCid: string,
|
|
23
|
+
appCid: string,
|
|
24
|
+
argv: string[],
|
|
25
|
+
): Association {
|
|
26
|
+
if (!argv.length || !argv.includes("%"))
|
|
27
|
+
syntax('Argument template must contain a whole "%" argument.');
|
|
28
|
+
return {
|
|
29
|
+
schemaCid: exactCid(schemaCid, true),
|
|
30
|
+
appCid: exactCid(appCid),
|
|
31
|
+
argv: [...argv],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
21
34
|
export interface RegisteredSwarm {
|
|
22
35
|
// Keep the storage column name, but values are configuration URLs; old CID rows are rejected.
|
|
23
36
|
cid: string;
|
|
24
37
|
alias: string | null;
|
|
25
38
|
}
|
|
39
|
+
export interface SwarmProjection {
|
|
40
|
+
account: string;
|
|
41
|
+
swarm: string;
|
|
42
|
+
stateCid: string;
|
|
43
|
+
root: string;
|
|
44
|
+
valueCid: string;
|
|
45
|
+
verifiedAt: number;
|
|
46
|
+
headId: string;
|
|
47
|
+
epoch: number;
|
|
48
|
+
membership: string;
|
|
49
|
+
}
|
|
50
|
+
const projectionVersion = 1;
|
|
26
51
|
export function swarmAlias(value: string): void {
|
|
27
52
|
pinName(value);
|
|
28
53
|
}
|
|
@@ -64,15 +89,24 @@ export interface NamedPin {
|
|
|
64
89
|
metadata: PinMetadata | null;
|
|
65
90
|
builtin?: true;
|
|
66
91
|
}
|
|
92
|
+
export interface ContentPin {
|
|
93
|
+
reference: string;
|
|
94
|
+
owner: string;
|
|
95
|
+
cid: string | null;
|
|
96
|
+
targetCid: string | null;
|
|
97
|
+
swarm: string | null;
|
|
98
|
+
name: string | null;
|
|
99
|
+
status: "pending" | "ready" | "deleting";
|
|
100
|
+
freshness: "current" | "stale" | "missing";
|
|
101
|
+
error: string | null;
|
|
102
|
+
}
|
|
67
103
|
export function bundlePinMetadata(pin: NamedPin) {
|
|
68
104
|
if (pin.status !== "ready" || !pin.metadata)
|
|
69
105
|
throw new HostError(
|
|
70
|
-
`
|
|
106
|
+
`Packaged reference "${pin.name}" is ${pin.status}; reinstall a verified CLI package before using it.`,
|
|
71
107
|
);
|
|
72
108
|
if (pin.metadata.kind !== "bundle")
|
|
73
|
-
throw new HostError(
|
|
74
|
-
`Pin "${pin.name}" is not a Bundle pin. Its retention is preserved; run ipab pin delete ${pin.name}, then wrap the content in a Bundle before adding a new pin.`,
|
|
75
|
-
);
|
|
109
|
+
throw new HostError(`Packaged reference "${pin.name}" is not a Bundle.`);
|
|
76
110
|
return pin.metadata;
|
|
77
111
|
}
|
|
78
112
|
|
|
@@ -138,33 +172,128 @@ function persistedHold(value: string, owner: string): Hold {
|
|
|
138
172
|
}
|
|
139
173
|
}
|
|
140
174
|
|
|
175
|
+
// SQLite can report BUSY immediately when concurrent first opens both try to
|
|
176
|
+
// upgrade their read lock to switch journal mode, even with a busy timeout.
|
|
177
|
+
function enableWal(db: DatabaseSync): void {
|
|
178
|
+
const until = performance.now() + 5000;
|
|
179
|
+
for (;;) {
|
|
180
|
+
try {
|
|
181
|
+
db.exec("PRAGMA journal_mode=WAL");
|
|
182
|
+
if (db.prepare("PRAGMA journal_mode").get()?.journal_mode !== "wal")
|
|
183
|
+
throw new HostError(
|
|
184
|
+
"Cannot enable WAL for the private state database.",
|
|
185
|
+
);
|
|
186
|
+
return;
|
|
187
|
+
} catch (error) {
|
|
188
|
+
if (
|
|
189
|
+
(((error as { errcode?: number }).errcode ?? 0) & 0xff) !== 5 ||
|
|
190
|
+
performance.now() >= until
|
|
191
|
+
)
|
|
192
|
+
throw error;
|
|
193
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
141
198
|
export class State {
|
|
142
199
|
readonly db: DatabaseSync;
|
|
143
200
|
readonly identity: string;
|
|
201
|
+
readonly directory: string;
|
|
202
|
+
readonly legacyRegistrationCount: number;
|
|
203
|
+
readonly legacyPinCount: number;
|
|
144
204
|
constructor(
|
|
145
|
-
|
|
205
|
+
directory: string,
|
|
146
206
|
private readonly builtins: readonly NamedPin[] = [],
|
|
207
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
147
208
|
) {
|
|
148
|
-
|
|
149
|
-
this.db = new DatabaseSync(
|
|
150
|
-
|
|
151
|
-
.
|
|
209
|
+
this.directory = resolve(directory);
|
|
210
|
+
this.db = new DatabaseSync(openStateStorage(this.directory, env));
|
|
211
|
+
try {
|
|
212
|
+
enableWal(this.db);
|
|
213
|
+
this.db.exec(`PRAGMA busy_timeout=5000; PRAGMA synchronous=FULL;
|
|
152
214
|
CREATE TABLE IF NOT EXISTS identity (id TEXT NOT NULL);
|
|
153
215
|
CREATE TABLE IF NOT EXISTS config (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
154
216
|
DROP TABLE IF EXISTS aliases;
|
|
155
|
-
CREATE TABLE IF NOT EXISTS
|
|
156
|
-
CREATE TABLE IF NOT EXISTS registrations (schema_cid TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
217
|
+
CREATE TABLE IF NOT EXISTS content_pins (reference TEXT PRIMARY KEY, owner TEXT UNIQUE NOT NULL, value TEXT NOT NULL);
|
|
157
218
|
CREATE TABLE IF NOT EXISTS swarms (cid TEXT PRIMARY KEY, alias TEXT UNIQUE);
|
|
158
219
|
CREATE TABLE IF NOT EXISTS swarm_headers (account TEXT NOT NULL, swarm TEXT NOT NULL, value TEXT NOT NULL, PRIMARY KEY (account,swarm));
|
|
220
|
+
CREATE TABLE IF NOT EXISTS swarm_projection_checkpoints (account TEXT NOT NULL, swarm TEXT NOT NULL, version INTEGER NOT NULL, state_cid TEXT NOT NULL, binding TEXT NOT NULL, PRIMARY KEY (account,swarm));
|
|
221
|
+
CREATE TABLE IF NOT EXISTS swarm_projection_names (account TEXT NOT NULL, swarm TEXT NOT NULL, version INTEGER NOT NULL, name TEXT NOT NULL, normalized TEXT NOT NULL, target_cid TEXT NOT NULL, PRIMARY KEY (account,swarm,name));
|
|
222
|
+
CREATE TABLE IF NOT EXISTS swarm_projection_registrations (account TEXT NOT NULL, swarm TEXT NOT NULL, version INTEGER NOT NULL, schema_cid TEXT NOT NULL, app_cid TEXT NOT NULL, argv TEXT NOT NULL, PRIMARY KEY (account,swarm,schema_cid));
|
|
159
223
|
CREATE TABLE IF NOT EXISTS runs (id TEXT PRIMARY KEY, value TEXT NOT NULL);
|
|
160
224
|
CREATE TABLE IF NOT EXISTS holds (owner TEXT PRIMARY KEY, value TEXT NOT NULL);`);
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
225
|
+
this.transaction(() => {
|
|
226
|
+
if (
|
|
227
|
+
this.db
|
|
228
|
+
.prepare(
|
|
229
|
+
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='named_pins'",
|
|
230
|
+
)
|
|
231
|
+
.get()
|
|
232
|
+
) {
|
|
233
|
+
if (
|
|
234
|
+
this.db
|
|
235
|
+
.prepare(
|
|
236
|
+
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='legacy_named_pins'",
|
|
237
|
+
)
|
|
238
|
+
.get()
|
|
239
|
+
)
|
|
240
|
+
throw new HostError(
|
|
241
|
+
"Both named_pins and legacy_named_pins exist; preserve both and resolve the archive conflict before retrying.",
|
|
242
|
+
);
|
|
243
|
+
this.db.exec("ALTER TABLE named_pins RENAME TO legacy_named_pins");
|
|
244
|
+
}
|
|
245
|
+
this.db.exec(
|
|
246
|
+
"CREATE TABLE IF NOT EXISTS legacy_named_pins (name TEXT PRIMARY KEY, owner TEXT UNIQUE NOT NULL, value TEXT NOT NULL)",
|
|
247
|
+
);
|
|
248
|
+
if (
|
|
249
|
+
this.db
|
|
250
|
+
.prepare(
|
|
251
|
+
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='registrations'",
|
|
252
|
+
)
|
|
253
|
+
.get()
|
|
254
|
+
) {
|
|
255
|
+
if (
|
|
256
|
+
this.db
|
|
257
|
+
.prepare(
|
|
258
|
+
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='legacy_user_registrations'",
|
|
259
|
+
)
|
|
260
|
+
.get()
|
|
261
|
+
)
|
|
262
|
+
throw new HostError(
|
|
263
|
+
"Both registrations and legacy_user_registrations exist in the state database. Preserve both tables and resolve the archive conflict before retrying; no User registrations were uploaded or deleted.",
|
|
264
|
+
);
|
|
265
|
+
this.db.exec(
|
|
266
|
+
"ALTER TABLE registrations RENAME TO legacy_user_registrations",
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
if (!this.db.prepare("SELECT id FROM identity").get())
|
|
270
|
+
this.db.prepare("INSERT INTO identity VALUES (?)").run(randomUUID());
|
|
271
|
+
});
|
|
272
|
+
this.legacyPinCount = Number(
|
|
273
|
+
this.db
|
|
274
|
+
.prepare("SELECT COUNT(*) AS count FROM legacy_named_pins")
|
|
275
|
+
.get()!.count,
|
|
276
|
+
);
|
|
277
|
+
this.legacyRegistrationCount = this.db
|
|
278
|
+
.prepare(
|
|
279
|
+
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='legacy_user_registrations'",
|
|
280
|
+
)
|
|
281
|
+
.get()
|
|
282
|
+
? Number(
|
|
283
|
+
this.db
|
|
284
|
+
.prepare(
|
|
285
|
+
"SELECT COUNT(*) AS count FROM legacy_user_registrations",
|
|
286
|
+
)
|
|
287
|
+
.get()!.count,
|
|
288
|
+
)
|
|
289
|
+
: 0;
|
|
290
|
+
this.identity = String(
|
|
291
|
+
this.db.prepare("SELECT id FROM identity").get()!.id,
|
|
292
|
+
);
|
|
293
|
+
} catch (error) {
|
|
294
|
+
this.db.close();
|
|
295
|
+
throw error;
|
|
296
|
+
}
|
|
168
297
|
}
|
|
169
298
|
close(): void {
|
|
170
299
|
this.db.close();
|
|
@@ -190,12 +319,6 @@ export class State {
|
|
|
190
319
|
: "SELECT key,value FROM config WHERE key != 'pointers.apiKey'",
|
|
191
320
|
)
|
|
192
321
|
.all();
|
|
193
|
-
if (
|
|
194
|
-
entries.some(
|
|
195
|
-
(row) => row.key === "pointers.apiKey" && row.value !== "null",
|
|
196
|
-
)
|
|
197
|
-
)
|
|
198
|
-
secureConfigurationStorage(this.directory, false);
|
|
199
322
|
if (entries.some((row) => row.key === "store.rpc"))
|
|
200
323
|
throw new HostError(
|
|
201
324
|
"store.rpc is no longer supported. Run ipab config reset store.rpc, then configure store.gateway with an HTTP gateway URL. Existing Kubo pins are not migrated or released.",
|
|
@@ -216,8 +339,6 @@ export class State {
|
|
|
216
339
|
}
|
|
217
340
|
}
|
|
218
341
|
setConfig(key: ConfigKey, value: ConfigValue): void {
|
|
219
|
-
if (key === "pointers.apiKey" && value !== null)
|
|
220
|
-
secureConfigurationStorage(this.directory, true);
|
|
221
342
|
this.db
|
|
222
343
|
.prepare("INSERT OR REPLACE INTO config VALUES (?,?)")
|
|
223
344
|
.run(key, JSON.stringify(value));
|
|
@@ -242,13 +363,12 @@ export class State {
|
|
|
242
363
|
}
|
|
243
364
|
resolveSwarm(reference: string): string {
|
|
244
365
|
if (/^https?:/.test(reference)) return swarmUrl(reference);
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
}
|
|
366
|
+
const entry = this.swarms().find((entry) => entry.alias === reference);
|
|
367
|
+
if (entry) return entry.cid;
|
|
368
|
+
rejectLegacySwarmCid(reference);
|
|
369
|
+
throw new HostError(
|
|
370
|
+
`Unknown swarm alias "${reference}". Run ipab swarm list.`,
|
|
371
|
+
);
|
|
252
372
|
}
|
|
253
373
|
registeredSwarm(reference: string): RegisteredSwarm {
|
|
254
374
|
const cid = this.resolveSwarm(reference);
|
|
@@ -261,11 +381,10 @@ export class State {
|
|
|
261
381
|
}
|
|
262
382
|
checkSwarmAlias(alias: string, cid?: string): void {
|
|
263
383
|
swarmAlias(alias);
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
)
|
|
384
|
+
const existing = this.swarms().find((entry) => entry.alias === alias);
|
|
385
|
+
if (existing && existing.cid !== cid)
|
|
267
386
|
throw new HostError(
|
|
268
|
-
`Swarm alias "${alias}" is already assigned.
|
|
387
|
+
`Swarm alias "${alias}" is already assigned to "${existing.cid}". To keep both Swarms, retry with a different --alias. To replace only this local registration, run ipab swarm remove ${alias} and retry. Removal clears a matching default but preserves credentials and remote membership.`,
|
|
269
388
|
);
|
|
270
389
|
}
|
|
271
390
|
registerSwarm(cid: string, alias?: string): RegisteredSwarm {
|
|
@@ -277,7 +396,7 @@ export class State {
|
|
|
277
396
|
this.db
|
|
278
397
|
.prepare("INSERT OR REPLACE INTO swarms VALUES (?,?)")
|
|
279
398
|
.run(cid, entry.alias);
|
|
280
|
-
if (this.config()["swarm.default"] == null)
|
|
399
|
+
if (this.config({ includeSecrets: false })["swarm.default"] == null)
|
|
281
400
|
this.setConfig("swarm.default", cid);
|
|
282
401
|
return entry;
|
|
283
402
|
});
|
|
@@ -293,7 +412,8 @@ export class State {
|
|
|
293
412
|
return this.transaction(() => {
|
|
294
413
|
const entry = this.registeredSwarm(reference);
|
|
295
414
|
this.db.prepare("DELETE FROM swarms WHERE cid=?").run(entry.cid);
|
|
296
|
-
|
|
415
|
+
this.deleteSwarmCache(entry.cid);
|
|
416
|
+
if (this.config({ includeSecrets: false })["swarm.default"] === entry.cid)
|
|
297
417
|
this.resetConfig("swarm.default");
|
|
298
418
|
return entry.cid;
|
|
299
419
|
});
|
|
@@ -315,7 +435,185 @@ export class State {
|
|
|
315
435
|
.run(account, swarm, JSON.stringify(value));
|
|
316
436
|
}
|
|
317
437
|
invalidateSwarmHeader(swarm: string): void {
|
|
438
|
+
this.transaction(() => {
|
|
439
|
+
this.deleteSwarmCache(swarm);
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
private deleteSwarmCache(swarm: string): void {
|
|
318
443
|
this.db.prepare("DELETE FROM swarm_headers WHERE swarm=?").run(swarm);
|
|
444
|
+
for (const table of [
|
|
445
|
+
"swarm_projection_checkpoints",
|
|
446
|
+
"swarm_projection_names",
|
|
447
|
+
"swarm_projection_registrations",
|
|
448
|
+
])
|
|
449
|
+
this.db.prepare(`DELETE FROM ${table} WHERE swarm=?`).run(swarm);
|
|
450
|
+
}
|
|
451
|
+
private checkProjectionHeader(binding: SwarmProjection): void {
|
|
452
|
+
const cached = this.swarmHeader(binding.account, binding.swarm) as
|
|
453
|
+
| {
|
|
454
|
+
head: { root: string; cid: string };
|
|
455
|
+
stateCid?: string;
|
|
456
|
+
verifiedAt: number;
|
|
457
|
+
headId: string;
|
|
458
|
+
epoch?: number;
|
|
459
|
+
}
|
|
460
|
+
| undefined;
|
|
461
|
+
if (
|
|
462
|
+
!cached ||
|
|
463
|
+
cached.head.root !== binding.root ||
|
|
464
|
+
cached.head.cid !== binding.valueCid ||
|
|
465
|
+
cached.verifiedAt !== binding.verifiedAt ||
|
|
466
|
+
cached.headId !== binding.headId ||
|
|
467
|
+
(cached.epoch !== undefined && cached.epoch !== binding.epoch) ||
|
|
468
|
+
(cached.stateCid !== undefined && cached.stateCid !== binding.stateCid)
|
|
469
|
+
)
|
|
470
|
+
throw new HostError("Swarm header changed; retry the verified read.");
|
|
471
|
+
}
|
|
472
|
+
projectSwarm(
|
|
473
|
+
binding: SwarmProjection,
|
|
474
|
+
names: Readonly<Record<string, CID>>,
|
|
475
|
+
registrations: () => readonly Association[],
|
|
476
|
+
): void {
|
|
477
|
+
this.transaction(() => {
|
|
478
|
+
this.checkProjectionHeader(binding);
|
|
479
|
+
const cached = this.swarmHeader(binding.account, binding.swarm) as Record<
|
|
480
|
+
string,
|
|
481
|
+
unknown
|
|
482
|
+
>;
|
|
483
|
+
if (cached.stateCid === undefined)
|
|
484
|
+
this.saveSwarmHeader(binding.account, binding.swarm, {
|
|
485
|
+
...cached,
|
|
486
|
+
stateCid: binding.stateCid,
|
|
487
|
+
});
|
|
488
|
+
const old = this.db
|
|
489
|
+
.prepare(
|
|
490
|
+
"SELECT version,state_cid,binding FROM swarm_projection_checkpoints WHERE account=? AND swarm=?",
|
|
491
|
+
)
|
|
492
|
+
.get(binding.account, binding.swarm);
|
|
493
|
+
const encoded = JSON.stringify(binding);
|
|
494
|
+
if (old?.version === projectionVersion && old.binding === encoded) return;
|
|
495
|
+
// A new Publication may reference the same State: only provenance changes.
|
|
496
|
+
if (
|
|
497
|
+
old?.version !== projectionVersion ||
|
|
498
|
+
old.state_cid !== binding.stateCid
|
|
499
|
+
) {
|
|
500
|
+
for (const table of [
|
|
501
|
+
"swarm_projection_names",
|
|
502
|
+
"swarm_projection_registrations",
|
|
503
|
+
])
|
|
504
|
+
this.db
|
|
505
|
+
.prepare(`DELETE FROM ${table} WHERE account=? AND swarm=?`)
|
|
506
|
+
.run(binding.account, binding.swarm);
|
|
507
|
+
const insertName = this.db.prepare(
|
|
508
|
+
"INSERT INTO swarm_projection_names VALUES (?,?,?,?,?,?)",
|
|
509
|
+
);
|
|
510
|
+
for (const [name, target] of Object.entries(names))
|
|
511
|
+
insertName.run(
|
|
512
|
+
binding.account,
|
|
513
|
+
binding.swarm,
|
|
514
|
+
projectionVersion,
|
|
515
|
+
name,
|
|
516
|
+
name.toLowerCase(),
|
|
517
|
+
target.toString(),
|
|
518
|
+
);
|
|
519
|
+
const insertRegistration = this.db.prepare(
|
|
520
|
+
"INSERT INTO swarm_projection_registrations VALUES (?,?,?,?,?,?)",
|
|
521
|
+
);
|
|
522
|
+
for (const item of registrations())
|
|
523
|
+
insertRegistration.run(
|
|
524
|
+
binding.account,
|
|
525
|
+
binding.swarm,
|
|
526
|
+
projectionVersion,
|
|
527
|
+
item.schemaCid,
|
|
528
|
+
item.appCid,
|
|
529
|
+
JSON.stringify(item.argv),
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
this.db
|
|
533
|
+
.prepare(
|
|
534
|
+
"INSERT OR REPLACE INTO swarm_projection_checkpoints VALUES (?,?,?,?,?)",
|
|
535
|
+
)
|
|
536
|
+
.run(
|
|
537
|
+
binding.account,
|
|
538
|
+
binding.swarm,
|
|
539
|
+
projectionVersion,
|
|
540
|
+
binding.stateCid,
|
|
541
|
+
encoded,
|
|
542
|
+
);
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
private checkProjection(binding: SwarmProjection): void {
|
|
546
|
+
this.checkProjectionHeader(binding);
|
|
547
|
+
if (
|
|
548
|
+
!this.db
|
|
549
|
+
.prepare(
|
|
550
|
+
"SELECT 1 FROM swarm_projection_checkpoints WHERE account=? AND swarm=? AND version=? AND binding=?",
|
|
551
|
+
)
|
|
552
|
+
.get(
|
|
553
|
+
binding.account,
|
|
554
|
+
binding.swarm,
|
|
555
|
+
projectionVersion,
|
|
556
|
+
JSON.stringify(binding),
|
|
557
|
+
)
|
|
558
|
+
)
|
|
559
|
+
throw new HostError("Swarm projection changed; retry the verified read.");
|
|
560
|
+
}
|
|
561
|
+
searchSwarmNames(
|
|
562
|
+
binding: SwarmProjection,
|
|
563
|
+
query: string,
|
|
564
|
+
): { name: string; targetCid: string }[] {
|
|
565
|
+
return this.transaction(() => {
|
|
566
|
+
this.checkProjection(binding);
|
|
567
|
+
return this.db
|
|
568
|
+
.prepare(
|
|
569
|
+
"SELECT name,target_cid FROM swarm_projection_names WHERE account=? AND swarm=? AND version=? AND instr(normalized,?)>0",
|
|
570
|
+
)
|
|
571
|
+
.all(
|
|
572
|
+
binding.account,
|
|
573
|
+
binding.swarm,
|
|
574
|
+
projectionVersion,
|
|
575
|
+
query.toLowerCase(),
|
|
576
|
+
)
|
|
577
|
+
.map((row) => ({
|
|
578
|
+
name: String(row.name),
|
|
579
|
+
targetCid: String(row.target_cid),
|
|
580
|
+
}))
|
|
581
|
+
.sort((left, right) =>
|
|
582
|
+
left.name < right.name ? -1 : left.name > right.name ? 1 : 0,
|
|
583
|
+
);
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
projectedSwarmRegistrations(
|
|
587
|
+
binding: SwarmProjection,
|
|
588
|
+
schemaCid?: string,
|
|
589
|
+
): Association[] {
|
|
590
|
+
return this.transaction(() => {
|
|
591
|
+
this.checkProjection(binding);
|
|
592
|
+
const rows =
|
|
593
|
+
schemaCid === undefined
|
|
594
|
+
? this.db
|
|
595
|
+
.prepare(
|
|
596
|
+
"SELECT schema_cid,app_cid,argv FROM swarm_projection_registrations WHERE account=? AND swarm=? AND version=?",
|
|
597
|
+
)
|
|
598
|
+
.all(binding.account, binding.swarm, projectionVersion)
|
|
599
|
+
: this.db
|
|
600
|
+
.prepare(
|
|
601
|
+
"SELECT schema_cid,app_cid,argv FROM swarm_projection_registrations WHERE account=? AND swarm=? AND version=? AND schema_cid=?",
|
|
602
|
+
)
|
|
603
|
+
.all(
|
|
604
|
+
binding.account,
|
|
605
|
+
binding.swarm,
|
|
606
|
+
projectionVersion,
|
|
607
|
+
schemaCid,
|
|
608
|
+
);
|
|
609
|
+
return rows
|
|
610
|
+
.map((row) => ({
|
|
611
|
+
schemaCid: String(row.schema_cid),
|
|
612
|
+
appCid: String(row.app_cid),
|
|
613
|
+
argv: JSON.parse(String(row.argv)) as string[],
|
|
614
|
+
}))
|
|
615
|
+
.sort((left, right) => left.schemaCid.localeCompare(right.schemaCid));
|
|
616
|
+
});
|
|
319
617
|
}
|
|
320
618
|
resolve(value: string, schema = false): string {
|
|
321
619
|
try {
|
|
@@ -324,43 +622,26 @@ export class State {
|
|
|
324
622
|
try {
|
|
325
623
|
CID.parse(value);
|
|
326
624
|
} catch {
|
|
327
|
-
const pin = this.pin(value);
|
|
625
|
+
const pin = value.startsWith("builtin:") ? this.pin(value) : undefined;
|
|
328
626
|
if (pin) {
|
|
329
627
|
const metadata = bundlePinMetadata(pin);
|
|
330
628
|
if (schema) return metadata.schemaCid;
|
|
331
629
|
return exactCid(pin.cid, schema);
|
|
332
630
|
}
|
|
333
631
|
throw new HostError(
|
|
334
|
-
`Unknown CID or
|
|
632
|
+
`Unknown CID or packaged builtin reference "${value}". Local content aliases are unsupported; use an exact CID or a Swarm IPAB URI.`,
|
|
335
633
|
);
|
|
336
634
|
}
|
|
337
635
|
throw error;
|
|
338
636
|
}
|
|
339
637
|
}
|
|
340
|
-
resolvePin(name: string): string | undefined {
|
|
341
|
-
const pin = this.pin(name);
|
|
342
|
-
if (!pin) return undefined;
|
|
343
|
-
bundlePinMetadata(pin);
|
|
344
|
-
return exactCid(pin.cid);
|
|
345
|
-
}
|
|
346
638
|
pins(): NamedPin[] {
|
|
347
|
-
|
|
348
|
-
.prepare("SELECT name,owner,value FROM named_pins ORDER BY name")
|
|
349
|
-
.all()
|
|
350
|
-
.map((row) =>
|
|
351
|
-
this.readPin(String(row.name), String(row.owner), String(row.value)),
|
|
352
|
-
);
|
|
353
|
-
return [...user, ...this.builtins];
|
|
639
|
+
return [...this.builtins];
|
|
354
640
|
}
|
|
355
641
|
pin(name: string): NamedPin | undefined {
|
|
356
642
|
const builtin = this.builtins.find((pin) => pin.name === name);
|
|
357
643
|
if (builtin) return builtin;
|
|
358
|
-
|
|
359
|
-
.prepare("SELECT owner,value FROM named_pins WHERE name=?")
|
|
360
|
-
.get(name);
|
|
361
|
-
return row
|
|
362
|
-
? this.readPin(name, String(row.owner), String(row.value))
|
|
363
|
-
: undefined;
|
|
644
|
+
return undefined;
|
|
364
645
|
}
|
|
365
646
|
private readPin(name: string, owner: string, value: string): NamedPin {
|
|
366
647
|
const data = object(JSON.parse(value) as unknown, "named pin");
|
|
@@ -405,73 +686,84 @@ export class State {
|
|
|
405
686
|
metadata,
|
|
406
687
|
};
|
|
407
688
|
}
|
|
408
|
-
|
|
409
|
-
|
|
689
|
+
archivedPins(): NamedPin[] {
|
|
690
|
+
return this.db
|
|
691
|
+
.prepare("SELECT name,owner,value FROM legacy_named_pins ORDER BY name")
|
|
692
|
+
.all()
|
|
693
|
+
.map((row) =>
|
|
694
|
+
this.readPin(String(row.name), String(row.owner), String(row.value)),
|
|
695
|
+
);
|
|
696
|
+
}
|
|
697
|
+
contentPins(): ContentPin[] {
|
|
698
|
+
return this.db
|
|
699
|
+
.prepare(
|
|
700
|
+
"SELECT reference,owner,value FROM content_pins ORDER BY reference",
|
|
701
|
+
)
|
|
702
|
+
.all()
|
|
703
|
+
.map((row) => {
|
|
704
|
+
const pin = JSON.parse(String(row.value)) as ContentPin;
|
|
705
|
+
if (
|
|
706
|
+
pin.reference !== row.reference ||
|
|
707
|
+
pin.owner !== row.owner ||
|
|
708
|
+
!["pending", "ready", "deleting"].includes(pin.status) ||
|
|
709
|
+
!["current", "stale", "missing"].includes(pin.freshness) ||
|
|
710
|
+
(pin.error !== null && typeof pin.error !== "string") ||
|
|
711
|
+
(pin.cid !== null && typeof pin.cid !== "string") ||
|
|
712
|
+
(pin.targetCid !== null && typeof pin.targetCid !== "string") ||
|
|
713
|
+
(pin.swarm !== null && typeof pin.swarm !== "string") ||
|
|
714
|
+
(pin.name !== null && typeof pin.name !== "string")
|
|
715
|
+
)
|
|
716
|
+
throw new HostError(
|
|
717
|
+
"Invalid persisted content pin; recovery is blocked.",
|
|
718
|
+
);
|
|
719
|
+
if (pin.cid) exactCid(pin.cid);
|
|
720
|
+
if (pin.targetCid) exactCid(pin.targetCid);
|
|
721
|
+
if (pin.swarm) swarmUrl(pin.swarm);
|
|
722
|
+
if (
|
|
723
|
+
(pin.swarm === null) !== (pin.name === null) ||
|
|
724
|
+
(pin.swarm === null &&
|
|
725
|
+
(exactCid(pin.reference) !== pin.reference ||
|
|
726
|
+
(pin.cid !== null && pin.cid !== pin.reference))) ||
|
|
727
|
+
(pin.swarm !== null &&
|
|
728
|
+
(!pin.name || !pin.reference.startsWith("ipab://"))) ||
|
|
729
|
+
(pin.status === "ready" && (!pin.cid || !pin.targetCid))
|
|
730
|
+
)
|
|
731
|
+
throw new HostError(
|
|
732
|
+
"Invalid persisted pin reference/version binding; recovery is blocked.",
|
|
733
|
+
);
|
|
734
|
+
return pin;
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
contentPin(reference: string): ContentPin | undefined {
|
|
738
|
+
return this.contentPins().find((pin) => pin.reference === reference);
|
|
739
|
+
}
|
|
740
|
+
saveContentPin(pin: ContentPin): void {
|
|
410
741
|
if (
|
|
411
742
|
!this.db
|
|
412
743
|
.prepare(
|
|
413
|
-
"INSERT INTO
|
|
744
|
+
"INSERT INTO content_pins VALUES (?,?,?) ON CONFLICT(reference) DO UPDATE SET value=excluded.value WHERE owner=excluded.owner",
|
|
414
745
|
)
|
|
415
|
-
.run(pin.
|
|
746
|
+
.run(pin.reference, pin.owner, JSON.stringify(pin)).changes
|
|
416
747
|
)
|
|
417
|
-
throw new HostError(
|
|
748
|
+
throw new HostError("Content pin reference is occupied.");
|
|
749
|
+
}
|
|
750
|
+
deleteContentPin(reference: string): void {
|
|
751
|
+
this.db
|
|
752
|
+
.prepare("DELETE FROM content_pins WHERE reference=?")
|
|
753
|
+
.run(reference);
|
|
418
754
|
}
|
|
419
755
|
isPinOwner(owner: string): boolean {
|
|
420
756
|
return Boolean(
|
|
421
|
-
this.db
|
|
757
|
+
this.db
|
|
758
|
+
.prepare(
|
|
759
|
+
"SELECT 1 FROM content_pins WHERE owner=? UNION ALL SELECT 1 FROM legacy_named_pins WHERE owner=?",
|
|
760
|
+
)
|
|
761
|
+
.get(owner, owner),
|
|
422
762
|
);
|
|
423
763
|
}
|
|
424
764
|
deletePin(name: string): void {
|
|
425
765
|
pinName(name);
|
|
426
|
-
this.db.prepare("DELETE FROM
|
|
427
|
-
}
|
|
428
|
-
registrations(): Association[] {
|
|
429
|
-
return this.db
|
|
430
|
-
.prepare("SELECT value FROM registrations ORDER BY schema_cid")
|
|
431
|
-
.all()
|
|
432
|
-
.map((row) => JSON.parse(String(row.value)) as Association);
|
|
433
|
-
}
|
|
434
|
-
association(schemaCid: string): Association | undefined {
|
|
435
|
-
const row = this.db
|
|
436
|
-
.prepare("SELECT value FROM registrations WHERE schema_cid=?")
|
|
437
|
-
.get(schemaCid);
|
|
438
|
-
return row ? (JSON.parse(String(row.value)) as Association) : undefined;
|
|
439
|
-
}
|
|
440
|
-
setAssociation(
|
|
441
|
-
schema: string,
|
|
442
|
-
app: string,
|
|
443
|
-
argv: string[],
|
|
444
|
-
replace: boolean,
|
|
445
|
-
): Association {
|
|
446
|
-
const value: Association = {
|
|
447
|
-
schemaCid: this.resolve(schema, true),
|
|
448
|
-
appCid: this.resolve(app),
|
|
449
|
-
argv,
|
|
450
|
-
};
|
|
451
|
-
if (!argv.length || !argv.includes("%"))
|
|
452
|
-
syntax('Argument template must contain a whole "%" argument.');
|
|
453
|
-
this.transaction(() => {
|
|
454
|
-
const old = this.association(value.schemaCid);
|
|
455
|
-
if (old && JSON.stringify(old) !== JSON.stringify(value) && !replace)
|
|
456
|
-
throw new HostError(
|
|
457
|
-
"Schema has another association; use --replace before --.",
|
|
458
|
-
);
|
|
459
|
-
this.db
|
|
460
|
-
.prepare("INSERT OR REPLACE INTO registrations VALUES (?,?)")
|
|
461
|
-
.run(value.schemaCid, JSON.stringify(value));
|
|
462
|
-
});
|
|
463
|
-
return value;
|
|
464
|
-
}
|
|
465
|
-
removeAssociation(schema: string): string {
|
|
466
|
-
const cid = this.resolve(schema, true);
|
|
467
|
-
if (
|
|
468
|
-
!this.db.prepare("DELETE FROM registrations WHERE schema_cid=?").run(cid)
|
|
469
|
-
.changes
|
|
470
|
-
)
|
|
471
|
-
throw new HostError(
|
|
472
|
-
"No association exists for this Schema. Run ipab register list.",
|
|
473
|
-
);
|
|
474
|
-
return cid;
|
|
766
|
+
this.db.prepare("DELETE FROM legacy_named_pins WHERE name=?").run(name);
|
|
475
767
|
}
|
|
476
768
|
runs(): RunRecord[] {
|
|
477
769
|
return this.db
|
package/src/swarm-envelope.ts
CHANGED
|
@@ -25,6 +25,20 @@ export interface EncryptionIdentity {
|
|
|
25
25
|
publicKey: string;
|
|
26
26
|
privateKey: string;
|
|
27
27
|
}
|
|
28
|
+
export function validateEncryptionIdentity(value: EncryptionIdentity): void {
|
|
29
|
+
const privateKey = createPrivateKey({
|
|
30
|
+
key: Buffer.from(value.privateKey, "base64url"),
|
|
31
|
+
type: "pkcs8",
|
|
32
|
+
format: "der",
|
|
33
|
+
});
|
|
34
|
+
if (
|
|
35
|
+
privateKey.asymmetricKeyType !== "x25519" ||
|
|
36
|
+
createPublicKey(privateKey)
|
|
37
|
+
.export({ type: "spki", format: "der" })
|
|
38
|
+
.toString("base64url") !== value.publicKey
|
|
39
|
+
)
|
|
40
|
+
throw new HostError("Stored encryption identity is inconsistent.");
|
|
41
|
+
}
|
|
28
42
|
export function encryptionIdentity(): EncryptionIdentity {
|
|
29
43
|
const keys = generateKeyPairSync("x25519");
|
|
30
44
|
return {
|