@ipab/cli 0.1.0-rc.5 → 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 +82 -15
- 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 +3 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +561 -272
- package/dist/cli.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.d.ts +0 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +0 -10
- 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 +1 -2
- package/dist/ipc.d.ts.map +1 -1
- package/dist/ipc.js +1 -10
- package/dist/ipc.js.map +1 -1
- package/dist/kubo-install.d.ts +18 -1
- package/dist/kubo-install.d.ts.map +1 -1
- package/dist/kubo-install.js +424 -61
- package/dist/kubo-install.js.map +1 -1
- package/dist/kubo-release.d.ts +19 -0
- package/dist/kubo-release.d.ts.map +1 -1
- package/dist/kubo-release.js +164 -0
- package/dist/kubo-release.js.map +1 -1
- package/dist/kubo.d.ts +1 -0
- package/dist/kubo.d.ts.map +1 -1
- package/dist/kubo.js +102 -96
- 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.map +1 -1
- package/dist/runtime.js +1 -3
- package/dist/runtime.js.map +1 -1
- package/dist/state-storage-windows.js +1 -1
- package/dist/state-storage.d.ts +3 -1
- package/dist/state-storage.d.ts.map +1 -1
- package/dist/state-storage.js +38 -7
- package/dist/state-storage.js.map +1 -1
- package/dist/state.d.ts +40 -6
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +208 -82
- 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.d.ts +64 -8
- package/dist/swarm.d.ts.map +1 -1
- package/dist/swarm.js +858 -106
- package/dist/swarm.js.map +1 -1
- package/package.json +4 -4
- package/src/app-templates.ts +1 -1
- package/src/cli.ts +702 -338
- package/src/content-provider.ts +4 -2
- package/src/context.ts +0 -10
- package/src/doctor.ts +902 -0
- package/src/identity.ts +10 -10
- package/src/ipc.ts +0 -8
- package/src/kubo-install.ts +562 -78
- package/src/kubo-release.ts +213 -0
- package/src/kubo.ts +134 -106
- package/src/pins.ts +188 -126
- package/src/publish-source.ts +103 -0
- package/src/runtime.ts +0 -3
- package/src/state-storage-windows.ts +1 -1
- package/src/state-storage.ts +53 -7
- package/src/state.ts +357 -98
- package/src/swarm-envelope.ts +14 -0
- package/src/swarm.ts +1234 -125
- package/dist/setup.d.ts +0 -8
- package/dist/setup.d.ts.map +0 -1
- package/dist/setup.js +0 -27
- package/dist/setup.js.map +0 -1
- package/src/setup.ts +0 -34
package/src/swarm.ts
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createReadStream } from "node:fs";
|
|
2
|
+
import { lstat, mkdir, open } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join, resolve } from "node:path";
|
|
2
5
|
import { createHash, randomBytes } from "node:crypto";
|
|
3
6
|
import { stringify } from "yaml";
|
|
4
7
|
import { CarBlockIterator } from "@ipld/car/iterator";
|
|
5
8
|
import * as dagCbor from "@ipld/dag-cbor";
|
|
6
9
|
import { CID } from "multiformats/cid";
|
|
7
10
|
import { sha256 } from "multiformats/hashes/sha2";
|
|
8
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
ContentBundleError,
|
|
13
|
+
encodeSchema,
|
|
14
|
+
openBundle,
|
|
15
|
+
type BundleType,
|
|
16
|
+
} from "@ipab/bundle";
|
|
9
17
|
import {
|
|
10
18
|
encodeBundleCar,
|
|
11
19
|
memoryTree,
|
|
@@ -35,12 +43,31 @@ import {
|
|
|
35
43
|
verifyEd25519,
|
|
36
44
|
} from "@ipab/bundle/pointer/crypto";
|
|
37
45
|
import { HostError, object, message } from "./errors.js";
|
|
38
|
-
import {
|
|
46
|
+
import {
|
|
47
|
+
accountIdentity,
|
|
48
|
+
protectedPath,
|
|
49
|
+
secureNew,
|
|
50
|
+
type Identity,
|
|
51
|
+
} from "./identity.js";
|
|
52
|
+
import { nodeError } from "./authoring.js";
|
|
39
53
|
import { swarmUrl as canonicalSwarmUrl } from "./config.js";
|
|
40
54
|
import { bundleJson, provideContent } from "./content-provider.js";
|
|
55
|
+
import {
|
|
56
|
+
failedPin,
|
|
57
|
+
pinCid,
|
|
58
|
+
refreshPinClaim,
|
|
59
|
+
reservePin,
|
|
60
|
+
retainPin,
|
|
61
|
+
} from "./pins.js";
|
|
41
62
|
import { bytes, relativePath } from "./space.js";
|
|
42
63
|
import { appSchemaCid } from "./app-definition.js";
|
|
43
|
-
import {
|
|
64
|
+
import {
|
|
65
|
+
State,
|
|
66
|
+
createAssociation,
|
|
67
|
+
exactCid,
|
|
68
|
+
type Association,
|
|
69
|
+
type SwarmProjection,
|
|
70
|
+
} from "./state.js";
|
|
44
71
|
import {
|
|
45
72
|
privateDirectory,
|
|
46
73
|
readPrivate,
|
|
@@ -56,6 +83,7 @@ import {
|
|
|
56
83
|
type EnvelopeBinding,
|
|
57
84
|
type MembershipSecret,
|
|
58
85
|
parseEnvelope,
|
|
86
|
+
validateEncryptionIdentity,
|
|
59
87
|
} from "./swarm-envelope.js";
|
|
60
88
|
import {
|
|
61
89
|
checkCachedNode,
|
|
@@ -71,6 +99,7 @@ import {
|
|
|
71
99
|
import { memberPeers } from "./kubo-peering.js";
|
|
72
100
|
|
|
73
101
|
export interface Member {
|
|
102
|
+
memberId?: string;
|
|
74
103
|
publicKey: string;
|
|
75
104
|
encryptionKey: string;
|
|
76
105
|
peerId: string;
|
|
@@ -79,7 +108,7 @@ export interface Member {
|
|
|
79
108
|
approval: Envelope | null;
|
|
80
109
|
}
|
|
81
110
|
export interface Organization {
|
|
82
|
-
v: 1;
|
|
111
|
+
v: 1 | 2;
|
|
83
112
|
scopeId: string;
|
|
84
113
|
owner: string;
|
|
85
114
|
name: string;
|
|
@@ -91,12 +120,20 @@ export interface Organization {
|
|
|
91
120
|
}
|
|
92
121
|
export interface Profile {
|
|
93
122
|
name: string;
|
|
123
|
+
description?: string;
|
|
94
124
|
addresses: string[];
|
|
95
125
|
updatedAt: string;
|
|
96
126
|
}
|
|
127
|
+
export interface AccountProfile {
|
|
128
|
+
publicKey: string;
|
|
129
|
+
name: string;
|
|
130
|
+
description: string;
|
|
131
|
+
updatedAt: string | null;
|
|
132
|
+
}
|
|
97
133
|
export interface Snapshot {
|
|
98
134
|
v: 2;
|
|
99
135
|
names: Record<string, CID>;
|
|
136
|
+
registrations?: Record<string, { appCid: CID; argv: string[] }>;
|
|
100
137
|
}
|
|
101
138
|
export interface MembershipRequest {
|
|
102
139
|
v: 1;
|
|
@@ -146,6 +183,67 @@ interface LocalMembership {
|
|
|
146
183
|
config?: Organization;
|
|
147
184
|
secret?: MembershipSecret;
|
|
148
185
|
}
|
|
186
|
+
export async function inspectLocalMembership(
|
|
187
|
+
input: unknown,
|
|
188
|
+
account: string | undefined,
|
|
189
|
+
) {
|
|
190
|
+
const value = object(input, "local membership");
|
|
191
|
+
if (
|
|
192
|
+
typeof value.configUrl !== "string" ||
|
|
193
|
+
!isCanonicalPublicKey(value.owner) ||
|
|
194
|
+
!isCanonicalPublicKey(value.account) ||
|
|
195
|
+
value.account !== account
|
|
196
|
+
)
|
|
197
|
+
throw new HostError(
|
|
198
|
+
"Stored membership is inconsistent with HOME identity.",
|
|
199
|
+
);
|
|
200
|
+
const configUrl = canonicalSwarmUrl(value.configUrl);
|
|
201
|
+
if (configUrl !== value.configUrl)
|
|
202
|
+
throw new HostError("Noncanonical membership URL.");
|
|
203
|
+
const encryption = object(value.encryption, "encryption identity");
|
|
204
|
+
if (
|
|
205
|
+
typeof encryption.privateKey !== "string" ||
|
|
206
|
+
typeof encryption.publicKey !== "string"
|
|
207
|
+
)
|
|
208
|
+
throw new HostError("Invalid encryption identity.");
|
|
209
|
+
validateEncryptionIdentity({
|
|
210
|
+
privateKey: encryption.privateKey,
|
|
211
|
+
publicKey: encryption.publicKey,
|
|
212
|
+
});
|
|
213
|
+
const request =
|
|
214
|
+
value.request === undefined
|
|
215
|
+
? undefined
|
|
216
|
+
: await verifyRequest(value.request);
|
|
217
|
+
if (
|
|
218
|
+
request &&
|
|
219
|
+
(request.configUrl !== configUrl ||
|
|
220
|
+
request.member !== account ||
|
|
221
|
+
request.owner !== value.owner ||
|
|
222
|
+
request.encryptionKey !== encryption.publicKey)
|
|
223
|
+
)
|
|
224
|
+
throw new HostError("Stored Request binding mismatch.");
|
|
225
|
+
const config =
|
|
226
|
+
value.config === undefined
|
|
227
|
+
? undefined
|
|
228
|
+
: parseOrganization(value.config, configUrl);
|
|
229
|
+
if (config && config.owner !== value.owner)
|
|
230
|
+
throw new HostError("Stored Owner binding mismatch.");
|
|
231
|
+
let peer = request?.peerId;
|
|
232
|
+
if (value.secret !== undefined) {
|
|
233
|
+
if (!config)
|
|
234
|
+
throw new HostError("Stored secret has no configuration binding.");
|
|
235
|
+
const member = config.members.find(
|
|
236
|
+
(member) => member.publicKey === account,
|
|
237
|
+
);
|
|
238
|
+
if (!member)
|
|
239
|
+
throw new HostError("Stored membership lacks its member binding.");
|
|
240
|
+
const record = value as unknown as LocalMembership;
|
|
241
|
+
await validateMembershipSecret(record, config, member);
|
|
242
|
+
peer = member.peerId;
|
|
243
|
+
}
|
|
244
|
+
const c = coordinates(configUrl);
|
|
245
|
+
return { configUrl, scope: nodeScope(c.service, c.scopeId), peer };
|
|
246
|
+
}
|
|
149
247
|
const cidField = { type: "string", minLength: 1, maxLength: 128 } as const;
|
|
150
248
|
export const artifactType: BundleType = {
|
|
151
249
|
contentType: "application/json",
|
|
@@ -249,7 +347,7 @@ function parseOrganization(value: unknown, configUrl: string): Organization {
|
|
|
249
347
|
if (
|
|
250
348
|
Object.keys(data).sort().join() !==
|
|
251
349
|
"bootstrap,description,epoch,headId,members,name,owner,scopeId,v" ||
|
|
252
|
-
data.v !== 1 ||
|
|
350
|
+
(data.v !== 1 && data.v !== 2) ||
|
|
253
351
|
data.scopeId !== scopeId ||
|
|
254
352
|
!isCanonicalPublicKey(data.owner) ||
|
|
255
353
|
typeof data.name !== "string" ||
|
|
@@ -265,7 +363,7 @@ function parseOrganization(value: unknown, configUrl: string): Organization {
|
|
|
265
363
|
data.bootstrap.length > 64 ||
|
|
266
364
|
!Array.isArray(data.members) ||
|
|
267
365
|
data.members.length < 1 ||
|
|
268
|
-
data.members.length >
|
|
366
|
+
data.members.length > 256
|
|
269
367
|
)
|
|
270
368
|
throw new HostError("Invalid Scope organization configuration.");
|
|
271
369
|
const identities = new Set<string>(),
|
|
@@ -275,8 +373,14 @@ function parseOrganization(value: unknown, configUrl: string): Organization {
|
|
|
275
373
|
for (const raw of data.members) {
|
|
276
374
|
const member = object(raw, "member");
|
|
277
375
|
if (
|
|
278
|
-
Object.keys(member)
|
|
376
|
+
Object.keys(member)
|
|
377
|
+
.filter((key) => key !== "memberId")
|
|
378
|
+
.sort()
|
|
379
|
+
.join() !==
|
|
279
380
|
"approval,encryptionKey,peerId,profileId,publicKey,requestId" ||
|
|
381
|
+
(member.memberId !== undefined &&
|
|
382
|
+
(typeof member.memberId !== "string" ||
|
|
383
|
+
!RESOURCE_ID_PATTERN.test(member.memberId))) ||
|
|
280
384
|
!isCanonicalPublicKey(member.publicKey) ||
|
|
281
385
|
typeof member.encryptionKey !== "string" ||
|
|
282
386
|
member.encryptionKey.length > 256 ||
|
|
@@ -296,6 +400,9 @@ function parseOrganization(value: unknown, configUrl: string): Organization {
|
|
|
296
400
|
peers.add(member.peerId);
|
|
297
401
|
profiles.add(member.profileId);
|
|
298
402
|
members.push({
|
|
403
|
+
...(typeof member.memberId === "string"
|
|
404
|
+
? { memberId: member.memberId }
|
|
405
|
+
: {}),
|
|
299
406
|
publicKey: member.publicKey,
|
|
300
407
|
encryptionKey: member.encryptionKey,
|
|
301
408
|
peerId: member.peerId,
|
|
@@ -317,7 +424,7 @@ function parseOrganization(value: unknown, configUrl: string): Organization {
|
|
|
317
424
|
bootstrap.push(address);
|
|
318
425
|
}
|
|
319
426
|
return {
|
|
320
|
-
v:
|
|
427
|
+
v: data.v,
|
|
321
428
|
scopeId,
|
|
322
429
|
owner: data.owner,
|
|
323
430
|
name: data.name,
|
|
@@ -331,9 +438,15 @@ function parseOrganization(value: unknown, configUrl: string): Organization {
|
|
|
331
438
|
function parseProfile(value: unknown, expectedPeer: string): Profile {
|
|
332
439
|
const data = object(value, "member profile");
|
|
333
440
|
if (
|
|
334
|
-
|
|
441
|
+
![
|
|
442
|
+
"addresses,name,updatedAt",
|
|
443
|
+
"addresses,description,name,updatedAt",
|
|
444
|
+
].includes(Object.keys(data).sort().join()) ||
|
|
335
445
|
typeof data.name !== "string" ||
|
|
336
446
|
data.name.length > 256 ||
|
|
447
|
+
(data.description !== undefined &&
|
|
448
|
+
(typeof data.description !== "string" ||
|
|
449
|
+
data.description.length > 2048)) ||
|
|
337
450
|
typeof data.updatedAt !== "string" ||
|
|
338
451
|
!Number.isFinite(Date.parse(data.updatedAt)) ||
|
|
339
452
|
!Array.isArray(data.addresses) ||
|
|
@@ -347,7 +460,245 @@ function parseProfile(value: unknown, expectedPeer: string): Profile {
|
|
|
347
460
|
validateAddress(address, expectedPeer);
|
|
348
461
|
addresses.push(address);
|
|
349
462
|
}
|
|
350
|
-
return {
|
|
463
|
+
return {
|
|
464
|
+
name: data.name,
|
|
465
|
+
...(typeof data.description === "string"
|
|
466
|
+
? { description: data.description }
|
|
467
|
+
: {}),
|
|
468
|
+
addresses,
|
|
469
|
+
updatedAt: data.updatedAt,
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
export async function swarmMembers(context: SwarmContext, url: string) {
|
|
473
|
+
const { config } = await fetchConfig(context, canonicalSwarmUrl(url));
|
|
474
|
+
const c = coordinates(url),
|
|
475
|
+
api = client(context, c.service);
|
|
476
|
+
const members = await boundedMap(config.members, async (member) => {
|
|
477
|
+
const profile = parseProfile(
|
|
478
|
+
(await api.read(c.scopeId, member.profileId)).value,
|
|
479
|
+
member.peerId,
|
|
480
|
+
);
|
|
481
|
+
return {
|
|
482
|
+
publicKey: member.publicKey,
|
|
483
|
+
role: member.publicKey === config.owner ? "owner" : "member",
|
|
484
|
+
name: profile.name,
|
|
485
|
+
description: profile.description ?? "",
|
|
486
|
+
};
|
|
487
|
+
});
|
|
488
|
+
return { configUrl: url, members };
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
function accountProfileLocation(context: SwarmContext, publicKey: string) {
|
|
492
|
+
return {
|
|
493
|
+
parent: join(
|
|
494
|
+
resolve(context.env.HOME ?? context.env.USERPROFILE ?? homedir()),
|
|
495
|
+
".ipab",
|
|
496
|
+
),
|
|
497
|
+
name: `${createHash("sha256").update(publicKey).digest("hex")}.json`,
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
async function storedAccountProfile(
|
|
501
|
+
context: SwarmContext,
|
|
502
|
+
): Promise<AccountProfile | undefined> {
|
|
503
|
+
const identity = await contextIdentity(context);
|
|
504
|
+
const { parent, name } = accountProfileLocation(context, identity.publicKey);
|
|
505
|
+
try {
|
|
506
|
+
await lstat(join(parent, "profiles"));
|
|
507
|
+
} catch (cause) {
|
|
508
|
+
if (nodeError(cause, "ENOENT")) return undefined;
|
|
509
|
+
throw cause;
|
|
510
|
+
}
|
|
511
|
+
await protectedPath(parent, true);
|
|
512
|
+
await protectedPath(join(parent, "profiles"), true);
|
|
513
|
+
const value = await readPrivate<unknown>(join(parent, "profiles"), name);
|
|
514
|
+
if (value === undefined) return undefined;
|
|
515
|
+
const data = object(value, "account profile");
|
|
516
|
+
if (
|
|
517
|
+
Object.keys(data).sort().join() !==
|
|
518
|
+
"description,name,publicKey,updatedAt" ||
|
|
519
|
+
data.publicKey !== identity.publicKey ||
|
|
520
|
+
typeof data.name !== "string" ||
|
|
521
|
+
data.name.length > 256 ||
|
|
522
|
+
typeof data.description !== "string" ||
|
|
523
|
+
data.description.length > 2048 ||
|
|
524
|
+
typeof data.updatedAt !== "string" ||
|
|
525
|
+
!Number.isFinite(Date.parse(data.updatedAt))
|
|
526
|
+
)
|
|
527
|
+
throw new HostError(
|
|
528
|
+
"Invalid protected account profile; restore the existing profile file.",
|
|
529
|
+
);
|
|
530
|
+
return {
|
|
531
|
+
publicKey: identity.publicKey,
|
|
532
|
+
name: data.name,
|
|
533
|
+
description: data.description,
|
|
534
|
+
updatedAt: data.updatedAt,
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
async function accountProfileDirectory(
|
|
538
|
+
context: SwarmContext,
|
|
539
|
+
publicKey: string,
|
|
540
|
+
) {
|
|
541
|
+
const { parent } = accountProfileLocation(context, publicKey);
|
|
542
|
+
try {
|
|
543
|
+
await lstat(parent);
|
|
544
|
+
} catch (cause) {
|
|
545
|
+
if (!nodeError(cause, "ENOENT")) throw cause;
|
|
546
|
+
await mkdir(parent, { mode: 0o700 });
|
|
547
|
+
await secureNew(parent, true);
|
|
548
|
+
}
|
|
549
|
+
await protectedPath(parent, true);
|
|
550
|
+
return privateDirectory(parent, "profiles");
|
|
551
|
+
}
|
|
552
|
+
async function initialAccountProfile(
|
|
553
|
+
context: SwarmContext,
|
|
554
|
+
changes: { name?: string; description?: string } = {},
|
|
555
|
+
): Promise<AccountProfile> {
|
|
556
|
+
const identity = await contextIdentity(context);
|
|
557
|
+
if (changes.name !== undefined && changes.description !== undefined)
|
|
558
|
+
return {
|
|
559
|
+
publicKey: identity.publicKey,
|
|
560
|
+
name: changes.name,
|
|
561
|
+
description: changes.description,
|
|
562
|
+
updatedAt: null,
|
|
563
|
+
};
|
|
564
|
+
const legacy: Profile[] = [];
|
|
565
|
+
for (const entry of withState(context, (state) => state.swarms())) {
|
|
566
|
+
const { config } = await fetchConfig(context, entry.cid);
|
|
567
|
+
const self = config.members.find(
|
|
568
|
+
(member) => member.publicKey === identity.publicKey,
|
|
569
|
+
);
|
|
570
|
+
if (!self) continue; // Pending requests have no public profile yet.
|
|
571
|
+
const c = coordinates(entry.cid);
|
|
572
|
+
legacy.push(
|
|
573
|
+
parseProfile(
|
|
574
|
+
(await client(context, c.service).read(c.scopeId, self.profileId))
|
|
575
|
+
.value,
|
|
576
|
+
self.peerId,
|
|
577
|
+
),
|
|
578
|
+
);
|
|
579
|
+
}
|
|
580
|
+
const field = (key: "name" | "description") => {
|
|
581
|
+
if (changes[key] !== undefined) return changes[key];
|
|
582
|
+
const values = new Set(legacy.map((profile) => profile[key] ?? ""));
|
|
583
|
+
if (values.size > 1)
|
|
584
|
+
throw new HostError(
|
|
585
|
+
`Legacy Swarm profiles disagree on ${key}. Run ipab profile --${key} TEXT to choose your account-wide value; no profile has been saved.`,
|
|
586
|
+
);
|
|
587
|
+
return values.values().next().value ?? "";
|
|
588
|
+
};
|
|
589
|
+
return {
|
|
590
|
+
publicKey: identity.publicKey,
|
|
591
|
+
name: field("name"),
|
|
592
|
+
description: field("description"),
|
|
593
|
+
updatedAt: null,
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
export async function accountProfile(
|
|
597
|
+
context: SwarmContext,
|
|
598
|
+
): Promise<AccountProfile> {
|
|
599
|
+
return (
|
|
600
|
+
(await storedAccountProfile(context)) ??
|
|
601
|
+
(await initialAccountProfile(context))
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
async function ensureAccountProfile(
|
|
605
|
+
context: SwarmContext,
|
|
606
|
+
changes: { name?: string; description?: string } = {},
|
|
607
|
+
) {
|
|
608
|
+
const identity = await contextIdentity(context);
|
|
609
|
+
const dir = await accountProfileDirectory(context, identity.publicKey);
|
|
610
|
+
return withPrivateLock(dir, async () => {
|
|
611
|
+
const existing = await storedAccountProfile(context);
|
|
612
|
+
if (
|
|
613
|
+
existing &&
|
|
614
|
+
changes.name === undefined &&
|
|
615
|
+
changes.description === undefined
|
|
616
|
+
)
|
|
617
|
+
return existing;
|
|
618
|
+
const profile = {
|
|
619
|
+
...(existing ?? (await initialAccountProfile(context, changes))),
|
|
620
|
+
...changes,
|
|
621
|
+
updatedAt: new Date().toISOString(),
|
|
622
|
+
};
|
|
623
|
+
await writePrivate(
|
|
624
|
+
dir,
|
|
625
|
+
accountProfileLocation(context, identity.publicKey).name,
|
|
626
|
+
profile,
|
|
627
|
+
);
|
|
628
|
+
return profile;
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
async function syncAccountProfile(
|
|
632
|
+
context: SwarmContext,
|
|
633
|
+
record: LocalMembership,
|
|
634
|
+
personal: AccountProfile,
|
|
635
|
+
fetched?: { profile: Profile; cid: string },
|
|
636
|
+
): Promise<Profile> {
|
|
637
|
+
const identity = await contextIdentity(context),
|
|
638
|
+
c = coordinates(record.configUrl);
|
|
639
|
+
const self = record.config!.members.find(
|
|
640
|
+
(member) => member.publicKey === identity.publicKey,
|
|
641
|
+
)!;
|
|
642
|
+
const api = client(
|
|
643
|
+
context,
|
|
644
|
+
c.service,
|
|
645
|
+
identity,
|
|
646
|
+
identity.publicKey === record.owner ? undefined : record.secret,
|
|
647
|
+
);
|
|
648
|
+
let existing = fetched;
|
|
649
|
+
if (!existing) {
|
|
650
|
+
const variable = await api.read(c.scopeId, self.profileId);
|
|
651
|
+
existing = {
|
|
652
|
+
profile: parseProfile(variable.value, self.peerId),
|
|
653
|
+
cid: variable.cid,
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
const old = existing.profile;
|
|
657
|
+
if (old.name === personal.name && old.description === personal.description)
|
|
658
|
+
return old;
|
|
659
|
+
const profile = {
|
|
660
|
+
...old,
|
|
661
|
+
name: personal.name,
|
|
662
|
+
description: personal.description,
|
|
663
|
+
updatedAt: new Date().toISOString(),
|
|
664
|
+
};
|
|
665
|
+
await api.write(c.scopeId, self.profileId, profile, existing.cid, uniqueId());
|
|
666
|
+
return profile;
|
|
667
|
+
}
|
|
668
|
+
export async function updateAccountProfile(
|
|
669
|
+
context: SwarmContext,
|
|
670
|
+
changes: { name?: string; description?: string },
|
|
671
|
+
) {
|
|
672
|
+
if (
|
|
673
|
+
(changes.name?.length ?? 0) > 256 ||
|
|
674
|
+
(changes.description?.length ?? 0) > 2048
|
|
675
|
+
)
|
|
676
|
+
throw new HostError("Profile name/description limit exceeded.");
|
|
677
|
+
await contextIdentity(context, true);
|
|
678
|
+
let profile: AccountProfile;
|
|
679
|
+
try {
|
|
680
|
+
profile = await ensureAccountProfile(context, changes);
|
|
681
|
+
} catch (cause) {
|
|
682
|
+
throw new HostError(
|
|
683
|
+
`Personal profile local persistence failed: ${message(cause)}. No Swarm synchronization was attempted.`,
|
|
684
|
+
);
|
|
685
|
+
}
|
|
686
|
+
const swarms: { configUrl: string; synchronized: boolean; error?: string }[] =
|
|
687
|
+
[];
|
|
688
|
+
for (const entry of withState(context, (state) => state.swarms())) {
|
|
689
|
+
try {
|
|
690
|
+
const record = await approvedMembership(context, entry.cid);
|
|
691
|
+
await syncAccountProfile(context, record, profile);
|
|
692
|
+
swarms.push({ configUrl: entry.cid, synchronized: true });
|
|
693
|
+
} catch (cause) {
|
|
694
|
+
swarms.push({
|
|
695
|
+
configUrl: entry.cid,
|
|
696
|
+
synchronized: false,
|
|
697
|
+
error: message(cause),
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
return { profile, localPersisted: true, swarms };
|
|
351
702
|
}
|
|
352
703
|
async function fetchConfig(
|
|
353
704
|
context: SwarmContext,
|
|
@@ -361,12 +712,96 @@ async function fetchConfig(
|
|
|
361
712
|
api.read(c.scopeId, c.configId, options),
|
|
362
713
|
api.send<{ owner: string }>("GET", c.scopeId, "", undefined, options),
|
|
363
714
|
]);
|
|
364
|
-
const
|
|
365
|
-
if (scope.owner !==
|
|
715
|
+
const raw = object(variable.value, "organization configuration");
|
|
716
|
+
if (scope.owner !== raw.owner)
|
|
366
717
|
throw new HostError(
|
|
367
718
|
"Organization Owner does not match the immutable Scope trust root.",
|
|
368
719
|
);
|
|
369
|
-
|
|
720
|
+
if (raw.v === 2) {
|
|
721
|
+
if (
|
|
722
|
+
!Array.isArray(raw.members) ||
|
|
723
|
+
raw.members.length < 1 ||
|
|
724
|
+
raw.members.length > 256
|
|
725
|
+
)
|
|
726
|
+
throw new HostError("Invalid organization member directory.");
|
|
727
|
+
const ids = new Set<string>(),
|
|
728
|
+
identities = new Set<string>();
|
|
729
|
+
const directory = raw.members.map((item: unknown) => {
|
|
730
|
+
const member = object(item, "member directory entry");
|
|
731
|
+
if (
|
|
732
|
+
Object.keys(member).sort().join() !== "memberId,publicKey" ||
|
|
733
|
+
!isCanonicalPublicKey(member.publicKey) ||
|
|
734
|
+
typeof member.memberId !== "string" ||
|
|
735
|
+
!RESOURCE_ID_PATTERN.test(member.memberId) ||
|
|
736
|
+
ids.has(member.memberId) ||
|
|
737
|
+
identities.has(member.publicKey)
|
|
738
|
+
)
|
|
739
|
+
throw new HostError("Invalid or duplicate member directory entry.");
|
|
740
|
+
ids.add(member.memberId);
|
|
741
|
+
identities.add(member.publicKey);
|
|
742
|
+
return { publicKey: member.publicKey, memberId: member.memberId };
|
|
743
|
+
});
|
|
744
|
+
const members = await boundedMap(directory, async (entry) => {
|
|
745
|
+
const member = object(
|
|
746
|
+
(await api.read(c.scopeId, entry.memberId, options)).value,
|
|
747
|
+
"member record",
|
|
748
|
+
);
|
|
749
|
+
if (member.publicKey !== entry.publicKey)
|
|
750
|
+
throw new HostError(
|
|
751
|
+
"Member record does not match its directory identity.",
|
|
752
|
+
);
|
|
753
|
+
return { ...member, memberId: entry.memberId };
|
|
754
|
+
});
|
|
755
|
+
return {
|
|
756
|
+
config: parseOrganization({ ...raw, members }, configUrl),
|
|
757
|
+
variable,
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
return { config: parseOrganization(raw, configUrl), variable };
|
|
761
|
+
}
|
|
762
|
+
async function boundedMap<T, U>(
|
|
763
|
+
items: readonly T[],
|
|
764
|
+
operation: (item: T) => Promise<U>,
|
|
765
|
+
): Promise<U[]> {
|
|
766
|
+
const results: U[] = [];
|
|
767
|
+
for (let offset = 0; offset < items.length; offset += 16) {
|
|
768
|
+
const batch = await Promise.allSettled(
|
|
769
|
+
items.slice(offset, offset + 16).map(operation),
|
|
770
|
+
);
|
|
771
|
+
for (const result of batch) {
|
|
772
|
+
if (result.status === "rejected") throw result.reason;
|
|
773
|
+
results.push(result.value);
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
return results;
|
|
777
|
+
}
|
|
778
|
+
function configValue(config: Organization): unknown {
|
|
779
|
+
if (config.v === 1) return config;
|
|
780
|
+
return {
|
|
781
|
+
...config,
|
|
782
|
+
members: config.members.map((member) => {
|
|
783
|
+
if (!member.memberId)
|
|
784
|
+
throw new HostError("Member directory is missing its record ID.");
|
|
785
|
+
return { publicKey: member.publicKey, memberId: member.memberId };
|
|
786
|
+
}),
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
async function createMemberRecord(
|
|
790
|
+
api: ScopeClient,
|
|
791
|
+
scopeId: string,
|
|
792
|
+
member: Member,
|
|
793
|
+
key: string,
|
|
794
|
+
): Promise<Member> {
|
|
795
|
+
const value = {
|
|
796
|
+
publicKey: member.publicKey,
|
|
797
|
+
encryptionKey: member.encryptionKey,
|
|
798
|
+
peerId: member.peerId,
|
|
799
|
+
profileId: member.profileId,
|
|
800
|
+
requestId: member.requestId,
|
|
801
|
+
approval: member.approval,
|
|
802
|
+
};
|
|
803
|
+
const created = await api.create(scopeId, value, key);
|
|
804
|
+
return { ...value, memberId: created.id };
|
|
370
805
|
}
|
|
371
806
|
function binding(
|
|
372
807
|
configUrl: string,
|
|
@@ -458,23 +893,29 @@ async function privateSource(
|
|
|
458
893
|
identity.publicKey,
|
|
459
894
|
);
|
|
460
895
|
}
|
|
896
|
+
const personal = await storedAccountProfile(context);
|
|
461
897
|
const addresses = new Map<string, readonly string[]>();
|
|
462
898
|
const api = client(context, c.service);
|
|
463
899
|
let selfProfile: { profile: Profile; cid: string } | undefined;
|
|
464
|
-
const profiles = await
|
|
465
|
-
|
|
466
|
-
api.read(c.scopeId, member.profileId, signal ? { signal } : {}),
|
|
467
|
-
),
|
|
900
|
+
const profiles = await boundedMap(config.members, (member) =>
|
|
901
|
+
api.read(c.scopeId, member.profileId, signal ? { signal } : {}),
|
|
468
902
|
);
|
|
469
903
|
for (const [index, member] of config.members.entries()) {
|
|
470
|
-
const
|
|
471
|
-
if (result.status === "rejected") throw result.reason;
|
|
472
|
-
const profile = result.value;
|
|
904
|
+
const profile = profiles[index]!;
|
|
473
905
|
const value = parseProfile(profile.value, member.peerId);
|
|
474
906
|
if (member.publicKey === identity.publicKey)
|
|
475
907
|
selfProfile = { profile: value, cid: profile.cid };
|
|
476
908
|
addresses.set(member.peerId, value.addresses);
|
|
477
909
|
}
|
|
910
|
+
if (personal && selfProfile) {
|
|
911
|
+
selfProfile.profile = await syncAccountProfile(
|
|
912
|
+
context,
|
|
913
|
+
record,
|
|
914
|
+
personal,
|
|
915
|
+
selfProfile,
|
|
916
|
+
);
|
|
917
|
+
selfProfile.cid = await valueCid(selfProfile.profile);
|
|
918
|
+
}
|
|
478
919
|
const desired = memberPeers(self.peerId, addresses, config.bootstrap);
|
|
479
920
|
signal?.throwIfAborted();
|
|
480
921
|
const node = await startNode(
|
|
@@ -633,6 +1074,9 @@ async function readyMembership(
|
|
|
633
1074
|
return refresh(context, configUrl);
|
|
634
1075
|
throw cause;
|
|
635
1076
|
}
|
|
1077
|
+
} else {
|
|
1078
|
+
const personal = await storedAccountProfile(context);
|
|
1079
|
+
if (personal) await syncAccountProfile(context, record, personal);
|
|
636
1080
|
}
|
|
637
1081
|
return record;
|
|
638
1082
|
}
|
|
@@ -785,15 +1229,49 @@ export async function writeSnapshot(
|
|
|
785
1229
|
store: IpfsStore,
|
|
786
1230
|
snapshot: Snapshot,
|
|
787
1231
|
): Promise<string> {
|
|
788
|
-
const
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
1232
|
+
const put = async (value: unknown) => {
|
|
1233
|
+
const encoded = dagCbor.encode(value);
|
|
1234
|
+
const cid = CID.createV1(dagCbor.code, await sha256.digest(encoded));
|
|
1235
|
+
await store.put(cid, encoded);
|
|
1236
|
+
return cid;
|
|
1237
|
+
};
|
|
1238
|
+
const names = await put(snapshot.names);
|
|
1239
|
+
const registrations = await put(snapshot.registrations ?? {});
|
|
1240
|
+
return (await put({ v: 3, names, registrations })).toString();
|
|
1241
|
+
}
|
|
1242
|
+
async function readState(store: IpfsStore, cid: CID): Promise<Snapshot> {
|
|
1243
|
+
const data = await store.block.get(cid.toString());
|
|
1244
|
+
const root = object(dagCbor.decode(data), "State");
|
|
1245
|
+
if (root.v !== 3) return decodeSnapshot(data);
|
|
1246
|
+
const names = CID.asCID(root.names),
|
|
1247
|
+
registrations = CID.asCID(root.registrations);
|
|
1248
|
+
if (
|
|
1249
|
+
!names ||
|
|
1250
|
+
!registrations ||
|
|
1251
|
+
Object.keys(root).sort().join() !== "names,registrations,v"
|
|
1252
|
+
)
|
|
1253
|
+
throw new HostError("Invalid State index roots.");
|
|
1254
|
+
const [namesBlock, registrationsBlock] = await Promise.all([
|
|
1255
|
+
store.block.get(names.toString()),
|
|
1256
|
+
store.block.get(registrations.toString()),
|
|
1257
|
+
]);
|
|
1258
|
+
return decodeSnapshot(
|
|
1259
|
+
dagCbor.encode({
|
|
1260
|
+
v: 2,
|
|
1261
|
+
names: dagCbor.decode(namesBlock),
|
|
1262
|
+
registrations: dagCbor.decode(registrationsBlock),
|
|
1263
|
+
}),
|
|
1264
|
+
);
|
|
792
1265
|
}
|
|
793
1266
|
function decodeSnapshot(data: Uint8Array): Snapshot {
|
|
794
1267
|
const value = object(dagCbor.decode(data), "State"),
|
|
795
1268
|
names = object(value.names, "Names");
|
|
796
|
-
if (
|
|
1269
|
+
if (
|
|
1270
|
+
value.v !== 2 ||
|
|
1271
|
+
Object.keys(value).some(
|
|
1272
|
+
(key) => !["v", "names", "registrations"].includes(key),
|
|
1273
|
+
)
|
|
1274
|
+
)
|
|
797
1275
|
throw new HostError("Unsupported old State; rebuild the Swarm.");
|
|
798
1276
|
const links: [string, CID][] = [];
|
|
799
1277
|
for (const [name, link] of Object.entries(names)) {
|
|
@@ -802,14 +1280,53 @@ function decodeSnapshot(data: Uint8Array): Snapshot {
|
|
|
802
1280
|
if (!cid) throw new HostError("Names must link CIDs.");
|
|
803
1281
|
links.push([name, cid]);
|
|
804
1282
|
}
|
|
805
|
-
|
|
1283
|
+
const registrations: NonNullable<Snapshot["registrations"]> = {};
|
|
1284
|
+
if (value.registrations !== undefined) {
|
|
1285
|
+
for (const [schema, raw] of Object.entries(
|
|
1286
|
+
object(value.registrations, "Registrations"),
|
|
1287
|
+
)) {
|
|
1288
|
+
const item = object(raw, "Association");
|
|
1289
|
+
const appCid = CID.asCID(item.appCid);
|
|
1290
|
+
if (
|
|
1291
|
+
Object.keys(item).sort().join() !== "appCid,argv" ||
|
|
1292
|
+
!appCid ||
|
|
1293
|
+
!Array.isArray(item.argv) ||
|
|
1294
|
+
!item.argv.every(
|
|
1295
|
+
(argument): argument is string => typeof argument === "string",
|
|
1296
|
+
)
|
|
1297
|
+
)
|
|
1298
|
+
throw new HostError("Invalid shared Association.");
|
|
1299
|
+
const association = createAssociation(
|
|
1300
|
+
schema,
|
|
1301
|
+
appCid.toString(),
|
|
1302
|
+
item.argv,
|
|
1303
|
+
);
|
|
1304
|
+
if (association.schemaCid !== schema)
|
|
1305
|
+
throw new HostError(
|
|
1306
|
+
"Shared Association requires a canonical type CID.",
|
|
1307
|
+
);
|
|
1308
|
+
registrations[schema] = { appCid, argv: association.argv };
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
return {
|
|
1312
|
+
v: 2,
|
|
1313
|
+
names: Object.fromEntries(links),
|
|
1314
|
+
...(value.registrations !== undefined ? { registrations } : {}),
|
|
1315
|
+
};
|
|
806
1316
|
}
|
|
807
1317
|
export async function readSnapshot(
|
|
808
1318
|
store: IpfsStore,
|
|
809
1319
|
root: string,
|
|
810
1320
|
): Promise<Snapshot> {
|
|
811
1321
|
const publication = await readPublication(store, root);
|
|
812
|
-
return
|
|
1322
|
+
return readState(store, publication.state);
|
|
1323
|
+
}
|
|
1324
|
+
async function readHeaderSnapshot(store: IpfsStore, root: string) {
|
|
1325
|
+
const publication = await readPublication(store, root);
|
|
1326
|
+
return {
|
|
1327
|
+
snapshot: await readState(store, publication.state),
|
|
1328
|
+
stateCid: publication.state.toString(),
|
|
1329
|
+
};
|
|
813
1330
|
}
|
|
814
1331
|
async function readPublication(
|
|
815
1332
|
store: IpfsStore,
|
|
@@ -857,6 +1374,9 @@ export interface SwarmHeader {
|
|
|
857
1374
|
config: Organization;
|
|
858
1375
|
source: "cache" | "network";
|
|
859
1376
|
verifiedAt: string;
|
|
1377
|
+
stateCid: string;
|
|
1378
|
+
projection?: SwarmProjection;
|
|
1379
|
+
localSourceReady?: boolean;
|
|
860
1380
|
}
|
|
861
1381
|
export async function sharedSwarmStatus(
|
|
862
1382
|
context: SwarmContext,
|
|
@@ -867,9 +1387,9 @@ export async function sharedSwarmStatus(
|
|
|
867
1387
|
const head = headValue(
|
|
868
1388
|
await client(context, c.service).read(c.scopeId, config.headId),
|
|
869
1389
|
);
|
|
870
|
-
let
|
|
1390
|
+
let content: Awaited<ReturnType<typeof readHeaderSnapshot>>;
|
|
871
1391
|
try {
|
|
872
|
-
|
|
1392
|
+
content = await readHeaderSnapshot(context.store, head.root);
|
|
873
1393
|
} catch (cause) {
|
|
874
1394
|
if (!(cause instanceof ContentError) || cause.code !== "missing_block")
|
|
875
1395
|
throw cause;
|
|
@@ -882,57 +1402,118 @@ export async function sharedSwarmStatus(
|
|
|
882
1402
|
);
|
|
883
1403
|
return swarmHeader(context, configUrl, true);
|
|
884
1404
|
}
|
|
885
|
-
|
|
1405
|
+
const header: SwarmHeader = {
|
|
886
1406
|
head,
|
|
887
|
-
|
|
1407
|
+
...content,
|
|
888
1408
|
config,
|
|
889
1409
|
source: "network",
|
|
890
1410
|
verifiedAt: new Date().toISOString(),
|
|
891
1411
|
};
|
|
1412
|
+
if (
|
|
1413
|
+
withState(context, (state) =>
|
|
1414
|
+
state
|
|
1415
|
+
.contentPins()
|
|
1416
|
+
.some((pin) => pin.swarm === configUrl && pin.status !== "deleting"),
|
|
1417
|
+
)
|
|
1418
|
+
) {
|
|
1419
|
+
const record = await localForRead(context, configUrl);
|
|
1420
|
+
await lazyPrivateSource(context, configUrl, record.account, config);
|
|
1421
|
+
await refreshNamePins(context, configUrl, header);
|
|
1422
|
+
}
|
|
1423
|
+
return header;
|
|
892
1424
|
}
|
|
893
1425
|
export async function swarmHeader(
|
|
894
1426
|
context: SwarmContext,
|
|
895
1427
|
configUrl: string,
|
|
896
1428
|
force = context.forceRefresh ?? false,
|
|
1429
|
+
mode: "active" | "local-first" = "active",
|
|
1430
|
+
): Promise<SwarmHeader> {
|
|
1431
|
+
try {
|
|
1432
|
+
return await readSwarmHeader(context, configUrl, force, mode);
|
|
1433
|
+
} catch (cause) {
|
|
1434
|
+
withState(context, (state) => {
|
|
1435
|
+
for (const pin of state.contentPins())
|
|
1436
|
+
if (pin.swarm === configUrl && pin.status !== "deleting")
|
|
1437
|
+
state.saveContentPin({
|
|
1438
|
+
...pin,
|
|
1439
|
+
freshness: "stale",
|
|
1440
|
+
error: message(cause),
|
|
1441
|
+
});
|
|
1442
|
+
});
|
|
1443
|
+
throw cause;
|
|
1444
|
+
}
|
|
1445
|
+
}
|
|
1446
|
+
async function readSwarmHeader(
|
|
1447
|
+
context: SwarmContext,
|
|
1448
|
+
configUrl: string,
|
|
1449
|
+
force = context.forceRefresh ?? false,
|
|
1450
|
+
mode: "active" | "local-first" = "active",
|
|
897
1451
|
): Promise<SwarmHeader> {
|
|
898
1452
|
canonicalSwarmUrl(configUrl);
|
|
899
|
-
let record =
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
1453
|
+
let record =
|
|
1454
|
+
mode === "local-first"
|
|
1455
|
+
? await approvedMembership(context, configUrl)
|
|
1456
|
+
: force
|
|
1457
|
+
? await refresh(context, configUrl)
|
|
1458
|
+
: await readyMembership(context, configUrl);
|
|
1459
|
+
const cached = !force && (await cachedHeader(context, record));
|
|
1460
|
+
if (cached) {
|
|
1461
|
+
projectHeader(context, record, cached);
|
|
1462
|
+
await refreshNamePins(context, configUrl, cached);
|
|
1463
|
+
return cached;
|
|
1464
|
+
}
|
|
1465
|
+
if (mode === "local-first") {
|
|
1466
|
+
await saveLocal(context, record);
|
|
1467
|
+
await lazyPrivateSource(context, configUrl, record.account, record.config!);
|
|
1468
|
+
}
|
|
1469
|
+
const c = coordinates(configUrl);
|
|
1470
|
+
let head =
|
|
1471
|
+
mode === "local-first"
|
|
1472
|
+
? headValue(
|
|
1473
|
+
await client(context, c.service).read(
|
|
1474
|
+
c.scopeId,
|
|
1475
|
+
record.config!.headId,
|
|
1476
|
+
),
|
|
1477
|
+
)
|
|
1478
|
+
: await (await boundPointer(context, configUrl, record)).read();
|
|
1479
|
+
let content: Awaited<ReturnType<typeof readHeaderSnapshot>>;
|
|
906
1480
|
try {
|
|
907
|
-
|
|
908
|
-
} catch {
|
|
1481
|
+
content = await readHeaderSnapshot(context.store, head.root);
|
|
1482
|
+
} catch (cause) {
|
|
1483
|
+
if (mode === "local-first") throw cause;
|
|
909
1484
|
record = await refresh(context, configUrl);
|
|
910
1485
|
head = await (await boundPointer(context, configUrl, record)).read();
|
|
911
|
-
|
|
1486
|
+
content = await readHeaderSnapshot(context.store, head.root);
|
|
912
1487
|
}
|
|
913
1488
|
const current = await local(context, configUrl),
|
|
914
1489
|
verifiedAt = Date.now();
|
|
915
1490
|
withState(context, (state) => {
|
|
916
1491
|
state.saveSwarmHeader(record.account, configUrl, {
|
|
917
1492
|
head,
|
|
918
|
-
snapshot: Buffer.from(dagCbor.encode(snapshot)).toString(
|
|
1493
|
+
snapshot: Buffer.from(dagCbor.encode(content.snapshot)).toString(
|
|
1494
|
+
"base64",
|
|
1495
|
+
),
|
|
1496
|
+
stateCid: content.stateCid,
|
|
919
1497
|
verifiedAt,
|
|
920
1498
|
headId: current.config!.headId,
|
|
921
1499
|
epoch: current.config!.epoch,
|
|
922
1500
|
});
|
|
923
1501
|
});
|
|
924
|
-
|
|
1502
|
+
const header: SwarmHeader = {
|
|
925
1503
|
head,
|
|
926
|
-
|
|
1504
|
+
...content,
|
|
927
1505
|
config: current.config!,
|
|
928
1506
|
source: "network",
|
|
929
1507
|
verifiedAt: new Date(verifiedAt).toISOString(),
|
|
930
1508
|
};
|
|
1509
|
+
projectHeader(context, current, header);
|
|
1510
|
+
await refreshNamePins(context, configUrl, header);
|
|
1511
|
+
return header;
|
|
931
1512
|
}
|
|
932
|
-
function cachedHeader(
|
|
1513
|
+
async function cachedHeader(
|
|
933
1514
|
context: SwarmContext,
|
|
934
1515
|
record: LocalMembership,
|
|
935
|
-
): SwarmHeader | undefined {
|
|
1516
|
+
): Promise<SwarmHeader | undefined> {
|
|
936
1517
|
const config = record.config;
|
|
937
1518
|
if (!config || !record.secret) return;
|
|
938
1519
|
const cached = withState(context, (state) =>
|
|
@@ -944,6 +1525,7 @@ function cachedHeader(
|
|
|
944
1525
|
verifiedAt: number;
|
|
945
1526
|
headId: string;
|
|
946
1527
|
epoch?: number;
|
|
1528
|
+
stateCid?: string;
|
|
947
1529
|
}
|
|
948
1530
|
| undefined;
|
|
949
1531
|
const age = cached ? Date.now() - cached.verifiedAt : NaN;
|
|
@@ -954,19 +1536,68 @@ function cachedHeader(
|
|
|
954
1536
|
!(age >= 0 && age < (context.cacheTtl ?? 300) * 1000)
|
|
955
1537
|
)
|
|
956
1538
|
return;
|
|
1539
|
+
const snapshot = decodeSnapshot(Buffer.from(cached.snapshot, "base64"));
|
|
1540
|
+
let stateCid = cached.stateCid;
|
|
1541
|
+
if (!stateCid) {
|
|
1542
|
+
try {
|
|
1543
|
+
stateCid = (
|
|
1544
|
+
await readPublication(context.store, headValue(cached.head).root)
|
|
1545
|
+
).state.toString();
|
|
1546
|
+
} catch (cause) {
|
|
1547
|
+
if (cause instanceof ContentError && cause.code === "missing_block")
|
|
1548
|
+
return;
|
|
1549
|
+
throw cause;
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
957
1552
|
return {
|
|
958
1553
|
head: headValue(cached.head),
|
|
959
|
-
snapshot
|
|
1554
|
+
snapshot,
|
|
1555
|
+
stateCid,
|
|
960
1556
|
config,
|
|
961
1557
|
source: "cache",
|
|
962
1558
|
verifiedAt: new Date(cached.verifiedAt).toISOString(),
|
|
963
1559
|
};
|
|
964
1560
|
}
|
|
1561
|
+
function projectHeader(
|
|
1562
|
+
context: SwarmContext,
|
|
1563
|
+
record: LocalMembership,
|
|
1564
|
+
header: SwarmHeader,
|
|
1565
|
+
): void {
|
|
1566
|
+
const member = header.config.members.find(
|
|
1567
|
+
(item) => item.publicKey === record.account,
|
|
1568
|
+
);
|
|
1569
|
+
if (!member || record.owner !== header.config.owner)
|
|
1570
|
+
throw new HostError(
|
|
1571
|
+
"Projection membership does not match the verified account.",
|
|
1572
|
+
);
|
|
1573
|
+
const binding: SwarmProjection = {
|
|
1574
|
+
account: record.account,
|
|
1575
|
+
swarm: record.configUrl,
|
|
1576
|
+
stateCid: header.stateCid,
|
|
1577
|
+
root: header.head.root,
|
|
1578
|
+
valueCid: header.head.cid,
|
|
1579
|
+
verifiedAt: Date.parse(header.verifiedAt),
|
|
1580
|
+
headId: header.config.headId,
|
|
1581
|
+
epoch: header.config.epoch,
|
|
1582
|
+
membership: JSON.stringify({ owner: record.owner, member }),
|
|
1583
|
+
};
|
|
1584
|
+
withState(context, (state) => {
|
|
1585
|
+
state.projectSwarm(binding, header.snapshot.names, () =>
|
|
1586
|
+
snapshotAssociations(header.snapshot),
|
|
1587
|
+
);
|
|
1588
|
+
});
|
|
1589
|
+
header.projection = binding;
|
|
1590
|
+
}
|
|
1591
|
+
function headerProjection(header: SwarmHeader): SwarmProjection {
|
|
1592
|
+
if (!header.projection)
|
|
1593
|
+
throw new HostError("A verified account projection is required.");
|
|
1594
|
+
return header.projection;
|
|
1595
|
+
}
|
|
965
1596
|
async function localHeader(context: SwarmContext, url: string) {
|
|
966
1597
|
if (context.forceRefresh) return;
|
|
967
1598
|
canonicalSwarmUrl(url);
|
|
968
1599
|
const record = await localForRead(context, url);
|
|
969
|
-
const header = cachedHeader(context, record);
|
|
1600
|
+
const header = await cachedHeader(context, record);
|
|
970
1601
|
if (!header) return;
|
|
971
1602
|
const config = parseOrganization(header.config, url);
|
|
972
1603
|
const member = config.members.find(
|
|
@@ -992,6 +1623,18 @@ async function localHeader(context: SwarmContext, url: string) {
|
|
|
992
1623
|
config.epoch,
|
|
993
1624
|
member.peerId,
|
|
994
1625
|
);
|
|
1626
|
+
projectHeader(context, record, header);
|
|
1627
|
+
if (
|
|
1628
|
+
withState(context, (state) =>
|
|
1629
|
+
state
|
|
1630
|
+
.contentPins()
|
|
1631
|
+
.some((pin) => pin.swarm === url && pin.status !== "deleting"),
|
|
1632
|
+
)
|
|
1633
|
+
) {
|
|
1634
|
+
await lazyPrivateSource(context, url, record.account, header.config);
|
|
1635
|
+
header.localSourceReady = true;
|
|
1636
|
+
await refreshNamePins(context, url, header);
|
|
1637
|
+
}
|
|
995
1638
|
return { header, account: record.account };
|
|
996
1639
|
}
|
|
997
1640
|
|
|
@@ -1003,6 +1646,9 @@ async function lazyPrivateSource(
|
|
|
1003
1646
|
account: string,
|
|
1004
1647
|
basis: Organization,
|
|
1005
1648
|
) {
|
|
1649
|
+
const finishPrevious = context.finishRead;
|
|
1650
|
+
delete context.finishRead;
|
|
1651
|
+
await finishPrevious?.();
|
|
1006
1652
|
const lifetime = new AbortController();
|
|
1007
1653
|
let pending: Promise<ReturnType<typeof kuboSource>> | undefined;
|
|
1008
1654
|
const load = async (signal: AbortSignal) => {
|
|
@@ -1066,6 +1712,8 @@ interface Creation {
|
|
|
1066
1712
|
name: string;
|
|
1067
1713
|
description: string;
|
|
1068
1714
|
createdAt: string;
|
|
1715
|
+
personal?: { name: string; description: string };
|
|
1716
|
+
profile?: Profile;
|
|
1069
1717
|
scopeId?: string;
|
|
1070
1718
|
headId?: string;
|
|
1071
1719
|
profileId?: string;
|
|
@@ -1101,6 +1749,7 @@ export async function createSwarm(
|
|
|
1101
1749
|
const identity = await contextIdentity(context, true),
|
|
1102
1750
|
op = options.resume ?? uniqueId(),
|
|
1103
1751
|
dir = await directory(context);
|
|
1752
|
+
const personal = await ensureAccountProfile(context);
|
|
1104
1753
|
if (!POINTER_ID_PATTERN.test(op))
|
|
1105
1754
|
throw new HostError("Invalid creation resume ID.");
|
|
1106
1755
|
return withPrivateLock(dir, async () => {
|
|
@@ -1124,6 +1773,7 @@ export async function createSwarm(
|
|
|
1124
1773
|
name: options.properties?.name ?? "",
|
|
1125
1774
|
description: options.properties?.description ?? "",
|
|
1126
1775
|
createdAt: new Date().toISOString(),
|
|
1776
|
+
personal: { name: personal.name, description: personal.description },
|
|
1127
1777
|
};
|
|
1128
1778
|
await writePrivate(dir, `creation-${op}.json`, record);
|
|
1129
1779
|
}
|
|
@@ -1152,20 +1802,38 @@ export async function createSwarm(
|
|
|
1152
1802
|
await save();
|
|
1153
1803
|
}
|
|
1154
1804
|
if (!record.profileId) {
|
|
1155
|
-
|
|
1156
|
-
|
|
1805
|
+
if (!record.profile) {
|
|
1806
|
+
const addresses = options.addresses ?? node.addresses;
|
|
1807
|
+
for (const address of addresses)
|
|
1808
|
+
validateAddress(address, node.peerId);
|
|
1809
|
+
record.profile = {
|
|
1810
|
+
...(record.personal ?? { name: record.name }),
|
|
1811
|
+
addresses,
|
|
1812
|
+
updatedAt: record.createdAt,
|
|
1813
|
+
};
|
|
1814
|
+
await save();
|
|
1815
|
+
}
|
|
1157
1816
|
record.profileId = (
|
|
1158
|
-
await api.create(
|
|
1159
|
-
record.scopeId,
|
|
1160
|
-
{ name: record.name, addresses, updatedAt: record.createdAt },
|
|
1161
|
-
`${op}.profile`,
|
|
1162
|
-
)
|
|
1817
|
+
await api.create(record.scopeId, record.profile, `${op}.profile`)
|
|
1163
1818
|
).id;
|
|
1164
1819
|
await save();
|
|
1165
1820
|
}
|
|
1166
1821
|
if (!record.config) {
|
|
1822
|
+
const member = await createMemberRecord(
|
|
1823
|
+
api,
|
|
1824
|
+
record.scopeId,
|
|
1825
|
+
{
|
|
1826
|
+
publicKey: identity.publicKey,
|
|
1827
|
+
encryptionKey: record.encryption.publicKey,
|
|
1828
|
+
peerId: node.peerId,
|
|
1829
|
+
profileId: record.profileId,
|
|
1830
|
+
requestId: op,
|
|
1831
|
+
approval: null,
|
|
1832
|
+
},
|
|
1833
|
+
`${op}.member`,
|
|
1834
|
+
);
|
|
1167
1835
|
record.config = {
|
|
1168
|
-
v:
|
|
1836
|
+
v: 2,
|
|
1169
1837
|
scopeId: record.scopeId,
|
|
1170
1838
|
owner: identity.publicKey,
|
|
1171
1839
|
name: record.name,
|
|
@@ -1173,22 +1841,17 @@ export async function createSwarm(
|
|
|
1173
1841
|
headId: record.headId,
|
|
1174
1842
|
epoch: 1,
|
|
1175
1843
|
bootstrap: options.addresses ?? node.addresses,
|
|
1176
|
-
members: [
|
|
1177
|
-
{
|
|
1178
|
-
publicKey: identity.publicKey,
|
|
1179
|
-
encryptionKey: record.encryption.publicKey,
|
|
1180
|
-
peerId: node.peerId,
|
|
1181
|
-
profileId: record.profileId,
|
|
1182
|
-
requestId: op,
|
|
1183
|
-
approval: null,
|
|
1184
|
-
},
|
|
1185
|
-
],
|
|
1844
|
+
members: [member],
|
|
1186
1845
|
};
|
|
1187
1846
|
await save();
|
|
1188
1847
|
}
|
|
1189
1848
|
if (!record.configId) {
|
|
1190
1849
|
record.configId = (
|
|
1191
|
-
await api.create(
|
|
1850
|
+
await api.create(
|
|
1851
|
+
record.scopeId,
|
|
1852
|
+
configValue(record.config),
|
|
1853
|
+
`${op}.config`,
|
|
1854
|
+
)
|
|
1192
1855
|
).id;
|
|
1193
1856
|
await save();
|
|
1194
1857
|
}
|
|
@@ -1255,21 +1918,23 @@ export async function requestMembership(
|
|
|
1255
1918
|
context: SwarmContext,
|
|
1256
1919
|
url: string,
|
|
1257
1920
|
options: {
|
|
1258
|
-
ownerFingerprint: string;
|
|
1259
|
-
name?: string;
|
|
1260
1921
|
alias?: string;
|
|
1261
1922
|
output?: string;
|
|
1262
1923
|
},
|
|
1263
|
-
): Promise<
|
|
1264
|
-
|
|
1265
|
-
|
|
1924
|
+
): Promise<{
|
|
1925
|
+
requestId: string;
|
|
1926
|
+
ownerFingerprint: string;
|
|
1927
|
+
request: MembershipRequest;
|
|
1928
|
+
file?: string;
|
|
1929
|
+
}> {
|
|
1930
|
+
if ("name" in options)
|
|
1931
|
+
throw new HostError(
|
|
1932
|
+
"Set your account-wide name with ipab profile --name NAME before requesting membership.",
|
|
1933
|
+
);
|
|
1266
1934
|
canonicalSwarmUrl(url);
|
|
1267
1935
|
const identity = await contextIdentity(context, true);
|
|
1268
1936
|
const { config } = await fetchConfig(context, url);
|
|
1269
|
-
|
|
1270
|
-
throw new HostError(
|
|
1271
|
-
`Trusted Owner fingerprint required; observed ${await keyId(config.owner)}. Verify through an independent trusted channel.`,
|
|
1272
|
-
);
|
|
1937
|
+
const personal = await ensureAccountProfile(context);
|
|
1273
1938
|
const dir = await directory(context);
|
|
1274
1939
|
await withPrivateLock(dir, async () => {
|
|
1275
1940
|
if (options.alias !== undefined)
|
|
@@ -1303,7 +1968,7 @@ export async function requestMembership(
|
|
|
1303
1968
|
configUrl: url,
|
|
1304
1969
|
scopeId: config.scopeId,
|
|
1305
1970
|
owner: config.owner,
|
|
1306
|
-
name:
|
|
1971
|
+
name: personal.name,
|
|
1307
1972
|
member: identity.publicKey,
|
|
1308
1973
|
encryptionKey: encryption.publicKey,
|
|
1309
1974
|
peerId: node.peerId,
|
|
@@ -1329,15 +1994,46 @@ export async function requestMembership(
|
|
|
1329
1994
|
});
|
|
1330
1995
|
withState(context, (state) => state.registerSwarm(url, options.alias));
|
|
1331
1996
|
});
|
|
1332
|
-
|
|
1333
|
-
|
|
1997
|
+
const file =
|
|
1998
|
+
options.output === undefined ? undefined : resolve(options.output);
|
|
1999
|
+
if (file !== undefined) await exportRequest(context, url, file);
|
|
2000
|
+
const record = await local(context, url);
|
|
2001
|
+
return {
|
|
2002
|
+
requestId: record.request!.id,
|
|
2003
|
+
ownerFingerprint: await keyId(record.owner),
|
|
2004
|
+
request: record.request!,
|
|
2005
|
+
...(file === undefined ? {} : { file }),
|
|
2006
|
+
};
|
|
1334
2007
|
}
|
|
1335
2008
|
export async function verifiedRequest(
|
|
1336
2009
|
path: string,
|
|
1337
2010
|
): Promise<MembershipRequest> {
|
|
1338
2011
|
if ((await lstat(path)).size > 16 * 1024)
|
|
1339
2012
|
throw new HostError("Request file exceeds 16 KiB.");
|
|
1340
|
-
return
|
|
2013
|
+
return verifiedRequestStream(createReadStream(path));
|
|
2014
|
+
}
|
|
2015
|
+
export async function verifiedRequestStream(
|
|
2016
|
+
input: AsyncIterable<string | Uint8Array>,
|
|
2017
|
+
): Promise<MembershipRequest> {
|
|
2018
|
+
const chunks: Uint8Array[] = [];
|
|
2019
|
+
let size = 0;
|
|
2020
|
+
for await (const chunk of input) {
|
|
2021
|
+
const bytes = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
|
|
2022
|
+
size += bytes.byteLength;
|
|
2023
|
+
if (size > 16 * 1024) throw new HostError("Request input exceeds 16 KiB.");
|
|
2024
|
+
chunks.push(bytes);
|
|
2025
|
+
}
|
|
2026
|
+
let value: unknown;
|
|
2027
|
+
try {
|
|
2028
|
+
value = JSON.parse(
|
|
2029
|
+
Buffer.concat(chunks)
|
|
2030
|
+
.toString("utf8")
|
|
2031
|
+
.replace(/^\uFEFF/, ""),
|
|
2032
|
+
);
|
|
2033
|
+
} catch (cause) {
|
|
2034
|
+
throw new HostError(`Invalid Request JSON: ${message(cause)}.`);
|
|
2035
|
+
}
|
|
2036
|
+
return verifyRequest(value);
|
|
1341
2037
|
}
|
|
1342
2038
|
async function verifyRequest(input: unknown): Promise<MembershipRequest> {
|
|
1343
2039
|
const fields = object(input, "Request");
|
|
@@ -1539,6 +2235,7 @@ export async function approveMembership(
|
|
|
1539
2235
|
c.scopeId,
|
|
1540
2236
|
{
|
|
1541
2237
|
name: request.name,
|
|
2238
|
+
description: "",
|
|
1542
2239
|
addresses: [],
|
|
1543
2240
|
updatedAt: operation.createdAt,
|
|
1544
2241
|
},
|
|
@@ -1558,6 +2255,7 @@ export async function approveMembership(
|
|
|
1558
2255
|
};
|
|
1559
2256
|
const config = {
|
|
1560
2257
|
...fetched.config,
|
|
2258
|
+
v: 2 as const,
|
|
1561
2259
|
members: fetched.config.members.filter(
|
|
1562
2260
|
(m) => m.publicKey !== request.member,
|
|
1563
2261
|
),
|
|
@@ -1566,8 +2264,8 @@ export async function approveMembership(
|
|
|
1566
2264
|
throw new HostError("PeerID is already assigned to another member.");
|
|
1567
2265
|
if (request.member === identity.publicKey)
|
|
1568
2266
|
throw new HostError("Owner cannot request member approval.");
|
|
1569
|
-
if (config.members.length >=
|
|
1570
|
-
throw new HostError("Organization member limit is
|
|
2267
|
+
if (config.members.length >= 256)
|
|
2268
|
+
throw new HostError("Organization member limit is 256.");
|
|
1571
2269
|
const certificate = await issueScopeCertificate(
|
|
1572
2270
|
{
|
|
1573
2271
|
v: 1,
|
|
@@ -1588,12 +2286,28 @@ export async function approveMembership(
|
|
|
1588
2286
|
operation.baseline = fetched.variable.cid;
|
|
1589
2287
|
await save();
|
|
1590
2288
|
}
|
|
2289
|
+
if (operation.value.v === 2) {
|
|
2290
|
+
for (const member of operation.value.members) {
|
|
2291
|
+
if (!member.memberId) {
|
|
2292
|
+
Object.assign(
|
|
2293
|
+
member,
|
|
2294
|
+
await createMemberRecord(
|
|
2295
|
+
api,
|
|
2296
|
+
c.scopeId,
|
|
2297
|
+
member,
|
|
2298
|
+
`${operation.operation}.${member.publicKey}.member`,
|
|
2299
|
+
),
|
|
2300
|
+
);
|
|
2301
|
+
await save();
|
|
2302
|
+
}
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
1591
2305
|
const observed = await api.read(c.scopeId, c.configId);
|
|
1592
|
-
if (observed.cid !== (await valueCid(operation.value)))
|
|
2306
|
+
if (observed.cid !== (await valueCid(configValue(operation.value))))
|
|
1593
2307
|
await api.write(
|
|
1594
2308
|
c.scopeId,
|
|
1595
2309
|
c.configId,
|
|
1596
|
-
operation.value,
|
|
2310
|
+
configValue(operation.value),
|
|
1597
2311
|
operation.baseline!,
|
|
1598
2312
|
`${operation.operation}.publish`,
|
|
1599
2313
|
);
|
|
@@ -1623,7 +2337,7 @@ export async function updateSwarm(
|
|
|
1623
2337
|
const result = await client(context, c.service, identity).write(
|
|
1624
2338
|
c.scopeId,
|
|
1625
2339
|
c.configId,
|
|
1626
|
-
config,
|
|
2340
|
+
configValue(config),
|
|
1627
2341
|
fetched.variable.cid,
|
|
1628
2342
|
uniqueId(),
|
|
1629
2343
|
);
|
|
@@ -1639,12 +2353,18 @@ export async function updateSwarm(
|
|
|
1639
2353
|
export async function updateProfile(
|
|
1640
2354
|
context: SwarmContext,
|
|
1641
2355
|
url: string,
|
|
1642
|
-
changes: {
|
|
2356
|
+
changes: { addresses?: string[] },
|
|
1643
2357
|
): Promise<Profile> {
|
|
1644
|
-
if (
|
|
1645
|
-
throw new HostError(
|
|
2358
|
+
if ("name" in changes || "description" in changes)
|
|
2359
|
+
throw new HostError(
|
|
2360
|
+
"Use ipab profile --name NAME --description TEXT for personal data; swarm profile configures network addresses only.",
|
|
2361
|
+
);
|
|
1646
2362
|
if ((changes.addresses?.length ?? 0) > 16)
|
|
1647
2363
|
throw new HostError("Profile address limit is 16.");
|
|
2364
|
+
if (changes.addresses === undefined)
|
|
2365
|
+
throw new HostError(
|
|
2366
|
+
"swarm profile requires --address MULTIADDR; use ipab profile to read personal data.",
|
|
2367
|
+
);
|
|
1648
2368
|
const record = await readyMembership(context, url),
|
|
1649
2369
|
identity = await contextIdentity(context),
|
|
1650
2370
|
c = coordinates(url);
|
|
@@ -1789,6 +2509,7 @@ export async function resumeRemoval(
|
|
|
1789
2509
|
if (!record.nextConfig) {
|
|
1790
2510
|
const config = {
|
|
1791
2511
|
...record.oldConfig,
|
|
2512
|
+
v: 2 as const,
|
|
1792
2513
|
headId: record.newHeadId,
|
|
1793
2514
|
epoch: record.oldConfig.epoch + 1,
|
|
1794
2515
|
members: structuredClone(
|
|
@@ -1820,10 +2541,27 @@ export async function resumeRemoval(
|
|
|
1820
2541
|
{ certificate, swarmKey: record.newKey },
|
|
1821
2542
|
identity,
|
|
1822
2543
|
);
|
|
2544
|
+
delete member.memberId;
|
|
1823
2545
|
}
|
|
1824
2546
|
record.nextConfig = config;
|
|
1825
2547
|
await save();
|
|
1826
2548
|
}
|
|
2549
|
+
if (record.nextConfig.v === 2) {
|
|
2550
|
+
for (const member of record.nextConfig.members) {
|
|
2551
|
+
if (!member.memberId) {
|
|
2552
|
+
Object.assign(
|
|
2553
|
+
member,
|
|
2554
|
+
await createMemberRecord(
|
|
2555
|
+
api,
|
|
2556
|
+
c.scopeId,
|
|
2557
|
+
member,
|
|
2558
|
+
`${record.operation}.${member.publicKey}.member`,
|
|
2559
|
+
),
|
|
2560
|
+
);
|
|
2561
|
+
await save();
|
|
2562
|
+
}
|
|
2563
|
+
}
|
|
2564
|
+
}
|
|
1827
2565
|
const leaving = record.oldConfig.members.find(
|
|
1828
2566
|
(m) => m.publicKey === record.member,
|
|
1829
2567
|
)!;
|
|
@@ -1850,11 +2588,11 @@ export async function resumeRemoval(
|
|
|
1850
2588
|
}
|
|
1851
2589
|
if (!record.published) {
|
|
1852
2590
|
const observed = await api.read(c.scopeId, c.configId);
|
|
1853
|
-
if (observed.cid !== (await valueCid(record.nextConfig)))
|
|
2591
|
+
if (observed.cid !== (await valueCid(configValue(record.nextConfig))))
|
|
1854
2592
|
await api.write(
|
|
1855
2593
|
c.scopeId,
|
|
1856
2594
|
c.configId,
|
|
1857
|
-
record.nextConfig,
|
|
2595
|
+
configValue(record.nextConfig),
|
|
1858
2596
|
record.configCid,
|
|
1859
2597
|
`${record.operation}.config`,
|
|
1860
2598
|
);
|
|
@@ -1883,6 +2621,34 @@ export async function resumeRemoval(
|
|
|
1883
2621
|
if (!(cause instanceof ScopeApiError) || cause.status !== 410)
|
|
1884
2622
|
throw cause;
|
|
1885
2623
|
}
|
|
2624
|
+
for (const member of record.oldConfig.members) {
|
|
2625
|
+
if (
|
|
2626
|
+
member.memberId &&
|
|
2627
|
+
!record.nextConfig.members.some(
|
|
2628
|
+
(next) => next.memberId === member.memberId,
|
|
2629
|
+
)
|
|
2630
|
+
) {
|
|
2631
|
+
for (const action of ["freeze", "delete"] as const) {
|
|
2632
|
+
try {
|
|
2633
|
+
if (action === "freeze")
|
|
2634
|
+
await api.freeze(
|
|
2635
|
+
c.scopeId,
|
|
2636
|
+
member.memberId,
|
|
2637
|
+
`${record.operation}.${member.publicKey}.freeze`,
|
|
2638
|
+
);
|
|
2639
|
+
else
|
|
2640
|
+
await api.delete(
|
|
2641
|
+
c.scopeId,
|
|
2642
|
+
member.memberId,
|
|
2643
|
+
`${record.operation}.${member.publicKey}.delete`,
|
|
2644
|
+
);
|
|
2645
|
+
} catch (cause) {
|
|
2646
|
+
if (!(cause instanceof ScopeApiError) || cause.status !== 410)
|
|
2647
|
+
throw cause;
|
|
2648
|
+
}
|
|
2649
|
+
}
|
|
2650
|
+
}
|
|
2651
|
+
}
|
|
1886
2652
|
record.completed = true;
|
|
1887
2653
|
await save();
|
|
1888
2654
|
withState(context, (state) => {
|
|
@@ -2005,33 +2771,219 @@ export async function resolveReference(
|
|
|
2005
2771
|
throw new HostError(
|
|
2006
2772
|
`Formal Name "${shared.name}" was not found after refreshing HEAD.`,
|
|
2007
2773
|
);
|
|
2008
|
-
if (warm && header === warm.header)
|
|
2774
|
+
if (warm && header === warm.header && !header.localSourceReady)
|
|
2009
2775
|
await lazyPrivateSource(context, selected, warm.account, header.config);
|
|
2010
2776
|
targetCid = link.toString();
|
|
2011
2777
|
} else {
|
|
2012
2778
|
if (swarm) await readyMembership(context, swarm);
|
|
2013
2779
|
targetCid = local(reference);
|
|
2014
2780
|
}
|
|
2781
|
+
const cid = raw ? targetCid : await artifactValue(context.store, targetCid);
|
|
2782
|
+
return { targetCid, cid };
|
|
2783
|
+
}
|
|
2784
|
+
async function artifactValue(
|
|
2785
|
+
store: IpfsStore,
|
|
2786
|
+
targetCid: string,
|
|
2787
|
+
): Promise<string> {
|
|
2015
2788
|
let cid = targetCid;
|
|
2016
2789
|
const visited = new Set<string>();
|
|
2017
|
-
for (let depth = 0;
|
|
2790
|
+
for (let depth = 0; ; depth++) {
|
|
2018
2791
|
if (depth >= 64 || visited.has(cid))
|
|
2019
2792
|
throw new HostError(
|
|
2020
2793
|
"Artifact resolution exceeds limits or contains a cycle.",
|
|
2021
2794
|
);
|
|
2022
2795
|
visited.add(cid);
|
|
2023
2796
|
const bundle = await openBundle(CID.parse(cid), {
|
|
2024
|
-
resolveCid: (cid) =>
|
|
2797
|
+
resolveCid: (cid) => store.block.get(cid.toString()),
|
|
2025
2798
|
});
|
|
2026
2799
|
if (bundle.schemaKey !== (await encodeSchema(artifactType)).cid.toString())
|
|
2027
2800
|
break;
|
|
2028
2801
|
cid = CID.parse(
|
|
2029
|
-
parseArtifact(await typedJson(
|
|
2802
|
+
parseArtifact(await typedJson(store, cid, artifactType)).value,
|
|
2030
2803
|
)
|
|
2031
2804
|
.toV1()
|
|
2032
2805
|
.toString();
|
|
2033
2806
|
}
|
|
2034
|
-
return
|
|
2807
|
+
return cid;
|
|
2808
|
+
}
|
|
2809
|
+
export function pinReference(
|
|
2810
|
+
state: State,
|
|
2811
|
+
reference: string,
|
|
2812
|
+
selected?: string,
|
|
2813
|
+
): { reference: string; swarm?: string; name?: string } {
|
|
2814
|
+
const shared = parseNameReference(reference);
|
|
2815
|
+
if (!shared) return { reference: exactCid(reference) };
|
|
2816
|
+
const swarm = shared.swarm ? state.resolveSwarm(shared.swarm) : selected;
|
|
2817
|
+
if (!swarm)
|
|
2818
|
+
throw new HostError(
|
|
2819
|
+
"Pinning a shared Name requires --swarm or swarm use, or a Swarm authority in the IPAB URI.",
|
|
2820
|
+
);
|
|
2821
|
+
return {
|
|
2822
|
+
reference: formatNameReference(swarm, shared.name),
|
|
2823
|
+
swarm,
|
|
2824
|
+
name: shared.name,
|
|
2825
|
+
};
|
|
2826
|
+
}
|
|
2827
|
+
export async function pinNameSubscription(
|
|
2828
|
+
context: SwarmContext,
|
|
2829
|
+
reference: string,
|
|
2830
|
+
selected?: string,
|
|
2831
|
+
) {
|
|
2832
|
+
const canonical = withState(context, (state) =>
|
|
2833
|
+
pinReference(state, reference, selected),
|
|
2834
|
+
);
|
|
2835
|
+
if (!canonical.swarm || !canonical.name) {
|
|
2836
|
+
let roots: string[];
|
|
2837
|
+
try {
|
|
2838
|
+
roots = [
|
|
2839
|
+
...(await currentDependencies(context.store, canonical.reference)),
|
|
2840
|
+
];
|
|
2841
|
+
} catch (cause) {
|
|
2842
|
+
let failure: unknown = cause;
|
|
2843
|
+
for (
|
|
2844
|
+
let depth = 0;
|
|
2845
|
+
depth < 8 && failure instanceof ContentBundleError;
|
|
2846
|
+
depth++
|
|
2847
|
+
)
|
|
2848
|
+
failure = failure.cause;
|
|
2849
|
+
if (
|
|
2850
|
+
!selected ||
|
|
2851
|
+
!(failure instanceof ContentError) ||
|
|
2852
|
+
failure.code !== "missing_block"
|
|
2853
|
+
)
|
|
2854
|
+
throw cause;
|
|
2855
|
+
const swarm = withState(context, (state) => state.resolveSwarm(selected));
|
|
2856
|
+
await readyMembership(context, swarm);
|
|
2857
|
+
roots = [
|
|
2858
|
+
...(await currentDependencies(context.store, canonical.reference)),
|
|
2859
|
+
];
|
|
2860
|
+
}
|
|
2861
|
+
return withStateAsync(context, (state) =>
|
|
2862
|
+
pinCid(state, canonical.reference, context.store, roots),
|
|
2863
|
+
);
|
|
2864
|
+
}
|
|
2865
|
+
const resolved = await resolveReference(
|
|
2866
|
+
context,
|
|
2867
|
+
canonical.reference,
|
|
2868
|
+
canonical.swarm,
|
|
2869
|
+
exactCid,
|
|
2870
|
+
false,
|
|
2871
|
+
"local-first",
|
|
2872
|
+
);
|
|
2873
|
+
const roots = await currentDependencies(context.store, resolved.targetCid);
|
|
2874
|
+
const pin = withState(context, (state) =>
|
|
2875
|
+
reservePin(state, canonical.reference, context.store.configuration, {
|
|
2876
|
+
swarm: canonical.swarm!,
|
|
2877
|
+
name: canonical.name!,
|
|
2878
|
+
}),
|
|
2879
|
+
);
|
|
2880
|
+
return withStateAsync(context, (state) =>
|
|
2881
|
+
retainPin(state, pin, context.store, { ...resolved, roots: [...roots] }),
|
|
2882
|
+
);
|
|
2883
|
+
}
|
|
2884
|
+
async function withStateAsync<T>(
|
|
2885
|
+
context: SwarmContext,
|
|
2886
|
+
operation: (state: State) => Promise<T>,
|
|
2887
|
+
): Promise<T> {
|
|
2888
|
+
if (context.state) return operation(context.state);
|
|
2889
|
+
const state = new State(context.stateDir);
|
|
2890
|
+
try {
|
|
2891
|
+
return await operation(state);
|
|
2892
|
+
} finally {
|
|
2893
|
+
state.close();
|
|
2894
|
+
}
|
|
2895
|
+
}
|
|
2896
|
+
export async function refreshNamePins(
|
|
2897
|
+
context: SwarmContext,
|
|
2898
|
+
url: string,
|
|
2899
|
+
header: Pick<SwarmHeader, "snapshot">,
|
|
2900
|
+
): Promise<void> {
|
|
2901
|
+
const pins = withState(context, (state) =>
|
|
2902
|
+
state
|
|
2903
|
+
.contentPins()
|
|
2904
|
+
.filter((pin) => pin.swarm === url && pin.status !== "deleting"),
|
|
2905
|
+
);
|
|
2906
|
+
const failures: string[] = [];
|
|
2907
|
+
for (const saved of pins) {
|
|
2908
|
+
const target = Object.hasOwn(header.snapshot.names, saved.name!)
|
|
2909
|
+
? header.snapshot.names[saved.name!]!.toString()
|
|
2910
|
+
: undefined;
|
|
2911
|
+
if (!target) {
|
|
2912
|
+
withState(context, (state) => {
|
|
2913
|
+
failedPin(
|
|
2914
|
+
state,
|
|
2915
|
+
saved.reference,
|
|
2916
|
+
new Error(
|
|
2917
|
+
"Shared Name is missing; keeping its last verified version.",
|
|
2918
|
+
),
|
|
2919
|
+
true,
|
|
2920
|
+
);
|
|
2921
|
+
});
|
|
2922
|
+
context.diagnostic?.(
|
|
2923
|
+
`Pin ${saved.reference} is missing; its last verified version remains retained.`,
|
|
2924
|
+
);
|
|
2925
|
+
continue;
|
|
2926
|
+
}
|
|
2927
|
+
let claimed = false;
|
|
2928
|
+
try {
|
|
2929
|
+
// A cached HEAD can differ from the saved subscription after publication or a failed refresh.
|
|
2930
|
+
if (
|
|
2931
|
+
saved.targetCid === target &&
|
|
2932
|
+
saved.status === "ready" &&
|
|
2933
|
+
saved.freshness === "current"
|
|
2934
|
+
)
|
|
2935
|
+
continue;
|
|
2936
|
+
const pin = withState(context, (state) => refreshPinClaim(state, saved));
|
|
2937
|
+
if (!pin) continue;
|
|
2938
|
+
claimed = true;
|
|
2939
|
+
const cid = await artifactValue(context.store, target);
|
|
2940
|
+
const roots = [...(await currentDependencies(context.store, target))];
|
|
2941
|
+
await withStateAsync(context, (state) =>
|
|
2942
|
+
retainPin(state, pin, context.store, { cid, targetCid: target, roots }),
|
|
2943
|
+
);
|
|
2944
|
+
} catch (cause) {
|
|
2945
|
+
if (claimed)
|
|
2946
|
+
withState(context, (state) => {
|
|
2947
|
+
failedPin(state, saved.reference, cause);
|
|
2948
|
+
});
|
|
2949
|
+
failures.push(`${saved.reference}: ${message(cause)}`);
|
|
2950
|
+
}
|
|
2951
|
+
}
|
|
2952
|
+
if (failures.length)
|
|
2953
|
+
throw new HostError(
|
|
2954
|
+
`Name subscription refresh failed; prior verified versions remain retained:\n${failures.join("\n")}`,
|
|
2955
|
+
1,
|
|
2956
|
+
"PIN_STALE",
|
|
2957
|
+
);
|
|
2958
|
+
}
|
|
2959
|
+
async function currentDependencies(
|
|
2960
|
+
store: IpfsStore,
|
|
2961
|
+
root: string,
|
|
2962
|
+
found = new Set<string>(),
|
|
2963
|
+
): Promise<Set<string>> {
|
|
2964
|
+
if (found.has(root)) return found;
|
|
2965
|
+
if (found.size >= 64)
|
|
2966
|
+
throw new HostError("Current pin dependency limit exceeded.");
|
|
2967
|
+
found.add(root);
|
|
2968
|
+
const bundle = await openBundle(CID.parse(root), {
|
|
2969
|
+
resolveCid: (cid) => store.block.get(cid.toString()),
|
|
2970
|
+
});
|
|
2971
|
+
if (bundle.schemaKey === (await encodeSchema(artifactType)).cid.toString()) {
|
|
2972
|
+
await currentDependencies(
|
|
2973
|
+
store,
|
|
2974
|
+
parseArtifact(await typedJson(store, root, artifactType)).value,
|
|
2975
|
+
found,
|
|
2976
|
+
);
|
|
2977
|
+
} else if (bundle.schemaKey === appSchemaCid.toString()) {
|
|
2978
|
+
const app = object((await bundleJson(store, root)).value, "App");
|
|
2979
|
+
if (app.source) {
|
|
2980
|
+
const source = object(app.source, "source");
|
|
2981
|
+
if (typeof source.cid !== "string")
|
|
2982
|
+
throw new HostError("Invalid App source association.");
|
|
2983
|
+
await currentDependencies(store, source.cid, found);
|
|
2984
|
+
}
|
|
2985
|
+
}
|
|
2986
|
+
return found;
|
|
2035
2987
|
}
|
|
2036
2988
|
export async function searchNames(
|
|
2037
2989
|
context: SwarmContext,
|
|
@@ -2065,7 +3017,12 @@ export async function searchNames(
|
|
|
2065
3017
|
for (const swarm of swarms) {
|
|
2066
3018
|
const header =
|
|
2067
3019
|
(await localHeader(context, swarm.cid))?.header ??
|
|
2068
|
-
(await swarmHeader(
|
|
3020
|
+
(await swarmHeader(
|
|
3021
|
+
context,
|
|
3022
|
+
swarm.cid,
|
|
3023
|
+
context.forceRefresh,
|
|
3024
|
+
"local-first",
|
|
3025
|
+
));
|
|
2069
3026
|
searched.push({
|
|
2070
3027
|
configUrl: swarm.cid,
|
|
2071
3028
|
source: header.source,
|
|
@@ -2073,18 +3030,167 @@ export async function searchNames(
|
|
|
2073
3030
|
valueCid: header.head.cid,
|
|
2074
3031
|
verifiedAt: header.verifiedAt,
|
|
2075
3032
|
});
|
|
2076
|
-
for (const name of
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
3033
|
+
for (const { name, targetCid } of withState(context, (state) =>
|
|
3034
|
+
state.searchSwarmNames(headerProjection(header), query),
|
|
3035
|
+
))
|
|
3036
|
+
matches.push({
|
|
3037
|
+
swarm: swarm.cid,
|
|
3038
|
+
alias: swarm.alias,
|
|
3039
|
+
name,
|
|
3040
|
+
reference: formatNameReference(swarm.cid, name),
|
|
3041
|
+
targetCid,
|
|
3042
|
+
});
|
|
2085
3043
|
}
|
|
2086
3044
|
return { matches, swarms: searched };
|
|
2087
3045
|
}
|
|
3046
|
+
function snapshotAssociations(snapshot: Snapshot): Association[] {
|
|
3047
|
+
return Object.entries(snapshot.registrations ?? {})
|
|
3048
|
+
.sort(([left], [right]) => left.localeCompare(right))
|
|
3049
|
+
.map(([schemaCid, item]) => ({
|
|
3050
|
+
schemaCid,
|
|
3051
|
+
appCid: item.appCid.toString(),
|
|
3052
|
+
argv: [...item.argv],
|
|
3053
|
+
}));
|
|
3054
|
+
}
|
|
3055
|
+
export async function swarmRegistrations(
|
|
3056
|
+
context: SwarmContext,
|
|
3057
|
+
configUrl: string,
|
|
3058
|
+
): Promise<Association[]> {
|
|
3059
|
+
const header = await swarmHeader(context, configUrl);
|
|
3060
|
+
return withState(context, (state) =>
|
|
3061
|
+
state.projectedSwarmRegistrations(headerProjection(header)),
|
|
3062
|
+
);
|
|
3063
|
+
}
|
|
3064
|
+
export async function swarmAssociation(
|
|
3065
|
+
context: SwarmContext,
|
|
3066
|
+
configUrl: string,
|
|
3067
|
+
schemaCid: string,
|
|
3068
|
+
mode: "active" | "local-first" = "active",
|
|
3069
|
+
): Promise<Association | undefined> {
|
|
3070
|
+
let header =
|
|
3071
|
+
(mode === "local-first"
|
|
3072
|
+
? (await localHeader(context, configUrl))?.header
|
|
3073
|
+
: undefined) ?? (await swarmHeader(context, configUrl));
|
|
3074
|
+
let association = withState(
|
|
3075
|
+
context,
|
|
3076
|
+
(state) =>
|
|
3077
|
+
state.projectedSwarmRegistrations(headerProjection(header), schemaCid)[0],
|
|
3078
|
+
);
|
|
3079
|
+
if (!association && header.source === "cache") {
|
|
3080
|
+
header = await swarmHeader(context, configUrl, true);
|
|
3081
|
+
association = withState(
|
|
3082
|
+
context,
|
|
3083
|
+
(state) =>
|
|
3084
|
+
state.projectedSwarmRegistrations(
|
|
3085
|
+
headerProjection(header),
|
|
3086
|
+
schemaCid,
|
|
3087
|
+
)[0],
|
|
3088
|
+
);
|
|
3089
|
+
}
|
|
3090
|
+
return association;
|
|
3091
|
+
}
|
|
3092
|
+
async function editSwarmAssociation(
|
|
3093
|
+
context: SwarmContext,
|
|
3094
|
+
configUrl: string,
|
|
3095
|
+
schemaCid: string,
|
|
3096
|
+
association: Association | undefined,
|
|
3097
|
+
replace: boolean,
|
|
3098
|
+
): Promise<void> {
|
|
3099
|
+
const head = await pointer(context, configUrl);
|
|
3100
|
+
const roots = association
|
|
3101
|
+
? await dependencies(context.store, association.appCid)
|
|
3102
|
+
: new Set<string>();
|
|
3103
|
+
for (let attempt = 0; attempt < 8; attempt++) {
|
|
3104
|
+
const currentHead = await head.read();
|
|
3105
|
+
const current = await readSnapshot(context.store, currentHead.root);
|
|
3106
|
+
const old = snapshotAssociations(current).find(
|
|
3107
|
+
(item) => item.schemaCid === schemaCid,
|
|
3108
|
+
);
|
|
3109
|
+
if (association) {
|
|
3110
|
+
if (
|
|
3111
|
+
old &&
|
|
3112
|
+
old.appCid === association.appCid &&
|
|
3113
|
+
JSON.stringify(old.argv) === JSON.stringify(association.argv)
|
|
3114
|
+
)
|
|
3115
|
+
return;
|
|
3116
|
+
if (old && !replace)
|
|
3117
|
+
throw new HostError(
|
|
3118
|
+
"Schema has another association; use --replace before --.",
|
|
3119
|
+
);
|
|
3120
|
+
} else if (!old) {
|
|
3121
|
+
throw new HostError(
|
|
3122
|
+
"No association exists for this Schema. Run ipab register list --swarm URL_OR_ALIAS.",
|
|
3123
|
+
);
|
|
3124
|
+
}
|
|
3125
|
+
const registrations = { ...current.registrations };
|
|
3126
|
+
if (association)
|
|
3127
|
+
registrations[schemaCid] = {
|
|
3128
|
+
appCid: CID.parse(association.appCid),
|
|
3129
|
+
argv: association.argv,
|
|
3130
|
+
};
|
|
3131
|
+
else delete registrations[schemaCid];
|
|
3132
|
+
const next = await publication(
|
|
3133
|
+
context.store,
|
|
3134
|
+
await writeSnapshot(context.store, { ...current, registrations }),
|
|
3135
|
+
currentHead.root,
|
|
3136
|
+
);
|
|
3137
|
+
await provideContent(
|
|
3138
|
+
context.store,
|
|
3139
|
+
[next, ...roots],
|
|
3140
|
+
context.node!.api,
|
|
3141
|
+
context.fetch,
|
|
3142
|
+
context.node!.auth,
|
|
3143
|
+
);
|
|
3144
|
+
withState(context, (state) => {
|
|
3145
|
+
state.invalidateSwarmHeader(configUrl);
|
|
3146
|
+
});
|
|
3147
|
+
try {
|
|
3148
|
+
await head.write(next, currentHead.cid, uniqueId());
|
|
3149
|
+
return;
|
|
3150
|
+
} catch (cause) {
|
|
3151
|
+
if (!(cause instanceof ScopeApiError) || cause.status !== 412)
|
|
3152
|
+
throw new HostError(
|
|
3153
|
+
`Registry publication unconfirmed for ${configUrl}: ${message(cause)}. Inspect the Swarm registry before retry.`,
|
|
3154
|
+
1,
|
|
3155
|
+
"HEAD_UNCONFIRMED",
|
|
3156
|
+
"publication",
|
|
3157
|
+
);
|
|
3158
|
+
continue;
|
|
3159
|
+
}
|
|
3160
|
+
}
|
|
3161
|
+
throw new HostError(
|
|
3162
|
+
`HEAD is busy for ${configUrl}; retry the registry edit.`,
|
|
3163
|
+
);
|
|
3164
|
+
}
|
|
3165
|
+
export async function setSwarmAssociation(
|
|
3166
|
+
context: SwarmContext,
|
|
3167
|
+
configUrl: string,
|
|
3168
|
+
value: Association,
|
|
3169
|
+
replace = false,
|
|
3170
|
+
): Promise<Association> {
|
|
3171
|
+
const association = createAssociation(
|
|
3172
|
+
value.schemaCid,
|
|
3173
|
+
value.appCid,
|
|
3174
|
+
value.argv,
|
|
3175
|
+
);
|
|
3176
|
+
await editSwarmAssociation(
|
|
3177
|
+
context,
|
|
3178
|
+
configUrl,
|
|
3179
|
+
association.schemaCid,
|
|
3180
|
+
association,
|
|
3181
|
+
replace,
|
|
3182
|
+
);
|
|
3183
|
+
return association;
|
|
3184
|
+
}
|
|
3185
|
+
export async function removeSwarmAssociation(
|
|
3186
|
+
context: SwarmContext,
|
|
3187
|
+
configUrl: string,
|
|
3188
|
+
schemaCid: string,
|
|
3189
|
+
): Promise<string> {
|
|
3190
|
+
const schema = exactCid(schemaCid, true);
|
|
3191
|
+
await editSwarmAssociation(context, configUrl, schema, undefined, false);
|
|
3192
|
+
return schema;
|
|
3193
|
+
}
|
|
2088
3194
|
async function dependencies(
|
|
2089
3195
|
store: IpfsStore,
|
|
2090
3196
|
root: string,
|
|
@@ -2133,11 +3239,11 @@ export async function frozenPublicationClosure(
|
|
|
2133
3239
|
);
|
|
2134
3240
|
publications.add(current);
|
|
2135
3241
|
const publication = await readPublication(store, current);
|
|
2136
|
-
const snapshot =
|
|
2137
|
-
await store.block.get(publication.state.toString()),
|
|
2138
|
-
);
|
|
3242
|
+
const snapshot = await readState(store, publication.state);
|
|
2139
3243
|
for (const target of Object.values(snapshot.names))
|
|
2140
3244
|
await dependencies(store, target.toString(), content);
|
|
3245
|
+
for (const association of Object.values(snapshot.registrations ?? {}))
|
|
3246
|
+
await dependencies(store, association.appCid.toString(), content);
|
|
2141
3247
|
current = publication.previous?.toString();
|
|
2142
3248
|
}
|
|
2143
3249
|
return [
|
|
@@ -2152,7 +3258,7 @@ export async function publish(
|
|
|
2152
3258
|
content: string,
|
|
2153
3259
|
options: {
|
|
2154
3260
|
swarm?: string;
|
|
2155
|
-
name
|
|
3261
|
+
name: string;
|
|
2156
3262
|
expect?: string;
|
|
2157
3263
|
snapshot?: boolean;
|
|
2158
3264
|
},
|
|
@@ -2163,19 +3269,10 @@ export async function publish(
|
|
|
2163
3269
|
);
|
|
2164
3270
|
const head = await pointer(context, options.swarm);
|
|
2165
3271
|
const roots = await dependencies(context.store, content);
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
await provideContent(
|
|
2171
|
-
context.store,
|
|
2172
|
-
[...roots],
|
|
2173
|
-
context.node!.api,
|
|
2174
|
-
context.fetch,
|
|
2175
|
-
context.node!.auth,
|
|
2176
|
-
);
|
|
2177
|
-
return content;
|
|
2178
|
-
}
|
|
3272
|
+
const currentRoots = [...(await currentDependencies(context.store, content))];
|
|
3273
|
+
await withStateAsync(context, (state) =>
|
|
3274
|
+
pinCid(state, content, context.store, currentRoots),
|
|
3275
|
+
);
|
|
2179
3276
|
const name = formalName(options.name);
|
|
2180
3277
|
if (options.snapshot && options.expect)
|
|
2181
3278
|
throw new HostError("Snapshots cannot replace an occupied Name.");
|
|
@@ -2204,7 +3301,7 @@ export async function publish(
|
|
|
2204
3301
|
: undefined) !== expected
|
|
2205
3302
|
)
|
|
2206
3303
|
throw new HostError(
|
|
2207
|
-
`Edit conflict for ${name};
|
|
3304
|
+
`Edit conflict for ${name}; content ${content} remains pinned. Occupied Names require --expect EXACT_OLD_ARTIFACT_CID; inspect the current Name before retrying.`,
|
|
2208
3305
|
3,
|
|
2209
3306
|
"EDIT_CONFLICT",
|
|
2210
3307
|
"publication",
|
|
@@ -2223,13 +3320,13 @@ export async function publish(
|
|
|
2223
3320
|
context.node!.api,
|
|
2224
3321
|
context.fetch,
|
|
2225
3322
|
context.node!.auth,
|
|
3323
|
+
[next, ...(expected ? await dependencies(context.store, expected) : [])],
|
|
2226
3324
|
);
|
|
2227
3325
|
withState(context, (state) => {
|
|
2228
3326
|
state.invalidateSwarmHeader(options.swarm!);
|
|
2229
3327
|
});
|
|
2230
3328
|
try {
|
|
2231
3329
|
await head.write(next, currentHead.cid, uniqueId());
|
|
2232
|
-
return candidate;
|
|
2233
3330
|
} catch (cause) {
|
|
2234
3331
|
if (!(cause instanceof ScopeApiError) || cause.status !== 412)
|
|
2235
3332
|
throw new HostError(
|
|
@@ -2238,7 +3335,19 @@ export async function publish(
|
|
|
2238
3335
|
"HEAD_UNCONFIRMED",
|
|
2239
3336
|
"publication",
|
|
2240
3337
|
);
|
|
3338
|
+
continue;
|
|
3339
|
+
}
|
|
3340
|
+
try {
|
|
3341
|
+
await swarmHeader(context, options.swarm, true);
|
|
3342
|
+
} catch (cause) {
|
|
3343
|
+
throw new HostError(
|
|
3344
|
+
`Publication committed as ${candidate}, but its HEAD/subscription refresh failed: ${message(cause)}. Published content remains pinned; inspect HEAD and retry the affected Name pin.`,
|
|
3345
|
+
1,
|
|
3346
|
+
"PIN_STALE",
|
|
3347
|
+
"publication",
|
|
3348
|
+
);
|
|
2241
3349
|
}
|
|
3350
|
+
return candidate;
|
|
2242
3351
|
}
|
|
2243
3352
|
throw new HostError(`HEAD is busy; candidate ${candidate} remains retained.`);
|
|
2244
3353
|
}
|