@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/pins.ts
CHANGED
package/src/setup.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { lstat, mkdir } from "node:fs/promises";
|
|
2
|
+
import { accountIdentity, secureNew } from "./identity.js";
|
|
3
|
+
import { kuboBinary, KUBO_VERSION } from "./kubo.js";
|
|
4
|
+
import { keyId } from "@ipab/bundle/pointer/crypto";
|
|
5
|
+
import { HostError } from "./errors.js";
|
|
6
|
+
|
|
7
|
+
export async function setupDevice(stateDir: string, env: NodeJS.ProcessEnv) {
|
|
8
|
+
const created = await mkdir(stateDir, { recursive: true, mode: 0o700 });
|
|
9
|
+
if (created) await secureNew(stateDir, true);
|
|
10
|
+
const directory = await lstat(stateDir);
|
|
11
|
+
if (!directory.isDirectory() || directory.isSymbolicLink())
|
|
12
|
+
throw new HostError("Setup state directory must be a regular directory.");
|
|
13
|
+
const binary = await kuboBinary(stateDir);
|
|
14
|
+
const identity = await accountIdentity(env, true);
|
|
15
|
+
return {
|
|
16
|
+
kuboVersion: KUBO_VERSION,
|
|
17
|
+
binary,
|
|
18
|
+
publicKey: identity.publicKey,
|
|
19
|
+
fingerprint: await keyId(identity.publicKey),
|
|
20
|
+
};
|
|
21
|
+
}
|
package/src/source-bundle.ts
CHANGED
|
@@ -2,21 +2,15 @@ import { relative, isAbsolute, sep } from "node:path";
|
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
3
|
import ignore from "ignore";
|
|
4
4
|
import { stringify } from "yaml";
|
|
5
|
-
import { bundleLimits } from "@ipab/bundle";
|
|
5
|
+
import { bundleLimits, parseBundleManifest } from "@ipab/bundle";
|
|
6
6
|
import { listTree, readBytes } from "@ipab/bundle-builder";
|
|
7
7
|
import { filesystemTree } from "@ipab/bundle-builder/node";
|
|
8
8
|
import { nodeError, type AuthoringOptions } from "./authoring.js";
|
|
9
9
|
import { HostError } from "./errors.js";
|
|
10
10
|
|
|
11
11
|
export const sourceSchema = {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"App project source snapshot; build inputs are ordinary resources.",
|
|
15
|
-
entry: {
|
|
16
|
-
path: "package.json",
|
|
17
|
-
contentType: "application/json",
|
|
18
|
-
schema: { type: "object" },
|
|
19
|
-
},
|
|
12
|
+
contentType: "application/json",
|
|
13
|
+
schema: { type: "object" },
|
|
20
14
|
} as const;
|
|
21
15
|
|
|
22
16
|
export async function captureSource(
|
|
@@ -113,11 +107,30 @@ export async function captureSource(
|
|
|
113
107
|
throw new HostError("Source .bundle.yaml is excluded by .gitignore.");
|
|
114
108
|
files.set(
|
|
115
109
|
".bundle.yaml",
|
|
116
|
-
new TextEncoder().encode(
|
|
110
|
+
new TextEncoder().encode(
|
|
111
|
+
stringify({
|
|
112
|
+
title: "IPAB App Source",
|
|
113
|
+
entry: "package.json",
|
|
114
|
+
type: ".bundle-type.yaml",
|
|
115
|
+
}),
|
|
116
|
+
),
|
|
117
117
|
);
|
|
118
|
+
if (excluded(".bundle-type.yaml", false))
|
|
119
|
+
throw new HostError(
|
|
120
|
+
"Source .bundle-type.yaml is excluded by .gitignore.",
|
|
121
|
+
);
|
|
122
|
+
if (!files.has(".bundle-type.yaml"))
|
|
123
|
+
files.set(
|
|
124
|
+
".bundle-type.yaml",
|
|
125
|
+
new TextEncoder().encode(stringify(sourceSchema)),
|
|
126
|
+
);
|
|
118
127
|
}
|
|
119
|
-
|
|
120
|
-
|
|
128
|
+
const manifest = parseBundleManifest(files.get(".bundle.yaml")!);
|
|
129
|
+
for (const required of [manifest.entry, manifest.type])
|
|
130
|
+
if (!files.has(required))
|
|
131
|
+
throw new HostError(
|
|
132
|
+
`Source required file is excluded or missing: ${required}.`,
|
|
133
|
+
);
|
|
121
134
|
return files;
|
|
122
135
|
}
|
|
123
136
|
|
package/src/space.ts
CHANGED
|
@@ -106,7 +106,7 @@ export async function loadApp(
|
|
|
106
106
|
const bundle = await validatedBundle(root, store, owner, signal);
|
|
107
107
|
if (
|
|
108
108
|
bundle.schemaKey !== appSchemaCid.toString() ||
|
|
109
|
-
bundle.
|
|
109
|
+
bundle.schema.type.contentType !== "application/json"
|
|
110
110
|
)
|
|
111
111
|
throw new HostError(
|
|
112
112
|
"App must use the documented exact builtin Node App type CID.",
|
|
@@ -117,10 +117,7 @@ export async function loadApp(
|
|
|
117
117
|
const description = appDescription(
|
|
118
118
|
JSON.parse(
|
|
119
119
|
new TextDecoder("utf-8", { fatal: true }).decode(
|
|
120
|
-
await bytes(
|
|
121
|
-
await bundle.readFile(bundle.manifest.entry.path),
|
|
122
|
-
64 * 1024,
|
|
123
|
-
),
|
|
120
|
+
await bytes(await bundle.readFile(bundle.manifest.entry), 64 * 1024),
|
|
124
121
|
),
|
|
125
122
|
) as unknown,
|
|
126
123
|
);
|
package/src/state.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { CID } from "multiformats/cid";
|
|
|
6
6
|
import type { StoreConfiguration } from "@ipab/ipfs-store";
|
|
7
7
|
import {
|
|
8
8
|
validateUserConfig,
|
|
9
|
+
swarmUrl,
|
|
9
10
|
type ConfigKey,
|
|
10
11
|
type ConfigValue,
|
|
11
12
|
} from "./config.js";
|
|
@@ -18,6 +19,7 @@ export interface Association {
|
|
|
18
19
|
argv: string[];
|
|
19
20
|
}
|
|
20
21
|
export interface RegisteredSwarm {
|
|
22
|
+
// Keep the storage column name, but values are configuration URLs; old CID rows are rejected.
|
|
21
23
|
cid: string;
|
|
22
24
|
alias: string | null;
|
|
23
25
|
}
|
|
@@ -51,9 +53,9 @@ export interface Hold {
|
|
|
51
53
|
recordId: string | null;
|
|
52
54
|
}
|
|
53
55
|
export type PinMetadata =
|
|
54
|
-
| { kind: "bundle";
|
|
55
|
-
| { kind: "schema";
|
|
56
|
-
| { kind: "content";
|
|
56
|
+
| { kind: "bundle"; title: string; schemaCid: string }
|
|
57
|
+
| { kind: "schema"; title: string; schemaCid: string }
|
|
58
|
+
| { kind: "content"; title: null; schemaCid: null };
|
|
57
59
|
export interface NamedPin {
|
|
58
60
|
name: string;
|
|
59
61
|
cid: string;
|
|
@@ -178,8 +180,16 @@ export class State {
|
|
|
178
180
|
throw error;
|
|
179
181
|
}
|
|
180
182
|
}
|
|
181
|
-
config(): Partial<
|
|
182
|
-
|
|
183
|
+
config({ includeSecrets = true }: { includeSecrets?: boolean } = {}): Partial<
|
|
184
|
+
Record<ConfigKey, ConfigValue>
|
|
185
|
+
> {
|
|
186
|
+
const entries = this.db
|
|
187
|
+
.prepare(
|
|
188
|
+
includeSecrets
|
|
189
|
+
? "SELECT key,value FROM config"
|
|
190
|
+
: "SELECT key,value FROM config WHERE key != 'pointers.apiKey'",
|
|
191
|
+
)
|
|
192
|
+
.all();
|
|
183
193
|
if (
|
|
184
194
|
entries.some(
|
|
185
195
|
(row) => row.key === "pointers.apiKey" && row.value !== "null",
|
|
@@ -225,15 +235,14 @@ export class State {
|
|
|
225
235
|
(row.alias !== null && typeof row.alias !== "string")
|
|
226
236
|
)
|
|
227
237
|
throw new HostError("Invalid persisted swarm registration.");
|
|
228
|
-
const cid =
|
|
238
|
+
const cid = swarmUrl(row.cid);
|
|
229
239
|
if (row.alias !== null) swarmAlias(row.alias);
|
|
230
240
|
return { cid, alias: row.alias };
|
|
231
241
|
});
|
|
232
242
|
}
|
|
233
243
|
resolveSwarm(reference: string): string {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
} catch {
|
|
244
|
+
if (/^https?:/.test(reference)) return swarmUrl(reference);
|
|
245
|
+
{
|
|
237
246
|
const entry = this.swarms().find((entry) => entry.alias === reference);
|
|
238
247
|
if (entry) return entry.cid;
|
|
239
248
|
throw new HostError(
|
|
@@ -260,7 +269,7 @@ export class State {
|
|
|
260
269
|
);
|
|
261
270
|
}
|
|
262
271
|
registerSwarm(cid: string, alias?: string): RegisteredSwarm {
|
|
263
|
-
cid =
|
|
272
|
+
cid = swarmUrl(cid);
|
|
264
273
|
return this.transaction(() => {
|
|
265
274
|
if (alias !== undefined) this.checkSwarmAlias(alias, cid);
|
|
266
275
|
const previous = this.swarms().find((entry) => entry.cid === cid);
|
|
@@ -369,20 +378,20 @@ export class State {
|
|
|
369
378
|
if (data.metadata !== null) {
|
|
370
379
|
const value = object(data.metadata, "pin type metadata");
|
|
371
380
|
if (value.kind === "content") {
|
|
372
|
-
if (value.
|
|
381
|
+
if (value.title !== null || value.schemaCid !== null)
|
|
373
382
|
throw new HostError("Invalid persisted pin type metadata.");
|
|
374
|
-
metadata = { kind: "content",
|
|
383
|
+
metadata = { kind: "content", title: null, schemaCid: null };
|
|
375
384
|
} else {
|
|
376
385
|
if (
|
|
377
386
|
(value.kind !== "bundle" && value.kind !== "schema") ||
|
|
378
|
-
typeof value.
|
|
379
|
-
!value.
|
|
387
|
+
typeof value.title !== "string" ||
|
|
388
|
+
!value.title.trim() ||
|
|
380
389
|
typeof value.schemaCid !== "string"
|
|
381
390
|
)
|
|
382
391
|
throw new HostError("Invalid persisted pin type metadata.");
|
|
383
392
|
metadata = {
|
|
384
393
|
kind: value.kind,
|
|
385
|
-
|
|
394
|
+
title: value.title,
|
|
386
395
|
schemaCid: exactCid(value.schemaCid, true),
|
|
387
396
|
};
|
|
388
397
|
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createCipheriv,
|
|
3
|
+
createDecipheriv,
|
|
4
|
+
createPrivateKey,
|
|
5
|
+
createPublicKey,
|
|
6
|
+
diffieHellman,
|
|
7
|
+
generateKeyPairSync,
|
|
8
|
+
hkdfSync,
|
|
9
|
+
randomBytes,
|
|
10
|
+
} from "node:crypto";
|
|
11
|
+
import {
|
|
12
|
+
importPrivateKeyPkcs8,
|
|
13
|
+
importPublicKeyRaw,
|
|
14
|
+
signEd25519,
|
|
15
|
+
verifyEd25519,
|
|
16
|
+
} from "@ipab/bundle/pointer/crypto";
|
|
17
|
+
import {
|
|
18
|
+
parseScopeCertificate,
|
|
19
|
+
type ScopeCertificate,
|
|
20
|
+
} from "@ipab/bundle/pointer/scope";
|
|
21
|
+
import { HostError, object } from "./errors.js";
|
|
22
|
+
import type { Identity } from "./identity.js";
|
|
23
|
+
|
|
24
|
+
export interface EncryptionIdentity {
|
|
25
|
+
publicKey: string;
|
|
26
|
+
privateKey: string;
|
|
27
|
+
}
|
|
28
|
+
export function encryptionIdentity(): EncryptionIdentity {
|
|
29
|
+
const keys = generateKeyPairSync("x25519");
|
|
30
|
+
return {
|
|
31
|
+
publicKey: keys.publicKey
|
|
32
|
+
.export({ format: "der", type: "spki" })
|
|
33
|
+
.toString("base64url"),
|
|
34
|
+
privateKey: keys.privateKey
|
|
35
|
+
.export({ format: "der", type: "pkcs8" })
|
|
36
|
+
.toString("base64url"),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export interface EnvelopeBinding {
|
|
40
|
+
configUrl: string;
|
|
41
|
+
scopeId: string;
|
|
42
|
+
owner: string;
|
|
43
|
+
member: string;
|
|
44
|
+
requestId: string;
|
|
45
|
+
peerId: string;
|
|
46
|
+
profileId: string;
|
|
47
|
+
headId: string;
|
|
48
|
+
epoch: number;
|
|
49
|
+
encryptionKey: string;
|
|
50
|
+
}
|
|
51
|
+
export interface MembershipSecret {
|
|
52
|
+
certificate: ScopeCertificate;
|
|
53
|
+
swarmKey: string;
|
|
54
|
+
}
|
|
55
|
+
export interface Envelope {
|
|
56
|
+
binding: EnvelopeBinding;
|
|
57
|
+
ephemeral: string;
|
|
58
|
+
salt: string;
|
|
59
|
+
iv: string;
|
|
60
|
+
ciphertext: string;
|
|
61
|
+
tag: string;
|
|
62
|
+
signature: string;
|
|
63
|
+
}
|
|
64
|
+
export function parseEnvelope(value: unknown): Envelope {
|
|
65
|
+
const envelope = object(value, "approval envelope");
|
|
66
|
+
if (
|
|
67
|
+
Object.keys(envelope).sort().join() !==
|
|
68
|
+
"binding,ciphertext,ephemeral,iv,salt,signature,tag"
|
|
69
|
+
)
|
|
70
|
+
throw new HostError("Invalid approval envelope encoding.");
|
|
71
|
+
const fields = object(envelope.binding, "approval binding");
|
|
72
|
+
if (
|
|
73
|
+
Object.keys(fields).sort().join() !==
|
|
74
|
+
"configUrl,encryptionKey,epoch,headId,member,owner,peerId,profileId,requestId,scopeId"
|
|
75
|
+
)
|
|
76
|
+
throw new HostError("Invalid approval binding.");
|
|
77
|
+
const text = (record: Record<string, unknown>, key: string): string => {
|
|
78
|
+
const field = record[key];
|
|
79
|
+
if (typeof field !== "string")
|
|
80
|
+
throw new HostError("Invalid approval field.");
|
|
81
|
+
return field;
|
|
82
|
+
};
|
|
83
|
+
const epoch = fields.epoch;
|
|
84
|
+
if (typeof epoch !== "number" || !Number.isSafeInteger(epoch) || epoch < 1)
|
|
85
|
+
throw new HostError("Invalid approval epoch.");
|
|
86
|
+
const binding: EnvelopeBinding = {
|
|
87
|
+
configUrl: text(fields, "configUrl"),
|
|
88
|
+
scopeId: text(fields, "scopeId"),
|
|
89
|
+
owner: text(fields, "owner"),
|
|
90
|
+
member: text(fields, "member"),
|
|
91
|
+
requestId: text(fields, "requestId"),
|
|
92
|
+
peerId: text(fields, "peerId"),
|
|
93
|
+
profileId: text(fields, "profileId"),
|
|
94
|
+
headId: text(fields, "headId"),
|
|
95
|
+
epoch,
|
|
96
|
+
encryptionKey: text(fields, "encryptionKey"),
|
|
97
|
+
};
|
|
98
|
+
const result: Envelope = {
|
|
99
|
+
binding,
|
|
100
|
+
ephemeral: text(envelope, "ephemeral"),
|
|
101
|
+
salt: text(envelope, "salt"),
|
|
102
|
+
iv: text(envelope, "iv"),
|
|
103
|
+
ciphertext: text(envelope, "ciphertext"),
|
|
104
|
+
tag: text(envelope, "tag"),
|
|
105
|
+
signature: text(envelope, "signature"),
|
|
106
|
+
};
|
|
107
|
+
if (
|
|
108
|
+
![
|
|
109
|
+
result.ephemeral,
|
|
110
|
+
result.salt,
|
|
111
|
+
result.iv,
|
|
112
|
+
result.ciphertext,
|
|
113
|
+
result.tag,
|
|
114
|
+
result.signature,
|
|
115
|
+
].every((field) => /^[A-Za-z0-9_-]+$/.test(field)) ||
|
|
116
|
+
result.salt.length !== 43 ||
|
|
117
|
+
result.iv.length !== 16 ||
|
|
118
|
+
result.tag.length !== 22 ||
|
|
119
|
+
result.signature.length !== 86 ||
|
|
120
|
+
result.ciphertext.length > 8192
|
|
121
|
+
)
|
|
122
|
+
throw new HostError("Invalid approval envelope encoding.");
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
const message = (e: Omit<Envelope, "signature">) =>
|
|
126
|
+
JSON.stringify([
|
|
127
|
+
"IPAB-APPROVAL/1",
|
|
128
|
+
e.binding,
|
|
129
|
+
e.ephemeral,
|
|
130
|
+
e.salt,
|
|
131
|
+
e.iv,
|
|
132
|
+
e.ciphertext,
|
|
133
|
+
e.tag,
|
|
134
|
+
]);
|
|
135
|
+
function encryptionKey(
|
|
136
|
+
privateKey: string,
|
|
137
|
+
publicKey: string,
|
|
138
|
+
salt: Buffer,
|
|
139
|
+
binding: EnvelopeBinding,
|
|
140
|
+
): Buffer {
|
|
141
|
+
const privateObject = createPrivateKey({
|
|
142
|
+
key: Buffer.from(privateKey, "base64url"),
|
|
143
|
+
type: "pkcs8",
|
|
144
|
+
format: "der",
|
|
145
|
+
});
|
|
146
|
+
const publicObject = createPublicKey({
|
|
147
|
+
key: Buffer.from(publicKey, "base64url"),
|
|
148
|
+
type: "spki",
|
|
149
|
+
format: "der",
|
|
150
|
+
});
|
|
151
|
+
if (
|
|
152
|
+
privateObject.asymmetricKeyType !== "x25519" ||
|
|
153
|
+
publicObject.asymmetricKeyType !== "x25519"
|
|
154
|
+
)
|
|
155
|
+
throw new HostError("Approval requires X25519 encryption keys.");
|
|
156
|
+
return Buffer.from(
|
|
157
|
+
hkdfSync(
|
|
158
|
+
"sha256",
|
|
159
|
+
diffieHellman({ privateKey: privateObject, publicKey: publicObject }),
|
|
160
|
+
salt,
|
|
161
|
+
Buffer.from(JSON.stringify(["IPAB-ENVELOPE/1", binding])),
|
|
162
|
+
32,
|
|
163
|
+
),
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
export async function sealApproval(
|
|
167
|
+
binding: EnvelopeBinding,
|
|
168
|
+
secret: MembershipSecret,
|
|
169
|
+
owner: Identity,
|
|
170
|
+
): Promise<Envelope> {
|
|
171
|
+
const ephemeral = encryptionIdentity();
|
|
172
|
+
const salt = randomBytes(32),
|
|
173
|
+
iv = randomBytes(12);
|
|
174
|
+
const cipher = createCipheriv(
|
|
175
|
+
"aes-256-gcm",
|
|
176
|
+
encryptionKey(ephemeral.privateKey, binding.encryptionKey, salt, binding),
|
|
177
|
+
iv,
|
|
178
|
+
);
|
|
179
|
+
cipher.setAAD(Buffer.from(JSON.stringify(binding)));
|
|
180
|
+
const ciphertext = Buffer.concat([
|
|
181
|
+
cipher.update(JSON.stringify(secret), "utf8"),
|
|
182
|
+
cipher.final(),
|
|
183
|
+
]);
|
|
184
|
+
const envelope = {
|
|
185
|
+
binding,
|
|
186
|
+
ephemeral: ephemeral.publicKey,
|
|
187
|
+
salt: salt.toString("base64url"),
|
|
188
|
+
iv: iv.toString("base64url"),
|
|
189
|
+
ciphertext: ciphertext.toString("base64url"),
|
|
190
|
+
tag: cipher.getAuthTag().toString("base64url"),
|
|
191
|
+
};
|
|
192
|
+
return {
|
|
193
|
+
...envelope,
|
|
194
|
+
signature: await signEd25519(
|
|
195
|
+
await importPrivateKeyPkcs8(owner.privateKey),
|
|
196
|
+
message(envelope),
|
|
197
|
+
),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
export async function openApproval(
|
|
201
|
+
value: unknown,
|
|
202
|
+
expected: EnvelopeBinding,
|
|
203
|
+
identity: EncryptionIdentity,
|
|
204
|
+
): Promise<MembershipSecret> {
|
|
205
|
+
const envelope = parseEnvelope(value);
|
|
206
|
+
if (
|
|
207
|
+
JSON.stringify(envelope.binding) !== JSON.stringify(expected) ||
|
|
208
|
+
expected.encryptionKey !== identity.publicKey ||
|
|
209
|
+
!(await verifyEd25519(
|
|
210
|
+
await importPublicKeyRaw(expected.owner),
|
|
211
|
+
envelope.signature,
|
|
212
|
+
message(envelope),
|
|
213
|
+
))
|
|
214
|
+
)
|
|
215
|
+
throw new HostError(
|
|
216
|
+
"Approval signature or exact request/resource/identity binding is invalid.",
|
|
217
|
+
);
|
|
218
|
+
try {
|
|
219
|
+
const decipher = createDecipheriv(
|
|
220
|
+
"aes-256-gcm",
|
|
221
|
+
encryptionKey(
|
|
222
|
+
identity.privateKey,
|
|
223
|
+
envelope.ephemeral,
|
|
224
|
+
Buffer.from(envelope.salt, "base64url"),
|
|
225
|
+
expected,
|
|
226
|
+
),
|
|
227
|
+
Buffer.from(envelope.iv, "base64url"),
|
|
228
|
+
);
|
|
229
|
+
decipher.setAAD(Buffer.from(JSON.stringify(expected)));
|
|
230
|
+
decipher.setAuthTag(Buffer.from(envelope.tag, "base64url"));
|
|
231
|
+
const secret = object(
|
|
232
|
+
JSON.parse(
|
|
233
|
+
Buffer.concat([
|
|
234
|
+
decipher.update(Buffer.from(envelope.ciphertext, "base64url")),
|
|
235
|
+
decipher.final(),
|
|
236
|
+
]).toString("utf8"),
|
|
237
|
+
) as unknown,
|
|
238
|
+
"membership secret",
|
|
239
|
+
);
|
|
240
|
+
if (
|
|
241
|
+
Object.keys(secret).sort().join() !== "certificate,swarmKey" ||
|
|
242
|
+
typeof secret.swarmKey !== "string" ||
|
|
243
|
+
!/^[a-f0-9]{64}$/.test(secret.swarmKey)
|
|
244
|
+
)
|
|
245
|
+
throw new HostError("Invalid membership secret.");
|
|
246
|
+
return {
|
|
247
|
+
certificate: parseScopeCertificate(secret.certificate),
|
|
248
|
+
swarmKey: secret.swarmKey,
|
|
249
|
+
};
|
|
250
|
+
} catch {
|
|
251
|
+
throw new HostError(
|
|
252
|
+
"Could not decrypt the approved package; preserve the original local Request identity.",
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { lstat, mkdir, open, readFile, rename, unlink } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { DatabaseSync } from "node:sqlite";
|
|
5
|
+
import {
|
|
6
|
+
protectedPath,
|
|
7
|
+
protectedPaths,
|
|
8
|
+
secureNew,
|
|
9
|
+
type ProtectedPath,
|
|
10
|
+
} from "./identity.js";
|
|
11
|
+
import { nodeError } from "./authoring.js";
|
|
12
|
+
import { HostError } from "./errors.js";
|
|
13
|
+
|
|
14
|
+
export async function privateDirectory(
|
|
15
|
+
parent: string,
|
|
16
|
+
name: string,
|
|
17
|
+
): Promise<string> {
|
|
18
|
+
const path = join(parent, name);
|
|
19
|
+
try {
|
|
20
|
+
await lstat(path);
|
|
21
|
+
} catch (cause) {
|
|
22
|
+
if (!nodeError(cause, "ENOENT")) throw cause;
|
|
23
|
+
await mkdir(path, { mode: 0o700 });
|
|
24
|
+
await secureNew(path, true);
|
|
25
|
+
}
|
|
26
|
+
await protectedPath(path, true);
|
|
27
|
+
return path;
|
|
28
|
+
}
|
|
29
|
+
export async function readPrivate<T>(
|
|
30
|
+
directory: string,
|
|
31
|
+
name: string,
|
|
32
|
+
additionalPaths: readonly ProtectedPath[] = [],
|
|
33
|
+
): Promise<T | undefined> {
|
|
34
|
+
const path = join(directory, name);
|
|
35
|
+
if (additionalPaths.length) {
|
|
36
|
+
try {
|
|
37
|
+
await lstat(path);
|
|
38
|
+
} catch (cause) {
|
|
39
|
+
if (nodeError(cause, "ENOENT")) return undefined;
|
|
40
|
+
throw cause;
|
|
41
|
+
}
|
|
42
|
+
await protectedPaths([{ path }, ...additionalPaths]);
|
|
43
|
+
} else {
|
|
44
|
+
try {
|
|
45
|
+
await protectedPath(path);
|
|
46
|
+
} catch (cause) {
|
|
47
|
+
if (nodeError(cause, "ENOENT")) return undefined;
|
|
48
|
+
throw cause;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if ((await lstat(path)).size > 2 * 1024 * 1024)
|
|
52
|
+
throw new HostError("Protected state is oversized.");
|
|
53
|
+
return JSON.parse(await readFile(path, "utf8")) as T;
|
|
54
|
+
}
|
|
55
|
+
export async function writePrivate(
|
|
56
|
+
directory: string,
|
|
57
|
+
name: string,
|
|
58
|
+
value: unknown,
|
|
59
|
+
): Promise<void> {
|
|
60
|
+
const target = join(directory, name);
|
|
61
|
+
const temporary = join(directory, `.write-${randomUUID()}`);
|
|
62
|
+
const file = await open(temporary, "wx", 0o600);
|
|
63
|
+
try {
|
|
64
|
+
await secureNew(temporary);
|
|
65
|
+
await file.writeFile(JSON.stringify(value));
|
|
66
|
+
await file.sync();
|
|
67
|
+
await file.close();
|
|
68
|
+
await rename(temporary, target);
|
|
69
|
+
} finally {
|
|
70
|
+
await file.close();
|
|
71
|
+
await unlink(temporary).catch((cause: unknown) => {
|
|
72
|
+
if (!nodeError(cause, "ENOENT")) throw cause;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
export async function withPrivateLock<T>(
|
|
77
|
+
directory: string,
|
|
78
|
+
operation: () => Promise<T>,
|
|
79
|
+
): Promise<T> {
|
|
80
|
+
const path = join(directory, "operation.sqlite");
|
|
81
|
+
try {
|
|
82
|
+
const handle = await open(path, "wx", 0o600);
|
|
83
|
+
await handle.close();
|
|
84
|
+
await secureNew(path);
|
|
85
|
+
} catch (cause) {
|
|
86
|
+
if (!nodeError(cause, "EEXIST")) throw cause;
|
|
87
|
+
}
|
|
88
|
+
await protectedPath(path);
|
|
89
|
+
const db = new DatabaseSync(path);
|
|
90
|
+
let locked = false;
|
|
91
|
+
try {
|
|
92
|
+
db.exec("PRAGMA busy_timeout=0");
|
|
93
|
+
for (let attempt = 0; attempt < 300; attempt++) {
|
|
94
|
+
try {
|
|
95
|
+
db.exec("BEGIN IMMEDIATE");
|
|
96
|
+
locked = true;
|
|
97
|
+
break;
|
|
98
|
+
} catch (cause) {
|
|
99
|
+
if (
|
|
100
|
+
!(cause instanceof Error) ||
|
|
101
|
+
!cause.message.includes("database is locked")
|
|
102
|
+
)
|
|
103
|
+
throw cause;
|
|
104
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (!locked)
|
|
108
|
+
throw new HostError(
|
|
109
|
+
"Another local Swarm operation is still running; retry later.",
|
|
110
|
+
);
|
|
111
|
+
return await operation();
|
|
112
|
+
} finally {
|
|
113
|
+
if (locked) db.exec("ROLLBACK");
|
|
114
|
+
db.close();
|
|
115
|
+
}
|
|
116
|
+
}
|