@aithos/sdk 0.1.0-alpha.4 → 0.1.0-alpha.41
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 +211 -7
- package/dist/src/apps.d.ts +155 -0
- package/dist/src/apps.js +288 -0
- package/dist/src/assets.d.ts +207 -0
- package/dist/src/assets.js +533 -0
- package/dist/src/auth-api.d.ts +138 -0
- package/dist/src/auth-api.js +168 -0
- package/dist/src/auth.d.ts +536 -119
- package/dist/src/auth.js +1207 -152
- package/dist/src/compute.d.ts +251 -9
- package/dist/src/compute.js +293 -16
- package/dist/src/data-schema-contacts-v1.d.ts +14 -0
- package/dist/src/data-schema-contacts-v1.js +28 -0
- package/dist/src/data.d.ts +153 -0
- package/dist/src/data.js +670 -0
- package/dist/src/endpoints.d.ts +9 -0
- package/dist/src/endpoints.js +5 -0
- package/dist/src/ethos.d.ts +202 -1
- package/dist/src/ethos.js +821 -16
- package/dist/src/index.d.ts +18 -6
- package/dist/src/index.js +39 -6
- package/dist/src/internal/delegate-bundle.d.ts +18 -0
- package/dist/src/internal/delegate-bundle.js +94 -0
- package/dist/src/internal/delegate-state.d.ts +45 -0
- package/dist/src/internal/delegate-state.js +120 -0
- package/dist/src/internal/envelope.d.ts +77 -0
- package/dist/src/internal/envelope.js +154 -0
- package/dist/src/internal/owner-signers.d.ts +78 -0
- package/dist/src/internal/owner-signers.js +179 -0
- package/dist/src/internal/protocol-client-bridge.d.ts +8 -0
- package/dist/src/internal/protocol-client-bridge.js +20 -0
- package/dist/src/internal/recovery-file.d.ts +29 -0
- package/dist/src/internal/recovery-file.js +98 -0
- package/dist/src/internal/signer.d.ts +59 -0
- package/dist/src/internal/signer.js +86 -0
- package/dist/src/key-store.d.ts +128 -0
- package/dist/src/key-store.js +244 -0
- package/dist/src/mandates.d.ts +163 -1
- package/dist/src/mandates.js +286 -8
- package/dist/src/react/AithosAsset.d.ts +66 -0
- package/dist/src/react/AithosAsset.js +67 -0
- package/dist/src/react/context.d.ts +29 -0
- package/dist/src/react/context.js +31 -0
- package/dist/src/react/index.d.ts +28 -0
- package/dist/src/react/index.js +30 -0
- package/dist/src/react/use-aithos-asset.d.ts +39 -0
- package/dist/src/react/use-aithos-asset.js +118 -0
- package/dist/src/sdk.d.ts +46 -3
- package/dist/src/sdk.js +49 -23
- package/dist/src/wallet.d.ts +4 -6
- package/dist/src/wallet.js +18 -8
- package/dist/src/web.d.ts +279 -0
- package/dist/src/web.js +186 -0
- package/dist/test/auth-j3.test.d.ts +2 -0
- package/dist/test/auth-j3.test.js +391 -0
- package/dist/test/compute-delegate-path.test.d.ts +2 -0
- package/dist/test/compute-delegate-path.test.js +183 -0
- package/dist/test/compute.test.js +26 -11
- package/dist/test/endpoints.test.js +20 -1
- package/dist/test/envelope.test.d.ts +2 -0
- package/dist/test/envelope.test.js +318 -0
- package/dist/test/ethos-first-edition.test.d.ts +2 -0
- package/dist/test/ethos-first-edition.test.js +248 -0
- package/dist/test/ethos.test.d.ts +2 -0
- package/dist/test/ethos.test.js +219 -0
- package/dist/test/key-store.test.d.ts +2 -0
- package/dist/test/key-store.test.js +161 -0
- package/dist/test/mandates-compute.test.d.ts +2 -0
- package/dist/test/mandates-compute.test.js +256 -0
- package/dist/test/mandates.test.d.ts +2 -0
- package/dist/test/mandates.test.js +93 -0
- package/dist/test/sdk.test.js +70 -30
- package/dist/test/signer.test.d.ts +2 -0
- package/dist/test/signer.test.js +117 -0
- package/dist/test/signup-bootstrap.test.d.ts +2 -0
- package/dist/test/signup-bootstrap.test.js +311 -0
- package/dist/test/wallet.test.js +20 -9
- package/dist/test/web.test.d.ts +2 -0
- package/dist/test/web.test.js +270 -0
- package/package.json +18 -3
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright 2026 Mathieu Colla
|
|
3
|
+
/**
|
|
4
|
+
* Tests for the `compute` namespace on `MandatesNamespace.create`.
|
|
5
|
+
*
|
|
6
|
+
* Invariants under test (mirror of the protocol-core v0.4.0 invariants,
|
|
7
|
+
* enforced at the SDK boundary so callers fail fast with a precise
|
|
8
|
+
* error rather than only blowing up server-side):
|
|
9
|
+
*
|
|
10
|
+
* 1. Passing `compute.invoke` directly in `scopes[]` is rejected.
|
|
11
|
+
* The opt-in must go through the typed `compute` namespace, which
|
|
12
|
+
* is what a consent UI can review.
|
|
13
|
+
* 2. The `compute` namespace requires at least one of
|
|
14
|
+
* `dailyCapMicrocredits` or `totalCapMicrocredits` — an unbounded
|
|
15
|
+
* compute mandate is the bearer-token footgun this whole namespace
|
|
16
|
+
* exists to prevent.
|
|
17
|
+
* 3. Numeric caps must be positive integers.
|
|
18
|
+
* 4. `allowedModels`, when present, must be an array of non-empty
|
|
19
|
+
* strings.
|
|
20
|
+
* 5. A compute-only mandate (`scopes: []` + `compute: {…}`) is
|
|
21
|
+
* legitimate and accepted — the grantee gets no ethos data
|
|
22
|
+
* access, only bounded compute spending. Empty scopes WITHOUT
|
|
23
|
+
* compute is still rejected.
|
|
24
|
+
*
|
|
25
|
+
* Network-dependent paths (real mint, list, revoke) ride on the same
|
|
26
|
+
* integration suite as the rest of the namespace — see the note in
|
|
27
|
+
* `mandates.test.ts`. We intentionally don't go through `mintDelegateBundle`
|
|
28
|
+
* here; failures fire BEFORE that boundary.
|
|
29
|
+
*/
|
|
30
|
+
import { strict as assert } from "node:assert";
|
|
31
|
+
import { describe, it } from "node:test";
|
|
32
|
+
import { createBrowserIdentity } from "@aithos/protocol-client";
|
|
33
|
+
import { AithosAuth, AithosSDKError, COMPUTE_INVOKE_SCOPE, MandatesNamespace, memoryKeyStore, noopStore, } from "../src/index.js";
|
|
34
|
+
import { DEFAULT_SDK_ENDPOINTS } from "../src/endpoints.js";
|
|
35
|
+
import { serializeRecoveryFile } from "../src/internal/recovery-file.js";
|
|
36
|
+
function makeAuth() {
|
|
37
|
+
return new AithosAuth({
|
|
38
|
+
authBaseUrl: "https://auth.test",
|
|
39
|
+
fetch: (() => {
|
|
40
|
+
throw new Error("network not expected");
|
|
41
|
+
}),
|
|
42
|
+
sessionStore: noopStore(),
|
|
43
|
+
keyStore: memoryKeyStore(),
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function makeMandates(auth) {
|
|
47
|
+
return new MandatesNamespace({
|
|
48
|
+
auth,
|
|
49
|
+
endpoints: DEFAULT_SDK_ENDPOINTS,
|
|
50
|
+
fetch: globalThis.fetch.bind(globalThis),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
async function signInAsAlice(auth) {
|
|
54
|
+
const id = createBrowserIdentity("alice", "Alice");
|
|
55
|
+
const { text } = serializeRecoveryFile(id);
|
|
56
|
+
const info = await auth.signInWithRecovery({ file: text });
|
|
57
|
+
return info.did;
|
|
58
|
+
}
|
|
59
|
+
/* -------------------------------------------------------------------------- */
|
|
60
|
+
/* Constant export sanity */
|
|
61
|
+
/* -------------------------------------------------------------------------- */
|
|
62
|
+
describe("COMPUTE_INVOKE_SCOPE constant", () => {
|
|
63
|
+
it("is the canonical 'compute.invoke' string", () => {
|
|
64
|
+
assert.equal(COMPUTE_INVOKE_SCOPE, "compute.invoke");
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
/* -------------------------------------------------------------------------- */
|
|
68
|
+
/* Smuggling guard: compute.invoke in scopes[] is rejected */
|
|
69
|
+
/* -------------------------------------------------------------------------- */
|
|
70
|
+
describe("MandatesNamespace.create — compute.invoke smuggling guard", () => {
|
|
71
|
+
it("rejects a mandate that smuggles compute.invoke into scopes[]", async () => {
|
|
72
|
+
const auth = makeAuth();
|
|
73
|
+
await signInAsAlice(auth);
|
|
74
|
+
const m = makeMandates(auth);
|
|
75
|
+
await assert.rejects(() => m.create({
|
|
76
|
+
granteeId: "urn:agent:bob",
|
|
77
|
+
// Up-cast through string[] — this is exactly the path the
|
|
78
|
+
// runtime check has to catch. Type-checking can't (the union
|
|
79
|
+
// doesn't include "compute.invoke", but cast slips through).
|
|
80
|
+
scopes: ["ethos.read.public", "compute.invoke"],
|
|
81
|
+
ttlSeconds: 3600,
|
|
82
|
+
}), (e) => e instanceof AithosSDKError &&
|
|
83
|
+
e.code === "mandates_invalid_scopes" &&
|
|
84
|
+
/compute' namespace/.test(e.message));
|
|
85
|
+
});
|
|
86
|
+
it("rejects even when 'compute.invoke' is the ONLY scope passed", async () => {
|
|
87
|
+
const auth = makeAuth();
|
|
88
|
+
await signInAsAlice(auth);
|
|
89
|
+
const m = makeMandates(auth);
|
|
90
|
+
await assert.rejects(() => m.create({
|
|
91
|
+
granteeId: "urn:agent:bob",
|
|
92
|
+
scopes: ["compute.invoke"],
|
|
93
|
+
ttlSeconds: 3600,
|
|
94
|
+
}), (e) => e instanceof AithosSDKError &&
|
|
95
|
+
e.code === "mandates_invalid_scopes");
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
/* -------------------------------------------------------------------------- */
|
|
99
|
+
/* Compute namespace — input validation */
|
|
100
|
+
/* -------------------------------------------------------------------------- */
|
|
101
|
+
describe("MandatesNamespace.create — compute namespace validation", () => {
|
|
102
|
+
it("rejects compute namespace with no caps at all", async () => {
|
|
103
|
+
const auth = makeAuth();
|
|
104
|
+
await signInAsAlice(auth);
|
|
105
|
+
const m = makeMandates(auth);
|
|
106
|
+
await assert.rejects(() => m.create({
|
|
107
|
+
granteeId: "urn:agent:bob",
|
|
108
|
+
scopes: ["ethos.read.public"],
|
|
109
|
+
ttlSeconds: 3600,
|
|
110
|
+
compute: {}, // empty — neither daily nor total cap
|
|
111
|
+
}), (e) => e instanceof AithosSDKError &&
|
|
112
|
+
e.code === "mandates_invalid_compute" &&
|
|
113
|
+
/at least one of dailyCapMicrocredits or totalCapMicrocredits/.test(e.message));
|
|
114
|
+
});
|
|
115
|
+
it("rejects compute namespace with only allowedModels (no cap)", async () => {
|
|
116
|
+
const auth = makeAuth();
|
|
117
|
+
await signInAsAlice(auth);
|
|
118
|
+
const m = makeMandates(auth);
|
|
119
|
+
await assert.rejects(() => m.create({
|
|
120
|
+
granteeId: "urn:agent:bob",
|
|
121
|
+
scopes: ["ethos.read.public"],
|
|
122
|
+
ttlSeconds: 3600,
|
|
123
|
+
compute: { allowedModels: ["claude-haiku-4-5"] },
|
|
124
|
+
}), (e) => e instanceof AithosSDKError &&
|
|
125
|
+
e.code === "mandates_invalid_compute");
|
|
126
|
+
});
|
|
127
|
+
it("rejects non-positive caps", async () => {
|
|
128
|
+
const auth = makeAuth();
|
|
129
|
+
await signInAsAlice(auth);
|
|
130
|
+
const m = makeMandates(auth);
|
|
131
|
+
for (const bad of [0, -1, 3.14]) {
|
|
132
|
+
await assert.rejects(() => m.create({
|
|
133
|
+
granteeId: "urn:agent:bob",
|
|
134
|
+
scopes: ["ethos.read.public"],
|
|
135
|
+
ttlSeconds: 3600,
|
|
136
|
+
compute: { dailyCapMicrocredits: bad },
|
|
137
|
+
}), (e) => e instanceof AithosSDKError &&
|
|
138
|
+
e.code === "mandates_invalid_compute" &&
|
|
139
|
+
/must be a positive integer/.test(e.message), `expected rejection for dailyCapMicrocredits=${bad}`);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
it("rejects malformed allowedModels (empty string entry)", async () => {
|
|
143
|
+
const auth = makeAuth();
|
|
144
|
+
await signInAsAlice(auth);
|
|
145
|
+
const m = makeMandates(auth);
|
|
146
|
+
await assert.rejects(() => m.create({
|
|
147
|
+
granteeId: "urn:agent:bob",
|
|
148
|
+
scopes: ["ethos.read.public"],
|
|
149
|
+
ttlSeconds: 3600,
|
|
150
|
+
compute: {
|
|
151
|
+
dailyCapMicrocredits: 5_000,
|
|
152
|
+
allowedModels: ["claude-haiku-4-5", ""],
|
|
153
|
+
},
|
|
154
|
+
}), (e) => e instanceof AithosSDKError &&
|
|
155
|
+
e.code === "mandates_invalid_compute" &&
|
|
156
|
+
/allowedModels entries must be non-empty strings/.test(e.message));
|
|
157
|
+
});
|
|
158
|
+
it("rejects allowedModels that is not an array", async () => {
|
|
159
|
+
const auth = makeAuth();
|
|
160
|
+
await signInAsAlice(auth);
|
|
161
|
+
const m = makeMandates(auth);
|
|
162
|
+
await assert.rejects(() => m.create({
|
|
163
|
+
granteeId: "urn:agent:bob",
|
|
164
|
+
scopes: ["ethos.read.public"],
|
|
165
|
+
ttlSeconds: 3600,
|
|
166
|
+
compute: {
|
|
167
|
+
dailyCapMicrocredits: 5_000,
|
|
168
|
+
// @ts-expect-error — runtime check guards against bad casts
|
|
169
|
+
allowedModels: "claude-haiku-4-5",
|
|
170
|
+
},
|
|
171
|
+
}), (e) => e instanceof AithosSDKError &&
|
|
172
|
+
e.code === "mandates_invalid_compute");
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
/* -------------------------------------------------------------------------- */
|
|
176
|
+
/* Read-only mandate is unaffected */
|
|
177
|
+
/* -------------------------------------------------------------------------- */
|
|
178
|
+
describe("MandatesNamespace.create — read-only mandates unaffected", () => {
|
|
179
|
+
it("does NOT reject a clean read-only mandate (no compute, no smuggling)", async () => {
|
|
180
|
+
// We can't run the full mint here because the installed
|
|
181
|
+
// @aithos/protocol-core (0.5.1, pre-0.4.0-mandate) doesn't yet
|
|
182
|
+
// accept compute.invoke on the public sphere whitelist — the real
|
|
183
|
+
// mint would still succeed for a read-only mandate, but it goes
|
|
184
|
+
// out to S3 etc. Instead we check the synchronous validation
|
|
185
|
+
// surface: passing a clean read-only input must not throw before
|
|
186
|
+
// the mint call.
|
|
187
|
+
//
|
|
188
|
+
// (The async `m.create(...)` would still error later on the
|
|
189
|
+
// network path — that's the integration suite's job.)
|
|
190
|
+
//
|
|
191
|
+
// What we ARE asserting here: no SDK-side validation rejection.
|
|
192
|
+
const auth = makeAuth();
|
|
193
|
+
await signInAsAlice(auth);
|
|
194
|
+
const m = makeMandates(auth);
|
|
195
|
+
// The mint will fail with a network error (fetch throws), but
|
|
196
|
+
// crucially NOT with a validation error — that proves our compute
|
|
197
|
+
// guard is precisely scoped to compute-related inputs.
|
|
198
|
+
await assert.rejects(() => m.create({
|
|
199
|
+
granteeId: "urn:viewer:bob",
|
|
200
|
+
scopes: ["ethos.read.public"],
|
|
201
|
+
ttlSeconds: 3600,
|
|
202
|
+
}), (e) => {
|
|
203
|
+
if (!(e instanceof AithosSDKError))
|
|
204
|
+
return true; // any non-validation error fine
|
|
205
|
+
const code = e.code;
|
|
206
|
+
// Anything except a validation rejection is fine — the network
|
|
207
|
+
// is not wired up in this fake auth.
|
|
208
|
+
return (code !== "mandates_invalid_scopes" &&
|
|
209
|
+
code !== "mandates_invalid_compute");
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
/* -------------------------------------------------------------------------- */
|
|
214
|
+
/* Compute-only mandate (scopes: [] + compute namespace) */
|
|
215
|
+
/* -------------------------------------------------------------------------- */
|
|
216
|
+
describe("MandatesNamespace.create — compute-only mandate", () => {
|
|
217
|
+
it("accepts an empty scopes[] when compute is provided (no ethos data access, just bounded spending)", async () => {
|
|
218
|
+
// Same caveat as the "read-only unaffected" suite: the installed
|
|
219
|
+
// @aithos/protocol-core does not yet whitelist compute.invoke on
|
|
220
|
+
// the public sphere, so the real `mintDelegateBundle` call would
|
|
221
|
+
// still reject downstream. What this test proves is that the
|
|
222
|
+
// SDK validation layer (a) does NOT throw `mandates_invalid_scopes`
|
|
223
|
+
// for the empty list when compute is set, and (b) does NOT throw
|
|
224
|
+
// `mandates_invalid_compute` for a properly-capped budget. The
|
|
225
|
+
// request is allowed to proceed past validation.
|
|
226
|
+
const auth = makeAuth();
|
|
227
|
+
await signInAsAlice(auth);
|
|
228
|
+
const m = makeMandates(auth);
|
|
229
|
+
await assert.rejects(() => m.create({
|
|
230
|
+
granteeId: "urn:agent:creative",
|
|
231
|
+
scopes: [], // no ethos data access
|
|
232
|
+
ttlSeconds: 3600,
|
|
233
|
+
compute: { dailyCapMicrocredits: 1_000 },
|
|
234
|
+
}), (e) => {
|
|
235
|
+
if (!(e instanceof AithosSDKError))
|
|
236
|
+
return true;
|
|
237
|
+
const code = e.code;
|
|
238
|
+
// Validation must NOT have rejected this input.
|
|
239
|
+
return (code !== "mandates_invalid_scopes" &&
|
|
240
|
+
code !== "mandates_invalid_compute");
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
it("STILL rejects empty scopes[] when compute is NOT provided", async () => {
|
|
244
|
+
const auth = makeAuth();
|
|
245
|
+
await signInAsAlice(auth);
|
|
246
|
+
const m = makeMandates(auth);
|
|
247
|
+
await assert.rejects(() => m.create({
|
|
248
|
+
granteeId: "urn:agent:bob",
|
|
249
|
+
scopes: [],
|
|
250
|
+
ttlSeconds: 3600,
|
|
251
|
+
}), (e) => e instanceof AithosSDKError &&
|
|
252
|
+
e.code === "mandates_invalid_scopes" &&
|
|
253
|
+
/compute-only mandate/.test(e.message));
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
//# sourceMappingURL=mandates-compute.test.js.map
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright 2026 Mathieu Colla
|
|
3
|
+
// Tests for J5 — sdk.mandates namespace. Covers input validation,
|
|
4
|
+
// owner-required guard rails, and the input → output projection.
|
|
5
|
+
// Network-dependent paths (real mint, list, revoke) ride on a future
|
|
6
|
+
// integration suite.
|
|
7
|
+
import { strict as assert } from "node:assert";
|
|
8
|
+
import { describe, it } from "node:test";
|
|
9
|
+
import { createBrowserIdentity } from "@aithos/protocol-client";
|
|
10
|
+
import { AithosAuth, AithosSDKError, MandatesNamespace, memoryKeyStore, noopStore, } from "../src/index.js";
|
|
11
|
+
import { DEFAULT_SDK_ENDPOINTS } from "../src/endpoints.js";
|
|
12
|
+
import { serializeRecoveryFile } from "../src/internal/recovery-file.js";
|
|
13
|
+
function makeAuth() {
|
|
14
|
+
return new AithosAuth({
|
|
15
|
+
authBaseUrl: "https://auth.test",
|
|
16
|
+
fetch: (() => {
|
|
17
|
+
throw new Error("network not expected");
|
|
18
|
+
}),
|
|
19
|
+
sessionStore: noopStore(),
|
|
20
|
+
keyStore: memoryKeyStore(),
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
function makeMandates(auth) {
|
|
24
|
+
return new MandatesNamespace({
|
|
25
|
+
auth,
|
|
26
|
+
endpoints: DEFAULT_SDK_ENDPOINTS,
|
|
27
|
+
fetch: globalThis.fetch.bind(globalThis),
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
async function signInAsAlice(auth) {
|
|
31
|
+
const id = createBrowserIdentity("alice", "Alice");
|
|
32
|
+
const { text } = serializeRecoveryFile(id);
|
|
33
|
+
const info = await auth.signInWithRecovery({ file: text });
|
|
34
|
+
return info.did;
|
|
35
|
+
}
|
|
36
|
+
/* -------------------------------------------------------------------------- */
|
|
37
|
+
/* Owner-required guard */
|
|
38
|
+
/* -------------------------------------------------------------------------- */
|
|
39
|
+
describe("MandatesNamespace owner guard", () => {
|
|
40
|
+
it("create rejects when no owner is signed in", async () => {
|
|
41
|
+
const m = makeMandates(makeAuth());
|
|
42
|
+
await assert.rejects(() => m.create({
|
|
43
|
+
granteeId: "urn:x",
|
|
44
|
+
scopes: ["ethos.read.public"],
|
|
45
|
+
ttlSeconds: 3600,
|
|
46
|
+
}), (e) => e instanceof AithosSDKError &&
|
|
47
|
+
e.code === "mandates_no_owner");
|
|
48
|
+
});
|
|
49
|
+
it("list rejects when no owner is signed in", async () => {
|
|
50
|
+
const m = makeMandates(makeAuth());
|
|
51
|
+
await assert.rejects(() => m.list(), (e) => e instanceof AithosSDKError &&
|
|
52
|
+
e.code === "mandates_no_owner");
|
|
53
|
+
});
|
|
54
|
+
it("revoke rejects when no owner is signed in", async () => {
|
|
55
|
+
const m = makeMandates(makeAuth());
|
|
56
|
+
await assert.rejects(() => m.revoke("mandate:x"), (e) => e instanceof AithosSDKError &&
|
|
57
|
+
e.code === "mandates_no_owner");
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
/* -------------------------------------------------------------------------- */
|
|
61
|
+
/* Input validation */
|
|
62
|
+
/* -------------------------------------------------------------------------- */
|
|
63
|
+
describe("MandatesNamespace.create input validation", () => {
|
|
64
|
+
it("rejects empty scopes", async () => {
|
|
65
|
+
const auth = makeAuth();
|
|
66
|
+
await signInAsAlice(auth);
|
|
67
|
+
const m = makeMandates(auth);
|
|
68
|
+
await assert.rejects(() => m.create({
|
|
69
|
+
granteeId: "urn:x",
|
|
70
|
+
scopes: [],
|
|
71
|
+
ttlSeconds: 3600,
|
|
72
|
+
}), (e) => e instanceof AithosSDKError &&
|
|
73
|
+
e.code === "mandates_invalid_scopes");
|
|
74
|
+
});
|
|
75
|
+
it("rejects non-positive ttl", async () => {
|
|
76
|
+
const auth = makeAuth();
|
|
77
|
+
await signInAsAlice(auth);
|
|
78
|
+
const m = makeMandates(auth);
|
|
79
|
+
await assert.rejects(() => m.create({
|
|
80
|
+
granteeId: "urn:x",
|
|
81
|
+
scopes: ["ethos.read.public"],
|
|
82
|
+
ttlSeconds: 0,
|
|
83
|
+
}), (e) => e instanceof AithosSDKError &&
|
|
84
|
+
e.code === "mandates_invalid_ttl");
|
|
85
|
+
await assert.rejects(() => m.create({
|
|
86
|
+
granteeId: "urn:x",
|
|
87
|
+
scopes: ["ethos.read.public"],
|
|
88
|
+
ttlSeconds: -1,
|
|
89
|
+
}), (e) => e instanceof AithosSDKError &&
|
|
90
|
+
e.code === "mandates_invalid_ttl");
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
//# sourceMappingURL=mandates.test.js.map
|
package/dist/test/sdk.test.js
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
// Copyright 2026 Mathieu Colla
|
|
3
|
-
// Smoke tests for the AithosSDK construction surface.
|
|
4
|
-
//
|
|
5
|
-
// Runtime tests of `compute.invokeBedrock` and `wallet.createTopupSession`
|
|
6
|
-
// require either a running compute proxy or a more elaborate fetch mock
|
|
7
|
-
// pattern; we land them in a follow-up commit alongside the rest of Phase 4
|
|
8
|
-
// (mock-fetch unit tests for both namespaces).
|
|
3
|
+
// Smoke tests for the AithosSDK construction surface (post-J6 wiring).
|
|
9
4
|
import { strict as assert } from "node:assert";
|
|
10
5
|
import { describe, it } from "node:test";
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
function
|
|
16
|
-
return {
|
|
6
|
+
import { createBrowserIdentity } from "@aithos/protocol-client";
|
|
7
|
+
import { AithosAuth, AithosSDK, AithosSDKError, DEFAULT_SDK_ENDPOINTS, memoryKeyStore, noopStore, VERSION, } from "../src/index.js";
|
|
8
|
+
import { serializeRecoveryFile } from "../src/internal/recovery-file.js";
|
|
9
|
+
const APP_DID = "did:aithos:app:smoke";
|
|
10
|
+
function makeAuth() {
|
|
11
|
+
return new AithosAuth({
|
|
12
|
+
authBaseUrl: "https://auth.test",
|
|
13
|
+
fetch: (() => {
|
|
14
|
+
throw new Error("network not expected");
|
|
15
|
+
}),
|
|
16
|
+
sessionStore: noopStore(),
|
|
17
|
+
keyStore: memoryKeyStore(),
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
async function signInAsAlice(auth) {
|
|
21
|
+
const id = createBrowserIdentity("alice", "Alice");
|
|
22
|
+
const { text } = serializeRecoveryFile(id);
|
|
23
|
+
const info = await auth.signInWithRecovery({ file: text });
|
|
24
|
+
return info.did;
|
|
17
25
|
}
|
|
18
26
|
describe("VERSION", () => {
|
|
19
27
|
it("is a non-empty semver-ish string", () => {
|
|
@@ -28,46 +36,53 @@ describe("DEFAULT_SDK_ENDPOINTS", () => {
|
|
|
28
36
|
});
|
|
29
37
|
});
|
|
30
38
|
describe("AithosSDK constructor", () => {
|
|
31
|
-
|
|
32
|
-
const appDid = "did:aithos:app:smoke";
|
|
33
|
-
it("requires an identity", () => {
|
|
39
|
+
it("requires an auth instance", () => {
|
|
34
40
|
assert.throws(() =>
|
|
35
41
|
// @ts-expect-error: deliberately passing an invalid config
|
|
36
|
-
new AithosSDK({ appDid }), /
|
|
42
|
+
new AithosSDK({ appDid: APP_DID }), /auth/);
|
|
37
43
|
});
|
|
38
44
|
it("requires an appDid", () => {
|
|
39
45
|
assert.throws(() =>
|
|
40
46
|
// @ts-expect-error: deliberately passing an invalid config
|
|
41
|
-
new AithosSDK({
|
|
47
|
+
new AithosSDK({ auth: makeAuth() }), /appDid/);
|
|
42
48
|
});
|
|
43
|
-
it("exposes endpoints, appDid, userDid", () => {
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
49
|
+
it("exposes endpoints, appDid, auth, userDid", async () => {
|
|
50
|
+
const auth = makeAuth();
|
|
51
|
+
const did = await signInAsAlice(auth);
|
|
52
|
+
const sdk = new AithosSDK({ auth, appDid: APP_DID });
|
|
53
|
+
assert.equal(sdk.appDid, APP_DID);
|
|
54
|
+
assert.equal(sdk.userDid, did);
|
|
55
|
+
assert.equal(sdk.auth, auth);
|
|
47
56
|
assert.equal(sdk.endpoints.compute, DEFAULT_SDK_ENDPOINTS.compute);
|
|
48
57
|
assert.equal(sdk.endpoints.wallet, DEFAULT_SDK_ENDPOINTS.wallet);
|
|
49
58
|
});
|
|
59
|
+
it("userDid is null when no owner is signed in", () => {
|
|
60
|
+
const auth = makeAuth();
|
|
61
|
+
const sdk = new AithosSDK({ auth, appDid: APP_DID });
|
|
62
|
+
assert.equal(sdk.userDid, null);
|
|
63
|
+
});
|
|
50
64
|
it("merges endpoint overrides on top of defaults", () => {
|
|
65
|
+
const auth = makeAuth();
|
|
51
66
|
const sdk = new AithosSDK({
|
|
52
|
-
|
|
53
|
-
appDid,
|
|
67
|
+
auth,
|
|
68
|
+
appDid: APP_DID,
|
|
54
69
|
endpoints: { wallet: "https://staging-wallet.aithos.be" },
|
|
55
70
|
});
|
|
56
71
|
assert.equal(sdk.endpoints.compute, DEFAULT_SDK_ENDPOINTS.compute);
|
|
57
72
|
assert.equal(sdk.endpoints.wallet, "https://staging-wallet.aithos.be");
|
|
58
73
|
});
|
|
59
74
|
it("trims trailing slashes on endpoint URLs at compose time", async () => {
|
|
60
|
-
|
|
75
|
+
const auth = makeAuth();
|
|
76
|
+
await signInAsAlice(auth);
|
|
61
77
|
let capturedUrl;
|
|
62
78
|
const fakeFetch = async (input) => {
|
|
63
79
|
capturedUrl = typeof input === "string" ? input : input.toString();
|
|
64
|
-
// Reject so we don't have to construct a full happy-path response.
|
|
65
80
|
throw new Error("captured");
|
|
66
81
|
};
|
|
67
82
|
const sdk = new AithosSDK({
|
|
68
|
-
|
|
69
|
-
appDid,
|
|
70
|
-
endpoints: { wallet: "https://wallet.example.com/" },
|
|
83
|
+
auth,
|
|
84
|
+
appDid: APP_DID,
|
|
85
|
+
endpoints: { wallet: "https://wallet.example.com/" },
|
|
71
86
|
fetch: fakeFetch,
|
|
72
87
|
});
|
|
73
88
|
await assert.rejects(sdk.wallet.createTopupSession({
|
|
@@ -77,10 +92,35 @@ describe("AithosSDK constructor", () => {
|
|
|
77
92
|
}), AithosSDKError);
|
|
78
93
|
assert.equal(capturedUrl, "https://wallet.example.com/v1/wallet/topup/checkout");
|
|
79
94
|
});
|
|
80
|
-
it("exposes namespaces compute, wallet", () => {
|
|
81
|
-
const
|
|
95
|
+
it("exposes namespaces compute, wallet, ethos, mandates", async () => {
|
|
96
|
+
const auth = makeAuth();
|
|
97
|
+
await signInAsAlice(auth);
|
|
98
|
+
const sdk = new AithosSDK({ auth, appDid: APP_DID });
|
|
82
99
|
assert.equal(typeof sdk.compute.invokeBedrock, "function");
|
|
83
100
|
assert.equal(typeof sdk.wallet.createTopupSession, "function");
|
|
101
|
+
assert.equal(typeof sdk.ethos.me, "function");
|
|
102
|
+
assert.equal(typeof sdk.mandates.create, "function");
|
|
103
|
+
});
|
|
104
|
+
it("compute.invokeBedrock rejects when no owner AND no delegate matches the mandate", async () => {
|
|
105
|
+
// Since alpha.9, invokeBedrock supports a delegate signing path. The
|
|
106
|
+
// failure mode here (no owner loaded AND no imported mandate matching
|
|
107
|
+
// the requested id) returns code `sdk_no_delegate_for_mandate`.
|
|
108
|
+
// The previous `sdk_no_owner` code now applies only to other entry
|
|
109
|
+
// points where there's no delegate fallback.
|
|
110
|
+
const auth = makeAuth();
|
|
111
|
+
const sdk = new AithosSDK({
|
|
112
|
+
auth,
|
|
113
|
+
appDid: APP_DID,
|
|
114
|
+
fetch: (() => {
|
|
115
|
+
throw new Error("not expected");
|
|
116
|
+
}),
|
|
117
|
+
});
|
|
118
|
+
await assert.rejects(sdk.compute.invokeBedrock({
|
|
119
|
+
mandateId: "mandate:x",
|
|
120
|
+
model: "claude-sonnet-4-6",
|
|
121
|
+
messages: [{ role: "user", content: "hi" }],
|
|
122
|
+
}), (e) => e instanceof AithosSDKError &&
|
|
123
|
+
e.code === "sdk_no_delegate_for_mandate");
|
|
84
124
|
});
|
|
85
125
|
});
|
|
86
126
|
//# sourceMappingURL=sdk.test.js.map
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright 2026 Mathieu Colla
|
|
3
|
+
// Unit tests for the internal Signer abstraction.
|
|
4
|
+
import { strict as assert } from "node:assert";
|
|
5
|
+
import { describe, it } from "node:test";
|
|
6
|
+
import { createBrowserIdentity, verify as ed25519Verify, } from "@aithos/protocol-client";
|
|
7
|
+
import { RawSeedSigner } from "../src/internal/signer.js";
|
|
8
|
+
import { OwnerSigners } from "../src/internal/owner-signers.js";
|
|
9
|
+
/* -------------------------------------------------------------------------- */
|
|
10
|
+
/* RawSeedSigner */
|
|
11
|
+
/* -------------------------------------------------------------------------- */
|
|
12
|
+
describe("RawSeedSigner", () => {
|
|
13
|
+
it("signs a message that verifies under its public key", async () => {
|
|
14
|
+
const id = createBrowserIdentity("alice", "Alice");
|
|
15
|
+
const signer = new RawSeedSigner(id.public.seed, id.public.publicKey);
|
|
16
|
+
const msg = new TextEncoder().encode("hello aithos");
|
|
17
|
+
const sig = await signer.sign(msg);
|
|
18
|
+
assert.equal(sig.length, 64, "Ed25519 signatures are 64 bytes");
|
|
19
|
+
assert.ok(ed25519Verify(sig, msg, signer.publicKey), "signature should verify under the signer's public key");
|
|
20
|
+
});
|
|
21
|
+
it("defensively copies the seed — mutating the input has no effect", async () => {
|
|
22
|
+
const id = createBrowserIdentity("alice", "Alice");
|
|
23
|
+
const seedCopy = new Uint8Array(id.public.seed);
|
|
24
|
+
const signer = new RawSeedSigner(id.public.seed, id.public.publicKey);
|
|
25
|
+
// Tamper with the original seed buffer the caller passed in.
|
|
26
|
+
id.public.seed.fill(0xff);
|
|
27
|
+
const msg = new TextEncoder().encode("hello");
|
|
28
|
+
const sig = await signer.sign(msg);
|
|
29
|
+
// Recompute with the pristine seed → must match.
|
|
30
|
+
const expected = await new RawSeedSigner(seedCopy, id.public.publicKey).sign(msg);
|
|
31
|
+
assert.deepEqual(sig, expected, "signer must use its own copy of the seed");
|
|
32
|
+
});
|
|
33
|
+
it("destroy zeroizes and blocks further use", async () => {
|
|
34
|
+
const id = createBrowserIdentity("alice", "Alice");
|
|
35
|
+
const signer = new RawSeedSigner(id.public.seed, id.public.publicKey);
|
|
36
|
+
signer.destroy();
|
|
37
|
+
await assert.rejects(() => signer.sign(new Uint8Array(1)), /destroyed/, "sign() must reject after destroy()");
|
|
38
|
+
assert.throws(() => signer._unsafeKeyPair(), /destroyed/);
|
|
39
|
+
// Idempotent — second destroy() is a no-op.
|
|
40
|
+
assert.doesNotThrow(() => signer.destroy());
|
|
41
|
+
});
|
|
42
|
+
it("rejects malformed seed/public-key sizes", () => {
|
|
43
|
+
const id = createBrowserIdentity("alice", "Alice");
|
|
44
|
+
assert.throws(() => new RawSeedSigner(new Uint8Array(31), id.public.publicKey), /seed must be 32 bytes/);
|
|
45
|
+
assert.throws(() => new RawSeedSigner(id.public.seed, new Uint8Array(33)), /publicKey must be 32 bytes/);
|
|
46
|
+
});
|
|
47
|
+
it("_unsafeKeyPair returns a KeyPair compatible with protocol-client", async () => {
|
|
48
|
+
const id = createBrowserIdentity("alice", "Alice");
|
|
49
|
+
const signer = new RawSeedSigner(id.public.seed, id.public.publicKey);
|
|
50
|
+
const kp = signer._unsafeKeyPair();
|
|
51
|
+
assert.equal(kp.seed.length, 32);
|
|
52
|
+
assert.equal(kp.publicKey.length, 32);
|
|
53
|
+
assert.deepEqual(kp.publicKey, signer.publicKey);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
/* -------------------------------------------------------------------------- */
|
|
57
|
+
/* OwnerSigners */
|
|
58
|
+
/* -------------------------------------------------------------------------- */
|
|
59
|
+
describe("OwnerSigners", () => {
|
|
60
|
+
it("fromBrowserIdentity yields four working signers", async () => {
|
|
61
|
+
const id = createBrowserIdentity("alice", "Alice");
|
|
62
|
+
const signers = OwnerSigners.fromBrowserIdentity(id);
|
|
63
|
+
assert.equal(signers.did, id.did);
|
|
64
|
+
assert.equal(signers.handle, id.handle);
|
|
65
|
+
assert.equal(signers.displayName, id.displayName);
|
|
66
|
+
for (const sphere of ["root", "public", "circle", "self"]) {
|
|
67
|
+
const sig = await signers[sphere].sign(new TextEncoder().encode(sphere));
|
|
68
|
+
assert.equal(sig.length, 64, `${sphere}: sig length`);
|
|
69
|
+
assert.ok(ed25519Verify(sig, new TextEncoder().encode(sphere), signers[sphere].publicKey), `${sphere}: signature must verify`);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
it("signerForSphere maps strings to the right signer", () => {
|
|
73
|
+
const id = createBrowserIdentity("alice", "Alice");
|
|
74
|
+
const signers = OwnerSigners.fromBrowserIdentity(id);
|
|
75
|
+
assert.strictEqual(signers.signerForSphere("root"), signers.root);
|
|
76
|
+
assert.strictEqual(signers.signerForSphere("public"), signers.public);
|
|
77
|
+
assert.strictEqual(signers.signerForSphere("circle"), signers.circle);
|
|
78
|
+
assert.strictEqual(signers.signerForSphere("self"), signers.self);
|
|
79
|
+
});
|
|
80
|
+
it("destroy zeroizes all signers", async () => {
|
|
81
|
+
const id = createBrowserIdentity("alice", "Alice");
|
|
82
|
+
const signers = OwnerSigners.fromBrowserIdentity(id);
|
|
83
|
+
signers.destroy();
|
|
84
|
+
assert.equal(signers.destroyed, true);
|
|
85
|
+
await assert.rejects(() => signers.public.sign(new Uint8Array(1)), /destroyed/);
|
|
86
|
+
// Idempotent.
|
|
87
|
+
assert.doesNotThrow(() => signers.destroy());
|
|
88
|
+
});
|
|
89
|
+
it("fromStoredIdentity round-trips through hex", async () => {
|
|
90
|
+
const id = createBrowserIdentity("alice", "Alice");
|
|
91
|
+
const stored = {
|
|
92
|
+
version: "0.1.0",
|
|
93
|
+
handle: id.handle,
|
|
94
|
+
displayName: id.displayName,
|
|
95
|
+
did: id.did,
|
|
96
|
+
seeds: {
|
|
97
|
+
root: bytesToHex(id.root.seed),
|
|
98
|
+
public: bytesToHex(id.public.seed),
|
|
99
|
+
circle: bytesToHex(id.circle.seed),
|
|
100
|
+
self: bytesToHex(id.self.seed),
|
|
101
|
+
},
|
|
102
|
+
savedAt: new Date().toISOString(),
|
|
103
|
+
};
|
|
104
|
+
const signers = OwnerSigners.fromStoredIdentity(stored);
|
|
105
|
+
const sig = await signers.public.sign(new TextEncoder().encode("test"));
|
|
106
|
+
assert.ok(ed25519Verify(sig, new TextEncoder().encode("test"), signers.public.publicKey));
|
|
107
|
+
// The pubkey must match what we'd derive from the original BrowserIdentity.
|
|
108
|
+
assert.deepEqual(signers.public.publicKey, id.public.publicKey);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
function bytesToHex(b) {
|
|
112
|
+
let out = "";
|
|
113
|
+
for (let i = 0; i < b.length; i++)
|
|
114
|
+
out += b[i].toString(16).padStart(2, "0");
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=signer.test.js.map
|