@ipab/cli 0.1.0-rc.2 → 0.1.0-rc.3
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 +35 -14
- 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 +306 -116
- 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 +19 -15
- 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/kubo.d.ts +38 -0
- package/dist/kubo.d.ts.map +1 -0
- package/dist/kubo.js +528 -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 +4 -4
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +11 -12
- 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 +5 -0
- package/dist/swarm-files.d.ts.map +1 -0
- package/dist/swarm-files.js +94 -0
- package/dist/swarm-files.js.map +1 -0
- package/dist/swarm.d.ts +117 -57
- package/dist/swarm.d.ts.map +1 -1
- package/dist/swarm.js +1033 -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 +362 -139
- package/src/config.ts +22 -18
- package/src/content-provider.ts +5 -4
- package/src/get.ts +37 -15
- package/src/kubo.ts +691 -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 +14 -13
- package/src/swarm-envelope.ts +255 -0
- package/src/swarm-files.ts +100 -0
- package/src/swarm.ts +1676 -837
- package/src/template-definition.ts +36 -41
- package/src/template-source.ts +10 -3
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;
|
|
@@ -225,15 +227,14 @@ export class State {
|
|
|
225
227
|
(row.alias !== null && typeof row.alias !== "string")
|
|
226
228
|
)
|
|
227
229
|
throw new HostError("Invalid persisted swarm registration.");
|
|
228
|
-
const cid =
|
|
230
|
+
const cid = swarmUrl(row.cid);
|
|
229
231
|
if (row.alias !== null) swarmAlias(row.alias);
|
|
230
232
|
return { cid, alias: row.alias };
|
|
231
233
|
});
|
|
232
234
|
}
|
|
233
235
|
resolveSwarm(reference: string): string {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
} catch {
|
|
236
|
+
if (/^https?:/.test(reference)) return swarmUrl(reference);
|
|
237
|
+
{
|
|
237
238
|
const entry = this.swarms().find((entry) => entry.alias === reference);
|
|
238
239
|
if (entry) return entry.cid;
|
|
239
240
|
throw new HostError(
|
|
@@ -260,7 +261,7 @@ export class State {
|
|
|
260
261
|
);
|
|
261
262
|
}
|
|
262
263
|
registerSwarm(cid: string, alias?: string): RegisteredSwarm {
|
|
263
|
-
cid =
|
|
264
|
+
cid = swarmUrl(cid);
|
|
264
265
|
return this.transaction(() => {
|
|
265
266
|
if (alias !== undefined) this.checkSwarmAlias(alias, cid);
|
|
266
267
|
const previous = this.swarms().find((entry) => entry.cid === cid);
|
|
@@ -369,20 +370,20 @@ export class State {
|
|
|
369
370
|
if (data.metadata !== null) {
|
|
370
371
|
const value = object(data.metadata, "pin type metadata");
|
|
371
372
|
if (value.kind === "content") {
|
|
372
|
-
if (value.
|
|
373
|
+
if (value.title !== null || value.schemaCid !== null)
|
|
373
374
|
throw new HostError("Invalid persisted pin type metadata.");
|
|
374
|
-
metadata = { kind: "content",
|
|
375
|
+
metadata = { kind: "content", title: null, schemaCid: null };
|
|
375
376
|
} else {
|
|
376
377
|
if (
|
|
377
378
|
(value.kind !== "bundle" && value.kind !== "schema") ||
|
|
378
|
-
typeof value.
|
|
379
|
-
!value.
|
|
379
|
+
typeof value.title !== "string" ||
|
|
380
|
+
!value.title.trim() ||
|
|
380
381
|
typeof value.schemaCid !== "string"
|
|
381
382
|
)
|
|
382
383
|
throw new HostError("Invalid persisted pin type metadata.");
|
|
383
384
|
metadata = {
|
|
384
385
|
kind: value.kind,
|
|
385
|
-
|
|
386
|
+
title: value.title,
|
|
386
387
|
schemaCid: exactCid(value.schemaCid, true),
|
|
387
388
|
};
|
|
388
389
|
}
|
|
@@ -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,100 @@
|
|
|
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 { protectedPath, secureNew } from "./identity.js";
|
|
6
|
+
import { nodeError } from "./authoring.js";
|
|
7
|
+
import { HostError } from "./errors.js";
|
|
8
|
+
|
|
9
|
+
export async function privateDirectory(
|
|
10
|
+
parent: string,
|
|
11
|
+
name: string,
|
|
12
|
+
): Promise<string> {
|
|
13
|
+
const path = join(parent, name);
|
|
14
|
+
try {
|
|
15
|
+
await lstat(path);
|
|
16
|
+
} catch (cause) {
|
|
17
|
+
if (!nodeError(cause, "ENOENT")) throw cause;
|
|
18
|
+
await mkdir(path, { mode: 0o700 });
|
|
19
|
+
await secureNew(path, true);
|
|
20
|
+
}
|
|
21
|
+
await protectedPath(path, true);
|
|
22
|
+
return path;
|
|
23
|
+
}
|
|
24
|
+
export async function readPrivate<T>(
|
|
25
|
+
directory: string,
|
|
26
|
+
name: string,
|
|
27
|
+
): Promise<T | undefined> {
|
|
28
|
+
const path = join(directory, name);
|
|
29
|
+
try {
|
|
30
|
+
await protectedPath(path);
|
|
31
|
+
} catch (cause) {
|
|
32
|
+
if (nodeError(cause, "ENOENT")) return undefined;
|
|
33
|
+
throw cause;
|
|
34
|
+
}
|
|
35
|
+
if ((await lstat(path)).size > 2 * 1024 * 1024)
|
|
36
|
+
throw new HostError("Protected state is oversized.");
|
|
37
|
+
return JSON.parse(await readFile(path, "utf8")) as T;
|
|
38
|
+
}
|
|
39
|
+
export async function writePrivate(
|
|
40
|
+
directory: string,
|
|
41
|
+
name: string,
|
|
42
|
+
value: unknown,
|
|
43
|
+
): Promise<void> {
|
|
44
|
+
const target = join(directory, name);
|
|
45
|
+
const temporary = join(directory, `.write-${randomUUID()}`);
|
|
46
|
+
const file = await open(temporary, "wx", 0o600);
|
|
47
|
+
try {
|
|
48
|
+
await secureNew(temporary);
|
|
49
|
+
await file.writeFile(JSON.stringify(value));
|
|
50
|
+
await file.sync();
|
|
51
|
+
await file.close();
|
|
52
|
+
await rename(temporary, target);
|
|
53
|
+
} finally {
|
|
54
|
+
await file.close();
|
|
55
|
+
await unlink(temporary).catch((cause: unknown) => {
|
|
56
|
+
if (!nodeError(cause, "ENOENT")) throw cause;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export async function withPrivateLock<T>(
|
|
61
|
+
directory: string,
|
|
62
|
+
operation: () => Promise<T>,
|
|
63
|
+
): Promise<T> {
|
|
64
|
+
const path = join(directory, "operation.sqlite");
|
|
65
|
+
try {
|
|
66
|
+
const handle = await open(path, "wx", 0o600);
|
|
67
|
+
await handle.close();
|
|
68
|
+
await secureNew(path);
|
|
69
|
+
} catch (cause) {
|
|
70
|
+
if (!nodeError(cause, "EEXIST")) throw cause;
|
|
71
|
+
}
|
|
72
|
+
await protectedPath(path);
|
|
73
|
+
const db = new DatabaseSync(path);
|
|
74
|
+
let locked = false;
|
|
75
|
+
try {
|
|
76
|
+
db.exec("PRAGMA busy_timeout=0");
|
|
77
|
+
for (let attempt = 0; attempt < 300; attempt++) {
|
|
78
|
+
try {
|
|
79
|
+
db.exec("BEGIN IMMEDIATE");
|
|
80
|
+
locked = true;
|
|
81
|
+
break;
|
|
82
|
+
} catch (cause) {
|
|
83
|
+
if (
|
|
84
|
+
!(cause instanceof Error) ||
|
|
85
|
+
!cause.message.includes("database is locked")
|
|
86
|
+
)
|
|
87
|
+
throw cause;
|
|
88
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (!locked)
|
|
92
|
+
throw new HostError(
|
|
93
|
+
"Another local Swarm operation is still running; retry later.",
|
|
94
|
+
);
|
|
95
|
+
return await operation();
|
|
96
|
+
} finally {
|
|
97
|
+
if (locked) db.exec("ROLLBACK");
|
|
98
|
+
db.close();
|
|
99
|
+
}
|
|
100
|
+
}
|