@ipab/cli 0.1.0-rc.2 → 0.1.0-rc.4
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 +40 -13
- package/dist/app-authoring.d.ts.map +1 -1
- package/dist/app-authoring.js +11 -2
- package/dist/app-authoring.js.map +1 -1
- package/dist/app-definition.d.ts +52 -57
- package/dist/app-definition.d.ts.map +1 -1
- package/dist/app-definition.js +33 -38
- package/dist/app-definition.js.map +1 -1
- package/dist/app-templates.d.ts.map +1 -1
- package/dist/app-templates.js +2 -1
- package/dist/app-templates.js.map +1 -1
- package/dist/assembly.d.ts.map +1 -1
- package/dist/assembly.js +3 -4
- package/dist/assembly.js.map +1 -1
- package/dist/authoring.d.ts +3 -7
- package/dist/authoring.d.ts.map +1 -1
- package/dist/authoring.js +22 -12
- package/dist/authoring.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/builtins.d.ts.map +1 -1
- package/dist/builtins.js +18 -2
- package/dist/builtins.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +367 -124
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +2 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +29 -16
- 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 +5 -2
- package/dist/content-provider.js.map +1 -1
- package/dist/get.d.ts.map +1 -1
- package/dist/get.js +34 -15
- package/dist/get.js.map +1 -1
- package/dist/identity.d.ts +5 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +5 -0
- package/dist/identity.js.map +1 -1
- package/dist/kubo.d.ts +40 -0
- package/dist/kubo.d.ts.map +1 -0
- package/dist/kubo.js +555 -0
- package/dist/kubo.js.map +1 -0
- package/dist/pins.d.ts +3 -3
- package/dist/pins.js +1 -1
- package/dist/pins.js.map +1 -1
- package/dist/setup.d.ts +7 -0
- package/dist/setup.d.ts.map +1 -0
- package/dist/setup.js +22 -0
- package/dist/setup.js.map +1 -0
- package/dist/source-bundle.d.ts +3 -8
- package/dist/source-bundle.d.ts.map +1 -1
- package/dist/source-bundle.js +16 -11
- package/dist/source-bundle.js.map +1 -1
- package/dist/space.d.ts.map +1 -1
- package/dist/space.js +2 -2
- package/dist/space.js.map +1 -1
- package/dist/state.d.ts +7 -5
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +17 -14
- package/dist/state.js.map +1 -1
- package/dist/swarm-envelope.d.ts +36 -0
- package/dist/swarm-envelope.d.ts.map +1 -0
- package/dist/swarm-envelope.js +145 -0
- package/dist/swarm-envelope.js.map +1 -0
- package/dist/swarm-files.d.ts +6 -0
- package/dist/swarm-files.d.ts.map +1 -0
- package/dist/swarm-files.js +107 -0
- package/dist/swarm-files.js.map +1 -0
- package/dist/swarm.d.ts +120 -58
- package/dist/swarm.d.ts.map +1 -1
- package/dist/swarm.js +1065 -548
- package/dist/swarm.js.map +1 -1
- package/dist/template-definition.d.ts +47 -52
- package/dist/template-definition.d.ts.map +1 -1
- package/dist/template-definition.js +36 -41
- package/dist/template-definition.js.map +1 -1
- package/dist/template-source.d.ts +2 -0
- package/dist/template-source.d.ts.map +1 -1
- package/dist/template-source.js +4 -3
- package/dist/template-source.js.map +1 -1
- package/package.json +3 -3
- package/src/app-authoring.ts +22 -5
- package/src/app-definition.ts +33 -39
- package/src/app-templates.ts +9 -1
- package/src/assembly.ts +7 -4
- package/src/authoring.ts +31 -14
- package/src/builtins.ts +23 -2
- package/src/cli.ts +427 -147
- package/src/config.ts +32 -19
- package/src/content-provider.ts +5 -4
- package/src/get.ts +37 -15
- package/src/identity.ts +13 -0
- package/src/kubo.ts +732 -0
- package/src/pins.ts +1 -1
- package/src/setup.ts +21 -0
- package/src/source-bundle.ts +25 -12
- package/src/space.ts +2 -5
- package/src/state.ts +24 -15
- package/src/swarm-envelope.ts +255 -0
- package/src/swarm-files.ts +116 -0
- package/src/swarm.ts +1728 -836
- package/src/template-definition.ts +36 -41
- package/src/template-source.ts +10 -3
package/src/swarm.ts
CHANGED
|
@@ -1,131 +1,119 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { randomUUID } from "node:crypto";
|
|
1
|
+
import { lstat, open, readFile } from "node:fs/promises";
|
|
2
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
4
3
|
import { stringify } from "yaml";
|
|
5
4
|
import { CarBlockIterator } from "@ipld/car/iterator";
|
|
6
5
|
import * as dagCbor from "@ipld/dag-cbor";
|
|
7
6
|
import { CID } from "multiformats/cid";
|
|
8
7
|
import { sha256 } from "multiformats/hashes/sha2";
|
|
9
|
-
import { encodeSchema, openBundle, type
|
|
8
|
+
import { encodeSchema, openBundle, type BundleType } from "@ipab/bundle";
|
|
10
9
|
import {
|
|
11
10
|
encodeBundleCar,
|
|
12
11
|
memoryTree,
|
|
13
12
|
streamChunks,
|
|
14
13
|
} from "@ipab/bundle-builder";
|
|
15
|
-
import type
|
|
14
|
+
import { ContentError, type IpfsStore } from "@ipab/ipfs-store";
|
|
16
15
|
import {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
ScopeClient,
|
|
17
|
+
ScopeApiError,
|
|
18
|
+
RESOURCE_ID_PATTERN,
|
|
19
|
+
issueScopeCertificate,
|
|
20
|
+
uniqueId,
|
|
21
|
+
verifyScopeCertificate,
|
|
22
|
+
valueCid,
|
|
23
|
+
type Variable,
|
|
24
|
+
} from "@ipab/bundle/pointer/scope";
|
|
21
25
|
import {
|
|
22
26
|
canonicalService,
|
|
27
|
+
isCanonicalPublicKey,
|
|
23
28
|
POINTER_ID_PATTERN,
|
|
24
29
|
} from "@ipab/bundle/pointer/protocol";
|
|
25
|
-
import {
|
|
26
|
-
encodeCertificate,
|
|
27
|
-
issueCertificate,
|
|
28
|
-
verifyMemberCertificate,
|
|
29
|
-
} from "@ipab/bundle/pointer/certificates";
|
|
30
30
|
import {
|
|
31
31
|
importPrivateKeyPkcs8,
|
|
32
32
|
importPublicKeyRaw,
|
|
33
|
+
keyId,
|
|
33
34
|
signEd25519,
|
|
34
35
|
verifyEd25519,
|
|
35
|
-
randomBytes,
|
|
36
|
-
toBase64Url,
|
|
37
36
|
} from "@ipab/bundle/pointer/crypto";
|
|
38
|
-
import type { Certificate } from "@ipab/bundle/pointer/protocol";
|
|
39
37
|
import { HostError, object, message } from "./errors.js";
|
|
40
|
-
import {
|
|
41
|
-
accountIdentity,
|
|
42
|
-
protectedPath,
|
|
43
|
-
secureNew,
|
|
44
|
-
type Identity,
|
|
45
|
-
} from "./identity.js";
|
|
46
|
-
import { nodeError } from "./authoring.js";
|
|
38
|
+
import { accountIdentity, type Identity } from "./identity.js";
|
|
47
39
|
import { bundleJson, provideContent } from "./content-provider.js";
|
|
48
40
|
import { bytes, relativePath } from "./space.js";
|
|
49
|
-
import { publicUrl } from "./config.js";
|
|
50
41
|
import { appSchemaCid } from "./app-definition.js";
|
|
51
42
|
import { State } from "./state.js";
|
|
43
|
+
import {
|
|
44
|
+
privateDirectory,
|
|
45
|
+
readPrivate,
|
|
46
|
+
withPrivateLock,
|
|
47
|
+
writePrivate,
|
|
48
|
+
} from "./swarm-files.js";
|
|
49
|
+
import {
|
|
50
|
+
encryptionIdentity,
|
|
51
|
+
openApproval,
|
|
52
|
+
sealApproval,
|
|
53
|
+
type EncryptionIdentity,
|
|
54
|
+
type Envelope,
|
|
55
|
+
type EnvelopeBinding,
|
|
56
|
+
type MembershipSecret,
|
|
57
|
+
parseEnvelope,
|
|
58
|
+
} from "./swarm-envelope.js";
|
|
59
|
+
import {
|
|
60
|
+
kuboSource,
|
|
61
|
+
peerId,
|
|
62
|
+
peerProof,
|
|
63
|
+
prepareNode,
|
|
64
|
+
startNode,
|
|
65
|
+
validateAddress,
|
|
66
|
+
type KuboNode,
|
|
67
|
+
type ListenMode,
|
|
68
|
+
} from "./kubo.js";
|
|
52
69
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
name,
|
|
61
|
-
description: "",
|
|
62
|
-
entry: {
|
|
63
|
-
path: "entry.json",
|
|
64
|
-
contentType: "application/json",
|
|
65
|
-
schema: {
|
|
66
|
-
type: "object",
|
|
67
|
-
additionalProperties: false,
|
|
68
|
-
required,
|
|
69
|
-
properties,
|
|
70
|
-
},
|
|
71
|
-
},
|
|
72
|
-
};
|
|
70
|
+
export interface Member {
|
|
71
|
+
publicKey: string;
|
|
72
|
+
encryptionKey: string;
|
|
73
|
+
peerId: string;
|
|
74
|
+
profileId: string;
|
|
75
|
+
requestId: string;
|
|
76
|
+
approval: Envelope | null;
|
|
73
77
|
}
|
|
74
|
-
export
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
owner: { type: "string" },
|
|
78
|
-
});
|
|
79
|
-
export const artifactType = definition(
|
|
80
|
-
"IPAB Artifact",
|
|
81
|
-
{
|
|
82
|
-
value: cidField,
|
|
83
|
-
previous: cidField,
|
|
84
|
-
timestamp: { type: "string", format: "date-time" },
|
|
85
|
-
},
|
|
86
|
-
["value", "timestamp"],
|
|
87
|
-
);
|
|
88
|
-
export const requestType = definition("IPAB Membership Request", {
|
|
89
|
-
swarm: cidField,
|
|
90
|
-
member: { type: "string" },
|
|
91
|
-
requestId: { type: "string" },
|
|
92
|
-
iat: { type: "integer" },
|
|
93
|
-
exp: { type: "integer" },
|
|
94
|
-
signature: { type: "string" },
|
|
95
|
-
});
|
|
96
|
-
export const responseType = definition("IPAB Membership Response", {
|
|
97
|
-
swarm: cidField,
|
|
98
|
-
request: cidField,
|
|
99
|
-
certificate: { type: "object" },
|
|
100
|
-
});
|
|
101
|
-
export interface Descriptor {
|
|
102
|
-
service: string;
|
|
103
|
-
variable: string;
|
|
78
|
+
export interface Organization {
|
|
79
|
+
v: 1;
|
|
80
|
+
scopeId: string;
|
|
104
81
|
owner: string;
|
|
82
|
+
name: string;
|
|
83
|
+
description: string;
|
|
84
|
+
headId: string;
|
|
85
|
+
epoch: number;
|
|
86
|
+
bootstrap: string[];
|
|
87
|
+
members: Member[];
|
|
105
88
|
}
|
|
106
|
-
interface
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
iat: number;
|
|
111
|
-
exp: number;
|
|
112
|
-
signature: string;
|
|
113
|
-
}
|
|
114
|
-
export interface Artifact {
|
|
115
|
-
value: string;
|
|
116
|
-
previous?: string;
|
|
117
|
-
timestamp: string;
|
|
89
|
+
export interface Profile {
|
|
90
|
+
name: string;
|
|
91
|
+
addresses: string[];
|
|
92
|
+
updatedAt: string;
|
|
118
93
|
}
|
|
119
94
|
export interface Snapshot {
|
|
120
|
-
v:
|
|
121
|
-
properties: { name: string; description: string };
|
|
95
|
+
v: 2;
|
|
122
96
|
names: Record<string, CID>;
|
|
123
97
|
}
|
|
98
|
+
export interface MembershipRequest {
|
|
99
|
+
v: 1;
|
|
100
|
+
id: string;
|
|
101
|
+
requestedAt: string;
|
|
102
|
+
configUrl: string;
|
|
103
|
+
scopeId: string;
|
|
104
|
+
owner: string;
|
|
105
|
+
name: string;
|
|
106
|
+
member: string;
|
|
107
|
+
encryptionKey: string;
|
|
108
|
+
peerId: string;
|
|
109
|
+
peerPublicKey: string;
|
|
110
|
+
peerSignature: string;
|
|
111
|
+
signature: string;
|
|
112
|
+
}
|
|
124
113
|
export interface SwarmContext {
|
|
125
114
|
store: IpfsStore;
|
|
126
115
|
env: NodeJS.ProcessEnv;
|
|
127
116
|
stateDir: string;
|
|
128
|
-
provider: string;
|
|
129
117
|
fetch?: typeof fetch;
|
|
130
118
|
pointerService?: string;
|
|
131
119
|
pointerApiKey?: string;
|
|
@@ -133,6 +121,44 @@ export interface SwarmContext {
|
|
|
133
121
|
cacheTtl?: number;
|
|
134
122
|
forceRefresh?: boolean;
|
|
135
123
|
diagnostic?: (text: string) => void;
|
|
124
|
+
node?: KuboNode;
|
|
125
|
+
identity?: Identity;
|
|
126
|
+
listen?: ListenMode;
|
|
127
|
+
}
|
|
128
|
+
async function contextIdentity(
|
|
129
|
+
context: SwarmContext,
|
|
130
|
+
initialize = false,
|
|
131
|
+
): Promise<Identity> {
|
|
132
|
+
context.identity ??= await accountIdentity(context.env, initialize);
|
|
133
|
+
return context.identity;
|
|
134
|
+
}
|
|
135
|
+
interface LocalMembership {
|
|
136
|
+
configUrl: string;
|
|
137
|
+
owner: string;
|
|
138
|
+
account: string;
|
|
139
|
+
encryption: EncryptionIdentity;
|
|
140
|
+
request?: MembershipRequest;
|
|
141
|
+
config?: Organization;
|
|
142
|
+
secret?: MembershipSecret;
|
|
143
|
+
}
|
|
144
|
+
const cidField = { type: "string", minLength: 1, maxLength: 128 } as const;
|
|
145
|
+
export const artifactType: BundleType = {
|
|
146
|
+
contentType: "application/json",
|
|
147
|
+
schema: {
|
|
148
|
+
type: "object",
|
|
149
|
+
additionalProperties: false,
|
|
150
|
+
required: ["value", "timestamp"],
|
|
151
|
+
properties: {
|
|
152
|
+
value: cidField,
|
|
153
|
+
previous: cidField,
|
|
154
|
+
timestamp: { type: "string", format: "date-time" },
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
export interface Artifact {
|
|
159
|
+
value: string;
|
|
160
|
+
previous?: string;
|
|
161
|
+
timestamp: string;
|
|
136
162
|
}
|
|
137
163
|
function withState<T>(
|
|
138
164
|
context: SwarmContext,
|
|
@@ -146,80 +172,576 @@ function withState<T>(
|
|
|
146
172
|
state.close();
|
|
147
173
|
}
|
|
148
174
|
}
|
|
149
|
-
|
|
150
|
-
|
|
175
|
+
export function canonicalSwarmUrl(value: string): string {
|
|
176
|
+
if (!/^https?:\/\//.test(value))
|
|
177
|
+
throw new HostError(
|
|
178
|
+
"Swarm requires a configuration URL; old Swarm CID protocol is unsupported.",
|
|
179
|
+
);
|
|
180
|
+
const url = new URL(value);
|
|
181
|
+
canonicalService(url.origin);
|
|
182
|
+
if (
|
|
183
|
+
url.search ||
|
|
184
|
+
url.hash ||
|
|
185
|
+
url.username ||
|
|
186
|
+
url.password ||
|
|
187
|
+
url.toString() !== value ||
|
|
188
|
+
!/^\/v1\/[a-f0-9]{16}\/[a-f0-9]{16}$/.test(url.pathname)
|
|
189
|
+
)
|
|
190
|
+
throw new HostError(
|
|
191
|
+
"Swarm must be an exact Scope configuration URL; old Swarm CID protocol is unsupported.",
|
|
192
|
+
);
|
|
193
|
+
return value;
|
|
194
|
+
}
|
|
195
|
+
function coordinates(configUrl: string): {
|
|
196
|
+
service: string;
|
|
197
|
+
scopeId: string;
|
|
198
|
+
configId: string;
|
|
199
|
+
} {
|
|
200
|
+
const url = new URL(canonicalSwarmUrl(configUrl));
|
|
201
|
+
const [, , scopeId, configId] = url.pathname.split("/");
|
|
202
|
+
return { service: url.origin, scopeId: scopeId!, configId: configId! };
|
|
203
|
+
}
|
|
204
|
+
const localName = (url: string) =>
|
|
205
|
+
`${createHash("sha256").update(url).digest("hex")}.json`;
|
|
206
|
+
const nodeScope = (service: string, scopeId: string) =>
|
|
207
|
+
createHash("sha256").update(`${service}/v1/${scopeId}`).digest("hex");
|
|
208
|
+
async function directory(context: SwarmContext): Promise<string> {
|
|
209
|
+
return privateDirectory(context.stateDir, "swarm-credentials");
|
|
210
|
+
}
|
|
211
|
+
async function local(
|
|
212
|
+
context: SwarmContext,
|
|
213
|
+
configUrl: string,
|
|
214
|
+
): Promise<LocalMembership> {
|
|
215
|
+
const value = await readPrivate<LocalMembership>(
|
|
216
|
+
await directory(context),
|
|
217
|
+
localName(configUrl),
|
|
218
|
+
);
|
|
219
|
+
const identity = await contextIdentity(context);
|
|
220
|
+
if (
|
|
221
|
+
!value ||
|
|
222
|
+
value.configUrl !== configUrl ||
|
|
223
|
+
value.account !== identity.publicKey
|
|
224
|
+
)
|
|
225
|
+
throw new HostError(
|
|
226
|
+
"No local Request for this configuration and HOME identity. Run swarm request with a trusted Owner fingerprint.",
|
|
227
|
+
);
|
|
228
|
+
return value;
|
|
229
|
+
}
|
|
230
|
+
async function saveLocal(
|
|
231
|
+
context: SwarmContext,
|
|
232
|
+
membership: LocalMembership,
|
|
233
|
+
): Promise<void> {
|
|
234
|
+
await writePrivate(
|
|
235
|
+
await directory(context),
|
|
236
|
+
localName(membership.configUrl),
|
|
237
|
+
membership,
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
function client(
|
|
241
|
+
context: SwarmContext,
|
|
242
|
+
service: string,
|
|
243
|
+
identity?: Identity,
|
|
244
|
+
secret?: MembershipSecret,
|
|
245
|
+
): ScopeClient {
|
|
246
|
+
return new ScopeClient({
|
|
247
|
+
service,
|
|
248
|
+
...(identity
|
|
249
|
+
? {
|
|
250
|
+
signer: {
|
|
251
|
+
...identity,
|
|
252
|
+
...(secret ? { certificate: secret.certificate } : {}),
|
|
253
|
+
},
|
|
254
|
+
}
|
|
255
|
+
: {}),
|
|
256
|
+
...(context.fetch ? { fetch: context.fetch } : {}),
|
|
257
|
+
...(context.pointerApiKey ? { apiKey: context.pointerApiKey } : {}),
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
function parseOrganization(value: unknown, configUrl: string): Organization {
|
|
261
|
+
const data = object(value, "organization configuration");
|
|
262
|
+
const { scopeId } = coordinates(configUrl);
|
|
263
|
+
if (
|
|
264
|
+
Object.keys(data).sort().join() !==
|
|
265
|
+
"bootstrap,description,epoch,headId,members,name,owner,scopeId,v" ||
|
|
266
|
+
data.v !== 1 ||
|
|
267
|
+
data.scopeId !== scopeId ||
|
|
268
|
+
!isCanonicalPublicKey(data.owner) ||
|
|
269
|
+
typeof data.name !== "string" ||
|
|
270
|
+
data.name.length > 256 ||
|
|
271
|
+
typeof data.description !== "string" ||
|
|
272
|
+
data.description.length > 2048 ||
|
|
273
|
+
typeof data.headId !== "string" ||
|
|
274
|
+
!RESOURCE_ID_PATTERN.test(data.headId) ||
|
|
275
|
+
typeof data.epoch !== "number" ||
|
|
276
|
+
!Number.isSafeInteger(data.epoch) ||
|
|
277
|
+
data.epoch < 1 ||
|
|
278
|
+
!Array.isArray(data.bootstrap) ||
|
|
279
|
+
data.bootstrap.length > 64 ||
|
|
280
|
+
!Array.isArray(data.members) ||
|
|
281
|
+
data.members.length < 1 ||
|
|
282
|
+
data.members.length > 16
|
|
283
|
+
)
|
|
284
|
+
throw new HostError("Invalid Scope organization configuration.");
|
|
285
|
+
const identities = new Set<string>(),
|
|
286
|
+
peers = new Set<string>(),
|
|
287
|
+
profiles = new Set<string>();
|
|
288
|
+
const members: Member[] = [];
|
|
289
|
+
for (const raw of data.members) {
|
|
290
|
+
const member = object(raw, "member");
|
|
291
|
+
if (
|
|
292
|
+
Object.keys(member).sort().join() !==
|
|
293
|
+
"approval,encryptionKey,peerId,profileId,publicKey,requestId" ||
|
|
294
|
+
!isCanonicalPublicKey(member.publicKey) ||
|
|
295
|
+
typeof member.encryptionKey !== "string" ||
|
|
296
|
+
member.encryptionKey.length > 256 ||
|
|
297
|
+
typeof member.peerId !== "string" ||
|
|
298
|
+
typeof member.profileId !== "string" ||
|
|
299
|
+
!RESOURCE_ID_PATTERN.test(member.profileId) ||
|
|
300
|
+
typeof member.requestId !== "string" ||
|
|
301
|
+
!POINTER_ID_PATTERN.test(member.requestId) ||
|
|
302
|
+
identities.has(member.publicKey) ||
|
|
303
|
+
peers.has(member.peerId) ||
|
|
304
|
+
profiles.has(member.profileId)
|
|
305
|
+
)
|
|
306
|
+
throw new HostError(
|
|
307
|
+
"Invalid or duplicate member/PeerID/profile binding.",
|
|
308
|
+
);
|
|
309
|
+
identities.add(member.publicKey);
|
|
310
|
+
peers.add(member.peerId);
|
|
311
|
+
profiles.add(member.profileId);
|
|
312
|
+
members.push({
|
|
313
|
+
publicKey: member.publicKey,
|
|
314
|
+
encryptionKey: member.encryptionKey,
|
|
315
|
+
peerId: member.peerId,
|
|
316
|
+
profileId: member.profileId,
|
|
317
|
+
requestId: member.requestId,
|
|
318
|
+
approval:
|
|
319
|
+
member.approval === null ? null : parseEnvelope(member.approval),
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
if (!identities.has(data.owner))
|
|
323
|
+
throw new HostError("Owner must remain an organization member.");
|
|
324
|
+
const bootstrap: string[] = [];
|
|
325
|
+
for (const address of data.bootstrap) {
|
|
326
|
+
if (typeof address !== "string")
|
|
327
|
+
throw new HostError("Invalid bootstrap address.");
|
|
328
|
+
validateAddress(address);
|
|
329
|
+
if (![...peers].some((peer) => address.endsWith(`/p2p/${peer}`)))
|
|
330
|
+
throw new HostError("Bootstrap address is not a member PeerID.");
|
|
331
|
+
bootstrap.push(address);
|
|
332
|
+
}
|
|
333
|
+
return {
|
|
334
|
+
v: 1,
|
|
335
|
+
scopeId,
|
|
336
|
+
owner: data.owner,
|
|
337
|
+
name: data.name,
|
|
338
|
+
description: data.description,
|
|
339
|
+
headId: data.headId,
|
|
340
|
+
epoch: data.epoch,
|
|
341
|
+
bootstrap,
|
|
342
|
+
members,
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
function parseProfile(value: unknown, expectedPeer: string): Profile {
|
|
346
|
+
const data = object(value, "member profile");
|
|
347
|
+
if (
|
|
348
|
+
Object.keys(data).sort().join() !== "addresses,name,updatedAt" ||
|
|
349
|
+
typeof data.name !== "string" ||
|
|
350
|
+
data.name.length > 256 ||
|
|
351
|
+
typeof data.updatedAt !== "string" ||
|
|
352
|
+
!Number.isFinite(Date.parse(data.updatedAt)) ||
|
|
353
|
+
!Array.isArray(data.addresses) ||
|
|
354
|
+
data.addresses.length > 16
|
|
355
|
+
)
|
|
356
|
+
throw new HostError("Invalid public member profile.");
|
|
357
|
+
const addresses: string[] = [];
|
|
358
|
+
for (const address of data.addresses) {
|
|
359
|
+
if (typeof address !== "string")
|
|
360
|
+
throw new HostError("Invalid member address.");
|
|
361
|
+
validateAddress(address, expectedPeer);
|
|
362
|
+
addresses.push(address);
|
|
363
|
+
}
|
|
364
|
+
return { name: data.name, addresses, updatedAt: data.updatedAt };
|
|
365
|
+
}
|
|
366
|
+
async function fetchConfig(
|
|
367
|
+
context: SwarmContext,
|
|
368
|
+
configUrl: string,
|
|
369
|
+
): Promise<{ config: Organization; variable: Variable }> {
|
|
370
|
+
const c = coordinates(configUrl),
|
|
371
|
+
api = client(context, c.service);
|
|
372
|
+
const [variable, scope] = await Promise.all([
|
|
373
|
+
api.read(c.scopeId, c.configId),
|
|
374
|
+
api.send<{ owner: string }>("GET", c.scopeId),
|
|
375
|
+
]);
|
|
376
|
+
const config = parseOrganization(variable.value, configUrl);
|
|
377
|
+
if (scope.owner !== config.owner)
|
|
378
|
+
throw new HostError(
|
|
379
|
+
"Organization Owner does not match the immutable Scope trust root.",
|
|
380
|
+
);
|
|
381
|
+
return { config, variable };
|
|
382
|
+
}
|
|
383
|
+
function binding(
|
|
384
|
+
configUrl: string,
|
|
385
|
+
config: Organization,
|
|
386
|
+
member: Member,
|
|
387
|
+
): EnvelopeBinding {
|
|
388
|
+
return {
|
|
389
|
+
configUrl,
|
|
390
|
+
scopeId: config.scopeId,
|
|
391
|
+
owner: config.owner,
|
|
392
|
+
member: member.publicKey,
|
|
393
|
+
requestId: member.requestId,
|
|
394
|
+
peerId: member.peerId,
|
|
395
|
+
profileId: member.profileId,
|
|
396
|
+
headId: config.headId,
|
|
397
|
+
epoch: config.epoch,
|
|
398
|
+
encryptionKey: member.encryptionKey,
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
async function validateMembershipSecret(
|
|
402
|
+
record: LocalMembership,
|
|
403
|
+
config: Organization,
|
|
404
|
+
member: Member,
|
|
405
|
+
): Promise<void> {
|
|
406
|
+
const secret = record.secret;
|
|
407
|
+
if (
|
|
408
|
+
!secret ||
|
|
409
|
+
typeof secret.swarmKey !== "string" ||
|
|
410
|
+
!/^[a-f0-9]{64}$/.test(secret.swarmKey)
|
|
411
|
+
)
|
|
412
|
+
throw new HostError(
|
|
413
|
+
"Invalid approved Swarm key; preserve the original Request.",
|
|
414
|
+
);
|
|
415
|
+
if (record.account !== config.owner) {
|
|
416
|
+
const c = coordinates(record.configUrl);
|
|
417
|
+
await verifyScopeCertificate(
|
|
418
|
+
secret.certificate,
|
|
419
|
+
config.owner,
|
|
420
|
+
c.service,
|
|
421
|
+
c.scopeId,
|
|
422
|
+
config.headId,
|
|
423
|
+
record.account,
|
|
424
|
+
);
|
|
425
|
+
await verifyScopeCertificate(
|
|
426
|
+
secret.certificate,
|
|
427
|
+
config.owner,
|
|
428
|
+
c.service,
|
|
429
|
+
c.scopeId,
|
|
430
|
+
member.profileId,
|
|
431
|
+
record.account,
|
|
432
|
+
);
|
|
433
|
+
if (secret.certificate.body.write.length !== 2)
|
|
434
|
+
throw new HostError(
|
|
435
|
+
"Membership package must grant exactly HEAD and this member's profile.",
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
async function activate(
|
|
440
|
+
context: SwarmContext,
|
|
441
|
+
record: LocalMembership,
|
|
442
|
+
): Promise<void> {
|
|
443
|
+
const config = record.config!,
|
|
444
|
+
secret = record.secret!;
|
|
445
|
+
const identity = await contextIdentity(context);
|
|
446
|
+
const self = config.members.find(
|
|
447
|
+
(member) => member.publicKey === identity.publicKey,
|
|
448
|
+
);
|
|
449
|
+
if (!self)
|
|
450
|
+
throw new HostError(
|
|
451
|
+
"This identity was removed. Make a new Request only after Owner confirmation.",
|
|
452
|
+
);
|
|
453
|
+
const c = coordinates(record.configUrl);
|
|
454
|
+
if (identity.publicKey !== config.owner) {
|
|
455
|
+
await verifyScopeCertificate(
|
|
456
|
+
secret.certificate,
|
|
457
|
+
config.owner,
|
|
458
|
+
c.service,
|
|
459
|
+
c.scopeId,
|
|
460
|
+
config.headId,
|
|
461
|
+
identity.publicKey,
|
|
462
|
+
);
|
|
463
|
+
await verifyScopeCertificate(
|
|
464
|
+
secret.certificate,
|
|
465
|
+
config.owner,
|
|
466
|
+
c.service,
|
|
467
|
+
c.scopeId,
|
|
468
|
+
self.profileId,
|
|
469
|
+
identity.publicKey,
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
const addresses = [...config.bootstrap];
|
|
473
|
+
const api = client(context, c.service);
|
|
474
|
+
let selfProfile: { profile: Profile; cid: string } | undefined;
|
|
475
|
+
const profiles = await Promise.allSettled(
|
|
476
|
+
config.members.map((member) => api.read(c.scopeId, member.profileId)),
|
|
477
|
+
);
|
|
478
|
+
for (const [index, member] of config.members.entries()) {
|
|
479
|
+
const result = profiles[index]!;
|
|
480
|
+
if (result.status === "rejected") throw result.reason;
|
|
481
|
+
const profile = result.value;
|
|
482
|
+
const value = parseProfile(profile.value, member.peerId);
|
|
483
|
+
if (member.publicKey === identity.publicKey)
|
|
484
|
+
selfProfile = { profile: value, cid: profile.cid };
|
|
485
|
+
addresses.push(...value.addresses);
|
|
486
|
+
}
|
|
487
|
+
const node = await startNode(
|
|
488
|
+
context.stateDir,
|
|
489
|
+
nodeScope(c.service, config.scopeId),
|
|
490
|
+
secret.swarmKey,
|
|
491
|
+
config.epoch,
|
|
492
|
+
[...new Set(addresses)],
|
|
493
|
+
context.diagnostic,
|
|
494
|
+
context.listen,
|
|
495
|
+
);
|
|
496
|
+
if (node.peerId !== self.peerId)
|
|
497
|
+
throw new HostError(
|
|
498
|
+
"Approved PeerID differs from this local Request's Kubo identity.",
|
|
499
|
+
);
|
|
500
|
+
if (context.node?.api !== node.api) {
|
|
501
|
+
const store = context.store.withLocalSource(kuboSource(node));
|
|
502
|
+
await context.store.close();
|
|
503
|
+
context.store = store;
|
|
504
|
+
}
|
|
505
|
+
context.node = node;
|
|
506
|
+
if (selfProfile && selfProfile.profile.addresses.length === 0) {
|
|
507
|
+
const writer = client(
|
|
508
|
+
context,
|
|
509
|
+
c.service,
|
|
510
|
+
identity,
|
|
511
|
+
identity.publicKey === config.owner ? undefined : secret,
|
|
512
|
+
);
|
|
513
|
+
const profile = {
|
|
514
|
+
...selfProfile.profile,
|
|
515
|
+
addresses: node.addresses,
|
|
516
|
+
updatedAt: new Date().toISOString(),
|
|
517
|
+
};
|
|
518
|
+
await writer.write(
|
|
519
|
+
c.scopeId,
|
|
520
|
+
self.profileId,
|
|
521
|
+
profile,
|
|
522
|
+
selfProfile.cid,
|
|
523
|
+
uniqueId(),
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
async function refresh(
|
|
528
|
+
context: SwarmContext,
|
|
529
|
+
configUrl: string,
|
|
530
|
+
): Promise<LocalMembership> {
|
|
531
|
+
const record = await local(context, configUrl);
|
|
532
|
+
if (record.account === record.owner) {
|
|
533
|
+
const transition = await readPrivate<Removal>(
|
|
534
|
+
await directory(context),
|
|
535
|
+
`transition-${localName(configUrl)}`,
|
|
536
|
+
);
|
|
537
|
+
if (transition && !transition.completed)
|
|
538
|
+
throw new HostError(
|
|
539
|
+
"Organization switch incomplete; only Owner can continue with swarm resume.",
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
const { config } = await fetchConfig(context, configUrl);
|
|
543
|
+
if (config.owner !== record.owner)
|
|
544
|
+
throw new HostError("Pinned Owner changed; refusing configuration.");
|
|
545
|
+
if (record.config && config.epoch < record.config.epoch)
|
|
546
|
+
throw new HostError("Organization epoch rollback.");
|
|
547
|
+
const member = config.members.find(
|
|
548
|
+
(member) => member.publicKey === record.account,
|
|
549
|
+
);
|
|
550
|
+
if (!member)
|
|
551
|
+
throw new HostError(
|
|
552
|
+
"No approved package yet, or membership removed; ask the Owner.",
|
|
553
|
+
);
|
|
554
|
+
if (record.account !== config.owner) {
|
|
555
|
+
if (
|
|
556
|
+
!record.request ||
|
|
557
|
+
member.requestId !== record.request.id ||
|
|
558
|
+
member.peerId !== record.request.peerId ||
|
|
559
|
+
!member.approval
|
|
560
|
+
)
|
|
561
|
+
throw new HostError(
|
|
562
|
+
"Published package does not match the locally saved Request.",
|
|
563
|
+
);
|
|
564
|
+
record.secret = await openApproval(
|
|
565
|
+
member.approval,
|
|
566
|
+
binding(configUrl, config, member),
|
|
567
|
+
record.encryption,
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
if (!record.secret)
|
|
571
|
+
throw new HostError(
|
|
572
|
+
"Owner key/Swarm key lost: no system recovery is available.",
|
|
573
|
+
);
|
|
574
|
+
record.config = config;
|
|
575
|
+
await validateMembershipSecret(record, config, member);
|
|
576
|
+
await saveLocal(context, record);
|
|
577
|
+
await activate(context, record);
|
|
578
|
+
return record;
|
|
579
|
+
}
|
|
580
|
+
async function readyMembership(
|
|
581
|
+
context: SwarmContext,
|
|
582
|
+
configUrl: string,
|
|
583
|
+
): Promise<LocalMembership> {
|
|
584
|
+
const record = await local(context, configUrl);
|
|
585
|
+
if (record.account === record.owner) {
|
|
586
|
+
const transition = await readPrivate<Removal>(
|
|
587
|
+
await directory(context),
|
|
588
|
+
`transition-${localName(configUrl)}`,
|
|
589
|
+
);
|
|
590
|
+
if (transition && !transition.completed)
|
|
591
|
+
throw new HostError(
|
|
592
|
+
"Organization switch incomplete; run swarm resume before normal Owner operations.",
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
if (!record.config || !record.secret) return refresh(context, configUrl);
|
|
596
|
+
if (
|
|
597
|
+
!context.node ||
|
|
598
|
+
context.node.peerId !==
|
|
599
|
+
record.config.members.find((m) => m.publicKey === record.account)?.peerId
|
|
600
|
+
) {
|
|
151
601
|
try {
|
|
152
|
-
|
|
602
|
+
await activate(context, record);
|
|
153
603
|
} catch (cause) {
|
|
154
604
|
if (
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
!["TypeError", "AbortError", "TimeoutError"].includes(cause.name)
|
|
605
|
+
cause instanceof ScopeApiError &&
|
|
606
|
+
[403, 404, 410].includes(cause.status)
|
|
158
607
|
)
|
|
159
|
-
|
|
608
|
+
return refresh(context, configUrl);
|
|
609
|
+
throw cause;
|
|
160
610
|
}
|
|
161
611
|
}
|
|
612
|
+
return record;
|
|
162
613
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
redirect: "error",
|
|
167
|
-
cache: "no-store",
|
|
168
|
-
signal: AbortSignal.timeout(30_000),
|
|
169
|
-
});
|
|
170
|
-
interface BoundPointer {
|
|
171
|
-
read(): Promise<ReadResult>;
|
|
172
|
-
write(
|
|
173
|
-
root: string,
|
|
174
|
-
revision: string,
|
|
175
|
-
idempotencyKey: string,
|
|
176
|
-
): Promise<ReadResult>;
|
|
614
|
+
interface BoundHead {
|
|
615
|
+
read(): Promise<Variable & { root: string; etag: string }>;
|
|
616
|
+
write(root: string, cid: string, key: string): Promise<Variable>;
|
|
177
617
|
}
|
|
178
|
-
function
|
|
618
|
+
function headValue(head: Variable): Variable & { root: string; etag: string } {
|
|
619
|
+
const value = object(head.value, "HEAD");
|
|
620
|
+
if (Object.keys(value).join() !== "root" || typeof value.root !== "string")
|
|
621
|
+
throw new HostError("Invalid HEAD value.");
|
|
622
|
+
return {
|
|
623
|
+
...head,
|
|
624
|
+
root: value.root,
|
|
625
|
+
etag: `"${head.cid}"`,
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
export async function pointer(
|
|
179
629
|
context: SwarmContext,
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
630
|
+
configUrl: string,
|
|
631
|
+
): Promise<BoundHead> {
|
|
632
|
+
return boundPointer(
|
|
633
|
+
context,
|
|
634
|
+
configUrl,
|
|
635
|
+
await readyMembership(context, configUrl),
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
async function boundPointer(
|
|
639
|
+
context: SwarmContext,
|
|
640
|
+
configUrl: string,
|
|
641
|
+
record: LocalMembership,
|
|
642
|
+
): Promise<BoundHead> {
|
|
643
|
+
const identity = await contextIdentity(context),
|
|
644
|
+
c = coordinates(configUrl);
|
|
645
|
+
const operation = async (
|
|
646
|
+
write: boolean,
|
|
647
|
+
root?: string,
|
|
648
|
+
cid?: string,
|
|
649
|
+
key?: string,
|
|
650
|
+
) => {
|
|
651
|
+
for (let attempt = 0; ; attempt++) {
|
|
652
|
+
const config = record.config!,
|
|
653
|
+
api = client(
|
|
654
|
+
context,
|
|
655
|
+
c.service,
|
|
656
|
+
identity,
|
|
657
|
+
identity.publicKey === config.owner ? undefined : record.secret,
|
|
658
|
+
);
|
|
659
|
+
try {
|
|
660
|
+
return write
|
|
661
|
+
? await api.write(
|
|
662
|
+
c.scopeId,
|
|
663
|
+
config.headId,
|
|
664
|
+
{ root: root! },
|
|
665
|
+
cid!,
|
|
666
|
+
key!,
|
|
667
|
+
)
|
|
668
|
+
: await api.read(c.scopeId, config.headId);
|
|
669
|
+
} catch (cause) {
|
|
670
|
+
if (
|
|
671
|
+
attempt === 0 &&
|
|
672
|
+
(!write ||
|
|
673
|
+
(cause instanceof ScopeApiError &&
|
|
674
|
+
[401, 403, 404, 410, 423].includes(cause.status)))
|
|
675
|
+
) {
|
|
676
|
+
const oldHead = record.config!.headId;
|
|
677
|
+
record = await refresh(context, configUrl);
|
|
678
|
+
if (write && record.config!.headId !== oldHead)
|
|
679
|
+
throw new ScopeApiError(
|
|
680
|
+
412,
|
|
681
|
+
"cas_conflict",
|
|
682
|
+
"HEAD switched; recompute against the new final snapshot",
|
|
683
|
+
cause,
|
|
684
|
+
);
|
|
685
|
+
continue;
|
|
686
|
+
}
|
|
687
|
+
throw cause;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
};
|
|
193
691
|
return {
|
|
194
|
-
read: () =>
|
|
195
|
-
write: (root,
|
|
196
|
-
requestPointer(() =>
|
|
197
|
-
client.write(data.variable, root, {
|
|
198
|
-
ifRevision: revision,
|
|
199
|
-
idempotencyKey,
|
|
200
|
-
}),
|
|
201
|
-
),
|
|
692
|
+
read: async () => headValue(await operation(false)),
|
|
693
|
+
write: (root, cid, key) => operation(true, root, cid, key),
|
|
202
694
|
};
|
|
203
695
|
}
|
|
204
|
-
export async function typedJson
|
|
696
|
+
export async function typedJson(
|
|
205
697
|
store: IpfsStore,
|
|
206
698
|
cid: string,
|
|
207
|
-
type:
|
|
208
|
-
): Promise<
|
|
699
|
+
type: BundleType,
|
|
700
|
+
): Promise<unknown> {
|
|
209
701
|
const { bundle, value } = await bundleJson(store, cid);
|
|
210
702
|
if (bundle.schemaKey !== (await encodeSchema(type)).cid.toString())
|
|
211
|
-
throw new HostError(
|
|
212
|
-
return value
|
|
703
|
+
throw new HostError("Expected exact Bundle type CID.");
|
|
704
|
+
return value;
|
|
705
|
+
}
|
|
706
|
+
function parseArtifact(value: unknown): Artifact {
|
|
707
|
+
const data = object(value, "Artifact");
|
|
708
|
+
if (
|
|
709
|
+
Object.keys(data).some(
|
|
710
|
+
(key) => !["value", "previous", "timestamp"].includes(key),
|
|
711
|
+
) ||
|
|
712
|
+
typeof data.value !== "string" ||
|
|
713
|
+
typeof data.timestamp !== "string" ||
|
|
714
|
+
!Number.isFinite(Date.parse(data.timestamp)) ||
|
|
715
|
+
(data.previous !== undefined && typeof data.previous !== "string")
|
|
716
|
+
)
|
|
717
|
+
throw new HostError("Invalid Artifact.");
|
|
718
|
+
CID.parse(data.value);
|
|
719
|
+
if (data.previous !== undefined) CID.parse(data.previous);
|
|
720
|
+
return {
|
|
721
|
+
value: data.value,
|
|
722
|
+
timestamp: data.timestamp,
|
|
723
|
+
...(data.previous !== undefined ? { previous: data.previous } : {}),
|
|
724
|
+
};
|
|
213
725
|
}
|
|
214
726
|
export async function jsonBundle(
|
|
215
727
|
store: IpfsStore,
|
|
216
|
-
type:
|
|
728
|
+
type: BundleType,
|
|
217
729
|
value: unknown,
|
|
218
730
|
): Promise<string> {
|
|
219
731
|
const encoded = await encodeBundleCar(
|
|
220
732
|
memoryTree(
|
|
221
733
|
new Map([
|
|
222
|
-
[
|
|
734
|
+
[
|
|
735
|
+
".bundle.yaml",
|
|
736
|
+
new TextEncoder().encode(
|
|
737
|
+
stringify({
|
|
738
|
+
title: "IPAB Data",
|
|
739
|
+
entry: "entry.json",
|
|
740
|
+
type: ".bundle-type.yaml",
|
|
741
|
+
}),
|
|
742
|
+
),
|
|
743
|
+
],
|
|
744
|
+
[".bundle-type.yaml", new TextEncoder().encode(stringify(type))],
|
|
223
745
|
["entry.json", new TextEncoder().encode(`${JSON.stringify(value)}\n`)],
|
|
224
746
|
]),
|
|
225
747
|
),
|
|
@@ -234,722 +756,1022 @@ export async function jsonBundle(
|
|
|
234
756
|
await encoded.close();
|
|
235
757
|
}
|
|
236
758
|
}
|
|
237
|
-
export async function descriptor(
|
|
238
|
-
store: IpfsStore,
|
|
239
|
-
cid: string,
|
|
240
|
-
): Promise<Descriptor> {
|
|
241
|
-
const data = await typedJson<Descriptor>(store, cid, swarmType);
|
|
242
|
-
return validateDescriptor(data);
|
|
243
|
-
}
|
|
244
|
-
async function validateDescriptor(data: Descriptor): Promise<Descriptor> {
|
|
245
|
-
data.service = publicUrl(data.service).replace(/\/$/, "");
|
|
246
|
-
const url = new URL(data.service);
|
|
247
|
-
if (
|
|
248
|
-
url.pathname !== "/" ||
|
|
249
|
-
(url.protocol !== "https:" &&
|
|
250
|
-
!["localhost", "127.0.0.1", "[::1]"].includes(url.hostname))
|
|
251
|
-
)
|
|
252
|
-
throw new HostError(
|
|
253
|
-
"Swarm service must be an HTTPS origin or isolated loopback HTTP origin.",
|
|
254
|
-
);
|
|
255
|
-
if (!POINTER_ID_PATTERN.test(data.variable))
|
|
256
|
-
throw new HostError("Invalid service pointer ID.");
|
|
257
|
-
await importPublicKeyRaw(data.owner);
|
|
258
|
-
return data;
|
|
259
|
-
}
|
|
260
|
-
const target = (data: Descriptor) =>
|
|
261
|
-
`${data.service}/v1/pointers/${data.variable}`;
|
|
262
|
-
const now = () => Math.floor(Date.now() / 1000);
|
|
263
|
-
function requestMessage(
|
|
264
|
-
data: Descriptor,
|
|
265
|
-
request: Omit<MembershipRequest, "signature">,
|
|
266
|
-
): string {
|
|
267
|
-
return `IPAB-MEMBERSHIP/1\n${target(data)}\n${JSON.stringify({ swarm: request.swarm, member: request.member, requestId: request.requestId, iat: request.iat, exp: request.exp })}`;
|
|
268
|
-
}
|
|
269
|
-
async function verifiedRequest(
|
|
270
|
-
store: IpfsStore,
|
|
271
|
-
cid: string,
|
|
272
|
-
): Promise<{ request: MembershipRequest; data: Descriptor }> {
|
|
273
|
-
const request = await typedJson<MembershipRequest>(store, cid, requestType);
|
|
274
|
-
const data = await descriptor(store, request.swarm);
|
|
275
|
-
if (
|
|
276
|
-
request.iat > now() + 60 ||
|
|
277
|
-
request.exp <= now() ||
|
|
278
|
-
request.exp <= request.iat ||
|
|
279
|
-
request.exp - request.iat > 7 * 86400 ||
|
|
280
|
-
!/^[A-Za-z0-9_-]{16,128}$/.test(request.requestId)
|
|
281
|
-
)
|
|
282
|
-
throw new HostError("Membership request is expired or invalid.");
|
|
283
|
-
if (
|
|
284
|
-
!(await verifyEd25519(
|
|
285
|
-
await importPublicKeyRaw(request.member),
|
|
286
|
-
request.signature,
|
|
287
|
-
requestMessage(data, request),
|
|
288
|
-
))
|
|
289
|
-
)
|
|
290
|
-
throw new HostError("Membership request possession proof is invalid.");
|
|
291
|
-
return { request, data };
|
|
292
|
-
}
|
|
293
|
-
async function credentialDirectory(context: SwarmContext): Promise<string> {
|
|
294
|
-
const path = join(context.stateDir, "swarm-credentials");
|
|
295
|
-
try {
|
|
296
|
-
await lstat(path);
|
|
297
|
-
} catch (cause) {
|
|
298
|
-
if (!nodeError(cause, "ENOENT")) throw cause;
|
|
299
|
-
await mkdir(path, { mode: 0o700 });
|
|
300
|
-
await secureNew(path, true);
|
|
301
|
-
}
|
|
302
|
-
await protectedPath(path, true);
|
|
303
|
-
return path;
|
|
304
|
-
}
|
|
305
|
-
async function save(
|
|
306
|
-
context: SwarmContext,
|
|
307
|
-
name: string,
|
|
308
|
-
value: unknown,
|
|
309
|
-
): Promise<void> {
|
|
310
|
-
const path = join(await credentialDirectory(context), name);
|
|
311
|
-
const handle = await open(path, "wx", 0o600);
|
|
312
|
-
try {
|
|
313
|
-
await handle.writeFile(JSON.stringify(value));
|
|
314
|
-
await handle.sync();
|
|
315
|
-
} finally {
|
|
316
|
-
await handle.close();
|
|
317
|
-
}
|
|
318
|
-
await secureNew(path);
|
|
319
|
-
}
|
|
320
|
-
async function saved(context: SwarmContext, name: string): Promise<unknown> {
|
|
321
|
-
const path = join(await credentialDirectory(context), name);
|
|
322
|
-
await protectedPath(path);
|
|
323
|
-
return JSON.parse(await readFile(path, "utf8")) as unknown;
|
|
324
|
-
}
|
|
325
|
-
export async function pointer(
|
|
326
|
-
context: SwarmContext,
|
|
327
|
-
swarm: string,
|
|
328
|
-
certificate?: Certificate,
|
|
329
|
-
): Promise<BoundPointer> {
|
|
330
|
-
const data = await descriptor(context.store, swarm);
|
|
331
|
-
const identity = await accountIdentity(context.env);
|
|
332
|
-
return authenticatedPointer(context, swarm, data, identity, certificate);
|
|
333
|
-
}
|
|
334
|
-
async function authenticatedPointer(
|
|
335
|
-
context: SwarmContext,
|
|
336
|
-
swarm: string,
|
|
337
|
-
data: Descriptor,
|
|
338
|
-
identity: Identity,
|
|
339
|
-
certificate?: Certificate,
|
|
340
|
-
): Promise<BoundPointer> {
|
|
341
|
-
if (identity.publicKey === data.owner && certificate === undefined)
|
|
342
|
-
return bindPointer(context, data, identity);
|
|
343
|
-
const cert =
|
|
344
|
-
certificate ??
|
|
345
|
-
((await saved(context, `${swarm}.json`)) as { certificate: Certificate })
|
|
346
|
-
.certificate;
|
|
347
|
-
const verified = await verifyMemberCertificate(
|
|
348
|
-
encodeCertificate(cert),
|
|
349
|
-
await importPublicKeyRaw(data.owner),
|
|
350
|
-
data.owner,
|
|
351
|
-
data.service,
|
|
352
|
-
data.variable,
|
|
353
|
-
now(),
|
|
354
|
-
);
|
|
355
|
-
if (verified.member !== identity.publicKey)
|
|
356
|
-
throw new HostError(
|
|
357
|
-
"Membership certificate does not match this account, swarm, owner or validity window.",
|
|
358
|
-
);
|
|
359
|
-
if (identity.publicKey === data.owner)
|
|
360
|
-
return bindPointer(context, data, identity);
|
|
361
|
-
return bindPointer(context, data, identity, cert);
|
|
362
|
-
}
|
|
363
759
|
export async function writeSnapshot(
|
|
364
760
|
store: IpfsStore,
|
|
365
761
|
snapshot: Snapshot,
|
|
366
762
|
): Promise<string> {
|
|
367
|
-
const encoded = dagCbor.encode(snapshot)
|
|
368
|
-
|
|
763
|
+
const encoded = dagCbor.encode(snapshot),
|
|
764
|
+
cid = CID.createV1(dagCbor.code, await sha256.digest(encoded));
|
|
369
765
|
await store.put(cid, encoded);
|
|
370
766
|
return cid.toString();
|
|
371
767
|
}
|
|
768
|
+
function decodeSnapshot(data: Uint8Array): Snapshot {
|
|
769
|
+
const value = object(dagCbor.decode(data), "State"),
|
|
770
|
+
names = object(value.names, "Names");
|
|
771
|
+
if (value.v !== 2 || Object.keys(value).sort().join() !== "names,v")
|
|
772
|
+
throw new HostError("Unsupported old State; rebuild the Swarm.");
|
|
773
|
+
const links: [string, CID][] = [];
|
|
774
|
+
for (const [name, link] of Object.entries(names)) {
|
|
775
|
+
formalName(name);
|
|
776
|
+
const cid = CID.asCID(link);
|
|
777
|
+
if (!cid) throw new HostError("Names must link CIDs.");
|
|
778
|
+
links.push([name, cid]);
|
|
779
|
+
}
|
|
780
|
+
return { v: 2, names: Object.fromEntries(links) };
|
|
781
|
+
}
|
|
372
782
|
export async function readSnapshot(
|
|
373
783
|
store: IpfsStore,
|
|
374
784
|
root: string,
|
|
375
785
|
): Promise<Snapshot> {
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
|
|
786
|
+
const publication = await readPublication(store, root);
|
|
787
|
+
return decodeSnapshot(await store.block.get(publication.state.toString()));
|
|
788
|
+
}
|
|
789
|
+
async function readPublication(
|
|
790
|
+
store: IpfsStore,
|
|
791
|
+
root: string,
|
|
792
|
+
): Promise<{ state: CID; previous?: CID }> {
|
|
793
|
+
if (CID.parse(root).code !== dagCbor.code)
|
|
794
|
+
throw new HostError("Publication must be DAG-CBOR.");
|
|
379
795
|
const publication = object(
|
|
380
796
|
dagCbor.decode(await store.block.get(root)),
|
|
381
|
-
"publication
|
|
797
|
+
"publication",
|
|
382
798
|
);
|
|
383
|
-
const
|
|
799
|
+
const state = CID.asCID(publication.state);
|
|
800
|
+
const previous =
|
|
801
|
+
publication.previous === undefined
|
|
802
|
+
? undefined
|
|
803
|
+
: CID.asCID(publication.previous);
|
|
384
804
|
if (
|
|
385
805
|
publication.v !== 1 ||
|
|
386
|
-
!
|
|
806
|
+
!state ||
|
|
387
807
|
Object.keys(publication).some(
|
|
388
|
-
(
|
|
808
|
+
(k) => !["v", "state", "previous"].includes(k),
|
|
389
809
|
) ||
|
|
390
|
-
(publication.previous !== undefined && !
|
|
810
|
+
(publication.previous !== undefined && !previous)
|
|
391
811
|
)
|
|
392
|
-
throw new HostError(
|
|
393
|
-
|
|
394
|
-
);
|
|
395
|
-
return decodeSnapshot(await store.block.get(stateCid.toString()));
|
|
812
|
+
throw new HostError("Invalid publication record.");
|
|
813
|
+
return { state, ...(previous ? { previous } : {}) };
|
|
396
814
|
}
|
|
397
|
-
function
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
)
|
|
409
|
-
|
|
410
|
-
for (const [name, link] of Object.entries(names)) {
|
|
411
|
-
formalName(name);
|
|
412
|
-
if (!CID.asCID(link))
|
|
413
|
-
throw new HostError("Snapshot Name targets must be CID links.");
|
|
414
|
-
}
|
|
415
|
-
return value as unknown as Snapshot;
|
|
815
|
+
async function publication(
|
|
816
|
+
store: IpfsStore,
|
|
817
|
+
state: string,
|
|
818
|
+
previous?: string,
|
|
819
|
+
): Promise<string> {
|
|
820
|
+
const data = dagCbor.encode({
|
|
821
|
+
v: 1,
|
|
822
|
+
state: CID.parse(state),
|
|
823
|
+
...(previous ? { previous: CID.parse(previous) } : {}),
|
|
824
|
+
});
|
|
825
|
+
const cid = CID.createV1(dagCbor.code, await sha256.digest(data));
|
|
826
|
+
await store.put(cid, data);
|
|
827
|
+
return cid.toString();
|
|
416
828
|
}
|
|
417
829
|
export interface SwarmHeader {
|
|
418
|
-
head:
|
|
830
|
+
head: Variable & { root: string; etag: string };
|
|
419
831
|
snapshot: Snapshot;
|
|
832
|
+
config: Organization;
|
|
420
833
|
source: "cache" | "network";
|
|
421
834
|
verifiedAt: string;
|
|
422
835
|
}
|
|
836
|
+
export async function sharedSwarmStatus(
|
|
837
|
+
context: SwarmContext,
|
|
838
|
+
configUrl: string,
|
|
839
|
+
): Promise<SwarmHeader> {
|
|
840
|
+
const { config } = await fetchConfig(context, configUrl);
|
|
841
|
+
const c = coordinates(configUrl);
|
|
842
|
+
const head = headValue(
|
|
843
|
+
await client(context, c.service).read(c.scopeId, config.headId),
|
|
844
|
+
);
|
|
845
|
+
let snapshot: Snapshot;
|
|
846
|
+
try {
|
|
847
|
+
snapshot = await readSnapshot(context.store, head.root);
|
|
848
|
+
} catch (cause) {
|
|
849
|
+
if (!(cause instanceof ContentError) || cause.code !== "missing_block")
|
|
850
|
+
throw cause;
|
|
851
|
+
const registered = withState(context, (state) =>
|
|
852
|
+
state.swarms().some((entry) => entry.cid === configUrl),
|
|
853
|
+
);
|
|
854
|
+
if (!registered) throw cause;
|
|
855
|
+
context.diagnostic?.(
|
|
856
|
+
"Shared State is missing locally; connecting the registered Swarm content source.",
|
|
857
|
+
);
|
|
858
|
+
return swarmHeader(context, configUrl, true);
|
|
859
|
+
}
|
|
860
|
+
return {
|
|
861
|
+
head,
|
|
862
|
+
snapshot,
|
|
863
|
+
config,
|
|
864
|
+
source: "network",
|
|
865
|
+
verifiedAt: new Date().toISOString(),
|
|
866
|
+
};
|
|
867
|
+
}
|
|
423
868
|
export async function swarmHeader(
|
|
424
869
|
context: SwarmContext,
|
|
425
|
-
|
|
870
|
+
configUrl: string,
|
|
426
871
|
force = context.forceRefresh ?? false,
|
|
427
872
|
): Promise<SwarmHeader> {
|
|
428
|
-
|
|
429
|
-
|
|
873
|
+
canonicalSwarmUrl(configUrl);
|
|
874
|
+
let record = force
|
|
875
|
+
? await refresh(context, configUrl)
|
|
876
|
+
: await readyMembership(context, configUrl);
|
|
430
877
|
const ttl = context.cacheTtl ?? 300;
|
|
431
|
-
if (!Number.isSafeInteger(ttl) || ttl < 0 || ttl > 86400)
|
|
432
|
-
throw new HostError(
|
|
433
|
-
"swarm.cacheTtl requires an integer number of seconds from 0 to 86400.",
|
|
434
|
-
);
|
|
435
878
|
const cached = force
|
|
436
879
|
? undefined
|
|
437
|
-
: withState(context, (state) =>
|
|
438
|
-
state.swarmHeader(
|
|
439
|
-
)
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
)
|
|
468
|
-
throw new HostError(
|
|
469
|
-
"Invalid cached swarm descriptor; retry with --force to replace it.",
|
|
470
|
-
);
|
|
471
|
-
const data = await validateDescriptor({
|
|
472
|
-
service: cachedDescriptor.service,
|
|
473
|
-
variable: cachedDescriptor.variable,
|
|
474
|
-
owner: cachedDescriptor.owner,
|
|
475
|
-
});
|
|
476
|
-
const age = Date.now() - value.verifiedAt;
|
|
477
|
-
if (age >= 0 && age < ttl * 1000) {
|
|
478
|
-
await authenticatedPointer(context, swarm, data, identity);
|
|
479
|
-
return {
|
|
480
|
-
head: { root: value.root, revision: value.revision, etag: value.etag },
|
|
481
|
-
snapshot,
|
|
482
|
-
source: "cache",
|
|
483
|
-
verifiedAt: new Date(value.verifiedAt).toISOString(),
|
|
484
|
-
};
|
|
485
|
-
}
|
|
880
|
+
: (withState(context, (state) =>
|
|
881
|
+
state.swarmHeader(record.account, configUrl),
|
|
882
|
+
) as
|
|
883
|
+
| {
|
|
884
|
+
head: Variable;
|
|
885
|
+
snapshot: string;
|
|
886
|
+
verifiedAt: number;
|
|
887
|
+
headId: string;
|
|
888
|
+
}
|
|
889
|
+
| undefined);
|
|
890
|
+
if (
|
|
891
|
+
cached &&
|
|
892
|
+
cached.headId === record.config!.headId &&
|
|
893
|
+
Date.now() >= cached.verifiedAt &&
|
|
894
|
+
Date.now() - cached.verifiedAt < ttl * 1000
|
|
895
|
+
)
|
|
896
|
+
return {
|
|
897
|
+
head: headValue(cached.head),
|
|
898
|
+
snapshot: decodeSnapshot(Buffer.from(cached.snapshot, "base64")),
|
|
899
|
+
config: record.config!,
|
|
900
|
+
source: "cache",
|
|
901
|
+
verifiedAt: new Date(cached.verifiedAt).toISOString(),
|
|
902
|
+
};
|
|
903
|
+
let head = await (await boundPointer(context, configUrl, record)).read();
|
|
904
|
+
let snapshot: Snapshot;
|
|
905
|
+
try {
|
|
906
|
+
snapshot = await readSnapshot(context.store, head.root);
|
|
907
|
+
} catch {
|
|
908
|
+
record = await refresh(context, configUrl);
|
|
909
|
+
head = await (await boundPointer(context, configUrl, record)).read();
|
|
910
|
+
snapshot = await readSnapshot(context.store, head.root);
|
|
486
911
|
}
|
|
487
|
-
const
|
|
488
|
-
|
|
489
|
-
context.diagnostic?.(`Refreshing swarm HEAD ${swarm}...`);
|
|
490
|
-
const head = await client.read();
|
|
491
|
-
const snapshot = await readSnapshot(context.store, head.root);
|
|
492
|
-
const verifiedAt = Date.now();
|
|
912
|
+
const current = await local(context, configUrl),
|
|
913
|
+
verifiedAt = Date.now();
|
|
493
914
|
withState(context, (state) => {
|
|
494
|
-
state.saveSwarmHeader(
|
|
495
|
-
|
|
496
|
-
descriptor: data,
|
|
915
|
+
state.saveSwarmHeader(record.account, configUrl, {
|
|
916
|
+
head,
|
|
497
917
|
snapshot: Buffer.from(dagCbor.encode(snapshot)).toString("base64"),
|
|
498
918
|
verifiedAt,
|
|
919
|
+
headId: current.config!.headId,
|
|
499
920
|
});
|
|
500
921
|
});
|
|
501
922
|
return {
|
|
502
923
|
head,
|
|
503
924
|
snapshot,
|
|
925
|
+
config: current.config!,
|
|
504
926
|
source: "network",
|
|
505
927
|
verifiedAt: new Date(verifiedAt).toISOString(),
|
|
506
928
|
};
|
|
507
929
|
}
|
|
508
930
|
export async function addSwarm(
|
|
509
931
|
context: SwarmContext,
|
|
510
|
-
|
|
932
|
+
url: string,
|
|
511
933
|
alias?: string,
|
|
512
934
|
): Promise<void> {
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
state.checkSwarmAlias(alias, swarm);
|
|
517
|
-
});
|
|
518
|
-
await swarmHeader(context, swarm, true);
|
|
519
|
-
withState(context, (state) => state.registerSwarm(swarm, alias));
|
|
935
|
+
canonicalSwarmUrl(url);
|
|
936
|
+
await readyMembership(context, url);
|
|
937
|
+
withState(context, (state) => state.registerSwarm(url, alias));
|
|
520
938
|
}
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
const client = await pointer(context, swarm);
|
|
535
|
-
for (let attempt = 0; attempt < 8; attempt++) {
|
|
536
|
-
const head = await client.read();
|
|
537
|
-
const current = await readSnapshot(context.store, head.root);
|
|
538
|
-
const properties = {
|
|
539
|
-
...current.properties,
|
|
540
|
-
...(changes.name !== undefined ? { name: changes.name } : {}),
|
|
541
|
-
...(changes.description !== undefined
|
|
542
|
-
? { description: changes.description }
|
|
543
|
-
: {}),
|
|
544
|
-
};
|
|
545
|
-
const state = await writeSnapshot(context.store, {
|
|
546
|
-
...current,
|
|
547
|
-
properties,
|
|
548
|
-
});
|
|
549
|
-
const next = await publication(context.store, state, head.root);
|
|
550
|
-
await provideContent(
|
|
551
|
-
context.store,
|
|
552
|
-
[next],
|
|
553
|
-
context.provider,
|
|
554
|
-
context.fetch,
|
|
555
|
-
);
|
|
556
|
-
withState(context, (state) => {
|
|
557
|
-
state.invalidateSwarmHeader(swarm);
|
|
558
|
-
});
|
|
559
|
-
try {
|
|
560
|
-
const written = await client.write(
|
|
561
|
-
next,
|
|
562
|
-
head.revision,
|
|
563
|
-
toBase64Url(randomBytes(18)),
|
|
564
|
-
);
|
|
565
|
-
return {
|
|
566
|
-
cid: swarm,
|
|
567
|
-
root: written.root,
|
|
568
|
-
revision: written.revision,
|
|
569
|
-
properties,
|
|
570
|
-
};
|
|
571
|
-
} catch (cause) {
|
|
572
|
-
if (!(cause instanceof PointerApiError) || cause.status !== 412)
|
|
573
|
-
throw new HostError(
|
|
574
|
-
`Swarm information update failed or its result is unknown: ${message(cause)}. Publication ${next} remains retained; authenticate swarm status before retrying.`,
|
|
575
|
-
1,
|
|
576
|
-
"HEAD_UNCONFIRMED",
|
|
577
|
-
"publication",
|
|
578
|
-
);
|
|
579
|
-
context.diagnostic?.(
|
|
580
|
-
"Swarm HEAD changed; retrying information update with the latest Names and properties.",
|
|
581
|
-
);
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
throw new HostError(
|
|
585
|
-
"Swarm HEAD is busy; information update could not complete. Retry later.",
|
|
586
|
-
1,
|
|
587
|
-
"HEAD_BUSY",
|
|
588
|
-
"publication",
|
|
589
|
-
);
|
|
939
|
+
interface Creation {
|
|
940
|
+
owner: string;
|
|
941
|
+
service: string;
|
|
942
|
+
encryption: EncryptionIdentity;
|
|
943
|
+
key: string;
|
|
944
|
+
name: string;
|
|
945
|
+
description: string;
|
|
946
|
+
createdAt: string;
|
|
947
|
+
scopeId?: string;
|
|
948
|
+
headId?: string;
|
|
949
|
+
profileId?: string;
|
|
950
|
+
configId?: string;
|
|
951
|
+
config?: Organization;
|
|
590
952
|
}
|
|
591
|
-
export async function
|
|
953
|
+
export async function createSwarm(
|
|
592
954
|
context: SwarmContext,
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
955
|
+
options: {
|
|
956
|
+
service?: string;
|
|
957
|
+
resume?: string;
|
|
958
|
+
properties?: { name: string; description: string };
|
|
959
|
+
alias?: string;
|
|
960
|
+
addresses?: string[];
|
|
961
|
+
} = {},
|
|
962
|
+
): Promise<string> {
|
|
596
963
|
if (
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
const code = character.charCodeAt(0);
|
|
601
|
-
return code < 32 || code === 127;
|
|
602
|
-
})
|
|
964
|
+
(options.properties?.name.length ?? 0) > 256 ||
|
|
965
|
+
(options.properties?.description.length ?? 0) > 2048 ||
|
|
966
|
+
(options.addresses?.length ?? 0) > 16
|
|
603
967
|
)
|
|
604
968
|
throw new HostError(
|
|
605
|
-
"
|
|
606
|
-
2,
|
|
969
|
+
"Organization name/description or address limit exceeded.",
|
|
607
970
|
);
|
|
608
|
-
const
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
971
|
+
const service = canonicalService(context.pointerService ?? "");
|
|
972
|
+
if (
|
|
973
|
+
!context.pointerApiKey ||
|
|
974
|
+
(options.service && options.service !== service)
|
|
975
|
+
)
|
|
976
|
+
throw new HostError(
|
|
977
|
+
"Configure origin-bound pointers.service/apiKey before creating a Scope.",
|
|
978
|
+
);
|
|
979
|
+
const identity = await contextIdentity(context, true),
|
|
980
|
+
op = options.resume ?? uniqueId(),
|
|
981
|
+
dir = await directory(context);
|
|
982
|
+
if (!POINTER_ID_PATTERN.test(op))
|
|
983
|
+
throw new HostError("Invalid creation resume ID.");
|
|
984
|
+
return withPrivateLock(dir, async () => {
|
|
985
|
+
let record = await readPrivate<Creation>(dir, `creation-${op}.json`);
|
|
986
|
+
if (options.alias !== undefined) {
|
|
987
|
+
const existingUrl =
|
|
988
|
+
record?.scopeId && record.configId
|
|
989
|
+
? `${service}/v1/${record.scopeId}/${record.configId}`
|
|
990
|
+
: undefined;
|
|
991
|
+
withState(context, (state) => {
|
|
992
|
+
state.checkSwarmAlias(options.alias!, existingUrl);
|
|
993
|
+
});
|
|
994
|
+
}
|
|
995
|
+
if (!record) {
|
|
996
|
+
if (options.resume) throw new HostError("Unknown creation operation.");
|
|
997
|
+
record = {
|
|
998
|
+
owner: identity.publicKey,
|
|
999
|
+
service,
|
|
1000
|
+
encryption: encryptionIdentity(),
|
|
1001
|
+
key: randomBytes(32).toString("hex"),
|
|
1002
|
+
name: options.properties?.name ?? "",
|
|
1003
|
+
description: options.properties?.description ?? "",
|
|
1004
|
+
createdAt: new Date().toISOString(),
|
|
1005
|
+
};
|
|
1006
|
+
await writePrivate(dir, `creation-${op}.json`, record);
|
|
1007
|
+
}
|
|
1008
|
+
if (record.owner !== identity.publicKey || record.service !== service)
|
|
1009
|
+
throw new HostError("Creation belongs to another Owner or service.");
|
|
1010
|
+
const save = () => writePrivate(dir, `creation-${op}.json`, record);
|
|
1011
|
+
const api = client(context, service, identity);
|
|
626
1012
|
try {
|
|
627
|
-
|
|
1013
|
+
if (!record.scopeId) {
|
|
1014
|
+
record.scopeId = (await api.createScope(`${op}.scope`)).id;
|
|
1015
|
+
await save();
|
|
1016
|
+
}
|
|
1017
|
+
const node = await prepareNode(
|
|
1018
|
+
context.stateDir,
|
|
1019
|
+
nodeScope(service, record.scopeId),
|
|
1020
|
+
context.listen,
|
|
1021
|
+
);
|
|
1022
|
+
if (!record.headId) {
|
|
1023
|
+
const root = await publication(
|
|
1024
|
+
context.store,
|
|
1025
|
+
await writeSnapshot(context.store, { v: 2, names: {} }),
|
|
1026
|
+
);
|
|
1027
|
+
record.headId = (
|
|
1028
|
+
await api.create(record.scopeId, { root }, `${op}.head`)
|
|
1029
|
+
).id;
|
|
1030
|
+
await save();
|
|
1031
|
+
}
|
|
1032
|
+
if (!record.profileId) {
|
|
1033
|
+
const addresses = options.addresses ?? node.addresses;
|
|
1034
|
+
for (const address of addresses) validateAddress(address, node.peerId);
|
|
1035
|
+
record.profileId = (
|
|
1036
|
+
await api.create(
|
|
1037
|
+
record.scopeId,
|
|
1038
|
+
{ name: record.name, addresses, updatedAt: record.createdAt },
|
|
1039
|
+
`${op}.profile`,
|
|
1040
|
+
)
|
|
1041
|
+
).id;
|
|
1042
|
+
await save();
|
|
1043
|
+
}
|
|
1044
|
+
if (!record.config) {
|
|
1045
|
+
record.config = {
|
|
1046
|
+
v: 1,
|
|
1047
|
+
scopeId: record.scopeId,
|
|
1048
|
+
owner: identity.publicKey,
|
|
1049
|
+
name: record.name,
|
|
1050
|
+
description: record.description,
|
|
1051
|
+
headId: record.headId,
|
|
1052
|
+
epoch: 1,
|
|
1053
|
+
bootstrap: options.addresses ?? node.addresses,
|
|
1054
|
+
members: [
|
|
1055
|
+
{
|
|
1056
|
+
publicKey: identity.publicKey,
|
|
1057
|
+
encryptionKey: record.encryption.publicKey,
|
|
1058
|
+
peerId: node.peerId,
|
|
1059
|
+
profileId: record.profileId,
|
|
1060
|
+
requestId: op,
|
|
1061
|
+
approval: null,
|
|
1062
|
+
},
|
|
1063
|
+
],
|
|
1064
|
+
};
|
|
1065
|
+
await save();
|
|
1066
|
+
}
|
|
1067
|
+
if (!record.configId) {
|
|
1068
|
+
record.configId = (
|
|
1069
|
+
await api.create(record.scopeId, record.config, `${op}.config`)
|
|
1070
|
+
).id;
|
|
1071
|
+
await save();
|
|
1072
|
+
}
|
|
1073
|
+
const configUrl = `${service}/v1/${record.scopeId}/${record.configId}`;
|
|
1074
|
+
const cert = await issueScopeCertificate(
|
|
1075
|
+
{
|
|
1076
|
+
v: 1,
|
|
1077
|
+
service,
|
|
1078
|
+
scopeId: record.scopeId,
|
|
1079
|
+
subject: identity.publicKey,
|
|
1080
|
+
write: [record.headId, record.profileId],
|
|
1081
|
+
},
|
|
1082
|
+
await importPrivateKeyPkcs8(identity.privateKey),
|
|
1083
|
+
);
|
|
1084
|
+
const membership: LocalMembership = {
|
|
1085
|
+
configUrl,
|
|
1086
|
+
owner: identity.publicKey,
|
|
1087
|
+
account: identity.publicKey,
|
|
1088
|
+
encryption: record.encryption,
|
|
1089
|
+
config: record.config,
|
|
1090
|
+
secret: { certificate: cert, swarmKey: record.key },
|
|
1091
|
+
};
|
|
1092
|
+
await saveLocal(context, membership);
|
|
1093
|
+
await activate(context, membership);
|
|
1094
|
+
const head = await api.read(record.scopeId, record.headId);
|
|
1095
|
+
await provideContent(
|
|
1096
|
+
context.store,
|
|
1097
|
+
[headValue(head).root],
|
|
1098
|
+
context.node!.api,
|
|
1099
|
+
context.fetch,
|
|
1100
|
+
context.node!.auth,
|
|
1101
|
+
);
|
|
1102
|
+
withState(context, (state) =>
|
|
1103
|
+
state.registerSwarm(configUrl, options.alias),
|
|
1104
|
+
);
|
|
1105
|
+
return configUrl;
|
|
628
1106
|
} catch (cause) {
|
|
629
1107
|
throw new HostError(
|
|
630
|
-
`
|
|
1108
|
+
`Swarm creation incomplete: ${message(cause)}. Resume only with swarm create --resume ${op}.`,
|
|
631
1109
|
);
|
|
632
|
-
}
|
|
633
|
-
searched.push({
|
|
634
|
-
cid: swarm.cid,
|
|
635
|
-
source: header.source,
|
|
636
|
-
root: header.head.root,
|
|
637
|
-
revision: header.head.revision,
|
|
638
|
-
verifiedAt: header.verifiedAt,
|
|
639
|
-
});
|
|
640
|
-
for (const name of Object.keys(header.snapshot.names).sort())
|
|
641
|
-
if (name.toLowerCase().includes(query.toLowerCase()))
|
|
642
|
-
matches.push({
|
|
643
|
-
swarm: swarm.cid,
|
|
644
|
-
alias: swarm.alias,
|
|
645
|
-
name,
|
|
646
|
-
reference: `ipab://${CID.parse(swarm.cid).toV1().toString()}/${name
|
|
647
|
-
.split("/")
|
|
648
|
-
.map((segment) =>
|
|
649
|
-
encodeURIComponent(segment).replace(
|
|
650
|
-
/[!'()*]/g,
|
|
651
|
-
(character) =>
|
|
652
|
-
`%${character.charCodeAt(0).toString(16).toUpperCase()}`,
|
|
653
|
-
),
|
|
654
|
-
)
|
|
655
|
-
.join("/")}`,
|
|
656
|
-
targetCid: header.snapshot.names[name]!.toString(),
|
|
657
|
-
});
|
|
658
|
-
}
|
|
659
|
-
return { matches, swarms: searched };
|
|
660
|
-
}
|
|
661
|
-
async function publication(
|
|
662
|
-
store: IpfsStore,
|
|
663
|
-
state: string,
|
|
664
|
-
previous?: string,
|
|
665
|
-
): Promise<string> {
|
|
666
|
-
const bytes = dagCbor.encode({
|
|
667
|
-
v: 1,
|
|
668
|
-
state: CID.parse(state),
|
|
669
|
-
...(previous ? { previous: CID.parse(previous) } : {}),
|
|
1110
|
+
}
|
|
670
1111
|
});
|
|
671
|
-
const cid = CID.createV1(dagCbor.code, await sha256.digest(bytes));
|
|
672
|
-
await store.put(cid, bytes);
|
|
673
|
-
return cid.toString();
|
|
674
1112
|
}
|
|
675
|
-
|
|
1113
|
+
const requestMessage = (
|
|
1114
|
+
request: Omit<MembershipRequest, "signature" | "peerSignature">,
|
|
1115
|
+
) => JSON.stringify(["IPAB-REQUEST/1", request]);
|
|
1116
|
+
export async function exportRequest(
|
|
676
1117
|
context: SwarmContext,
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
): Promise<
|
|
680
|
-
const
|
|
681
|
-
|
|
682
|
-
const
|
|
683
|
-
const handle = await open(temporary, "wx", 0o600);
|
|
1118
|
+
url: string,
|
|
1119
|
+
output: string,
|
|
1120
|
+
): Promise<string> {
|
|
1121
|
+
const record = await local(context, canonicalSwarmUrl(url));
|
|
1122
|
+
if (!record.request) throw new HostError("Owner membership has no Request.");
|
|
1123
|
+
const handle = await open(output, "wx", 0o600);
|
|
684
1124
|
try {
|
|
685
|
-
await
|
|
686
|
-
await handle.writeFile(JSON.stringify(record));
|
|
1125
|
+
await handle.writeFile(`${JSON.stringify(record.request, null, 2)}\n`);
|
|
687
1126
|
await handle.sync();
|
|
688
|
-
await handle.close();
|
|
689
|
-
await rename(temporary, target);
|
|
690
1127
|
} finally {
|
|
691
1128
|
await handle.close();
|
|
692
|
-
await unlink(temporary).catch((cause: unknown) => {
|
|
693
|
-
if (!nodeError(cause, "ENOENT"))
|
|
694
|
-
return Promise.reject(
|
|
695
|
-
cause instanceof Error
|
|
696
|
-
? cause
|
|
697
|
-
: new HostError("Owned creation temporary-file cleanup failed."),
|
|
698
|
-
);
|
|
699
|
-
return undefined;
|
|
700
|
-
});
|
|
701
1129
|
}
|
|
1130
|
+
return record.request.id;
|
|
702
1131
|
}
|
|
703
|
-
export async function
|
|
1132
|
+
export async function requestMembership(
|
|
704
1133
|
context: SwarmContext,
|
|
1134
|
+
url: string,
|
|
705
1135
|
options: {
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
properties?: { name: string; description: string };
|
|
1136
|
+
ownerFingerprint: string;
|
|
1137
|
+
name?: string;
|
|
709
1138
|
alias?: string;
|
|
710
|
-
|
|
1139
|
+
output?: string;
|
|
1140
|
+
},
|
|
711
1141
|
): Promise<string> {
|
|
712
|
-
if (options.
|
|
713
|
-
|
|
714
|
-
|
|
1142
|
+
if ((options.name?.length ?? 0) > 256)
|
|
1143
|
+
throw new HostError("Request name exceeds 256 characters.");
|
|
1144
|
+
canonicalSwarmUrl(url);
|
|
1145
|
+
const identity = await contextIdentity(context, true);
|
|
1146
|
+
const { config } = await fetchConfig(context, url);
|
|
1147
|
+
if (options.ownerFingerprint !== (await keyId(config.owner)))
|
|
1148
|
+
throw new HostError(
|
|
1149
|
+
`Trusted Owner fingerprint required; observed ${await keyId(config.owner)}. Verify through an independent trusted channel.`,
|
|
1150
|
+
);
|
|
1151
|
+
const dir = await directory(context);
|
|
1152
|
+
await withPrivateLock(dir, async () => {
|
|
1153
|
+
if (options.alias !== undefined)
|
|
1154
|
+
withState(context, (state) => {
|
|
1155
|
+
state.checkSwarmAlias(options.alias!, url);
|
|
1156
|
+
});
|
|
1157
|
+
const existing = await readPrivate<LocalMembership>(dir, localName(url));
|
|
1158
|
+
if (existing) {
|
|
1159
|
+
if (
|
|
1160
|
+
existing.account !== identity.publicKey ||
|
|
1161
|
+
existing.owner !== config.owner
|
|
1162
|
+
)
|
|
1163
|
+
throw new HostError(
|
|
1164
|
+
"Existing Request belongs to a different identity or pinned Owner.",
|
|
1165
|
+
);
|
|
1166
|
+
if (!existing.request)
|
|
1167
|
+
throw new HostError("This identity is already the Owner.");
|
|
1168
|
+
withState(context, (state) => state.registerSwarm(url, options.alias));
|
|
1169
|
+
return;
|
|
1170
|
+
}
|
|
1171
|
+
const node = await prepareNode(
|
|
1172
|
+
context.stateDir,
|
|
1173
|
+
nodeScope(coordinates(url).service, config.scopeId),
|
|
1174
|
+
context.listen,
|
|
1175
|
+
),
|
|
1176
|
+
encryption = encryptionIdentity();
|
|
1177
|
+
const body = {
|
|
1178
|
+
v: 1 as const,
|
|
1179
|
+
id: uniqueId(),
|
|
1180
|
+
requestedAt: new Date().toISOString(),
|
|
1181
|
+
configUrl: url,
|
|
1182
|
+
scopeId: config.scopeId,
|
|
1183
|
+
owner: config.owner,
|
|
1184
|
+
name: options.name ?? "",
|
|
1185
|
+
member: identity.publicKey,
|
|
1186
|
+
encryptionKey: encryption.publicKey,
|
|
1187
|
+
peerId: node.peerId,
|
|
1188
|
+
peerPublicKey: "",
|
|
1189
|
+
};
|
|
1190
|
+
body.peerPublicKey = (await peerProof(node, "identity")).publicKey;
|
|
1191
|
+
const proof = await peerProof(node, requestMessage(body));
|
|
1192
|
+
const peerSignature = proof.signature;
|
|
1193
|
+
const request = {
|
|
1194
|
+
...body,
|
|
1195
|
+
peerSignature,
|
|
1196
|
+
signature: await signEd25519(
|
|
1197
|
+
await importPrivateKeyPkcs8(identity.privateKey),
|
|
1198
|
+
JSON.stringify(["IPAB-REQUEST-MEMBER/1", body, peerSignature]),
|
|
1199
|
+
),
|
|
1200
|
+
};
|
|
1201
|
+
await saveLocal(context, {
|
|
1202
|
+
configUrl: url,
|
|
1203
|
+
owner: config.owner,
|
|
1204
|
+
account: identity.publicKey,
|
|
1205
|
+
encryption,
|
|
1206
|
+
request,
|
|
715
1207
|
});
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
1208
|
+
withState(context, (state) => state.registerSwarm(url, options.alias));
|
|
1209
|
+
});
|
|
1210
|
+
if (options.output) await exportRequest(context, url, options.output);
|
|
1211
|
+
return (await local(context, url)).request!.id;
|
|
1212
|
+
}
|
|
1213
|
+
export async function verifiedRequest(
|
|
1214
|
+
path: string,
|
|
1215
|
+
): Promise<MembershipRequest> {
|
|
1216
|
+
if ((await lstat(path)).size > 16 * 1024)
|
|
1217
|
+
throw new HostError("Request file exceeds 16 KiB.");
|
|
1218
|
+
return verifyRequest(JSON.parse(await readFile(path, "utf8")) as unknown);
|
|
1219
|
+
}
|
|
1220
|
+
async function verifyRequest(input: unknown): Promise<MembershipRequest> {
|
|
1221
|
+
const fields = object(input, "Request");
|
|
1222
|
+
if (
|
|
1223
|
+
Object.keys(fields).sort().join() !==
|
|
1224
|
+
"configUrl,encryptionKey,id,member,name,owner,peerId,peerPublicKey,peerSignature,requestedAt,scopeId,signature,v" ||
|
|
1225
|
+
fields.v !== 1
|
|
1226
|
+
)
|
|
1227
|
+
throw new HostError("Invalid self-contained Request format.");
|
|
1228
|
+
const {
|
|
1229
|
+
id,
|
|
1230
|
+
member,
|
|
1231
|
+
owner,
|
|
1232
|
+
name,
|
|
1233
|
+
encryptionKey,
|
|
1234
|
+
requestedAt,
|
|
1235
|
+
configUrl,
|
|
1236
|
+
scopeId,
|
|
1237
|
+
peerId: requestPeer,
|
|
1238
|
+
peerPublicKey,
|
|
1239
|
+
peerSignature,
|
|
1240
|
+
signature,
|
|
1241
|
+
} = fields;
|
|
1242
|
+
if (
|
|
1243
|
+
typeof id !== "string" ||
|
|
1244
|
+
!POINTER_ID_PATTERN.test(id) ||
|
|
1245
|
+
!isCanonicalPublicKey(member) ||
|
|
1246
|
+
!isCanonicalPublicKey(owner) ||
|
|
1247
|
+
typeof name !== "string" ||
|
|
1248
|
+
name.length > 256 ||
|
|
1249
|
+
typeof encryptionKey !== "string" ||
|
|
1250
|
+
encryptionKey.length > 256 ||
|
|
1251
|
+
typeof requestedAt !== "string" ||
|
|
1252
|
+
!Number.isFinite(Date.parse(requestedAt)) ||
|
|
1253
|
+
typeof configUrl !== "string" ||
|
|
1254
|
+
typeof scopeId !== "string" ||
|
|
1255
|
+
coordinates(configUrl).scopeId !== scopeId ||
|
|
1256
|
+
!isCanonicalPublicKey(peerPublicKey) ||
|
|
1257
|
+
typeof requestPeer !== "string" ||
|
|
1258
|
+
peerId(peerPublicKey) !== requestPeer ||
|
|
1259
|
+
typeof peerSignature !== "string" ||
|
|
1260
|
+
typeof signature !== "string"
|
|
1261
|
+
)
|
|
1262
|
+
throw new HostError("Invalid self-contained Request.");
|
|
1263
|
+
const body: Omit<MembershipRequest, "signature" | "peerSignature"> = {
|
|
1264
|
+
v: 1,
|
|
1265
|
+
id,
|
|
1266
|
+
requestedAt,
|
|
1267
|
+
configUrl,
|
|
1268
|
+
scopeId,
|
|
1269
|
+
owner,
|
|
1270
|
+
name,
|
|
1271
|
+
member,
|
|
1272
|
+
encryptionKey,
|
|
1273
|
+
peerId: requestPeer,
|
|
1274
|
+
peerPublicKey,
|
|
719
1275
|
};
|
|
720
|
-
if (
|
|
1276
|
+
if (
|
|
1277
|
+
!(await verifyEd25519(
|
|
1278
|
+
await importPublicKeyRaw(peerPublicKey),
|
|
1279
|
+
peerSignature,
|
|
1280
|
+
requestMessage(body),
|
|
1281
|
+
)) ||
|
|
1282
|
+
!(await verifyEd25519(
|
|
1283
|
+
await importPublicKeyRaw(member),
|
|
1284
|
+
signature,
|
|
1285
|
+
JSON.stringify(["IPAB-REQUEST-MEMBER/1", body, peerSignature]),
|
|
1286
|
+
))
|
|
1287
|
+
)
|
|
1288
|
+
throw new HostError(
|
|
1289
|
+
"Request member or PeerID possession proof is invalid.",
|
|
1290
|
+
);
|
|
1291
|
+
return { ...body, peerSignature, signature };
|
|
1292
|
+
}
|
|
1293
|
+
export async function membershipRequestSummary(
|
|
1294
|
+
request: MembershipRequest,
|
|
1295
|
+
): Promise<string> {
|
|
1296
|
+
return [
|
|
1297
|
+
`Request: ${request.id}`,
|
|
1298
|
+
`Target: ${request.configUrl}`,
|
|
1299
|
+
`Name: ${JSON.stringify(request.name)}`,
|
|
1300
|
+
`Member public key: ${request.member}`,
|
|
1301
|
+
`Member fingerprint: ${await keyId(request.member)}`,
|
|
1302
|
+
`PeerID: ${request.peerId}`,
|
|
1303
|
+
`Requested at: ${JSON.stringify(request.requestedAt)}`,
|
|
1304
|
+
"Verify the applicant through an independent trusted channel before approving.",
|
|
1305
|
+
].join("\n");
|
|
1306
|
+
}
|
|
1307
|
+
interface ApprovalOperation {
|
|
1308
|
+
owner: string;
|
|
1309
|
+
request: MembershipRequest;
|
|
1310
|
+
operation: string;
|
|
1311
|
+
createdAt: string;
|
|
1312
|
+
profileId?: string;
|
|
1313
|
+
completed?: boolean;
|
|
1314
|
+
value?: Organization;
|
|
1315
|
+
baseline?: string;
|
|
1316
|
+
}
|
|
1317
|
+
async function ownerCheck(
|
|
1318
|
+
context: SwarmContext,
|
|
1319
|
+
url: string,
|
|
1320
|
+
): Promise<{ identity: Identity; record: LocalMembership }> {
|
|
1321
|
+
const identity = await contextIdentity(context),
|
|
1322
|
+
record = await readyMembership(context, url);
|
|
1323
|
+
if (identity.publicKey !== record.owner)
|
|
721
1324
|
throw new HostError(
|
|
722
|
-
"
|
|
1325
|
+
"Only the pinned Owner can manage membership or organization configuration.",
|
|
723
1326
|
);
|
|
724
|
-
|
|
1327
|
+
const transition = await readPrivate<Removal>(
|
|
1328
|
+
await directory(context),
|
|
1329
|
+
`transition-${localName(url)}`,
|
|
1330
|
+
);
|
|
1331
|
+
if (transition && !transition.completed)
|
|
725
1332
|
throw new HostError(
|
|
726
|
-
"
|
|
1333
|
+
"Organization switch incomplete: run swarm resume for this URL before other Owner changes.",
|
|
727
1334
|
);
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
1335
|
+
return { identity, record };
|
|
1336
|
+
}
|
|
1337
|
+
export async function approveMembership(
|
|
1338
|
+
context: SwarmContext,
|
|
1339
|
+
input: string | MembershipRequest,
|
|
1340
|
+
options: { again?: boolean } = {},
|
|
1341
|
+
): Promise<string> {
|
|
1342
|
+
const request =
|
|
1343
|
+
typeof input === "string"
|
|
1344
|
+
? await verifiedRequest(input)
|
|
1345
|
+
: await verifyRequest(input),
|
|
1346
|
+
url = request.configUrl;
|
|
1347
|
+
const dir = await directory(context);
|
|
1348
|
+
return withPrivateLock(dir, async () => {
|
|
1349
|
+
const { identity, record } = await ownerCheck(context, url);
|
|
1350
|
+
if (request.owner !== identity.publicKey)
|
|
1351
|
+
throw new HostError("Request targets a different Owner.");
|
|
1352
|
+
let operation = await readPrivate<ApprovalOperation>(
|
|
1353
|
+
dir,
|
|
1354
|
+
`approval-${request.id}.json`,
|
|
744
1355
|
);
|
|
1356
|
+
if (operation && !operation.completed && options.again)
|
|
1357
|
+
throw new HostError(
|
|
1358
|
+
"The prior approval is unconfirmed; resume the same Request without --again before starting a new explicit approval.",
|
|
1359
|
+
);
|
|
1360
|
+
if (operation?.completed && !options.again) {
|
|
1361
|
+
if (
|
|
1362
|
+
operation.owner !== identity.publicKey ||
|
|
1363
|
+
JSON.stringify(operation.request) !== JSON.stringify(request)
|
|
1364
|
+
)
|
|
1365
|
+
throw new HostError(
|
|
1366
|
+
"This Request ID differs from advisory history; manually verify it and use --again for a new explicit approval.",
|
|
1367
|
+
);
|
|
1368
|
+
context.diagnostic?.(
|
|
1369
|
+
`Previously approved Request ${request.id} from ${request.requestedAt}. History is advisory, not a revocation boundary.`,
|
|
1370
|
+
);
|
|
1371
|
+
context.diagnostic?.(
|
|
1372
|
+
"Reusing the completed approval result without granting new authority. Use --again only after fresh manual confirmation.",
|
|
1373
|
+
);
|
|
1374
|
+
return url;
|
|
1375
|
+
}
|
|
1376
|
+
if (!operation || options.again) {
|
|
1377
|
+
operation = {
|
|
1378
|
+
owner: identity.publicKey,
|
|
1379
|
+
request,
|
|
1380
|
+
operation: uniqueId(),
|
|
1381
|
+
createdAt: new Date().toISOString(),
|
|
1382
|
+
};
|
|
1383
|
+
await writePrivate(dir, `approval-${request.id}.json`, operation);
|
|
1384
|
+
}
|
|
1385
|
+
if (
|
|
1386
|
+
operation.owner !== identity.publicKey ||
|
|
1387
|
+
JSON.stringify(operation.request) !== JSON.stringify(request)
|
|
1388
|
+
)
|
|
1389
|
+
throw new HostError("Saved approval does not match this exact Request.");
|
|
1390
|
+
const save = () =>
|
|
1391
|
+
writePrivate(dir, `approval-${request.id}.json`, operation);
|
|
1392
|
+
const c = coordinates(url),
|
|
1393
|
+
api = client(context, c.service, identity);
|
|
1394
|
+
const fetched = await fetchConfig(context, url);
|
|
745
1395
|
if (
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
typeof value.root !== "string" ||
|
|
749
|
-
!Number.isSafeInteger(value.createdAt) ||
|
|
750
|
-
(value.id !== undefined && typeof value.id !== "string") ||
|
|
751
|
-
(value.cid !== undefined && typeof value.cid !== "string")
|
|
1396
|
+
fetched.config.epoch !== record.config!.epoch ||
|
|
1397
|
+
fetched.config.headId !== record.config!.headId
|
|
752
1398
|
)
|
|
753
1399
|
throw new HostError(
|
|
754
|
-
"
|
|
1400
|
+
"Owner local keys are not synchronized with this organization epoch.",
|
|
755
1401
|
);
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
state.checkSwarmAlias(options.alias!, record.cid);
|
|
760
|
-
});
|
|
761
|
-
if (!record.id && Date.now() - record.createdAt >= 10 * 60 * 1000)
|
|
762
|
-
throw new HostError(
|
|
763
|
-
"Unconfirmed creation is beyond the service's minimum idempotency window. Ask the operator to recover it; do not allocate another pointer.",
|
|
1402
|
+
if (!operation.profileId) {
|
|
1403
|
+
const previousMember = fetched.config.members.find(
|
|
1404
|
+
(m) => m.publicKey === request.member,
|
|
764
1405
|
);
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
1406
|
+
if (previousMember && previousMember.peerId !== request.peerId)
|
|
1407
|
+
throw new HostError(
|
|
1408
|
+
"PeerID rebinding requires removing the old membership first, then explicitly approving this Request.",
|
|
1409
|
+
);
|
|
1410
|
+
const existingProfile = fetched.config.members.find(
|
|
1411
|
+
(m) => m.publicKey === request.member,
|
|
1412
|
+
)?.profileId;
|
|
1413
|
+
if (existingProfile) operation.profileId = existingProfile;
|
|
1414
|
+
if (!operation.profileId)
|
|
1415
|
+
operation.profileId = (
|
|
1416
|
+
await api.create(
|
|
1417
|
+
c.scopeId,
|
|
1418
|
+
{
|
|
1419
|
+
name: request.name,
|
|
1420
|
+
addresses: [],
|
|
1421
|
+
updatedAt: operation.createdAt,
|
|
1422
|
+
},
|
|
1423
|
+
`${operation.operation}.profile`,
|
|
1424
|
+
)
|
|
1425
|
+
).id;
|
|
1426
|
+
await save();
|
|
1427
|
+
}
|
|
1428
|
+
if (!operation.value) {
|
|
1429
|
+
const member: Member = {
|
|
1430
|
+
publicKey: request.member,
|
|
1431
|
+
encryptionKey: request.encryptionKey,
|
|
1432
|
+
peerId: request.peerId,
|
|
1433
|
+
profileId: operation.profileId,
|
|
1434
|
+
requestId: request.id,
|
|
1435
|
+
approval: null,
|
|
1436
|
+
};
|
|
1437
|
+
const config = {
|
|
1438
|
+
...fetched.config,
|
|
1439
|
+
members: fetched.config.members.filter(
|
|
1440
|
+
(m) => m.publicKey !== request.member,
|
|
1441
|
+
),
|
|
1442
|
+
};
|
|
1443
|
+
if (config.members.some((m) => m.peerId === request.peerId))
|
|
1444
|
+
throw new HostError("PeerID is already assigned to another member.");
|
|
1445
|
+
if (request.member === identity.publicKey)
|
|
1446
|
+
throw new HostError("Owner cannot request member approval.");
|
|
1447
|
+
if (config.members.length >= 16)
|
|
1448
|
+
throw new HostError("Organization member limit is 16.");
|
|
1449
|
+
const certificate = await issueScopeCertificate(
|
|
1450
|
+
{
|
|
1451
|
+
v: 1,
|
|
1452
|
+
service: c.service,
|
|
1453
|
+
scopeId: c.scopeId,
|
|
1454
|
+
subject: request.member,
|
|
1455
|
+
write: [config.headId, member.profileId],
|
|
793
1456
|
},
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
)
|
|
801
|
-
|
|
1457
|
+
await importPrivateKeyPkcs8(identity.privateKey),
|
|
1458
|
+
);
|
|
1459
|
+
member.approval = await sealApproval(
|
|
1460
|
+
binding(url, config, member),
|
|
1461
|
+
{ certificate, swarmKey: record.secret!.swarmKey },
|
|
1462
|
+
identity,
|
|
1463
|
+
);
|
|
1464
|
+
config.members.push(member);
|
|
1465
|
+
operation.value = config;
|
|
1466
|
+
operation.baseline = fetched.variable.cid;
|
|
1467
|
+
await save();
|
|
802
1468
|
}
|
|
803
|
-
const
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
return cid;
|
|
819
|
-
} catch (cause) {
|
|
820
|
-
throw new HostError(
|
|
821
|
-
`Swarm creation was not confirmed: ${message(cause)}. ${record.id ? `Pointer ${record.id} is saved.` : "The same creation idempotency key is saved."} Recover with ipab swarm create --resume ${operation}; do not allocate a replacement pointer.`,
|
|
822
|
-
1,
|
|
823
|
-
"CREATE_UNCONFIRMED",
|
|
824
|
-
"authorization",
|
|
825
|
-
);
|
|
826
|
-
}
|
|
1469
|
+
const observed = await api.read(c.scopeId, c.configId);
|
|
1470
|
+
if (observed.cid !== (await valueCid(operation.value)))
|
|
1471
|
+
await api.write(
|
|
1472
|
+
c.scopeId,
|
|
1473
|
+
c.configId,
|
|
1474
|
+
operation.value,
|
|
1475
|
+
operation.baseline!,
|
|
1476
|
+
`${operation.operation}.publish`,
|
|
1477
|
+
);
|
|
1478
|
+
operation.completed = true;
|
|
1479
|
+
await save();
|
|
1480
|
+
record.config = operation.value;
|
|
1481
|
+
await saveLocal(context, record);
|
|
1482
|
+
return url;
|
|
1483
|
+
});
|
|
827
1484
|
}
|
|
828
|
-
export async function
|
|
1485
|
+
export async function updateSwarm(
|
|
829
1486
|
context: SwarmContext,
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
1487
|
+
url: string,
|
|
1488
|
+
changes: { name?: string; description?: string },
|
|
1489
|
+
) {
|
|
1490
|
+
if (
|
|
1491
|
+
(changes.name?.length ?? 0) > 256 ||
|
|
1492
|
+
(changes.description?.length ?? 0) > 2048
|
|
1493
|
+
)
|
|
1494
|
+
throw new HostError("Organization name/description limit exceeded.");
|
|
1495
|
+
const { identity, record } = await ownerCheck(context, url),
|
|
1496
|
+
c = coordinates(url);
|
|
1497
|
+
if (changes.name === undefined && changes.description === undefined)
|
|
1498
|
+
throw new HostError("swarm update requires name or description.");
|
|
1499
|
+
const fetched = await fetchConfig(context, url),
|
|
1500
|
+
config = { ...fetched.config, ...changes };
|
|
1501
|
+
const result = await client(context, c.service, identity).write(
|
|
1502
|
+
c.scopeId,
|
|
1503
|
+
c.configId,
|
|
1504
|
+
config,
|
|
1505
|
+
fetched.variable.cid,
|
|
1506
|
+
uniqueId(),
|
|
1507
|
+
);
|
|
1508
|
+
record.config = config;
|
|
1509
|
+
await saveLocal(context, record);
|
|
1510
|
+
return {
|
|
1511
|
+
configUrl: url,
|
|
1512
|
+
headId: config.headId,
|
|
1513
|
+
valueCid: result.cid,
|
|
1514
|
+
properties: { name: config.name, description: config.description },
|
|
840
1515
|
};
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
1516
|
+
}
|
|
1517
|
+
export async function updateProfile(
|
|
1518
|
+
context: SwarmContext,
|
|
1519
|
+
url: string,
|
|
1520
|
+
changes: { name?: string; addresses?: string[] },
|
|
1521
|
+
): Promise<Profile> {
|
|
1522
|
+
if (changes.name !== undefined && changes.name.length > 256)
|
|
1523
|
+
throw new HostError("Profile name exceeds 256 characters.");
|
|
1524
|
+
if ((changes.addresses?.length ?? 0) > 16)
|
|
1525
|
+
throw new HostError("Profile address limit is 16.");
|
|
1526
|
+
const record = await readyMembership(context, url),
|
|
1527
|
+
identity = await contextIdentity(context),
|
|
1528
|
+
c = coordinates(url);
|
|
1529
|
+
const member = record.config!.members.find(
|
|
1530
|
+
(m) => m.publicKey === identity.publicKey,
|
|
1531
|
+
)!;
|
|
1532
|
+
for (const address of changes.addresses ?? [])
|
|
1533
|
+
validateAddress(address, member.peerId);
|
|
1534
|
+
const api = client(
|
|
1535
|
+
context,
|
|
1536
|
+
c.service,
|
|
1537
|
+
identity,
|
|
1538
|
+
identity.publicKey === record.owner ? undefined : record.secret,
|
|
1539
|
+
);
|
|
1540
|
+
const existing = await api.read(c.scopeId, member.profileId);
|
|
1541
|
+
const profile = {
|
|
1542
|
+
...parseProfile(existing.value, member.peerId),
|
|
1543
|
+
...changes,
|
|
1544
|
+
updatedAt: new Date().toISOString(),
|
|
847
1545
|
};
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
1546
|
+
await api.write(
|
|
1547
|
+
c.scopeId,
|
|
1548
|
+
member.profileId,
|
|
1549
|
+
profile,
|
|
1550
|
+
existing.cid,
|
|
1551
|
+
uniqueId(),
|
|
854
1552
|
);
|
|
855
|
-
|
|
856
|
-
swarm,
|
|
857
|
-
member: identity.publicKey,
|
|
858
|
-
});
|
|
859
|
-
return cid;
|
|
1553
|
+
return profile;
|
|
860
1554
|
}
|
|
861
|
-
|
|
1555
|
+
interface Removal {
|
|
1556
|
+
owner: string;
|
|
1557
|
+
url: string;
|
|
1558
|
+
operation: string;
|
|
1559
|
+
member: string;
|
|
1560
|
+
oldConfig: Organization;
|
|
1561
|
+
configCid: string;
|
|
1562
|
+
newKey: string;
|
|
1563
|
+
oldKey: string;
|
|
1564
|
+
finalHead?: Variable;
|
|
1565
|
+
finalCaptured?: boolean;
|
|
1566
|
+
finalClosureRoots?: string[];
|
|
1567
|
+
newHeadId?: string;
|
|
1568
|
+
nextConfig?: Organization;
|
|
1569
|
+
published?: boolean;
|
|
1570
|
+
completed?: boolean;
|
|
1571
|
+
}
|
|
1572
|
+
export async function removeMember(
|
|
862
1573
|
context: SwarmContext,
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
): Promise<
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
member
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
const cid = await jsonBundle(context.store, responseType, {
|
|
886
|
-
swarm: request.swarm,
|
|
887
|
-
request: CID.parse(requestCid).toV1().toString(),
|
|
888
|
-
certificate,
|
|
1574
|
+
url: string,
|
|
1575
|
+
member: string,
|
|
1576
|
+
): Promise<void> {
|
|
1577
|
+
const dir = await directory(context);
|
|
1578
|
+
await withPrivateLock(dir, async () => {
|
|
1579
|
+
const { identity, record: membership } = await ownerCheck(context, url);
|
|
1580
|
+
if (member === identity.publicKey)
|
|
1581
|
+
throw new HostError("Owner removal/recovery is unsupported.");
|
|
1582
|
+
const fetched = await fetchConfig(context, url);
|
|
1583
|
+
if (!fetched.config.members.some((m) => m.publicKey === member))
|
|
1584
|
+
throw new HostError("Unknown member public key.");
|
|
1585
|
+
const record: Removal = {
|
|
1586
|
+
owner: identity.publicKey,
|
|
1587
|
+
url,
|
|
1588
|
+
operation: uniqueId(),
|
|
1589
|
+
member,
|
|
1590
|
+
oldConfig: fetched.config,
|
|
1591
|
+
configCid: fetched.variable.cid,
|
|
1592
|
+
oldKey: membership.secret!.swarmKey,
|
|
1593
|
+
newKey: randomBytes(32).toString("hex"),
|
|
1594
|
+
};
|
|
1595
|
+
await writePrivate(dir, `transition-${localName(url)}`, record);
|
|
889
1596
|
});
|
|
890
|
-
await
|
|
891
|
-
context.store,
|
|
892
|
-
[cid, requestCid, request.swarm],
|
|
893
|
-
context.provider,
|
|
894
|
-
context.fetch,
|
|
895
|
-
);
|
|
896
|
-
return cid;
|
|
1597
|
+
await resumeRemoval(context, url);
|
|
897
1598
|
}
|
|
898
|
-
export async function
|
|
1599
|
+
export async function resumeRemoval(
|
|
899
1600
|
context: SwarmContext,
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
const { request } = await verifiedRequest(context.store, response.request);
|
|
909
|
-
const identity = await accountIdentity(context.env);
|
|
910
|
-
const pending = object(
|
|
911
|
-
await saved(context, `${response.request}.pending.json`),
|
|
912
|
-
"pending request",
|
|
913
|
-
);
|
|
914
|
-
if (
|
|
915
|
-
request.swarm !== response.swarm ||
|
|
916
|
-
request.member !== identity.publicKey ||
|
|
917
|
-
pending.swarm !== response.swarm ||
|
|
918
|
-
pending.member !== identity.publicKey
|
|
919
|
-
)
|
|
920
|
-
throw new HostError(
|
|
921
|
-
"Response does not match this account's exact pending request and target swarm.",
|
|
922
|
-
);
|
|
923
|
-
const client = await pointer(context, response.swarm, response.certificate);
|
|
924
|
-
if (alias !== undefined)
|
|
925
|
-
withState(context, (state) => {
|
|
926
|
-
state.checkSwarmAlias(alias, response.swarm);
|
|
927
|
-
});
|
|
928
|
-
await client.read();
|
|
929
|
-
try {
|
|
930
|
-
await save(context, `${response.swarm}.json`, {
|
|
931
|
-
certificate: response.certificate,
|
|
932
|
-
request: response.request,
|
|
933
|
-
});
|
|
934
|
-
} catch (cause) {
|
|
935
|
-
if (!nodeError(cause, "EEXIST")) throw cause;
|
|
936
|
-
const previous = object(
|
|
937
|
-
await saved(context, `${response.swarm}.json`),
|
|
938
|
-
"membership",
|
|
1601
|
+
url: string,
|
|
1602
|
+
): Promise<void> {
|
|
1603
|
+
const dir = await directory(context);
|
|
1604
|
+
return withPrivateLock(dir, async () => {
|
|
1605
|
+
const identity = await contextIdentity(context);
|
|
1606
|
+
const record = await readPrivate<Removal>(
|
|
1607
|
+
dir,
|
|
1608
|
+
`transition-${localName(url)}`,
|
|
939
1609
|
);
|
|
940
|
-
if (
|
|
1610
|
+
if (!record || record.owner !== identity.publicKey || record.url !== url)
|
|
1611
|
+
throw new HostError("No resumable switch owned by this identity.");
|
|
1612
|
+
if (record.completed) return;
|
|
1613
|
+
const c = coordinates(url),
|
|
1614
|
+
api = client(context, c.service, identity);
|
|
1615
|
+
const save = () =>
|
|
1616
|
+
writePrivate(dir, `transition-${localName(url)}`, record);
|
|
1617
|
+
try {
|
|
1618
|
+
if (!record.finalHead) {
|
|
1619
|
+
record.finalHead = await api.freeze(
|
|
1620
|
+
c.scopeId,
|
|
1621
|
+
record.oldConfig.headId,
|
|
1622
|
+
`${record.operation}.freeze`,
|
|
1623
|
+
);
|
|
1624
|
+
await save();
|
|
1625
|
+
}
|
|
1626
|
+
// Fetch the frozen final DAG while the old private network is still connected.
|
|
1627
|
+
// A publication acknowledged immediately before freeze may not be cached on Owner.
|
|
1628
|
+
const membershipBeforeSwitch = await local(context, url);
|
|
1629
|
+
if (!record.finalCaptured) {
|
|
1630
|
+
membershipBeforeSwitch.config = record.oldConfig;
|
|
1631
|
+
membershipBeforeSwitch.secret!.swarmKey = record.oldKey;
|
|
1632
|
+
await activate(context, membershipBeforeSwitch);
|
|
1633
|
+
if (!record.finalClosureRoots) {
|
|
1634
|
+
record.finalClosureRoots = await frozenPublicationClosure(
|
|
1635
|
+
context.store,
|
|
1636
|
+
headValue(record.finalHead).root,
|
|
1637
|
+
);
|
|
1638
|
+
await save();
|
|
1639
|
+
}
|
|
1640
|
+
await provideContent(
|
|
1641
|
+
context.store,
|
|
1642
|
+
record.finalClosureRoots,
|
|
1643
|
+
context.node!.api,
|
|
1644
|
+
context.fetch,
|
|
1645
|
+
context.node!.auth,
|
|
1646
|
+
);
|
|
1647
|
+
record.finalCaptured = true;
|
|
1648
|
+
await save();
|
|
1649
|
+
}
|
|
1650
|
+
if (
|
|
1651
|
+
!record.finalClosureRoots ||
|
|
1652
|
+
record.finalClosureRoots[0] !== headValue(record.finalHead).root
|
|
1653
|
+
)
|
|
1654
|
+
throw new HostError(
|
|
1655
|
+
"Frozen publication closure is missing; refusing to retire the old network or HEAD.",
|
|
1656
|
+
);
|
|
1657
|
+
if (!record.newHeadId) {
|
|
1658
|
+
record.newHeadId = (
|
|
1659
|
+
await api.create(
|
|
1660
|
+
c.scopeId,
|
|
1661
|
+
record.finalHead.value,
|
|
1662
|
+
`${record.operation}.newhead`,
|
|
1663
|
+
)
|
|
1664
|
+
).id;
|
|
1665
|
+
await save();
|
|
1666
|
+
}
|
|
1667
|
+
if (!record.nextConfig) {
|
|
1668
|
+
const config = {
|
|
1669
|
+
...record.oldConfig,
|
|
1670
|
+
headId: record.newHeadId,
|
|
1671
|
+
epoch: record.oldConfig.epoch + 1,
|
|
1672
|
+
members: structuredClone(
|
|
1673
|
+
record.oldConfig.members.filter(
|
|
1674
|
+
(m) => m.publicKey !== record.member,
|
|
1675
|
+
),
|
|
1676
|
+
),
|
|
1677
|
+
bootstrap: record.oldConfig.bootstrap.filter(
|
|
1678
|
+
(address) =>
|
|
1679
|
+
!address.endsWith(
|
|
1680
|
+
`/p2p/${record.oldConfig.members.find((m) => m.publicKey === record.member)!.peerId}`,
|
|
1681
|
+
),
|
|
1682
|
+
),
|
|
1683
|
+
};
|
|
1684
|
+
for (const member of config.members) {
|
|
1685
|
+
if (member.publicKey === identity.publicKey) continue;
|
|
1686
|
+
const certificate = await issueScopeCertificate(
|
|
1687
|
+
{
|
|
1688
|
+
v: 1,
|
|
1689
|
+
service: c.service,
|
|
1690
|
+
scopeId: c.scopeId,
|
|
1691
|
+
subject: member.publicKey,
|
|
1692
|
+
write: [config.headId, member.profileId],
|
|
1693
|
+
},
|
|
1694
|
+
await importPrivateKeyPkcs8(identity.privateKey),
|
|
1695
|
+
);
|
|
1696
|
+
member.approval = await sealApproval(
|
|
1697
|
+
binding(url, config, member),
|
|
1698
|
+
{ certificate, swarmKey: record.newKey },
|
|
1699
|
+
identity,
|
|
1700
|
+
);
|
|
1701
|
+
}
|
|
1702
|
+
record.nextConfig = config;
|
|
1703
|
+
await save();
|
|
1704
|
+
}
|
|
1705
|
+
const leaving = record.oldConfig.members.find(
|
|
1706
|
+
(m) => m.publicKey === record.member,
|
|
1707
|
+
)!;
|
|
1708
|
+
// Delete the profile before exposing the new configuration. Old HEAD stays readable until final cleanup.
|
|
1709
|
+
try {
|
|
1710
|
+
await api.freeze(
|
|
1711
|
+
c.scopeId,
|
|
1712
|
+
leaving.profileId,
|
|
1713
|
+
`${record.operation}.profilefreeze`,
|
|
1714
|
+
);
|
|
1715
|
+
} catch (cause) {
|
|
1716
|
+
if (!(cause instanceof ScopeApiError) || cause.status !== 410)
|
|
1717
|
+
throw cause;
|
|
1718
|
+
}
|
|
1719
|
+
try {
|
|
1720
|
+
await api.delete(
|
|
1721
|
+
c.scopeId,
|
|
1722
|
+
leaving.profileId,
|
|
1723
|
+
`${record.operation}.profiledelete`,
|
|
1724
|
+
);
|
|
1725
|
+
} catch (cause) {
|
|
1726
|
+
if (!(cause instanceof ScopeApiError) || cause.status !== 410)
|
|
1727
|
+
throw cause;
|
|
1728
|
+
}
|
|
1729
|
+
if (!record.published) {
|
|
1730
|
+
const observed = await api.read(c.scopeId, c.configId);
|
|
1731
|
+
if (observed.cid !== (await valueCid(record.nextConfig)))
|
|
1732
|
+
await api.write(
|
|
1733
|
+
c.scopeId,
|
|
1734
|
+
c.configId,
|
|
1735
|
+
record.nextConfig,
|
|
1736
|
+
record.configCid,
|
|
1737
|
+
`${record.operation}.config`,
|
|
1738
|
+
);
|
|
1739
|
+
record.published = true;
|
|
1740
|
+
await save();
|
|
1741
|
+
}
|
|
1742
|
+
const membership = await local(context, url);
|
|
1743
|
+
membership.config = record.nextConfig;
|
|
1744
|
+
membership.secret!.swarmKey = record.newKey;
|
|
1745
|
+
await saveLocal(context, membership);
|
|
1746
|
+
await activate(context, membership);
|
|
1747
|
+
await provideContent(
|
|
1748
|
+
context.store,
|
|
1749
|
+
record.finalClosureRoots,
|
|
1750
|
+
context.node!.api,
|
|
1751
|
+
context.fetch,
|
|
1752
|
+
context.node!.auth,
|
|
1753
|
+
);
|
|
1754
|
+
try {
|
|
1755
|
+
await api.delete(
|
|
1756
|
+
c.scopeId,
|
|
1757
|
+
record.oldConfig.headId,
|
|
1758
|
+
`${record.operation}.delete`,
|
|
1759
|
+
);
|
|
1760
|
+
} catch (cause) {
|
|
1761
|
+
if (!(cause instanceof ScopeApiError) || cause.status !== 410)
|
|
1762
|
+
throw cause;
|
|
1763
|
+
}
|
|
1764
|
+
record.completed = true;
|
|
1765
|
+
await save();
|
|
1766
|
+
withState(context, (state) => {
|
|
1767
|
+
state.invalidateSwarmHeader(url);
|
|
1768
|
+
});
|
|
1769
|
+
} catch (cause) {
|
|
941
1770
|
throw new HostError(
|
|
942
|
-
|
|
1771
|
+
`Organization switch incomplete (old HEAD is never unfrozen): ${message(cause)}. Owner must run swarm resume ${url}.`,
|
|
943
1772
|
);
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
await addSwarm(context, response.swarm, alias);
|
|
947
|
-
} catch (cause) {
|
|
948
|
-
throw new HostError(
|
|
949
|
-
`Membership certificate was saved, but swarm registration failed: ${message(cause)}. Retry the same response or run ipab swarm add ${response.swarm}.`,
|
|
950
|
-
);
|
|
951
|
-
}
|
|
952
|
-
return response.swarm;
|
|
1773
|
+
}
|
|
1774
|
+
});
|
|
953
1775
|
}
|
|
954
1776
|
export function formalName(name: string): string {
|
|
955
1777
|
relativePath(name);
|
|
@@ -961,36 +1783,35 @@ export function parseNameReference(
|
|
|
961
1783
|
reference: string,
|
|
962
1784
|
): { swarm?: string; name: string } | undefined {
|
|
963
1785
|
if (/^name:/i.test(reference))
|
|
964
|
-
throw new HostError(
|
|
965
|
-
"name: references are no longer supported. Use ipab://SWARM_CID_OR_ALIAS/PATH or ipab:///PATH.",
|
|
966
|
-
2,
|
|
967
|
-
);
|
|
1786
|
+
throw new HostError("name: is unsupported; use ipab://ALIAS/PATH.");
|
|
968
1787
|
if (!/^ipab:/i.test(reference)) return undefined;
|
|
969
1788
|
const match = /^ipab:\/\/([^/]*)\/([^?#\s]*)$/i.exec(reference);
|
|
970
|
-
if (!match
|
|
1789
|
+
if (!match)
|
|
971
1790
|
throw new HostError(
|
|
972
|
-
"Invalid IPAB URI
|
|
973
|
-
2,
|
|
1791
|
+
"Invalid IPAB URI; percent-encode the configuration URL authority and each path segment.",
|
|
974
1792
|
);
|
|
1793
|
+
const authority = decodeURIComponent(match[1]!);
|
|
1794
|
+
if (authority && !/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(authority))
|
|
1795
|
+
canonicalSwarmUrl(authority);
|
|
975
1796
|
const segments = match[2]!.split("/").map((segment) => {
|
|
976
|
-
|
|
977
|
-
try {
|
|
978
|
-
decoded = decodeURIComponent(segment);
|
|
979
|
-
} catch {
|
|
980
|
-
throw new HostError("Invalid percent encoding in IPAB URI path.", 2);
|
|
981
|
-
}
|
|
1797
|
+
const decoded = decodeURIComponent(segment);
|
|
982
1798
|
if (decoded.includes("/"))
|
|
983
|
-
throw new HostError(
|
|
984
|
-
"Encoded path separators are not allowed in IPAB URIs.",
|
|
985
|
-
2,
|
|
986
|
-
);
|
|
1799
|
+
throw new HostError("Encoded path separators are unsupported.");
|
|
987
1800
|
return decoded;
|
|
988
1801
|
});
|
|
989
1802
|
return {
|
|
990
|
-
...(
|
|
1803
|
+
...(authority ? { swarm: authority } : {}),
|
|
991
1804
|
name: formalName(segments.join("/")),
|
|
992
1805
|
};
|
|
993
1806
|
}
|
|
1807
|
+
function referenceFor(url: string, name: string): string {
|
|
1808
|
+
const segment = (value: string) =>
|
|
1809
|
+
encodeURIComponent(value).replace(
|
|
1810
|
+
/[!'()*]/g,
|
|
1811
|
+
(char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`,
|
|
1812
|
+
);
|
|
1813
|
+
return `ipab://${segment(url)}/${name.split("/").map(segment).join("/")}`;
|
|
1814
|
+
}
|
|
994
1815
|
export async function resolveReference(
|
|
995
1816
|
context: SwarmContext,
|
|
996
1817
|
reference: string,
|
|
@@ -998,34 +1819,35 @@ export async function resolveReference(
|
|
|
998
1819
|
local: (reference: string) => string,
|
|
999
1820
|
raw = false,
|
|
1000
1821
|
): Promise<{ targetCid: string; cid: string }> {
|
|
1001
|
-
let targetCid: string;
|
|
1002
1822
|
const shared = parseNameReference(reference);
|
|
1823
|
+
let targetCid: string;
|
|
1003
1824
|
if (shared) {
|
|
1004
|
-
const
|
|
1005
|
-
|
|
1006
|
-
? withState(context, (state) => state.resolveSwarm(authority))
|
|
1825
|
+
const selected = shared.swarm
|
|
1826
|
+
? withState(context, (state) => state.resolveSwarm(shared.swarm!))
|
|
1007
1827
|
: swarm;
|
|
1008
|
-
if (!
|
|
1828
|
+
if (!selected)
|
|
1009
1829
|
throw new HostError(
|
|
1010
|
-
"
|
|
1830
|
+
"Select a configuration URL/alias using --swarm or swarm use.",
|
|
1011
1831
|
);
|
|
1012
|
-
|
|
1013
|
-
let
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
? header.snapshot.names[name]
|
|
1017
|
-
: undefined;
|
|
1018
|
-
let link = lookup();
|
|
1832
|
+
let header = await swarmHeader(context, selected);
|
|
1833
|
+
let link = Object.hasOwn(header.snapshot.names, shared.name)
|
|
1834
|
+
? header.snapshot.names[shared.name]
|
|
1835
|
+
: undefined;
|
|
1019
1836
|
if (!link && header.source === "cache") {
|
|
1020
|
-
header = await swarmHeader(context,
|
|
1021
|
-
link =
|
|
1837
|
+
header = await swarmHeader(context, selected, true);
|
|
1838
|
+
link = Object.hasOwn(header.snapshot.names, shared.name)
|
|
1839
|
+
? header.snapshot.names[shared.name]
|
|
1840
|
+
: undefined;
|
|
1022
1841
|
}
|
|
1023
1842
|
if (!link)
|
|
1024
1843
|
throw new HostError(
|
|
1025
|
-
`Formal Name "${name}"
|
|
1844
|
+
`Formal Name "${shared.name}" was not found after refreshing HEAD.`,
|
|
1026
1845
|
);
|
|
1027
1846
|
targetCid = link.toString();
|
|
1028
|
-
} else
|
|
1847
|
+
} else {
|
|
1848
|
+
if (swarm) await readyMembership(context, swarm);
|
|
1849
|
+
targetCid = local(reference);
|
|
1850
|
+
}
|
|
1029
1851
|
let cid = targetCid;
|
|
1030
1852
|
const visited = new Set<string>();
|
|
1031
1853
|
for (let depth = 0; !raw; depth++) {
|
|
@@ -1039,15 +1861,64 @@ export async function resolveReference(
|
|
|
1039
1861
|
});
|
|
1040
1862
|
if (bundle.schemaKey !== (await encodeSchema(artifactType)).cid.toString())
|
|
1041
1863
|
break;
|
|
1042
|
-
|
|
1043
|
-
context.store,
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
cid = CID.parse(artifact.value).toV1().toString();
|
|
1864
|
+
cid = CID.parse(
|
|
1865
|
+
parseArtifact(await typedJson(context.store, cid, artifactType)).value,
|
|
1866
|
+
)
|
|
1867
|
+
.toV1()
|
|
1868
|
+
.toString();
|
|
1048
1869
|
}
|
|
1049
1870
|
return { targetCid, cid };
|
|
1050
1871
|
}
|
|
1872
|
+
export async function searchNames(
|
|
1873
|
+
context: SwarmContext,
|
|
1874
|
+
query: string,
|
|
1875
|
+
swarms: readonly { cid: string; alias: string | null }[],
|
|
1876
|
+
) {
|
|
1877
|
+
if (
|
|
1878
|
+
!query ||
|
|
1879
|
+
query.length > 256 ||
|
|
1880
|
+
[...query].some(
|
|
1881
|
+
(char) => char.charCodeAt(0) < 32 || char.charCodeAt(0) === 127,
|
|
1882
|
+
)
|
|
1883
|
+
)
|
|
1884
|
+
throw new HostError(
|
|
1885
|
+
"Search query must be 1..256 characters without controls.",
|
|
1886
|
+
);
|
|
1887
|
+
const matches: {
|
|
1888
|
+
swarm: string;
|
|
1889
|
+
alias: string | null;
|
|
1890
|
+
name: string;
|
|
1891
|
+
reference: string;
|
|
1892
|
+
targetCid: string;
|
|
1893
|
+
}[] = [];
|
|
1894
|
+
const searched: {
|
|
1895
|
+
configUrl: string;
|
|
1896
|
+
source: "cache" | "network";
|
|
1897
|
+
root: string;
|
|
1898
|
+
valueCid: string;
|
|
1899
|
+
verifiedAt: string;
|
|
1900
|
+
}[] = [];
|
|
1901
|
+
for (const swarm of swarms) {
|
|
1902
|
+
const header = await swarmHeader(context, swarm.cid);
|
|
1903
|
+
searched.push({
|
|
1904
|
+
configUrl: swarm.cid,
|
|
1905
|
+
source: header.source,
|
|
1906
|
+
root: header.head.root,
|
|
1907
|
+
valueCid: header.head.cid,
|
|
1908
|
+
verifiedAt: header.verifiedAt,
|
|
1909
|
+
});
|
|
1910
|
+
for (const name of Object.keys(header.snapshot.names).sort())
|
|
1911
|
+
if (name.toLowerCase().includes(query.toLowerCase()))
|
|
1912
|
+
matches.push({
|
|
1913
|
+
swarm: swarm.cid,
|
|
1914
|
+
alias: swarm.alias,
|
|
1915
|
+
name,
|
|
1916
|
+
reference: referenceFor(swarm.cid, name),
|
|
1917
|
+
targetCid: header.snapshot.names[name]!.toString(),
|
|
1918
|
+
});
|
|
1919
|
+
}
|
|
1920
|
+
return { matches, swarms: searched };
|
|
1921
|
+
}
|
|
1051
1922
|
async function dependencies(
|
|
1052
1923
|
store: IpfsStore,
|
|
1053
1924
|
root: string,
|
|
@@ -1061,23 +1932,20 @@ async function dependencies(
|
|
|
1061
1932
|
resolveCid: (cid) => store.block.get(cid.toString()),
|
|
1062
1933
|
});
|
|
1063
1934
|
if (bundle.schemaKey === (await encodeSchema(artifactType)).cid.toString()) {
|
|
1064
|
-
const node = await typedJson
|
|
1935
|
+
const node = parseArtifact(await typedJson(store, root, artifactType));
|
|
1065
1936
|
await dependencies(store, node.value, found);
|
|
1066
1937
|
if (node.previous) await dependencies(store, node.previous, found);
|
|
1067
1938
|
} else if (bundle.schemaKey === appSchemaCid.toString()) {
|
|
1068
1939
|
const app = object(
|
|
1069
1940
|
JSON.parse(
|
|
1070
1941
|
new TextDecoder().decode(
|
|
1071
|
-
await bytes(
|
|
1072
|
-
await bundle.readFile(bundle.manifest.entry.path),
|
|
1073
|
-
64 * 1024,
|
|
1074
|
-
),
|
|
1942
|
+
await bytes(await bundle.readFile(bundle.manifest.entry), 64 * 1024),
|
|
1075
1943
|
),
|
|
1076
1944
|
) as unknown,
|
|
1077
1945
|
"App",
|
|
1078
1946
|
);
|
|
1079
1947
|
if (app.source) {
|
|
1080
|
-
const source = object(app.source, "
|
|
1948
|
+
const source = object(app.source, "source");
|
|
1081
1949
|
if (typeof source.cid !== "string")
|
|
1082
1950
|
throw new HostError("Invalid App source association.");
|
|
1083
1951
|
await dependencies(store, source.cid, found);
|
|
@@ -1085,6 +1953,34 @@ async function dependencies(
|
|
|
1085
1953
|
}
|
|
1086
1954
|
return found;
|
|
1087
1955
|
}
|
|
1956
|
+
export async function frozenPublicationClosure(
|
|
1957
|
+
store: IpfsStore,
|
|
1958
|
+
root: string,
|
|
1959
|
+
): Promise<string[]> {
|
|
1960
|
+
const publications = new Set<string>(),
|
|
1961
|
+
content = new Set<string>();
|
|
1962
|
+
let current: string | undefined = root;
|
|
1963
|
+
while (current !== undefined) {
|
|
1964
|
+
if (publications.has(current) || publications.size >= 256)
|
|
1965
|
+
throw new HostError(
|
|
1966
|
+
"Frozen publication history exceeds limits or contains a cycle.",
|
|
1967
|
+
);
|
|
1968
|
+
publications.add(current);
|
|
1969
|
+
const publication = await readPublication(store, current);
|
|
1970
|
+
const snapshot = decodeSnapshot(
|
|
1971
|
+
await store.block.get(publication.state.toString()),
|
|
1972
|
+
);
|
|
1973
|
+
for (const target of Object.values(snapshot.names))
|
|
1974
|
+
await dependencies(store, target.toString(), content);
|
|
1975
|
+
current = publication.previous?.toString();
|
|
1976
|
+
}
|
|
1977
|
+
return [
|
|
1978
|
+
root,
|
|
1979
|
+
...[...new Set([...publications, ...content])]
|
|
1980
|
+
.filter((cid) => cid !== root)
|
|
1981
|
+
.sort(),
|
|
1982
|
+
];
|
|
1983
|
+
}
|
|
1088
1984
|
export async function publish(
|
|
1089
1985
|
context: SwarmContext,
|
|
1090
1986
|
content: string,
|
|
@@ -1095,32 +1991,33 @@ export async function publish(
|
|
|
1095
1991
|
snapshot?: boolean;
|
|
1096
1992
|
},
|
|
1097
1993
|
): Promise<string> {
|
|
1994
|
+
if (!options.swarm)
|
|
1995
|
+
throw new HostError(
|
|
1996
|
+
"Publication requires a selected Swarm configuration URL or alias.",
|
|
1997
|
+
);
|
|
1998
|
+
const head = await pointer(context, options.swarm);
|
|
1098
1999
|
const roots = await dependencies(context.store, content);
|
|
1099
2000
|
if (!options.name) {
|
|
1100
2001
|
if (options.expect || options.snapshot)
|
|
1101
2002
|
throw new HostError("--expect/--snapshot require --name.");
|
|
2003
|
+
await head.read();
|
|
1102
2004
|
await provideContent(
|
|
1103
2005
|
context.store,
|
|
1104
2006
|
[...roots],
|
|
1105
|
-
context.
|
|
2007
|
+
context.node!.api,
|
|
1106
2008
|
context.fetch,
|
|
2009
|
+
context.node!.auth,
|
|
1107
2010
|
);
|
|
1108
2011
|
return content;
|
|
1109
2012
|
}
|
|
1110
2013
|
const name = formalName(options.name);
|
|
1111
|
-
if (!options.swarm)
|
|
1112
|
-
throw new HostError(
|
|
1113
|
-
"Named publication requires --swarm CID or swarm.default.",
|
|
1114
|
-
);
|
|
1115
2014
|
if (options.snapshot && options.expect)
|
|
1116
|
-
throw new HostError(
|
|
1117
|
-
"Ordinary named snapshots cannot replace an occupied Name.",
|
|
1118
|
-
);
|
|
2015
|
+
throw new HostError("Snapshots cannot replace an occupied Name.");
|
|
1119
2016
|
const expected = options.expect
|
|
1120
2017
|
? CID.parse(options.expect).toV1().toString()
|
|
1121
2018
|
: undefined;
|
|
1122
2019
|
if (expected)
|
|
1123
|
-
await typedJson
|
|
2020
|
+
parseArtifact(await typedJson(context.store, expected, artifactType));
|
|
1124
2021
|
const candidate = options.snapshot
|
|
1125
2022
|
? content
|
|
1126
2023
|
: await jsonBundle(context.store, artifactType, {
|
|
@@ -1132,55 +2029,50 @@ export async function publish(
|
|
|
1132
2029
|
if (expected)
|
|
1133
2030
|
for (const root of await dependencies(context.store, expected))
|
|
1134
2031
|
roots.add(root);
|
|
1135
|
-
await provideContent(
|
|
1136
|
-
context.store,
|
|
1137
|
-
[...roots],
|
|
1138
|
-
context.provider,
|
|
1139
|
-
context.fetch,
|
|
1140
|
-
);
|
|
1141
|
-
const client = await pointer(context, options.swarm);
|
|
1142
2032
|
for (let attempt = 0; attempt < 8; attempt++) {
|
|
1143
|
-
const
|
|
1144
|
-
|
|
2033
|
+
const currentHead = await head.read(),
|
|
2034
|
+
current = await readSnapshot(context.store, currentHead.root);
|
|
1145
2035
|
if (
|
|
1146
2036
|
(Object.hasOwn(current.names, name)
|
|
1147
2037
|
? current.names[name]?.toString()
|
|
1148
2038
|
: undefined) !== expected
|
|
1149
2039
|
)
|
|
1150
2040
|
throw new HostError(
|
|
1151
|
-
`Edit conflict for ${name}; candidate ${candidate}
|
|
2041
|
+
`Edit conflict for ${name}; candidate ${candidate} remains retained. Choose an explicit baseline.`,
|
|
1152
2042
|
3,
|
|
1153
2043
|
"EDIT_CONFLICT",
|
|
1154
2044
|
"publication",
|
|
1155
2045
|
);
|
|
1156
|
-
const
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
2046
|
+
const next = await publication(
|
|
2047
|
+
context.store,
|
|
2048
|
+
await writeSnapshot(context.store, {
|
|
2049
|
+
...current,
|
|
2050
|
+
names: { ...current.names, [name]: CID.parse(candidate) },
|
|
2051
|
+
}),
|
|
2052
|
+
currentHead.root,
|
|
2053
|
+
);
|
|
1161
2054
|
await provideContent(
|
|
1162
2055
|
context.store,
|
|
1163
2056
|
[next, ...roots],
|
|
1164
|
-
context.
|
|
2057
|
+
context.node!.api,
|
|
1165
2058
|
context.fetch,
|
|
2059
|
+
context.node!.auth,
|
|
1166
2060
|
);
|
|
2061
|
+
withState(context, (state) => {
|
|
2062
|
+
state.invalidateSwarmHeader(options.swarm!);
|
|
2063
|
+
});
|
|
1167
2064
|
try {
|
|
1168
|
-
|
|
1169
|
-
state.invalidateSwarmHeader(options.swarm!);
|
|
1170
|
-
});
|
|
1171
|
-
await client.write(next, head.revision, toBase64Url(randomBytes(18)));
|
|
2065
|
+
await head.write(next, currentHead.cid, uniqueId());
|
|
1172
2066
|
return candidate;
|
|
1173
2067
|
} catch (cause) {
|
|
1174
|
-
if (!(cause instanceof
|
|
2068
|
+
if (!(cause instanceof ScopeApiError) || cause.status !== 412)
|
|
1175
2069
|
throw new HostError(
|
|
1176
|
-
`HEAD
|
|
2070
|
+
`HEAD publication unconfirmed: ${message(cause)}. Candidate ${candidate} remains retained; inspect HEAD before retry.`,
|
|
1177
2071
|
1,
|
|
1178
2072
|
"HEAD_UNCONFIRMED",
|
|
1179
2073
|
"publication",
|
|
1180
2074
|
);
|
|
1181
2075
|
}
|
|
1182
2076
|
}
|
|
1183
|
-
throw new HostError(
|
|
1184
|
-
`HEAD is busy; candidate ${candidate} remains retained. Retry the same explicit Name baseline.`,
|
|
1185
|
-
);
|
|
2077
|
+
throw new HostError(`HEAD is busy; candidate ${candidate} remains retained.`);
|
|
1186
2078
|
}
|