@calimero-network/mero-js 22.7.0 → 22.8.0
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/dist/account/account.d.ts +99 -4
- package/dist/account/account.js +0 -0
- package/dist/account/index.d.ts +6 -2
- package/dist/account/index.js +3 -1
- package/dist/admin-api/admin-client.d.ts +138 -2
- package/dist/admin-api/admin-client.js +237 -1
- package/dist/admin-api/admin-types.d.ts +159 -5
- package/dist/cloud/cloud-client.d.ts +38 -8
- package/dist/cloud/cloud-client.js +38 -21
- package/dist/cloud/connect.d.ts +101 -2
- package/dist/cloud/connect.js +84 -8
- package/dist/cloud/enrol-redirect.d.ts +195 -0
- package/dist/cloud/enrol-redirect.js +236 -0
- package/dist/cloud/index.d.ts +6 -2
- package/dist/cloud/index.js +7 -1
- package/dist/cloud/link-redirect.d.ts +85 -0
- package/dist/cloud/link-redirect.js +105 -0
- package/dist/crypto/internal.d.ts +15 -0
- package/dist/crypto/internal.js +18 -0
- package/dist/device-cert/device-cert.d.ts +85 -2
- package/dist/device-cert/device-cert.js +100 -9
- package/dist/device-cert/index.d.ts +2 -2
- package/dist/device-cert/index.js +1 -1
- package/dist/events/sse.d.ts +22 -2
- package/dist/events/sse.js +20 -11
- package/dist/http-client/http-types.d.ts +18 -0
- package/dist/http-client/web-client.js +21 -1
- package/dist/index.browser.mjs +2 -2
- package/dist/index.browser.mjs.map +4 -4
- package/dist/index.cjs +1666 -365
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +9 -4
- package/dist/index.js +16 -2
- package/dist/index.mjs +1666 -365
- package/dist/index.mjs.map +4 -4
- package/dist/login/login.d.ts +11 -1
- package/dist/login/login.js +5 -4
- package/dist/login/session.d.ts +21 -5
- package/dist/login/session.js +7 -15
- package/dist/namespace-op/namespace-op.d.ts +14 -2
- package/dist/namespace-op/namespace-op.js +9 -5
- package/dist/relay/index.d.ts +2 -2
- package/dist/relay/index.js +1 -1
- package/dist/relay/nonce-source.d.ts +135 -0
- package/dist/relay/nonce-source.js +187 -19
- package/dist/relay/relay-client.d.ts +60 -5
- package/dist/relay/relay-client.js +84 -4
- package/dist/relay-attestation/attest-node-key.d.ts +66 -0
- package/dist/relay-attestation/attest-node-key.js +110 -0
- package/dist/relay-attestation/index.d.ts +1 -0
- package/dist/relay-attestation/index.js +1 -0
- package/dist/request-proof/authorizer.d.ts +48 -0
- package/dist/request-proof/authorizer.js +65 -0
- package/dist/request-proof/index.d.ts +4 -0
- package/dist/request-proof/index.js +2 -0
- package/dist/request-proof/request-proof.d.ts +56 -0
- package/dist/request-proof/request-proof.js +111 -0
- package/dist/rpc/index.d.ts +21 -0
- package/dist/rpc/index.js +20 -0
- package/dist/transport/index.d.ts +6 -0
- package/dist/transport/index.js +3 -0
- package/dist/transport/mero-client.d.ts +286 -0
- package/dist/transport/mero-client.js +379 -0
- package/dist/transport/relay-observer.d.ts +152 -0
- package/dist/transport/relay-observer.js +173 -0
- package/dist/transport/relay-transport.d.ts +73 -0
- package/dist/transport/relay-transport.js +94 -0
- package/dist/transport/types.d.ts +100 -0
- package/dist/transport/types.js +32 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -33,9 +33,13 @@ __export(index_exports, {
|
|
|
33
33
|
IntentRefusedError: () => IntentRefusedError,
|
|
34
34
|
LocalStorageTokenStore: () => LocalStorageTokenStore,
|
|
35
35
|
MemoryTokenStore: () => MemoryTokenStore,
|
|
36
|
+
MeroClient: () => MeroClient,
|
|
36
37
|
MeroJs: () => MeroJs,
|
|
37
38
|
NODE_RELEASE_SIGNER: () => NODE_RELEASE_SIGNER,
|
|
39
|
+
REQUEST_PROOF_HEADER: () => REQUEST_PROOF_HEADER,
|
|
38
40
|
RelayClient: () => RelayClient,
|
|
41
|
+
RelayObserver: () => RelayObserver,
|
|
42
|
+
RelayTransport: () => RelayTransport,
|
|
39
43
|
RpcClient: () => RpcClient,
|
|
40
44
|
RpcError: () => RpcError,
|
|
41
45
|
SEALED_CONTENT_TYPE: () => SEALED_CONTENT_TYPE,
|
|
@@ -44,21 +48,36 @@ __export(index_exports, {
|
|
|
44
48
|
SealedTransportError: () => SealedTransportError,
|
|
45
49
|
SseClient: () => SseClient,
|
|
46
50
|
StaleTransportKeyError: () => StaleTransportKeyError,
|
|
51
|
+
WarrantNonceExhaustedError: () => WarrantNonceExhaustedError,
|
|
52
|
+
WarrantNonceRouteUnavailableError: () => WarrantNonceRouteUnavailableError,
|
|
47
53
|
WebHttpClient: () => WebHttpClient,
|
|
48
54
|
WsClient: () => WsClient,
|
|
49
55
|
accountForRoot: () => accountForRoot,
|
|
56
|
+
accountForRootPublicKey: () => accountForRootPublicKey,
|
|
50
57
|
accountRootFromPhrase: () => accountRootFromPhrase,
|
|
51
58
|
accountRootFromSecret: () => accountRootFromSecret,
|
|
59
|
+
accountRootSignerFromPhrase: () => accountRootSignerFromPhrase,
|
|
52
60
|
admitterAddrsFromNetworkStatus: () => admitterAddrsFromNetworkStatus,
|
|
61
|
+
attestKeyBinding: () => attestKeyBinding,
|
|
62
|
+
attestRelayNodeKey: () => attestRelayNodeKey,
|
|
63
|
+
authorNonceLookup: () => authorNonceLookup,
|
|
64
|
+
beginCloudLink: () => beginCloudLink,
|
|
65
|
+
beginDeviceEnrolment: () => beginDeviceEnrolment,
|
|
53
66
|
buildAuthLoginUrl: () => buildAuthLoginUrl,
|
|
67
|
+
callbackIsForeign: () => callbackIsForeign,
|
|
54
68
|
callerProof: () => callerProof,
|
|
55
69
|
capMask: () => capMask,
|
|
56
70
|
capNames: () => capNames,
|
|
57
71
|
classifyError: () => classifyError,
|
|
72
|
+
cloudLinkUrl: () => cloudLinkUrl,
|
|
58
73
|
cloudNodeReleaseUrl: () => cloudNodeReleaseUrl,
|
|
59
74
|
combineSignals: () => combineSignals,
|
|
60
75
|
compareSemver: () => compareSemver,
|
|
76
|
+
completeCloudLink: () => completeCloudLink,
|
|
77
|
+
completeDeviceEnrolment: () => completeDeviceEnrolment,
|
|
61
78
|
connectCloud: () => connectCloud,
|
|
79
|
+
connectCloudWithAccount: () => connectCloudWithAccount,
|
|
80
|
+
createAccountRootSigner: () => createAccountRootSigner,
|
|
62
81
|
createAdminApiClient: () => createAdminApiClient,
|
|
63
82
|
createAdminApiClientFromHttpClient: () => createAdminApiClientFromHttpClient,
|
|
64
83
|
createAttestedSealedFetch: () => createAttestedSealedFetch,
|
|
@@ -70,12 +89,15 @@ __export(index_exports, {
|
|
|
70
89
|
createHttpClient: () => createHttpClient,
|
|
71
90
|
createLocalStorageNonceSource: () => createLocalStorageNonceSource,
|
|
72
91
|
createMemoryNonceSource: () => createMemoryNonceSource,
|
|
92
|
+
createMeroClient: () => createMeroClient,
|
|
73
93
|
createMeroJs: () => createMeroJs,
|
|
74
94
|
createNodeAdminApiClient: () => createNodeAdminApiClient,
|
|
75
95
|
createNodeAuthApiClient: () => createNodeAuthApiClient,
|
|
76
96
|
createNodeHttpClient: () => createNodeHttpClient,
|
|
97
|
+
createProofAuthorizer: () => createProofAuthorizer,
|
|
77
98
|
createProofSigner: () => createProofSigner,
|
|
78
99
|
createQuoteVerifier: () => createQuoteVerifier,
|
|
100
|
+
createRecoveringNonceSource: () => createRecoveringNonceSource,
|
|
79
101
|
createRetryableMethod: () => createRetryableMethod,
|
|
80
102
|
createSealedFetch: () => createSealedFetch,
|
|
81
103
|
createSignedReleaseSealedFetch: () => createSignedReleaseSealedFetch,
|
|
@@ -83,8 +105,10 @@ __export(index_exports, {
|
|
|
83
105
|
createTimeoutSignal: () => createTimeoutSignal,
|
|
84
106
|
createUniversalHttpClient: () => createUniversalHttpClient,
|
|
85
107
|
creationInitHash: () => creationInitHash,
|
|
108
|
+
defaultAudience: () => defaultAudience,
|
|
86
109
|
defaultCapabilitiesSetOp: () => defaultCapabilitiesSetOp,
|
|
87
110
|
deviceCertPayload: () => deviceCertPayload,
|
|
111
|
+
deviceEnrolmentUrl: () => deviceEnrolmentUrl,
|
|
88
112
|
discoverLocalNodes: () => discoverLocalNodes,
|
|
89
113
|
encodeSignedInvitation: () => encodeSignedInvitation,
|
|
90
114
|
fetchAttestedTransportKey: () => fetchAttestedTransportKey,
|
|
@@ -114,10 +138,17 @@ __export(index_exports, {
|
|
|
114
138
|
nodeReleaseVersionFromImage: () => nodeReleaseVersionFromImage,
|
|
115
139
|
parseAuthCallback: () => parseAuthCallback,
|
|
116
140
|
parseCreationWarrant: () => parseCreationWarrant,
|
|
141
|
+
parseDeviceCredential: () => parseDeviceCredential,
|
|
117
142
|
parseGovernanceWarrant: () => parseGovernanceWarrant,
|
|
143
|
+
parseWarrantNonce: () => parseWarrantNonce,
|
|
118
144
|
probeNodeHealth: () => probeNodeHealth,
|
|
145
|
+
readCloudLinkCallback: () => readCloudLinkCallback,
|
|
146
|
+
readEnrolmentCallback: () => readEnrolmentCallback,
|
|
119
147
|
redeemInvitation: () => redeemInvitation,
|
|
148
|
+
reportDataOf: () => reportDataOf,
|
|
120
149
|
requestBodyHash: () => requestBodyHash,
|
|
150
|
+
resolveRoot: () => resolveRoot,
|
|
151
|
+
resolveRootPair: () => resolveRootPair,
|
|
121
152
|
routingProofHeaders: () => routingProofHeaders,
|
|
122
153
|
shouldRetain: () => shouldRetain,
|
|
123
154
|
signAccountLink: () => signAccountLink,
|
|
@@ -128,13 +159,16 @@ __export(index_exports, {
|
|
|
128
159
|
signLoginStatement: () => signLoginStatement,
|
|
129
160
|
signMemberJoinOp: () => signMemberJoinOp,
|
|
130
161
|
signRequest: () => signRequest,
|
|
162
|
+
signRequestProof: () => signRequestProof,
|
|
131
163
|
signRoutingChallenge: () => signRoutingChallenge,
|
|
132
164
|
signWarrant: () => signWarrant,
|
|
165
|
+
signedPath: () => signedPath,
|
|
133
166
|
signerFromCryptoKey: () => signerFromCryptoKey,
|
|
134
167
|
signerFromSecret: () => signerFromSecret,
|
|
135
168
|
transportKeyBinding: () => transportKeyBinding,
|
|
136
169
|
trustSignedRelease: () => trustSignedRelease,
|
|
137
170
|
trustedMeasurementsFromReleases: () => trustedMeasurementsFromReleases,
|
|
171
|
+
verifyDeviceCredential: () => verifyDeviceCredential,
|
|
138
172
|
verifySignedNodeRelease: () => verifySignedNodeRelease,
|
|
139
173
|
withCap: () => withCap,
|
|
140
174
|
withRetry: () => withRetry,
|
|
@@ -314,8 +348,27 @@ var WebHttpClient = class {
|
|
|
314
348
|
} else {
|
|
315
349
|
headersObj = headers;
|
|
316
350
|
}
|
|
351
|
+
const method = init?.method || "GET";
|
|
352
|
+
if (this.transport.authorizeRequest) {
|
|
353
|
+
const body = init?.body;
|
|
354
|
+
if (body !== void 0 && typeof body !== "string" && !(body instanceof Uint8Array)) {
|
|
355
|
+
throw new Error(
|
|
356
|
+
"this transport authorizes each request against its body, so the body must be a string or Uint8Array \u2014 a stream cannot be signed, because the signature would have to read bytes the request then cannot send."
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
Object.assign(
|
|
360
|
+
headersObj,
|
|
361
|
+
await this.transport.authorizeRequest({
|
|
362
|
+
method,
|
|
363
|
+
path: new URL(url).pathname,
|
|
364
|
+
// Narrowed by the guard above; `BodyInit` is wider than what can be
|
|
365
|
+
// signed.
|
|
366
|
+
body
|
|
367
|
+
})
|
|
368
|
+
);
|
|
369
|
+
}
|
|
317
370
|
const requestInit = {
|
|
318
|
-
method
|
|
371
|
+
method,
|
|
319
372
|
headers: headersObj
|
|
320
373
|
};
|
|
321
374
|
const isStreamBody = init?.body instanceof ReadableStream || typeof init?.body === "object" && init?.body !== null && "getReader" in init.body && !(init.body instanceof Blob);
|
|
@@ -1075,6 +1128,50 @@ async function redeemInvitation(parsed, redeemer) {
|
|
|
1075
1128
|
}
|
|
1076
1129
|
|
|
1077
1130
|
// src/admin-api/admin-client.ts
|
|
1131
|
+
var WarrantNonceRouteUnavailableError = class extends Error {
|
|
1132
|
+
constructor(path, cause) {
|
|
1133
|
+
super(
|
|
1134
|
+
`this node does not serve warrant-nonce discovery (404 on ${path}); it predates the route, so a client must fall back to its own nonce counter`
|
|
1135
|
+
);
|
|
1136
|
+
this.path = path;
|
|
1137
|
+
this.cause = cause;
|
|
1138
|
+
this.name = "WarrantNonceRouteUnavailableError";
|
|
1139
|
+
}
|
|
1140
|
+
};
|
|
1141
|
+
function u64FromBody(body, parsed, field) {
|
|
1142
|
+
const value = parsed[field];
|
|
1143
|
+
if (value === void 0 || value === null) return void 0;
|
|
1144
|
+
const digits = new RegExp(`"${field}"\\s*:\\s*(\\d+)`).exec(body);
|
|
1145
|
+
if (digits) return BigInt(digits[1]);
|
|
1146
|
+
if (typeof value === "string" && /^\d+$/.test(value)) return BigInt(value);
|
|
1147
|
+
throw new Error(`warrant-nonce response field \`${field}\` is not a u64: ${JSON.stringify(value)}`);
|
|
1148
|
+
}
|
|
1149
|
+
function parseWarrantNonce(body) {
|
|
1150
|
+
const parsed = JSON.parse(body);
|
|
1151
|
+
const data = parsed?.data;
|
|
1152
|
+
if (!data || typeof data !== "object") {
|
|
1153
|
+
throw new Error(`warrant-nonce response had no \`data\`: ${body.slice(0, 200)}`);
|
|
1154
|
+
}
|
|
1155
|
+
const common = {
|
|
1156
|
+
contextId: String(data.contextId ?? ""),
|
|
1157
|
+
authorDeviceKey: String(data.authorDeviceKey ?? ""),
|
|
1158
|
+
seen: data.seen === true,
|
|
1159
|
+
highWaterNonce: u64FromBody(body, data, "highWaterNonce"),
|
|
1160
|
+
// Not defaulted: a node that stopped sending it is a node whose window this
|
|
1161
|
+
// client would be guessing at, and the window is not a nonce input anyway.
|
|
1162
|
+
windowWidth: u64FromBody(body, data, "windowWidth") ?? 0n
|
|
1163
|
+
};
|
|
1164
|
+
const nextNonce = u64FromBody(body, data, "nextNonce");
|
|
1165
|
+
if (nextNonce === void 0) {
|
|
1166
|
+
return {
|
|
1167
|
+
...common,
|
|
1168
|
+
kind: "exhausted",
|
|
1169
|
+
seen: true,
|
|
1170
|
+
highWaterNonce: common.highWaterNonce ?? 0xffffffffffffffffn
|
|
1171
|
+
};
|
|
1172
|
+
}
|
|
1173
|
+
return { ...common, kind: "open", nextNonce };
|
|
1174
|
+
}
|
|
1078
1175
|
function unwrap(response) {
|
|
1079
1176
|
return response?.data;
|
|
1080
1177
|
}
|
|
@@ -1875,6 +1972,13 @@ var AdminApiClient = class {
|
|
|
1875
1972
|
* `namespaceId` names where to publish from, not the extent of the revocation:
|
|
1876
1973
|
* a device belongs to the account, so every namespace holding a binding for it
|
|
1877
1974
|
* is withdrawn from and reported back.
|
|
1975
|
+
*
|
|
1976
|
+
* **Revocation is forward-only.** It withdraws the device's authority from
|
|
1977
|
+
* here on and does NOT retract past authorship: ops the device already signed
|
|
1978
|
+
* keep verifying and stay in the DAG, because the certificate that authorised
|
|
1979
|
+
* them was valid when the signature was made. Nothing in this system can
|
|
1980
|
+
* un-author what a device wrote, so a UI built on this call must not offer
|
|
1981
|
+
* that - "this device can no longer act" is the only claim it supports.
|
|
1878
1982
|
*/
|
|
1879
1983
|
async revokeAccountDevice(namespaceId, request) {
|
|
1880
1984
|
return unwrap(
|
|
@@ -1917,6 +2021,138 @@ var AdminApiClient = class {
|
|
|
1917
2021
|
}
|
|
1918
2022
|
return response;
|
|
1919
2023
|
}
|
|
2024
|
+
/**
|
|
2025
|
+
* Account -> devices for the members of a group, as its live bindings record
|
|
2026
|
+
* them.
|
|
2027
|
+
*
|
|
2028
|
+
* The join between a group member listing, which names accounts, and a
|
|
2029
|
+
* context's identities, which name bare signing keys - both 64 hex, and
|
|
2030
|
+
* nothing but the source tells them apart. Batch rather than per-account
|
|
2031
|
+
* because the key -> account direction gets asked about arbitrary authors,
|
|
2032
|
+
* which a per-account route would answer in N calls.
|
|
2033
|
+
*
|
|
2034
|
+
* What comes back is **caller-scoped by the node**: an admin of the group sees
|
|
2035
|
+
* every member's devices, a plain member only its own entry. A single-entry
|
|
2036
|
+
* answer is therefore a statement about this caller's role, not about the
|
|
2037
|
+
* group's size.
|
|
2038
|
+
*
|
|
2039
|
+
* Only live bindings appear, so a revoked device is absent here - use
|
|
2040
|
+
* {@link listAccountDevices} for the account's own devices, which keeps a
|
|
2041
|
+
* revoked one with `revoked: true`. Absence here never means the device never
|
|
2042
|
+
* acted: revocation is forward-only and its past ops remain in the DAG.
|
|
2043
|
+
*
|
|
2044
|
+
* Wrapped in `members` on the wire rather than `data`, unwrapped here for the
|
|
2045
|
+
* same reason {@link listAccountDevices} unwraps `devices`.
|
|
2046
|
+
*/
|
|
2047
|
+
async listGroupMemberDevices(groupId, options) {
|
|
2048
|
+
const params = new URLSearchParams();
|
|
2049
|
+
if (options?.offset !== void 0) params.set("offset", String(options.offset));
|
|
2050
|
+
if (options?.limit !== void 0) params.set("limit", String(options.limit));
|
|
2051
|
+
const query = params.toString();
|
|
2052
|
+
const base = `/admin-api/groups/${encodeURIComponent(groupId)}/member-devices`;
|
|
2053
|
+
const response = await this.httpClient.get(
|
|
2054
|
+
query ? `${base}?${query}` : base
|
|
2055
|
+
);
|
|
2056
|
+
return response.members;
|
|
2057
|
+
}
|
|
2058
|
+
/**
|
|
2059
|
+
* Where `authorDeviceKey` stands in its warrant-nonce sequence in `contextId`.
|
|
2060
|
+
*
|
|
2061
|
+
* # What this is for
|
|
2062
|
+
*
|
|
2063
|
+
* The nonce is the one input to a warrant that is stateful on the *client*.
|
|
2064
|
+
* Everything else an author needs it either holds (its keys, the method, the
|
|
2065
|
+
* args) or can read from the relay ({@link RelayClient.describe} gives the
|
|
2066
|
+
* executor account and the grant). So an author that has lost its counter —
|
|
2067
|
+
* a browser keyholder in partitioned or periodically cleared storage, which
|
|
2068
|
+
* is its normal lifecycle rather than a fault — otherwise has exactly one
|
|
2069
|
+
* strategy: guess upward and burn a refused round trip per wrong guess, with
|
|
2070
|
+
* each refusal indistinguishable at the API from a genuinely spent nonce.
|
|
2071
|
+
*
|
|
2072
|
+
* Read {@link WarrantNonceOpen.nextNonce} and mint there. If you still hold
|
|
2073
|
+
* your own counter, mint at `max(ownNext, nextNonce)`: nonce state folds per
|
|
2074
|
+
* peer, so a node can only be BEHIND a client whose warrants it has not
|
|
2075
|
+
* applied — never ahead of the truth. An author spread across several relays
|
|
2076
|
+
* asks each and takes the highest answer. {@link createRecoveringNonceSource}
|
|
2077
|
+
* does all of that.
|
|
2078
|
+
*
|
|
2079
|
+
* Purely a read. It spends nothing and advances nothing, so it may be called
|
|
2080
|
+
* as often as you like.
|
|
2081
|
+
*
|
|
2082
|
+
* # Forward compatibility — this route is NEW
|
|
2083
|
+
*
|
|
2084
|
+
* It is not on core `master` yet, so a current node answers **404**, which
|
|
2085
|
+
* this method surfaces as {@link WarrantNonceRouteUnavailableError} rather
|
|
2086
|
+
* than as an opaque {@link HTTPError}. That distinction matters because the
|
|
2087
|
+
* handler itself never 404s: a device with no row is a `200` with
|
|
2088
|
+
* `seen: false`, and a malformed id is a `400`. So a 404 here means the node
|
|
2089
|
+
* does not serve the route at all — an older build, or a deployment that has
|
|
2090
|
+
* not re-mounted it — and the answer is to fall back to a local counter, not
|
|
2091
|
+
* to retry.
|
|
2092
|
+
*
|
|
2093
|
+
* # Authentication
|
|
2094
|
+
*
|
|
2095
|
+
* On the PROTECTED admin router, unlike the two delegated-execution routes it
|
|
2096
|
+
* sits beside: it takes another principal's device key as input and reports
|
|
2097
|
+
* that principal's activity. A `--public-intents` deployment does not serve
|
|
2098
|
+
* this anonymously, so an admin credential is required.
|
|
2099
|
+
*
|
|
2100
|
+
* @param authorDeviceKey the device's signing key, base58 (43-44 chars) or
|
|
2101
|
+
* hex (exactly 64). The node accepts either; the two never collide.
|
|
2102
|
+
*/
|
|
2103
|
+
async getWarrantNonce(contextId, authorDeviceKey) {
|
|
2104
|
+
const path = `/admin-api/contexts/${encodeURIComponent(contextId)}/warrant-nonce/${encodeURIComponent(authorDeviceKey)}`;
|
|
2105
|
+
let raw;
|
|
2106
|
+
try {
|
|
2107
|
+
raw = await this.httpClient.get(path, { parse: "text" });
|
|
2108
|
+
} catch (error) {
|
|
2109
|
+
if (error instanceof HTTPError && error.status === 404) {
|
|
2110
|
+
throw new WarrantNonceRouteUnavailableError(path, error);
|
|
2111
|
+
}
|
|
2112
|
+
throw error;
|
|
2113
|
+
}
|
|
2114
|
+
return parseWarrantNonce(raw);
|
|
2115
|
+
}
|
|
2116
|
+
/**
|
|
2117
|
+
* The same answer, asked for the way a delegated client has to ask.
|
|
2118
|
+
*
|
|
2119
|
+
* {@link getWarrantNonce} is on the protected admin router, so a keyholder
|
|
2120
|
+
* writing through a relay — the client this whole facility exists for — is
|
|
2121
|
+
* refused it: measured `403` for a session carrying exactly
|
|
2122
|
+
* `context:intent`, `context:query`, `context:subscribe`. That client has no
|
|
2123
|
+
* admin credential and cannot be given one without defeating the point of the
|
|
2124
|
+
* delegated surface.
|
|
2125
|
+
*
|
|
2126
|
+
* What it does have is the credential proving the device is its own, which it
|
|
2127
|
+
* already sends on every write. Presenting it here makes the request
|
|
2128
|
+
* self-scoped: you learn a device's position only by proving, at call time,
|
|
2129
|
+
* that the device is yours. So there is no oracle to protect against, and no
|
|
2130
|
+
* reason for an admin gate.
|
|
2131
|
+
*
|
|
2132
|
+
* The device key is NOT a parameter. It is read from the verified
|
|
2133
|
+
* certificate server-side, which is the only reading that cannot disagree
|
|
2134
|
+
* with the signature — a key supplied alongside would be a second spelling of
|
|
2135
|
+
* the same fact, and two spellings can differ.
|
|
2136
|
+
*
|
|
2137
|
+
* Returns the identical shape {@link getWarrantNonce} returns, so a caller can
|
|
2138
|
+
* swap one for the other without touching what it does with the answer.
|
|
2139
|
+
*
|
|
2140
|
+
* @param authorProof hex-encoded borsh `AccountProof<DeviceCert>` — the same
|
|
2141
|
+
* value passed as `authorProof` to the relay client.
|
|
2142
|
+
*/
|
|
2143
|
+
async getWarrantNonceAsAuthor(contextId, authorProof) {
|
|
2144
|
+
const path = `/admin-api/contexts/${encodeURIComponent(contextId)}/warrant-nonce`;
|
|
2145
|
+
let raw;
|
|
2146
|
+
try {
|
|
2147
|
+
raw = await this.httpClient.post(path, { authorProof }, { parse: "text" });
|
|
2148
|
+
} catch (error) {
|
|
2149
|
+
if (error instanceof HTTPError && error.status === 404) {
|
|
2150
|
+
throw new WarrantNonceRouteUnavailableError(path, error);
|
|
2151
|
+
}
|
|
2152
|
+
throw error;
|
|
2153
|
+
}
|
|
2154
|
+
return parseWarrantNonce(raw);
|
|
2155
|
+
}
|
|
1920
2156
|
/**
|
|
1921
2157
|
* Member accounts in the group and the devices bound to each. An admin node
|
|
1922
2158
|
* sees every account; a plain member only its own. `403` if this node is not a
|
|
@@ -2427,6 +2663,16 @@ async function importSigningKey(secretHex) {
|
|
|
2427
2663
|
);
|
|
2428
2664
|
}
|
|
2429
2665
|
}
|
|
2666
|
+
async function verifySignature(publicKey, signature, payload) {
|
|
2667
|
+
const key = await crypto.subtle.importKey(
|
|
2668
|
+
"raw",
|
|
2669
|
+
fromHex(publicKey, "publicKey", 32),
|
|
2670
|
+
{ name: "Ed25519" },
|
|
2671
|
+
false,
|
|
2672
|
+
["verify"]
|
|
2673
|
+
);
|
|
2674
|
+
return crypto.subtle.verify({ name: "Ed25519" }, key, signature, payload);
|
|
2675
|
+
}
|
|
2430
2676
|
async function derivePublicKey(secretHex) {
|
|
2431
2677
|
const seed = fromHex(secretHex, "deviceSecret", 32);
|
|
2432
2678
|
const pkcs8 = concat(PKCS8_ED25519_PREFIX, seed);
|
|
@@ -2550,6 +2796,15 @@ async function jsonRpcCall(httpClient, method, params) {
|
|
|
2550
2796
|
}
|
|
2551
2797
|
var RpcClient = class {
|
|
2552
2798
|
constructor(opts) {
|
|
2799
|
+
/**
|
|
2800
|
+
* Which write path this is. Present so `RpcClient` satisfies
|
|
2801
|
+
* `ExecuteTransport` as-is — the transport abstraction adopted *this* class's
|
|
2802
|
+
* `execute` shape as the canonical one, so the node side is the interface
|
|
2803
|
+
* rather than an adapter onto it.
|
|
2804
|
+
*/
|
|
2805
|
+
this.kind = "node";
|
|
2806
|
+
/** A node serves `/sse` and `/ws` to the credential it issued. */
|
|
2807
|
+
this.canSubscribe = true;
|
|
2553
2808
|
this.httpClient = opts.httpClient;
|
|
2554
2809
|
}
|
|
2555
2810
|
async execute(params) {
|
|
@@ -2563,6 +2818,17 @@ var RpcClient = class {
|
|
|
2563
2818
|
}
|
|
2564
2819
|
return result;
|
|
2565
2820
|
}
|
|
2821
|
+
/**
|
|
2822
|
+
* The same call, in the shape the transport abstraction shares with the
|
|
2823
|
+
* relay.
|
|
2824
|
+
*
|
|
2825
|
+
* `rootHash` is left absent rather than synthesized: JSON-RPC `execute` does
|
|
2826
|
+
* not report the context's scope root, and inventing a value — or a `''` —
|
|
2827
|
+
* would make "the node does not tell us" indistinguishable from an answer.
|
|
2828
|
+
*/
|
|
2829
|
+
async executeWithMetadata(params) {
|
|
2830
|
+
return { returns: await this.execute(params), transport: "node" };
|
|
2831
|
+
}
|
|
2566
2832
|
/**
|
|
2567
2833
|
* One-tap owner-driven convert: re-signs the caller's identity-gated entries
|
|
2568
2834
|
* to the current schema. The export converts all of the caller's
|
|
@@ -2608,7 +2874,14 @@ var SseClient = class {
|
|
|
2608
2874
|
this.closed = false;
|
|
2609
2875
|
this.listeners = { connect: [], event: [], error: [] };
|
|
2610
2876
|
this.baseUrl = opts.baseUrl.replace(/\/+$/, "");
|
|
2611
|
-
|
|
2877
|
+
if (opts.authorize) {
|
|
2878
|
+
this.authorize = opts.authorize;
|
|
2879
|
+
} else if (opts.getAuthToken) {
|
|
2880
|
+
const getAuthToken = opts.getAuthToken;
|
|
2881
|
+
this.authorize = async () => ({ Authorization: `Bearer ${await getAuthToken()}` });
|
|
2882
|
+
} else {
|
|
2883
|
+
throw new Error("SseClient needs either `getAuthToken` or `authorize`");
|
|
2884
|
+
}
|
|
2612
2885
|
this.reconnectDelayMs = opts.reconnectDelayMs ?? 3e3;
|
|
2613
2886
|
this.fetchImpl = (url, init) => opts.fetch ? opts.fetch(url, init) : globalThis.fetch(url, init);
|
|
2614
2887
|
}
|
|
@@ -2675,11 +2948,10 @@ var SseClient = class {
|
|
|
2675
2948
|
this.closed = false;
|
|
2676
2949
|
this.abortController = new AbortController();
|
|
2677
2950
|
try {
|
|
2678
|
-
const token = await this.getAuthToken();
|
|
2679
2951
|
const url = `${this.baseUrl}/sse`;
|
|
2680
2952
|
const response = await this.fetchImpl(url, {
|
|
2681
2953
|
headers: {
|
|
2682
|
-
"
|
|
2954
|
+
...await this.authorize({ method: "GET", path: new URL(url).pathname }),
|
|
2683
2955
|
"Accept": "text/event-stream"
|
|
2684
2956
|
},
|
|
2685
2957
|
signal: this.abortController.signal
|
|
@@ -2832,21 +3104,18 @@ var SseClient = class {
|
|
|
2832
3104
|
}
|
|
2833
3105
|
async sendSubscription(method, ids) {
|
|
2834
3106
|
try {
|
|
2835
|
-
const token = await this.getAuthToken();
|
|
2836
3107
|
const params = {};
|
|
2837
3108
|
if (ids.contextIds && ids.contextIds.length > 0) params.contextIds = ids.contextIds;
|
|
2838
3109
|
if (ids.groupIds && ids.groupIds.length > 0) params.groupIds = ids.groupIds;
|
|
2839
|
-
const
|
|
3110
|
+
const url = `${this.baseUrl}/sse/subscription`;
|
|
3111
|
+
const body = JSON.stringify({ id: this.sessionId, method, params });
|
|
3112
|
+
const response = await this.fetchImpl(url, {
|
|
2840
3113
|
method: "POST",
|
|
2841
3114
|
headers: {
|
|
2842
|
-
"
|
|
3115
|
+
...await this.authorize({ method: "POST", path: new URL(url).pathname, body }),
|
|
2843
3116
|
"Content-Type": "application/json"
|
|
2844
3117
|
},
|
|
2845
|
-
body
|
|
2846
|
-
id: this.sessionId,
|
|
2847
|
-
method,
|
|
2848
|
-
params
|
|
2849
|
-
})
|
|
3118
|
+
body
|
|
2850
3119
|
});
|
|
2851
3120
|
if (!response.ok) {
|
|
2852
3121
|
const terminal = isTerminalAuthFailure(response);
|
|
@@ -3165,11 +3434,123 @@ var EphemeralClient = class {
|
|
|
3165
3434
|
}
|
|
3166
3435
|
};
|
|
3167
3436
|
|
|
3437
|
+
// src/signer/signer.ts
|
|
3438
|
+
var PKCS8_ED25519_PREFIX2 = new Uint8Array([
|
|
3439
|
+
48,
|
|
3440
|
+
46,
|
|
3441
|
+
2,
|
|
3442
|
+
1,
|
|
3443
|
+
0,
|
|
3444
|
+
48,
|
|
3445
|
+
5,
|
|
3446
|
+
6,
|
|
3447
|
+
3,
|
|
3448
|
+
43,
|
|
3449
|
+
101,
|
|
3450
|
+
112,
|
|
3451
|
+
4,
|
|
3452
|
+
34,
|
|
3453
|
+
4,
|
|
3454
|
+
32
|
|
3455
|
+
]);
|
|
3456
|
+
function unsupported(cause) {
|
|
3457
|
+
const detail = cause instanceof Error ? `: ${cause.message}` : "";
|
|
3458
|
+
return new Error(
|
|
3459
|
+
"this runtime has no WebCrypto Ed25519, so nothing can be signed here. Node 18.4+, Safari 17+, Firefox 129+ and Chrome 137+ have it" + detail
|
|
3460
|
+
);
|
|
3461
|
+
}
|
|
3462
|
+
async function signerFromSecret(secret, label2 = "secret") {
|
|
3463
|
+
const seed = fromHex(secret, label2, 32);
|
|
3464
|
+
const pkcs8 = concat(PKCS8_ED25519_PREFIX2, seed);
|
|
3465
|
+
let privateKey;
|
|
3466
|
+
let publicKey;
|
|
3467
|
+
try {
|
|
3468
|
+
const exportable = await crypto.subtle.importKey(
|
|
3469
|
+
"pkcs8",
|
|
3470
|
+
pkcs8,
|
|
3471
|
+
{ name: "Ed25519" },
|
|
3472
|
+
true,
|
|
3473
|
+
["sign"]
|
|
3474
|
+
);
|
|
3475
|
+
const jwk = await crypto.subtle.exportKey("jwk", exportable);
|
|
3476
|
+
publicKey = hex(
|
|
3477
|
+
Uint8Array.from(
|
|
3478
|
+
atob(jwk.x.replace(/-/g, "+").replace(/_/g, "/")),
|
|
3479
|
+
(c) => c.charCodeAt(0)
|
|
3480
|
+
)
|
|
3481
|
+
);
|
|
3482
|
+
privateKey = await crypto.subtle.importKey(
|
|
3483
|
+
"pkcs8",
|
|
3484
|
+
pkcs8,
|
|
3485
|
+
{ name: "Ed25519" },
|
|
3486
|
+
false,
|
|
3487
|
+
["sign"]
|
|
3488
|
+
);
|
|
3489
|
+
} catch (cause) {
|
|
3490
|
+
throw unsupported(cause);
|
|
3491
|
+
}
|
|
3492
|
+
return fromKeyAndPublic(privateKey, publicKey);
|
|
3493
|
+
}
|
|
3494
|
+
async function signerFromCryptoKey(privateKey, publicKey) {
|
|
3495
|
+
if (privateKey.type !== "private") {
|
|
3496
|
+
throw new Error(
|
|
3497
|
+
`signerFromCryptoKey needs the private half of the pair, got a ${privateKey.type} key`
|
|
3498
|
+
);
|
|
3499
|
+
}
|
|
3500
|
+
if (!privateKey.usages.includes("sign")) {
|
|
3501
|
+
throw new Error(
|
|
3502
|
+
"this CryptoKey cannot sign: generate it with usages ['sign', 'verify']"
|
|
3503
|
+
);
|
|
3504
|
+
}
|
|
3505
|
+
let publicHex;
|
|
3506
|
+
if (typeof publicKey === "string") {
|
|
3507
|
+
fromHex(publicKey, "publicKey", 32);
|
|
3508
|
+
publicHex = publicKey.trim().toLowerCase();
|
|
3509
|
+
} else {
|
|
3510
|
+
try {
|
|
3511
|
+
publicHex = hex(
|
|
3512
|
+
new Uint8Array(await crypto.subtle.exportKey("raw", publicKey))
|
|
3513
|
+
);
|
|
3514
|
+
} catch (cause) {
|
|
3515
|
+
const detail = cause instanceof Error ? `: ${cause.message}` : "";
|
|
3516
|
+
throw new Error(
|
|
3517
|
+
"could not read the public half of this key pair; pass the public key as 64 hex instead" + detail
|
|
3518
|
+
);
|
|
3519
|
+
}
|
|
3520
|
+
}
|
|
3521
|
+
return fromKeyAndPublic(privateKey, publicHex);
|
|
3522
|
+
}
|
|
3523
|
+
function fromKeyAndPublic(privateKey, publicKey) {
|
|
3524
|
+
return {
|
|
3525
|
+
publicKey,
|
|
3526
|
+
async sign(payload) {
|
|
3527
|
+
try {
|
|
3528
|
+
return new Uint8Array(
|
|
3529
|
+
await crypto.subtle.sign({ name: "Ed25519" }, privateKey, payload)
|
|
3530
|
+
);
|
|
3531
|
+
} catch (cause) {
|
|
3532
|
+
throw unsupported(cause);
|
|
3533
|
+
}
|
|
3534
|
+
}
|
|
3535
|
+
};
|
|
3536
|
+
}
|
|
3537
|
+
async function resolveSigner(secret, signer, label2) {
|
|
3538
|
+
if (signer && secret !== void 0) {
|
|
3539
|
+
throw new Error(
|
|
3540
|
+
`pass either ${label2} or signer, not both \u2014 they may name different keys`
|
|
3541
|
+
);
|
|
3542
|
+
}
|
|
3543
|
+
if (signer) return signer;
|
|
3544
|
+
if (secret !== void 0) return signerFromSecret(secret, label2);
|
|
3545
|
+
throw new Error(`${label2} or signer is required`);
|
|
3546
|
+
}
|
|
3547
|
+
|
|
3168
3548
|
// src/device-cert/device-cert.ts
|
|
3169
3549
|
var CERT_DOMAIN = new TextEncoder().encode("calimero.device.cert.v1");
|
|
3170
3550
|
var ACCOUNT_ID_DOMAIN = new TextEncoder().encode("calimero.account.genesis.v1");
|
|
3171
3551
|
var DEVICE_ID_DOMAIN = new TextEncoder().encode("calimero.device.id.v1");
|
|
3172
3552
|
var ACCOUNT_GENESIS_VERSION = 2;
|
|
3553
|
+
var ACCOUNT_PROOF_BYTES = 237;
|
|
3173
3554
|
async function mintDeviceId(account, nonce2) {
|
|
3174
3555
|
if (nonce2.length !== 16) {
|
|
3175
3556
|
throw new Error(`nonce must be 16 bytes, got ${nonce2.length}`);
|
|
@@ -3202,12 +3583,9 @@ function accountProofBytes(input) {
|
|
|
3202
3583
|
);
|
|
3203
3584
|
}
|
|
3204
3585
|
async function signDeviceCert(input) {
|
|
3205
|
-
const
|
|
3206
|
-
const
|
|
3207
|
-
|
|
3208
|
-
concat(new Uint8Array([ACCOUNT_GENESIS_VERSION]), rootPublicKey)
|
|
3209
|
-
])
|
|
3210
|
-
);
|
|
3586
|
+
const signer = await resolveSigner(input.rootSecret, input.signer, "rootSecret");
|
|
3587
|
+
const rootPublicKey = fromHex(signer.publicKey, "signer.publicKey", 32);
|
|
3588
|
+
const account = await accountForRootPublicKey(signer.publicKey);
|
|
3211
3589
|
const keyEpoch = 0;
|
|
3212
3590
|
const payload = await deviceCertPayload({
|
|
3213
3591
|
account,
|
|
@@ -3217,10 +3595,7 @@ async function signDeviceCert(input) {
|
|
|
3217
3595
|
keyEpoch,
|
|
3218
3596
|
deviceEpoch: input.deviceEpoch
|
|
3219
3597
|
});
|
|
3220
|
-
const
|
|
3221
|
-
const signature = new Uint8Array(
|
|
3222
|
-
await crypto.subtle.sign("Ed25519", key, payload)
|
|
3223
|
-
);
|
|
3598
|
+
const signature = await signer.sign(payload);
|
|
3224
3599
|
const credential = accountProofBytes({
|
|
3225
3600
|
rootPublicKey,
|
|
3226
3601
|
account,
|
|
@@ -3233,11 +3608,67 @@ async function signDeviceCert(input) {
|
|
|
3233
3608
|
});
|
|
3234
3609
|
return hex(credential);
|
|
3235
3610
|
}
|
|
3611
|
+
function parseDeviceCredential(credential) {
|
|
3612
|
+
const bytes = fromHex(
|
|
3613
|
+
credential,
|
|
3614
|
+
"credential",
|
|
3615
|
+
// Fixed-width with an empty chain: 1 + 32 + 4 + 32*4 + 4 + 4 + 64.
|
|
3616
|
+
ACCOUNT_PROOF_BYTES
|
|
3617
|
+
);
|
|
3618
|
+
if (bytes[0] !== ACCOUNT_GENESIS_VERSION) {
|
|
3619
|
+
throw new Error(
|
|
3620
|
+
`this credential has account genesis version ${bytes[0]}, not ${ACCOUNT_GENESIS_VERSION} \u2014 it names a different account than its root key derives here, and core would refuse it`
|
|
3621
|
+
);
|
|
3622
|
+
}
|
|
3623
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
3624
|
+
const chainLength = view.getUint32(33, true);
|
|
3625
|
+
if (chainLength !== 0) {
|
|
3626
|
+
throw new Error(
|
|
3627
|
+
`this credential carries a ${chainLength}-handoff root chain, which this SDK cannot verify yet \u2014 the signing root is not the genesis root, so checking it means walking the chain`
|
|
3628
|
+
);
|
|
3629
|
+
}
|
|
3630
|
+
const at = (offset, length) => hex(bytes.subarray(offset, offset + length));
|
|
3631
|
+
return {
|
|
3632
|
+
rootPublicKey: at(1, 32),
|
|
3633
|
+
account: at(37, 32),
|
|
3634
|
+
device: at(69, 32),
|
|
3635
|
+
signPublicKey: at(101, 32),
|
|
3636
|
+
kemPublicKey: at(133, 32),
|
|
3637
|
+
keyEpoch: view.getUint32(165, true),
|
|
3638
|
+
deviceEpoch: view.getUint32(169, true),
|
|
3639
|
+
signature: at(173, 64)
|
|
3640
|
+
};
|
|
3641
|
+
}
|
|
3642
|
+
async function verifyDeviceCredential(credential) {
|
|
3643
|
+
const parsed = parseDeviceCredential(credential);
|
|
3644
|
+
const derived = await accountForRootPublicKey(parsed.rootPublicKey);
|
|
3645
|
+
if (derived !== parsed.account) {
|
|
3646
|
+
throw new Error(
|
|
3647
|
+
`this credential names account ${parsed.account}, but its root key derives ${derived} \u2014 the certificate was re-pointed at another account`
|
|
3648
|
+
);
|
|
3649
|
+
}
|
|
3650
|
+
const signed = await verifySignature(
|
|
3651
|
+
parsed.rootPublicKey,
|
|
3652
|
+
fromHex(parsed.signature, "signature", 64),
|
|
3653
|
+
await deviceCertPayload(parsed)
|
|
3654
|
+
);
|
|
3655
|
+
if (!signed) {
|
|
3656
|
+
throw new Error(
|
|
3657
|
+
`the root key of account ${parsed.account} did not sign this certificate`
|
|
3658
|
+
);
|
|
3659
|
+
}
|
|
3660
|
+
return parsed;
|
|
3661
|
+
}
|
|
3236
3662
|
async function accountForRoot(rootSecret) {
|
|
3237
|
-
|
|
3663
|
+
return accountForRootPublicKey(hex(await derivePublicKey(rootSecret)));
|
|
3664
|
+
}
|
|
3665
|
+
async function accountForRootPublicKey(rootPublicKey) {
|
|
3238
3666
|
return hex(
|
|
3239
3667
|
await domainHash(ACCOUNT_ID_DOMAIN, [
|
|
3240
|
-
concat(
|
|
3668
|
+
concat(
|
|
3669
|
+
new Uint8Array([ACCOUNT_GENESIS_VERSION]),
|
|
3670
|
+
fromHex(rootPublicKey, "rootPublicKey", 32)
|
|
3671
|
+
)
|
|
3241
3672
|
])
|
|
3242
3673
|
);
|
|
3243
3674
|
}
|
|
@@ -5320,27 +5751,50 @@ async function accountRootFromSecret(secret) {
|
|
|
5320
5751
|
accountId: await accountForRoot(secret)
|
|
5321
5752
|
};
|
|
5322
5753
|
}
|
|
5754
|
+
async function createAccountRootSigner() {
|
|
5755
|
+
const entropy = crypto.getRandomValues(new Uint8Array(ENTROPY_BITS / 8));
|
|
5756
|
+
const phrase = await encodePhrase(entropy);
|
|
5757
|
+
return { ...await rootSignerFromEntropy(entropy), phrase };
|
|
5758
|
+
}
|
|
5759
|
+
async function accountRootSignerFromPhrase(phrase) {
|
|
5760
|
+
return rootSignerFromEntropy(await decodePhrase(normalisePhrase(phrase)));
|
|
5761
|
+
}
|
|
5762
|
+
async function rootSignerFromEntropy(entropy) {
|
|
5763
|
+
const signer = await signerFromSecret(hex(entropy), "rootSecret");
|
|
5764
|
+
entropy.fill(0);
|
|
5765
|
+
return {
|
|
5766
|
+
publicKey: signer.publicKey,
|
|
5767
|
+
accountId: await accountForRootPublicKey(signer.publicKey),
|
|
5768
|
+
signer
|
|
5769
|
+
};
|
|
5770
|
+
}
|
|
5771
|
+
async function resolveRoot(source) {
|
|
5772
|
+
const signer = typeof source === "string" ? await signerFromSecret(source, "rootSecret") : source;
|
|
5773
|
+
return identify(signer);
|
|
5774
|
+
}
|
|
5775
|
+
async function resolveRootPair(rootSecret, signer) {
|
|
5776
|
+
return identify(await resolveSigner(rootSecret, signer, "rootSecret"));
|
|
5777
|
+
}
|
|
5778
|
+
async function identify(signer) {
|
|
5779
|
+
return {
|
|
5780
|
+
signer,
|
|
5781
|
+
publicKey: signer.publicKey,
|
|
5782
|
+
accountId: await accountForRootPublicKey(signer.publicKey)
|
|
5783
|
+
};
|
|
5784
|
+
}
|
|
5323
5785
|
async function signAccountLink(input) {
|
|
5324
5786
|
if (!input.nonce) throw new Error("nonce is required to sign an account link");
|
|
5325
|
-
const
|
|
5326
|
-
const signature =
|
|
5327
|
-
|
|
5328
|
-
"Ed25519",
|
|
5329
|
-
key,
|
|
5330
|
-
concat(ACCOUNT_LINK_DOMAIN, new TextEncoder().encode(input.nonce))
|
|
5331
|
-
)
|
|
5787
|
+
const signer = await resolveSigner(input.rootSecret, input.signer, "rootSecret");
|
|
5788
|
+
const signature = await signer.sign(
|
|
5789
|
+
concat(ACCOUNT_LINK_DOMAIN, new TextEncoder().encode(input.nonce))
|
|
5332
5790
|
);
|
|
5333
5791
|
return base64(signature);
|
|
5334
5792
|
}
|
|
5335
5793
|
async function signAccountLogin(input) {
|
|
5336
5794
|
if (!input.nonce) throw new Error("nonce is required to sign an account login");
|
|
5337
|
-
const
|
|
5338
|
-
const signature =
|
|
5339
|
-
|
|
5340
|
-
"Ed25519",
|
|
5341
|
-
key,
|
|
5342
|
-
concat(ACCOUNT_LOGIN_DOMAIN, new TextEncoder().encode(input.nonce))
|
|
5343
|
-
)
|
|
5795
|
+
const signer = await resolveSigner(input.rootSecret, input.signer, "rootSecret");
|
|
5796
|
+
const signature = await signer.sign(
|
|
5797
|
+
concat(ACCOUNT_LOGIN_DOMAIN, new TextEncoder().encode(input.nonce))
|
|
5344
5798
|
);
|
|
5345
5799
|
return base64(signature);
|
|
5346
5800
|
}
|
|
@@ -5434,7 +5888,7 @@ function base642(bytes) {
|
|
|
5434
5888
|
}
|
|
5435
5889
|
|
|
5436
5890
|
// src/cloud/cloud-client.ts
|
|
5437
|
-
var DEFAULT_CLOUD_BASE_URL = "https://cloud.calimero.network";
|
|
5891
|
+
var DEFAULT_CLOUD_BASE_URL = "https://manager.cloud.calimero.network";
|
|
5438
5892
|
function canExecuteOf(row, legacy) {
|
|
5439
5893
|
return typeof row.can_execute === "boolean" ? row.can_execute : legacy;
|
|
5440
5894
|
}
|
|
@@ -5498,8 +5952,10 @@ var CloudClient = class {
|
|
|
5498
5952
|
* account behind those reads was claimed by its owner. The claim is recorded
|
|
5499
5953
|
* even when the session is refused.
|
|
5500
5954
|
*
|
|
5501
|
-
* @param
|
|
5502
|
-
*
|
|
5955
|
+
* @param root the account root: its 64-hex secret, or a `Signer` over a root
|
|
5956
|
+
* this process may use and cannot read — the form a phrase-held root in a
|
|
5957
|
+
* browser takes. Either way nothing but a signature over the cloud's nonce
|
|
5958
|
+
* travels.
|
|
5503
5959
|
*
|
|
5504
5960
|
* A `403` means either the signature did not verify, or the account is not
|
|
5505
5961
|
* linked to a cloud login: ownership alone says who you are, and the link
|
|
@@ -5507,13 +5963,13 @@ var CloudClient = class {
|
|
|
5507
5963
|
* on the proof alone would authenticate perfectly and authorize nothing. The
|
|
5508
5964
|
* ownership claim is recorded either way, so linking later needs no re-proof.
|
|
5509
5965
|
*/
|
|
5510
|
-
async signInWithAccount(
|
|
5511
|
-
const
|
|
5966
|
+
async signInWithAccount(root) {
|
|
5967
|
+
const { signer, publicKey } = await resolveRoot(root);
|
|
5512
5968
|
const { nonce: nonce2 } = await this.getAccountLoginChallenge();
|
|
5513
5969
|
return this.submitAccountLogin({
|
|
5514
|
-
rootPublicKey:
|
|
5970
|
+
rootPublicKey: publicKey,
|
|
5515
5971
|
nonce: nonce2,
|
|
5516
|
-
signature: await signAccountLogin({
|
|
5972
|
+
signature: await signAccountLogin({ signer, nonce: nonce2 })
|
|
5517
5973
|
});
|
|
5518
5974
|
}
|
|
5519
5975
|
/**
|
|
@@ -5610,6 +6066,7 @@ var CloudClient = class {
|
|
|
5610
6066
|
peerId: String(row.peer_id ?? ""),
|
|
5611
6067
|
relayUrl,
|
|
5612
6068
|
executorAccount,
|
|
6069
|
+
nodeKey: row.node_key ?? row.node_public_key ?? null,
|
|
5613
6070
|
status: String(row.status ?? ""),
|
|
5614
6071
|
authorshipReady,
|
|
5615
6072
|
teeRole: teeRoleOf(row),
|
|
@@ -5952,22 +6409,23 @@ var CloudClient = class {
|
|
|
5952
6409
|
* an intent; the cloud needs it to know whose plan a hosted node is spending,
|
|
5953
6410
|
* and to sign that account back in later.
|
|
5954
6411
|
*
|
|
5955
|
-
* @param
|
|
5956
|
-
* {@link generateAccountRoot},
|
|
5957
|
-
*
|
|
6412
|
+
* @param root the account root: its 64-hex secret — from
|
|
6413
|
+
* {@link generateAccountRoot}, a restored recovery phrase, or a desktop app
|
|
6414
|
+
* or hardware key that already holds one — or a `Signer` over a root this
|
|
6415
|
+
* process cannot read, which is what a phrase-held browser root is.
|
|
5958
6416
|
*
|
|
5959
6417
|
* Idempotent for the same login. An account already linked to a *different*
|
|
5960
6418
|
* cloud login is a `409`; a replayed or expired challenge is a `403`; and
|
|
5961
6419
|
* exceeding the plan's account limit is a `402`.
|
|
5962
6420
|
*/
|
|
5963
|
-
async linkAccount(
|
|
5964
|
-
const
|
|
6421
|
+
async linkAccount(root) {
|
|
6422
|
+
const { signer, publicKey, accountId } = await resolveRoot(root);
|
|
5965
6423
|
const { nonce: nonce2 } = await this.getAccountLinkChallenge();
|
|
5966
6424
|
return this.submitAccountLink({
|
|
5967
|
-
accountId
|
|
5968
|
-
rootPublicKey:
|
|
6425
|
+
accountId,
|
|
6426
|
+
rootPublicKey: publicKey,
|
|
5969
6427
|
nonce: nonce2,
|
|
5970
|
-
signature: await signAccountLink({
|
|
6428
|
+
signature: await signAccountLink({ signer, nonce: nonce2 })
|
|
5971
6429
|
});
|
|
5972
6430
|
}
|
|
5973
6431
|
/**
|
|
@@ -6087,16 +6545,19 @@ var CloudClient = class {
|
|
|
6087
6545
|
* login; `402` that the plan is full.
|
|
6088
6546
|
*/
|
|
6089
6547
|
async linkAccountWithGrant(options) {
|
|
6090
|
-
const
|
|
6548
|
+
const { signer, publicKey, accountId } = await resolveRootPair(
|
|
6549
|
+
options.rootSecret,
|
|
6550
|
+
options.signer
|
|
6551
|
+
);
|
|
6091
6552
|
const body = await this.request(
|
|
6092
6553
|
"POST",
|
|
6093
6554
|
"/api/cloud/accounts/link",
|
|
6094
6555
|
{
|
|
6095
6556
|
body: {
|
|
6096
6557
|
grant: options.grant,
|
|
6097
|
-
root_public_key:
|
|
6558
|
+
root_public_key: publicKey,
|
|
6098
6559
|
signature: await signAccountLink({
|
|
6099
|
-
|
|
6560
|
+
signer,
|
|
6100
6561
|
nonce: options.grant
|
|
6101
6562
|
})
|
|
6102
6563
|
},
|
|
@@ -6104,7 +6565,7 @@ var CloudClient = class {
|
|
|
6104
6565
|
}
|
|
6105
6566
|
);
|
|
6106
6567
|
return {
|
|
6107
|
-
accountId: String(body.account_id ??
|
|
6568
|
+
accountId: String(body.account_id ?? accountId),
|
|
6108
6569
|
linkedAt: body.linked_at ?? null,
|
|
6109
6570
|
alreadyLinked: body.already_linked === true
|
|
6110
6571
|
};
|
|
@@ -6628,117 +7089,6 @@ var LocalStorageTokenStore = class {
|
|
|
6628
7089
|
}
|
|
6629
7090
|
};
|
|
6630
7091
|
|
|
6631
|
-
// src/signer/signer.ts
|
|
6632
|
-
var PKCS8_ED25519_PREFIX2 = new Uint8Array([
|
|
6633
|
-
48,
|
|
6634
|
-
46,
|
|
6635
|
-
2,
|
|
6636
|
-
1,
|
|
6637
|
-
0,
|
|
6638
|
-
48,
|
|
6639
|
-
5,
|
|
6640
|
-
6,
|
|
6641
|
-
3,
|
|
6642
|
-
43,
|
|
6643
|
-
101,
|
|
6644
|
-
112,
|
|
6645
|
-
4,
|
|
6646
|
-
34,
|
|
6647
|
-
4,
|
|
6648
|
-
32
|
|
6649
|
-
]);
|
|
6650
|
-
function unsupported(cause) {
|
|
6651
|
-
const detail = cause instanceof Error ? `: ${cause.message}` : "";
|
|
6652
|
-
return new Error(
|
|
6653
|
-
"this runtime has no WebCrypto Ed25519, so nothing can be signed here. Node 18.4+, Safari 17+, Firefox 129+ and Chrome 137+ have it" + detail
|
|
6654
|
-
);
|
|
6655
|
-
}
|
|
6656
|
-
async function signerFromSecret(secret, label2 = "secret") {
|
|
6657
|
-
const seed = fromHex(secret, label2, 32);
|
|
6658
|
-
const pkcs8 = concat(PKCS8_ED25519_PREFIX2, seed);
|
|
6659
|
-
let privateKey;
|
|
6660
|
-
let publicKey;
|
|
6661
|
-
try {
|
|
6662
|
-
const exportable = await crypto.subtle.importKey(
|
|
6663
|
-
"pkcs8",
|
|
6664
|
-
pkcs8,
|
|
6665
|
-
{ name: "Ed25519" },
|
|
6666
|
-
true,
|
|
6667
|
-
["sign"]
|
|
6668
|
-
);
|
|
6669
|
-
const jwk = await crypto.subtle.exportKey("jwk", exportable);
|
|
6670
|
-
publicKey = hex(
|
|
6671
|
-
Uint8Array.from(
|
|
6672
|
-
atob(jwk.x.replace(/-/g, "+").replace(/_/g, "/")),
|
|
6673
|
-
(c) => c.charCodeAt(0)
|
|
6674
|
-
)
|
|
6675
|
-
);
|
|
6676
|
-
privateKey = await crypto.subtle.importKey(
|
|
6677
|
-
"pkcs8",
|
|
6678
|
-
pkcs8,
|
|
6679
|
-
{ name: "Ed25519" },
|
|
6680
|
-
false,
|
|
6681
|
-
["sign"]
|
|
6682
|
-
);
|
|
6683
|
-
} catch (cause) {
|
|
6684
|
-
throw unsupported(cause);
|
|
6685
|
-
}
|
|
6686
|
-
return fromKeyAndPublic(privateKey, publicKey);
|
|
6687
|
-
}
|
|
6688
|
-
async function signerFromCryptoKey(privateKey, publicKey) {
|
|
6689
|
-
if (privateKey.type !== "private") {
|
|
6690
|
-
throw new Error(
|
|
6691
|
-
`signerFromCryptoKey needs the private half of the pair, got a ${privateKey.type} key`
|
|
6692
|
-
);
|
|
6693
|
-
}
|
|
6694
|
-
if (!privateKey.usages.includes("sign")) {
|
|
6695
|
-
throw new Error(
|
|
6696
|
-
"this CryptoKey cannot sign: generate it with usages ['sign', 'verify']"
|
|
6697
|
-
);
|
|
6698
|
-
}
|
|
6699
|
-
let publicHex;
|
|
6700
|
-
if (typeof publicKey === "string") {
|
|
6701
|
-
fromHex(publicKey, "publicKey", 32);
|
|
6702
|
-
publicHex = publicKey.trim().toLowerCase();
|
|
6703
|
-
} else {
|
|
6704
|
-
try {
|
|
6705
|
-
publicHex = hex(
|
|
6706
|
-
new Uint8Array(await crypto.subtle.exportKey("raw", publicKey))
|
|
6707
|
-
);
|
|
6708
|
-
} catch (cause) {
|
|
6709
|
-
const detail = cause instanceof Error ? `: ${cause.message}` : "";
|
|
6710
|
-
throw new Error(
|
|
6711
|
-
"could not read the public half of this key pair; pass the public key as 64 hex instead" + detail
|
|
6712
|
-
);
|
|
6713
|
-
}
|
|
6714
|
-
}
|
|
6715
|
-
return fromKeyAndPublic(privateKey, publicHex);
|
|
6716
|
-
}
|
|
6717
|
-
function fromKeyAndPublic(privateKey, publicKey) {
|
|
6718
|
-
return {
|
|
6719
|
-
publicKey,
|
|
6720
|
-
async sign(payload) {
|
|
6721
|
-
try {
|
|
6722
|
-
return new Uint8Array(
|
|
6723
|
-
await crypto.subtle.sign({ name: "Ed25519" }, privateKey, payload)
|
|
6724
|
-
);
|
|
6725
|
-
} catch (cause) {
|
|
6726
|
-
throw unsupported(cause);
|
|
6727
|
-
}
|
|
6728
|
-
}
|
|
6729
|
-
};
|
|
6730
|
-
}
|
|
6731
|
-
async function resolveSigner(secret, signer, label2) {
|
|
6732
|
-
if (signer && secret !== void 0) {
|
|
6733
|
-
throw new Error(
|
|
6734
|
-
`pass either ${label2} or signer, not both \u2014 they may name different keys`
|
|
6735
|
-
);
|
|
6736
|
-
}
|
|
6737
|
-
if (signer) return signer;
|
|
6738
|
-
if (secret !== void 0) return signerFromSecret(secret, label2);
|
|
6739
|
-
throw new Error(`${label2} or signer is required`);
|
|
6740
|
-
}
|
|
6741
|
-
|
|
6742
7092
|
// src/warrant/warrant.ts
|
|
6743
7093
|
var SIGN_DOMAIN2 = new TextEncoder().encode("calimero.warrant.v2");
|
|
6744
7094
|
var INTENT_DOMAIN = new TextEncoder().encode("calimero.warrant.intent.v1");
|
|
@@ -7243,6 +7593,16 @@ var IntentRefusedError = class extends Error {
|
|
|
7243
7593
|
this.name = "IntentRefusedError";
|
|
7244
7594
|
}
|
|
7245
7595
|
};
|
|
7596
|
+
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
7597
|
+
function retryAfterMs(headers) {
|
|
7598
|
+
const raw = headers?.get?.("Retry-After");
|
|
7599
|
+
if (!raw) return null;
|
|
7600
|
+
const seconds = Number.parseInt(raw, 10);
|
|
7601
|
+
if (Number.isFinite(seconds) && seconds >= 0) return seconds * 1e3;
|
|
7602
|
+
const at = Date.parse(raw);
|
|
7603
|
+
if (Number.isFinite(at)) return Math.max(0, at - Date.now());
|
|
7604
|
+
return null;
|
|
7605
|
+
}
|
|
7246
7606
|
var RelayClient = class {
|
|
7247
7607
|
constructor(config) {
|
|
7248
7608
|
this.config = config;
|
|
@@ -7251,11 +7611,40 @@ var RelayClient = class {
|
|
|
7251
7611
|
const injected = config.fetch;
|
|
7252
7612
|
this.fetchImpl = injected ? (input, init) => injected(input, init) : (input, init) => globalThis.fetch(input, init);
|
|
7253
7613
|
this.timeoutMs = config.timeoutMs ?? 1e4;
|
|
7614
|
+
this.rateLimitRetries = config.rateLimitRetries ?? 3;
|
|
7254
7615
|
}
|
|
7255
7616
|
/**
|
|
7256
|
-
* The
|
|
7257
|
-
*
|
|
7258
|
-
*
|
|
7617
|
+
* The relay's origin, trailing slash stripped.
|
|
7618
|
+
*
|
|
7619
|
+
* Exposed because a relay **is a node**: the same origin that answers the
|
|
7620
|
+
* intents routes also answers `/auth/challenge`, `/auth/token`, `/sse` and
|
|
7621
|
+
* `/ws`. Anything building an observing session over this relay needs the
|
|
7622
|
+
* URL, and asking the caller to pass it a second time is how the two drift.
|
|
7623
|
+
*/
|
|
7624
|
+
get relayUrl() {
|
|
7625
|
+
return this.baseUrl;
|
|
7626
|
+
}
|
|
7627
|
+
/** The author's account, hex — whose consent every warrant here carries. */
|
|
7628
|
+
get authorAccount() {
|
|
7629
|
+
return this.config.authorAccount;
|
|
7630
|
+
}
|
|
7631
|
+
/**
|
|
7632
|
+
* The author's `AccountProof<DeviceCert>`, hex.
|
|
7633
|
+
*
|
|
7634
|
+
* The same credential `login()` wants, which is why it is readable: a caller
|
|
7635
|
+
* that already handed it to this client should not have to hand an identical
|
|
7636
|
+
* copy to the login as well, where the two could disagree.
|
|
7637
|
+
*/
|
|
7638
|
+
get authorProof() {
|
|
7639
|
+
return this.config.authorProof;
|
|
7640
|
+
}
|
|
7641
|
+
/**
|
|
7642
|
+
* The author device's signer.
|
|
7643
|
+
*
|
|
7644
|
+
* A {@link Signer} and never the secret: this returns something that can
|
|
7645
|
+
* *sign*, so no caller of a `RelayClient` gains the ability to read a key
|
|
7646
|
+
* that was passed in as `deviceSecret`. That asymmetry is the point — the
|
|
7647
|
+
* login needs a signature, not key material.
|
|
7259
7648
|
*/
|
|
7260
7649
|
async authorSigner() {
|
|
7261
7650
|
return resolveSigner(this.config.deviceSecret, this.config.signer, "deviceSecret");
|
|
@@ -7534,7 +7923,37 @@ var RelayClient = class {
|
|
|
7534
7923
|
this.executorAccount = answered;
|
|
7535
7924
|
return answered;
|
|
7536
7925
|
}
|
|
7926
|
+
/**
|
|
7927
|
+
* One request, waiting out a rate limit rather than failing the caller.
|
|
7928
|
+
*
|
|
7929
|
+
* The relay's ingress limits the delegated-execution routes to 20 requests a
|
|
7930
|
+
* second per source IP, burst 40 — tighter than everything else it serves,
|
|
7931
|
+
* because those two routes are the ones it answers to anonymous callers.
|
|
7932
|
+
*
|
|
7933
|
+
* Retrying a 429 is safe here in a way retrying most refusals is not: the
|
|
7934
|
+
* limiter sits *first* in the middleware chain, so a throttled request never
|
|
7935
|
+
* reached the node and its warrant nonce was never spent. The same bytes can
|
|
7936
|
+
* go again. Contrast the spent-nonce 403 below, which needs a fresh warrant.
|
|
7937
|
+
*
|
|
7938
|
+
* `Retry-After` is honoured when the ingress sends one; otherwise the wait
|
|
7939
|
+
* doubles from a little over the limiter's own window, which is the shortest
|
|
7940
|
+
* delay that can actually clear.
|
|
7941
|
+
*/
|
|
7537
7942
|
async json(method, path, body) {
|
|
7943
|
+
let wait = 250;
|
|
7944
|
+
for (let attempt = 0; ; attempt += 1) {
|
|
7945
|
+
try {
|
|
7946
|
+
return await this.jsonOnce(method, path, body);
|
|
7947
|
+
} catch (err) {
|
|
7948
|
+
const limited = err instanceof HTTPError && err.status === 429;
|
|
7949
|
+
if (!limited || attempt >= this.rateLimitRetries) throw err;
|
|
7950
|
+
const after = retryAfterMs(err.headers);
|
|
7951
|
+
await sleep(after ?? wait);
|
|
7952
|
+
wait *= 2;
|
|
7953
|
+
}
|
|
7954
|
+
}
|
|
7955
|
+
}
|
|
7956
|
+
async jsonOnce(method, path, body) {
|
|
7538
7957
|
const headers = { Accept: "application/json" };
|
|
7539
7958
|
if (body !== void 0) headers["Content-Type"] = "application/json";
|
|
7540
7959
|
const controller = new AbortController();
|
|
@@ -7587,6 +8006,668 @@ function extractReason(bodyText) {
|
|
|
7587
8006
|
return bodyText;
|
|
7588
8007
|
}
|
|
7589
8008
|
|
|
8009
|
+
// src/transport/relay-transport.ts
|
|
8010
|
+
var RelayTransport = class {
|
|
8011
|
+
constructor(relay, observer = null) {
|
|
8012
|
+
this.relay = relay;
|
|
8013
|
+
this.observer = observer;
|
|
8014
|
+
this.kind = "relay";
|
|
8015
|
+
}
|
|
8016
|
+
/**
|
|
8017
|
+
* Whether this relay can be observed — a fact, not a constant.
|
|
8018
|
+
*
|
|
8019
|
+
* A relay IS a node: it serves `/auth/challenge`, `/auth/token`, `/sse` and
|
|
8020
|
+
* `/ws`, and the `account_proof` provider grants `context:subscribe` by
|
|
8021
|
+
* default. What decides the answer is therefore not the transport but whether
|
|
8022
|
+
* a caller supplied the node's device signing key, which cannot be inferred
|
|
8023
|
+
* from the node or from its `peerId`. See `./relay-observer.ts`.
|
|
8024
|
+
*/
|
|
8025
|
+
get canSubscribe() {
|
|
8026
|
+
return this.observer !== null;
|
|
8027
|
+
}
|
|
8028
|
+
/** The event session, or `null` when no node key was supplied. */
|
|
8029
|
+
get events() {
|
|
8030
|
+
return this.observer;
|
|
8031
|
+
}
|
|
8032
|
+
/** The underlying client, for `describe()` and anything else relay-specific. */
|
|
8033
|
+
get client() {
|
|
8034
|
+
return this.relay;
|
|
8035
|
+
}
|
|
8036
|
+
async execute(params) {
|
|
8037
|
+
const { returns } = await this.executeWithMetadata(params);
|
|
8038
|
+
return returns;
|
|
8039
|
+
}
|
|
8040
|
+
async executeWithMetadata(params) {
|
|
8041
|
+
const result = await this.relay.execute(
|
|
8042
|
+
params.contextId,
|
|
8043
|
+
params.method,
|
|
8044
|
+
// The node defaults a missing `argsJson` to `{}`; matching that here keeps
|
|
8045
|
+
// a zero-argument call identical on both transports rather than sending
|
|
8046
|
+
// `undefined` and letting the relay's JSON body decide.
|
|
8047
|
+
params.argsJson ?? {}
|
|
8048
|
+
);
|
|
8049
|
+
return {
|
|
8050
|
+
// Verbatim. `RelayClient` has already normalized a missing `returns` to
|
|
8051
|
+
// `null`, which is what a node reports for a null output — see the
|
|
8052
|
+
// module note on why this is not rewritten to `undefined`.
|
|
8053
|
+
returns: result.returns,
|
|
8054
|
+
transport: "relay",
|
|
8055
|
+
rootHash: result.rootHash
|
|
8056
|
+
};
|
|
8057
|
+
}
|
|
8058
|
+
/**
|
|
8059
|
+
* Both of these are plain context methods, so they work through a relay
|
|
8060
|
+
* exactly as they do on a node — the warrant names `migrate_my_entries` and
|
|
8061
|
+
* the delta is attributed to the author, which is precisely the identity the
|
|
8062
|
+
* convert is scoped to. They are re-declared rather than inherited because
|
|
8063
|
+
* `ExecuteTransport` is an interface, not a base class.
|
|
8064
|
+
*/
|
|
8065
|
+
async migrateMyEntries(contextId) {
|
|
8066
|
+
return this.execute({
|
|
8067
|
+
contextId,
|
|
8068
|
+
method: "migrate_my_entries"
|
|
8069
|
+
});
|
|
8070
|
+
}
|
|
8071
|
+
/** Read-only count of the caller's entries still below the target schema. */
|
|
8072
|
+
async countMyPending(contextId) {
|
|
8073
|
+
return this.execute({ contextId, method: "count_my_pending" });
|
|
8074
|
+
}
|
|
8075
|
+
};
|
|
8076
|
+
|
|
8077
|
+
// src/login/login.ts
|
|
8078
|
+
var SIGN_DOMAIN5 = new TextEncoder().encode("calimero.auth.login.v1");
|
|
8079
|
+
var AUDIENCE_TAG = {
|
|
8080
|
+
webOrigin: 0,
|
|
8081
|
+
codeSigningId: 1,
|
|
8082
|
+
cli: 2
|
|
8083
|
+
};
|
|
8084
|
+
function audienceBody(audience) {
|
|
8085
|
+
const encoder = new TextEncoder();
|
|
8086
|
+
switch (audience.kind) {
|
|
8087
|
+
case "webOrigin":
|
|
8088
|
+
return encoder.encode(audience.origin);
|
|
8089
|
+
case "codeSigningId":
|
|
8090
|
+
return encoder.encode(audience.id);
|
|
8091
|
+
case "cli":
|
|
8092
|
+
return new Uint8Array(0);
|
|
8093
|
+
}
|
|
8094
|
+
}
|
|
8095
|
+
function audienceSigningBytes(audience) {
|
|
8096
|
+
return concat(
|
|
8097
|
+
Uint8Array.from([AUDIENCE_TAG[audience.kind]]),
|
|
8098
|
+
audienceBody(audience)
|
|
8099
|
+
);
|
|
8100
|
+
}
|
|
8101
|
+
function audienceWire(audience) {
|
|
8102
|
+
const tag2 = Uint8Array.from([AUDIENCE_TAG[audience.kind]]);
|
|
8103
|
+
if (audience.kind === "cli") {
|
|
8104
|
+
return tag2;
|
|
8105
|
+
}
|
|
8106
|
+
const body = audienceBody(audience);
|
|
8107
|
+
return concat(tag2, u32le(body.length), body);
|
|
8108
|
+
}
|
|
8109
|
+
async function signLoginStatement(input) {
|
|
8110
|
+
const node = fromHex(input.node, "node", 32);
|
|
8111
|
+
const challenge = fromHex(input.challenge, "challenge", 32);
|
|
8112
|
+
const sessionKey = fromHex(input.sessionKey, "sessionKey", 32);
|
|
8113
|
+
const signer = await resolveSigner(
|
|
8114
|
+
input.deviceSecret,
|
|
8115
|
+
input.signer,
|
|
8116
|
+
"deviceSecret"
|
|
8117
|
+
);
|
|
8118
|
+
const deviceKey = fromHex(signer.publicKey, "signer.publicKey", 32);
|
|
8119
|
+
const issuedAt = u64le(input.issuedAt);
|
|
8120
|
+
const expiresAt = u64le(input.expiresAt);
|
|
8121
|
+
const preimage = await domainHash(SIGN_DOMAIN5, [
|
|
8122
|
+
node,
|
|
8123
|
+
audienceSigningBytes(input.audience),
|
|
8124
|
+
challenge,
|
|
8125
|
+
sessionKey,
|
|
8126
|
+
deviceKey,
|
|
8127
|
+
issuedAt,
|
|
8128
|
+
expiresAt
|
|
8129
|
+
]);
|
|
8130
|
+
const signature = await signer.sign(preimage);
|
|
8131
|
+
return hex(
|
|
8132
|
+
concat(
|
|
8133
|
+
node,
|
|
8134
|
+
audienceWire(input.audience),
|
|
8135
|
+
challenge,
|
|
8136
|
+
sessionKey,
|
|
8137
|
+
deviceKey,
|
|
8138
|
+
issuedAt,
|
|
8139
|
+
expiresAt,
|
|
8140
|
+
signature
|
|
8141
|
+
)
|
|
8142
|
+
);
|
|
8143
|
+
}
|
|
8144
|
+
|
|
8145
|
+
// src/login/session.ts
|
|
8146
|
+
var DEFAULT_TTL_SECONDS2 = 300;
|
|
8147
|
+
async function generateSessionKey() {
|
|
8148
|
+
const secret = hex(crypto.getRandomValues(new Uint8Array(32)));
|
|
8149
|
+
return { publicKey: hex(await derivePublicKey(secret)), secret };
|
|
8150
|
+
}
|
|
8151
|
+
async function login(config) {
|
|
8152
|
+
const baseUrl = config.nodeUrl.replace(/\/+$/, "");
|
|
8153
|
+
const timeoutMs = config.timeoutMs ?? 1e4;
|
|
8154
|
+
const request = async (path, init) => {
|
|
8155
|
+
const controller = new AbortController();
|
|
8156
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
8157
|
+
let response;
|
|
8158
|
+
try {
|
|
8159
|
+
const requestInit = { ...init, signal: controller.signal };
|
|
8160
|
+
response = config.fetch ? await config.fetch(`${baseUrl}${path}`, requestInit) : await globalThis.fetch(`${baseUrl}${path}`, requestInit);
|
|
8161
|
+
} catch (err) {
|
|
8162
|
+
throw new HTTPError(
|
|
8163
|
+
0,
|
|
8164
|
+
err instanceof Error ? err.name : "NetworkError",
|
|
8165
|
+
`${baseUrl}${path}`,
|
|
8166
|
+
new Headers(),
|
|
8167
|
+
err instanceof Error ? err.message : String(err)
|
|
8168
|
+
);
|
|
8169
|
+
} finally {
|
|
8170
|
+
clearTimeout(timer);
|
|
8171
|
+
}
|
|
8172
|
+
const text = await response.text().catch(() => "");
|
|
8173
|
+
if (!response.ok) {
|
|
8174
|
+
throw new HTTPError(
|
|
8175
|
+
response.status,
|
|
8176
|
+
response.statusText,
|
|
8177
|
+
`${baseUrl}${path}`,
|
|
8178
|
+
response.headers,
|
|
8179
|
+
text
|
|
8180
|
+
);
|
|
8181
|
+
}
|
|
8182
|
+
return text ? JSON.parse(text) : {};
|
|
8183
|
+
};
|
|
8184
|
+
const challengeBody = await request("/auth/challenge", {
|
|
8185
|
+
method: "GET",
|
|
8186
|
+
headers: { Accept: "application/json" }
|
|
8187
|
+
});
|
|
8188
|
+
const challenge = challengeBody.challenge ?? challengeBody.data?.challenge;
|
|
8189
|
+
if (!challenge) {
|
|
8190
|
+
throw new Error("the node issued no challenge");
|
|
8191
|
+
}
|
|
8192
|
+
const session = await generateSessionKey();
|
|
8193
|
+
const issuedAt = Math.floor(Date.now() / 1e3);
|
|
8194
|
+
const statement = await signLoginStatement({
|
|
8195
|
+
node: config.node,
|
|
8196
|
+
audience: config.audience,
|
|
8197
|
+
challenge,
|
|
8198
|
+
sessionKey: session.publicKey,
|
|
8199
|
+
issuedAt,
|
|
8200
|
+
expiresAt: issuedAt + (config.ttlSeconds ?? DEFAULT_TTL_SECONDS2),
|
|
8201
|
+
// Resolved once and handed on, so a config carrying a secret and a config
|
|
8202
|
+
// carrying a `CryptoKey` take exactly the same path from here down.
|
|
8203
|
+
signer: await resolveSigner(config.deviceSecret, config.signer, "deviceSecret")
|
|
8204
|
+
});
|
|
8205
|
+
const tokenBody = await request("/auth/token", {
|
|
8206
|
+
method: "POST",
|
|
8207
|
+
headers: { "Content-Type": "application/json", Accept: "application/json" },
|
|
8208
|
+
body: JSON.stringify({
|
|
8209
|
+
auth_method: "account_proof",
|
|
8210
|
+
public_key: session.publicKey,
|
|
8211
|
+
client_name: config.clientName ?? baseUrl,
|
|
8212
|
+
timestamp: issuedAt,
|
|
8213
|
+
provider_data: {
|
|
8214
|
+
challenge,
|
|
8215
|
+
login_statement: statement,
|
|
8216
|
+
account_proof: config.accountProof
|
|
8217
|
+
}
|
|
8218
|
+
})
|
|
8219
|
+
});
|
|
8220
|
+
const accessToken = tokenBody.access_token ?? tokenBody.data?.access_token;
|
|
8221
|
+
const refreshToken = tokenBody.refresh_token ?? tokenBody.data?.refresh_token;
|
|
8222
|
+
if (!accessToken) {
|
|
8223
|
+
throw new Error("the node minted no session");
|
|
8224
|
+
}
|
|
8225
|
+
return {
|
|
8226
|
+
accessToken,
|
|
8227
|
+
refreshToken: refreshToken ?? "",
|
|
8228
|
+
sessionKey: session.publicKey,
|
|
8229
|
+
sessionSecret: session.secret
|
|
8230
|
+
};
|
|
8231
|
+
}
|
|
8232
|
+
|
|
8233
|
+
// src/transport/relay-observer.ts
|
|
8234
|
+
var EXPIRY_SKEW_MS = 3e4;
|
|
8235
|
+
function defaultAudience() {
|
|
8236
|
+
const origin = globalThis.location?.origin;
|
|
8237
|
+
return origin ? { kind: "webOrigin", origin } : { kind: "cli" };
|
|
8238
|
+
}
|
|
8239
|
+
function jwtExpiryMs(token) {
|
|
8240
|
+
try {
|
|
8241
|
+
const parts = token.split(".");
|
|
8242
|
+
if (parts.length !== 3) return null;
|
|
8243
|
+
let b64 = parts[1].replace(/-/g, "+").replace(/_/g, "/");
|
|
8244
|
+
while (b64.length % 4) b64 += "=";
|
|
8245
|
+
const payload = JSON.parse(atob(b64));
|
|
8246
|
+
return typeof payload.exp === "number" ? payload.exp * 1e3 : null;
|
|
8247
|
+
} catch {
|
|
8248
|
+
return null;
|
|
8249
|
+
}
|
|
8250
|
+
}
|
|
8251
|
+
var RelayObserver = class {
|
|
8252
|
+
constructor(config) {
|
|
8253
|
+
this.session = null;
|
|
8254
|
+
this.sessionExpiresAt = null;
|
|
8255
|
+
/** In-flight login, so two surfaces asking at once mint one session, not two. */
|
|
8256
|
+
this.pending = null;
|
|
8257
|
+
this.sseClient = null;
|
|
8258
|
+
this.wsClient = null;
|
|
8259
|
+
this.config = config;
|
|
8260
|
+
}
|
|
8261
|
+
/** The node key this observer logs in with. Read for diagnostics. */
|
|
8262
|
+
get nodeKey() {
|
|
8263
|
+
return this.config.nodeKey;
|
|
8264
|
+
}
|
|
8265
|
+
/**
|
|
8266
|
+
* A usable access token, logging in if there is not one.
|
|
8267
|
+
*
|
|
8268
|
+
* Re-mints once the current token is within {@link EXPIRY_SKEW_MS} of
|
|
8269
|
+
* expiring, and on every call when the token's lifetime cannot be read —
|
|
8270
|
+
* reconnects are backed off, so an extra handshake costs far less than a
|
|
8271
|
+
* stream that silently stops. The refresh token is *held* but not spent:
|
|
8272
|
+
* rotating it is tracked in #87/#88 and a second login is correct in the
|
|
8273
|
+
* meantime, where re-presenting an unrotated refresh token would not be.
|
|
8274
|
+
*/
|
|
8275
|
+
async accessToken() {
|
|
8276
|
+
const now = Date.now();
|
|
8277
|
+
if (this.session && this.sessionExpiresAt !== null && this.sessionExpiresAt - EXPIRY_SKEW_MS > now) {
|
|
8278
|
+
return this.session.accessToken;
|
|
8279
|
+
}
|
|
8280
|
+
if (!this.pending) {
|
|
8281
|
+
this.pending = this.loginNow().finally(() => {
|
|
8282
|
+
this.pending = null;
|
|
8283
|
+
});
|
|
8284
|
+
}
|
|
8285
|
+
const session = await this.pending;
|
|
8286
|
+
return session.accessToken;
|
|
8287
|
+
}
|
|
8288
|
+
async loginNow() {
|
|
8289
|
+
const session = await login({
|
|
8290
|
+
nodeUrl: this.config.nodeUrl,
|
|
8291
|
+
node: this.config.nodeKey,
|
|
8292
|
+
signer: await this.config.signer(),
|
|
8293
|
+
accountProof: this.config.accountProof,
|
|
8294
|
+
audience: this.config.audience,
|
|
8295
|
+
clientName: this.config.clientName,
|
|
8296
|
+
ttlSeconds: this.config.ttlSeconds,
|
|
8297
|
+
fetch: this.config.fetch,
|
|
8298
|
+
timeoutMs: this.config.timeoutMs
|
|
8299
|
+
});
|
|
8300
|
+
this.session = session;
|
|
8301
|
+
this.sessionExpiresAt = jwtExpiryMs(session.accessToken);
|
|
8302
|
+
return session;
|
|
8303
|
+
}
|
|
8304
|
+
/**
|
|
8305
|
+
* SSE events over the relay node — the same `SseClient` a node client hands
|
|
8306
|
+
* out, constructed the same way, so nothing downstream can tell which
|
|
8307
|
+
* transport produced it.
|
|
8308
|
+
*/
|
|
8309
|
+
get events() {
|
|
8310
|
+
if (!this.sseClient) {
|
|
8311
|
+
this.sseClient = new SseClient({
|
|
8312
|
+
baseUrl: this.config.nodeUrl,
|
|
8313
|
+
getAuthToken: () => this.accessToken()
|
|
8314
|
+
});
|
|
8315
|
+
}
|
|
8316
|
+
return this.sseClient;
|
|
8317
|
+
}
|
|
8318
|
+
/** WebSocket events over the relay node. @experimental — prefer `events`. */
|
|
8319
|
+
get ws() {
|
|
8320
|
+
if (!this.wsClient) {
|
|
8321
|
+
this.wsClient = new WsClient({
|
|
8322
|
+
baseUrl: this.config.nodeUrl,
|
|
8323
|
+
getAuthToken: () => this.accessToken()
|
|
8324
|
+
});
|
|
8325
|
+
}
|
|
8326
|
+
return this.wsClient;
|
|
8327
|
+
}
|
|
8328
|
+
/** Close whatever was opened. Never touches the relay's write path. */
|
|
8329
|
+
close() {
|
|
8330
|
+
this.sseClient?.close();
|
|
8331
|
+
this.wsClient?.close();
|
|
8332
|
+
}
|
|
8333
|
+
};
|
|
8334
|
+
|
|
8335
|
+
// src/request-proof/request-proof.ts
|
|
8336
|
+
var utf8 = (text) => new TextEncoder().encode(text);
|
|
8337
|
+
var REQUEST_SIGN_DOMAIN = utf8("calimero.auth.request.v1");
|
|
8338
|
+
var REQUEST_BODY_DOMAIN = utf8("calimero.auth.request.body.v1");
|
|
8339
|
+
var REQUEST_PROOF_HEADER = "X-Calimero-Proof";
|
|
8340
|
+
var DEFAULT_VALID_FOR = 300;
|
|
8341
|
+
async function signRequestProof(input) {
|
|
8342
|
+
const { method, path, credential } = input;
|
|
8343
|
+
if (!method) throw new Error("signRequestProof needs the method the request will use");
|
|
8344
|
+
if (!path.startsWith("/")) {
|
|
8345
|
+
throw new Error(`signRequestProof needs a path, got ${JSON.stringify(path)}`);
|
|
8346
|
+
}
|
|
8347
|
+
if (path.includes("?")) {
|
|
8348
|
+
throw new Error(
|
|
8349
|
+
`the path must carry no query string, got ${JSON.stringify(path)} \u2014 a query is excluded from the signature on purpose, because a proxy may rewrite one. Anything that must be bound belongs in the body.`
|
|
8350
|
+
);
|
|
8351
|
+
}
|
|
8352
|
+
const bodyBytes2 = input.body === void 0 ? new Uint8Array() : typeof input.body === "string" ? utf8(input.body) : input.body;
|
|
8353
|
+
const bodyHash = await domainHash(REQUEST_BODY_DOMAIN, [bodyBytes2]);
|
|
8354
|
+
const issuedAt = Math.floor(input.now ?? Date.now() / 1e3);
|
|
8355
|
+
const expiresAt = issuedAt + (input.validForSeconds ?? DEFAULT_VALID_FOR);
|
|
8356
|
+
const payload = await domainHash(REQUEST_SIGN_DOMAIN, [
|
|
8357
|
+
utf8(method),
|
|
8358
|
+
utf8(path),
|
|
8359
|
+
bodyHash,
|
|
8360
|
+
u64le(issuedAt),
|
|
8361
|
+
u64le(expiresAt)
|
|
8362
|
+
]);
|
|
8363
|
+
const signature = await signPayload(payload, input);
|
|
8364
|
+
if (signature.length !== 64) {
|
|
8365
|
+
throw new Error(`the signer returned ${signature.length} bytes, not a 64-byte signature`);
|
|
8366
|
+
}
|
|
8367
|
+
const requestSig = concat(
|
|
8368
|
+
u32le(utf8(method).length),
|
|
8369
|
+
utf8(method),
|
|
8370
|
+
u32le(utf8(path).length),
|
|
8371
|
+
utf8(path),
|
|
8372
|
+
bodyHash,
|
|
8373
|
+
u64le(issuedAt),
|
|
8374
|
+
u64le(expiresAt),
|
|
8375
|
+
signature
|
|
8376
|
+
);
|
|
8377
|
+
const accountProof = hexToBytes(credential, "credential");
|
|
8378
|
+
return hex(concat(accountProof, new Uint8Array([0]), requestSig));
|
|
8379
|
+
}
|
|
8380
|
+
async function signPayload(payload, input) {
|
|
8381
|
+
if (input.signer) return input.signer.sign(payload);
|
|
8382
|
+
if (!input.deviceSecret) {
|
|
8383
|
+
throw new Error("signRequestProof needs either `deviceSecret` or `signer`");
|
|
8384
|
+
}
|
|
8385
|
+
const key = await importSigningKey(input.deviceSecret);
|
|
8386
|
+
return new Uint8Array(await crypto.subtle.sign({ name: "Ed25519" }, key, payload));
|
|
8387
|
+
}
|
|
8388
|
+
function hexToBytes(value, label2) {
|
|
8389
|
+
const clean = value.trim();
|
|
8390
|
+
if (!/^[0-9a-fA-F]*$/.test(clean) || clean.length % 2 !== 0) {
|
|
8391
|
+
throw new Error(`${label2} is not an even-length hex string`);
|
|
8392
|
+
}
|
|
8393
|
+
return fromHex(clean, label2, clean.length / 2);
|
|
8394
|
+
}
|
|
8395
|
+
|
|
8396
|
+
// src/request-proof/authorizer.ts
|
|
8397
|
+
function createProofAuthorizer(credential) {
|
|
8398
|
+
if (!credential.credential) {
|
|
8399
|
+
throw new Error("a proof authorizer needs the `credential` from signDeviceCert");
|
|
8400
|
+
}
|
|
8401
|
+
if (!credential.deviceSecret && !credential.signer) {
|
|
8402
|
+
throw new Error("a proof authorizer needs either `deviceSecret` or `signer`");
|
|
8403
|
+
}
|
|
8404
|
+
return async ({ method, path, body }) => {
|
|
8405
|
+
const proof = await signRequestProof({
|
|
8406
|
+
method,
|
|
8407
|
+
path,
|
|
8408
|
+
body,
|
|
8409
|
+
credential: credential.credential,
|
|
8410
|
+
deviceSecret: credential.deviceSecret,
|
|
8411
|
+
signer: credential.signer,
|
|
8412
|
+
validForSeconds: credential.validForSeconds
|
|
8413
|
+
});
|
|
8414
|
+
return { [REQUEST_PROOF_HEADER]: proof };
|
|
8415
|
+
};
|
|
8416
|
+
}
|
|
8417
|
+
function signedPath(url) {
|
|
8418
|
+
return new URL(url).pathname;
|
|
8419
|
+
}
|
|
8420
|
+
|
|
8421
|
+
// src/transport/mero-client.ts
|
|
8422
|
+
function relayHasNo(surface, why) {
|
|
8423
|
+
return new Error(
|
|
8424
|
+
`this client was constructed with the relay transport, which has no ${surface}: ${why}. Construct a node client (createMeroClient({ baseUrl })) for that surface \u2014 nothing here will silently connect to a node you did not choose.`
|
|
8425
|
+
);
|
|
8426
|
+
}
|
|
8427
|
+
function missingNodeKey(surface) {
|
|
8428
|
+
return new Error(
|
|
8429
|
+
`this relay-transport client cannot open ${surface}: it was given no relay node key. A relay is an ordinary node and does serve /sse and /ws \u2014 but the login statement must be signed against the node's device signing key, learned out of band (never read from the node, and never derived from its peerId). Pass it as \`observe: { nodeKey }\`. For a cloud-hosted relay the cloud does not publish it yet \u2014 see mdma #312.`
|
|
8430
|
+
);
|
|
8431
|
+
}
|
|
8432
|
+
function buildObserver(relay, observe) {
|
|
8433
|
+
const nodeKey = observe?.nodeKey;
|
|
8434
|
+
if (!nodeKey) return null;
|
|
8435
|
+
return new RelayObserver({
|
|
8436
|
+
nodeUrl: observe?.nodeUrl ?? relay.relayUrl,
|
|
8437
|
+
nodeKey,
|
|
8438
|
+
accountProof: observe?.accountProof ?? relay.authorProof,
|
|
8439
|
+
audience: observe?.audience ?? defaultAudience(),
|
|
8440
|
+
signer: observe?.signer ? async () => observe.signer : () => relay.authorSigner(),
|
|
8441
|
+
clientName: observe?.clientName,
|
|
8442
|
+
ttlSeconds: observe?.ttlSeconds,
|
|
8443
|
+
fetch: observe?.fetch,
|
|
8444
|
+
timeoutMs: observe?.timeoutMs
|
|
8445
|
+
});
|
|
8446
|
+
}
|
|
8447
|
+
var MeroClient = class {
|
|
8448
|
+
constructor(config) {
|
|
8449
|
+
/** The relay's event session, or `null` on a node client / with no node key. */
|
|
8450
|
+
this.relayObserver = null;
|
|
8451
|
+
/** Admin reads against the relay, when a session or a proof was supplied. */
|
|
8452
|
+
this.relayAdmin = null;
|
|
8453
|
+
/**
|
|
8454
|
+
* Events off the relay authenticated by a request proof, built lazily.
|
|
8455
|
+
*
|
|
8456
|
+
* Separate from {@link relayObserver} rather than a mode inside it: the
|
|
8457
|
+
* observer's whole subject is a session addressed to a node key, and a proof
|
|
8458
|
+
* has neither. Holding the authorizer instead of the client keeps construction
|
|
8459
|
+
* free of the first signature.
|
|
8460
|
+
*/
|
|
8461
|
+
this.proofAuthorizer = null;
|
|
8462
|
+
this.relayUrl = null;
|
|
8463
|
+
this.proofEvents = null;
|
|
8464
|
+
if (config.transport === "relay") {
|
|
8465
|
+
this.transport = "relay";
|
|
8466
|
+
this.nodeClient = null;
|
|
8467
|
+
const relay = config.relay instanceof RelayClient ? config.relay : new RelayClient(config.relay);
|
|
8468
|
+
this.relayObserver = buildObserver(relay, config.observe);
|
|
8469
|
+
this.rpcTransport = new RelayTransport(relay, this.relayObserver);
|
|
8470
|
+
this.relayUrl = relay.relayUrl;
|
|
8471
|
+
if (config.proof) {
|
|
8472
|
+
this.proofAuthorizer = createProofAuthorizer(config.proof);
|
|
8473
|
+
}
|
|
8474
|
+
if (config.session) {
|
|
8475
|
+
const token = config.session;
|
|
8476
|
+
this.relayAdmin = createAdminApiClient({
|
|
8477
|
+
baseUrl: relay.relayUrl,
|
|
8478
|
+
getAuthToken: typeof token === "string" ? async () => token : token
|
|
8479
|
+
});
|
|
8480
|
+
} else if (this.proofAuthorizer) {
|
|
8481
|
+
const authorizeRequest = this.proofAuthorizer;
|
|
8482
|
+
this.relayAdmin = createAdminApiClientFromHttpClient(
|
|
8483
|
+
createHttpClient({
|
|
8484
|
+
fetch: (url, init) => globalThis.fetch(url, init),
|
|
8485
|
+
baseUrl: relay.relayUrl,
|
|
8486
|
+
authorizeRequest
|
|
8487
|
+
}),
|
|
8488
|
+
{ baseUrl: relay.relayUrl }
|
|
8489
|
+
);
|
|
8490
|
+
}
|
|
8491
|
+
return;
|
|
8492
|
+
}
|
|
8493
|
+
this.transport = "node";
|
|
8494
|
+
const { transport: _ignored, ...nodeConfig } = config;
|
|
8495
|
+
this.nodeClient = new MeroJs(nodeConfig);
|
|
8496
|
+
this.rpcTransport = this.nodeClient.rpc;
|
|
8497
|
+
}
|
|
8498
|
+
/**
|
|
8499
|
+
* The write surface. Identical on both transports — this is the whole point.
|
|
8500
|
+
*
|
|
8501
|
+
* On a node this IS the `RpcClient` a `MeroJs` would have handed out, not a
|
|
8502
|
+
* wrapper around it, so `client.rpc` and `client.node.rpc` are the same
|
|
8503
|
+
* object.
|
|
8504
|
+
*/
|
|
8505
|
+
get rpc() {
|
|
8506
|
+
return this.rpcTransport;
|
|
8507
|
+
}
|
|
8508
|
+
/**
|
|
8509
|
+
* Whether `events` / `ws` will work on this client.
|
|
8510
|
+
*
|
|
8511
|
+
* Always `true` on the node transport. On the relay transport it says whether
|
|
8512
|
+
* a node key was supplied — the relay can be observed, but only by a caller
|
|
8513
|
+
* who can name the key the login statement is signed against.
|
|
8514
|
+
*/
|
|
8515
|
+
get canSubscribe() {
|
|
8516
|
+
return this.rpcTransport.canSubscribe || this.proofAuthorizer !== null;
|
|
8517
|
+
}
|
|
8518
|
+
/** The underlying `MeroJs`. Node transport only. */
|
|
8519
|
+
get node() {
|
|
8520
|
+
if (!this.nodeClient) {
|
|
8521
|
+
throw relayHasNo("node connection", "it holds a signing key and a relay URL, not a node");
|
|
8522
|
+
}
|
|
8523
|
+
return this.nodeClient;
|
|
8524
|
+
}
|
|
8525
|
+
/** The underlying `RelayClient`, for `describe()`. Relay transport only. */
|
|
8526
|
+
get relay() {
|
|
8527
|
+
if (!(this.rpcTransport instanceof RelayTransport)) {
|
|
8528
|
+
throw new Error("this client was constructed with the node transport and has no relay");
|
|
8529
|
+
}
|
|
8530
|
+
return this.rpcTransport.client;
|
|
8531
|
+
}
|
|
8532
|
+
/**
|
|
8533
|
+
* Admin API. Node transport only.
|
|
8534
|
+
*
|
|
8535
|
+
* Two different things live behind this accessor today, and only one of them
|
|
8536
|
+
* is genuinely unavailable through a relay.
|
|
8537
|
+
*
|
|
8538
|
+
* The **mutations** — `createNamespace`, `setMemberCapabilities`,
|
|
8539
|
+
* `upgradeGroup` and the rest — are operator actions on somebody else's
|
|
8540
|
+
* machine. A keyholder does not get those, and should not; admin is for
|
|
8541
|
+
* admins.
|
|
8542
|
+
*
|
|
8543
|
+
* The **reads** are a different matter. `getContext`, `getContexts`,
|
|
8544
|
+
* `getApplication`, `listNamespaces`, `getBlob` are an app asking what it is
|
|
8545
|
+
* looking at, not an operator administering anything. They sit under
|
|
8546
|
+
* `/admin-api/` for historical reasons, and a delegated session is refused
|
|
8547
|
+
* them (measured: `403`, while the same session reads context state through
|
|
8548
|
+
* `POST /contexts/{ctx}/query` and writes through `/intents` quite happily).
|
|
8549
|
+
*
|
|
8550
|
+
* So this is the one place where "the same code runs on both transports"
|
|
8551
|
+
* does not hold yet, and it is a missing surface rather than a missing
|
|
8552
|
+
* permission. A transport-independent read API is designed but not built; the
|
|
8553
|
+
* throw below is deliberate until it exists, because the alternative — quietly
|
|
8554
|
+
* reaching for a node the caller did not choose — is worse than a clear stop.
|
|
8555
|
+
*/
|
|
8556
|
+
/**
|
|
8557
|
+
* The admin surface.
|
|
8558
|
+
*
|
|
8559
|
+
* On a node client this is the node's own. On a relay client it exists only
|
|
8560
|
+
* when a `session` was supplied, and it is **the relay's admin API
|
|
8561
|
+
* authenticated as the account** — so the caller-scoped reads answer:
|
|
8562
|
+
* `getContexts` and `listNamespaces` return this account's own, because
|
|
8563
|
+
* `account_proof` grants `context:list-own` and `namespace:list-own`.
|
|
8564
|
+
*
|
|
8565
|
+
* Mutations are not filtered out here, deliberately. A delegated session does
|
|
8566
|
+
* not carry the grants they require, so the node refuses them with a `403`
|
|
8567
|
+
* that names the missing permission — which is a better answer than a
|
|
8568
|
+
* client-side guess, and keeps the policy in the one place that owns it.
|
|
8569
|
+
*/
|
|
8570
|
+
get admin() {
|
|
8571
|
+
if (this.nodeClient) return this.nodeClient.admin;
|
|
8572
|
+
if (this.relayAdmin) return this.relayAdmin;
|
|
8573
|
+
throw new Error(
|
|
8574
|
+
"this relay-transport client has no admin surface because it was given neither a `proof` nor a `session`. A warrant authorises one intent and is not a credential for reading. The caller-scoped reads (getContexts, listNamespaces) want one of the other two: pass `proof: { credential, deviceSecret }` \u2014 the device cert you already hold, which needs no login and no node key \u2014 or a `session` minted with `login()`. Context STATE reads work through execute/query either way."
|
|
8575
|
+
);
|
|
8576
|
+
}
|
|
8577
|
+
/** Auth API. Node transport only — a relay issues no node credential. */
|
|
8578
|
+
get auth() {
|
|
8579
|
+
return this.node.auth;
|
|
8580
|
+
}
|
|
8581
|
+
/**
|
|
8582
|
+
* SSE events — on **both** transports.
|
|
8583
|
+
*
|
|
8584
|
+
* On a relay client this is an `SseClient` against the relay node, fed by a
|
|
8585
|
+
* session minted with the author's account proof. The session is minted
|
|
8586
|
+
* lazily, on the first token the stream asks for, so reading this getter
|
|
8587
|
+
* performs no network call.
|
|
8588
|
+
*
|
|
8589
|
+
* Throws only when the relay client was given no node key: the one input
|
|
8590
|
+
* that cannot be inferred. See {@link missingNodeKey}.
|
|
8591
|
+
*/
|
|
8592
|
+
get events() {
|
|
8593
|
+
if (this.nodeClient) return this.nodeClient.events;
|
|
8594
|
+
if (this.relayObserver) return this.relayObserver.events;
|
|
8595
|
+
if (this.proofAuthorizer && this.relayUrl) {
|
|
8596
|
+
this.proofEvents ?? (this.proofEvents = new SseClient({
|
|
8597
|
+
baseUrl: this.relayUrl,
|
|
8598
|
+
authorize: this.proofAuthorizer
|
|
8599
|
+
}));
|
|
8600
|
+
return this.proofEvents;
|
|
8601
|
+
}
|
|
8602
|
+
throw missingNodeKey("SSE events");
|
|
8603
|
+
}
|
|
8604
|
+
/** WebSocket events, on both transports. @experimental — prefer `events`. */
|
|
8605
|
+
get ws() {
|
|
8606
|
+
if (this.nodeClient) return this.nodeClient.ws;
|
|
8607
|
+
if (this.relayObserver) return this.relayObserver.ws;
|
|
8608
|
+
if (this.proofAuthorizer) {
|
|
8609
|
+
throw new Error(
|
|
8610
|
+
"this client authenticates with a request proof, which cannot open a WebSocket from a browser: `WebSocket` cannot set the X-Calimero-Proof header, and core's guard reads the proof from a header only (the ?token= query parameter is the session equivalent and has no proof counterpart). Use `client.events` \u2014 SSE is fetch-based, carries the header, and is the recommended transport anyway."
|
|
8611
|
+
);
|
|
8612
|
+
}
|
|
8613
|
+
throw missingNodeKey("WebSocket events");
|
|
8614
|
+
}
|
|
8615
|
+
/**
|
|
8616
|
+
* Ephemeral presence. Node transport only.
|
|
8617
|
+
*
|
|
8618
|
+
* Not extended to the relay even though a logged-in session could reach both
|
|
8619
|
+
* halves of it mechanically. Publishing resolves the author from an *owned
|
|
8620
|
+
* context identity on the node*, and whether a delegated session has one has
|
|
8621
|
+
* not been established — a presence client that silently published nothing,
|
|
8622
|
+
* or published as the wrong identity, is worse than one that says it is not
|
|
8623
|
+
* available. The read half is not lost: presence arrives on `client.events`
|
|
8624
|
+
* like any other context event, and can be filtered there.
|
|
8625
|
+
*
|
|
8626
|
+
* The rejected alternative was to expose it over the relay session and let
|
|
8627
|
+
* `set` fail at the node. That trades a clear construction-time answer for a
|
|
8628
|
+
* runtime failure inside a UI that has already rendered.
|
|
8629
|
+
*/
|
|
8630
|
+
get ephemeral() {
|
|
8631
|
+
if (!this.nodeClient) {
|
|
8632
|
+
throw relayHasNo(
|
|
8633
|
+
"ephemeral presence",
|
|
8634
|
+
"publishing resolves the author from an owned context identity on the node, which a delegated session is not known to have \u2014 read presence off `client.events` instead"
|
|
8635
|
+
);
|
|
8636
|
+
}
|
|
8637
|
+
return this.nodeClient.ephemeral;
|
|
8638
|
+
}
|
|
8639
|
+
/**
|
|
8640
|
+
* Calimero Cloud.
|
|
8641
|
+
*
|
|
8642
|
+
* Available on both transports, because it is neither: the cloud session is
|
|
8643
|
+
* independent of the node credential and of the relay's signing key, and a
|
|
8644
|
+
* relay-transport app still needs it to list namespaces and manage billing.
|
|
8645
|
+
*/
|
|
8646
|
+
get cloud() {
|
|
8647
|
+
if (!this.nodeClient) {
|
|
8648
|
+
throw relayHasNo(
|
|
8649
|
+
"bundled cloud client",
|
|
8650
|
+
"a relay client is built from a cloud connection rather than owning one \u2014 reach it through the `cloud` field that `connectCloud` returns, or construct a `CloudClient` directly"
|
|
8651
|
+
);
|
|
8652
|
+
}
|
|
8653
|
+
return this.nodeClient.cloud;
|
|
8654
|
+
}
|
|
8655
|
+
/**
|
|
8656
|
+
* Release whatever the transport is holding.
|
|
8657
|
+
*
|
|
8658
|
+
* Safe on a relay client, where there is nothing open to close — a caller
|
|
8659
|
+
* that tears down on unmount should not have to know which transport it got.
|
|
8660
|
+
*/
|
|
8661
|
+
close() {
|
|
8662
|
+
this.nodeClient?.close();
|
|
8663
|
+
this.relayObserver?.close();
|
|
8664
|
+
this.proofEvents?.close();
|
|
8665
|
+
}
|
|
8666
|
+
};
|
|
8667
|
+
function createMeroClient(config) {
|
|
8668
|
+
return new MeroClient(config);
|
|
8669
|
+
}
|
|
8670
|
+
|
|
7590
8671
|
// src/relay/nonce-source.ts
|
|
7591
8672
|
function createMemoryNonceSource(start = 1) {
|
|
7592
8673
|
let next = BigInt(start);
|
|
@@ -7598,6 +8679,10 @@ function createMemoryNonceSource(start = 1) {
|
|
|
7598
8679
|
const value = next;
|
|
7599
8680
|
next += 1n;
|
|
7600
8681
|
return Promise.resolve(value);
|
|
8682
|
+
},
|
|
8683
|
+
advanceTo(floor) {
|
|
8684
|
+
if (floor > next) next = floor;
|
|
8685
|
+
return Promise.resolve();
|
|
7601
8686
|
}
|
|
7602
8687
|
};
|
|
7603
8688
|
}
|
|
@@ -7608,21 +8693,101 @@ function createLocalStorageNonceSource(key, storage) {
|
|
|
7608
8693
|
"no localStorage in this runtime: pass a storage implementation, or use createMemoryNonceSource for a process that owns its whole sequence"
|
|
7609
8694
|
);
|
|
7610
8695
|
}
|
|
8696
|
+
const lastHandedOut = () => {
|
|
8697
|
+
const raw = store.getItem(key);
|
|
8698
|
+
if (raw === null || raw === "") return 0n;
|
|
8699
|
+
try {
|
|
8700
|
+
const parsed = BigInt(raw);
|
|
8701
|
+
return parsed > 0n ? parsed : 0n;
|
|
8702
|
+
} catch {
|
|
8703
|
+
return 0n;
|
|
8704
|
+
}
|
|
8705
|
+
};
|
|
7611
8706
|
return {
|
|
7612
8707
|
next() {
|
|
7613
|
-
const
|
|
7614
|
-
|
|
7615
|
-
|
|
7616
|
-
|
|
7617
|
-
|
|
7618
|
-
|
|
7619
|
-
|
|
7620
|
-
|
|
7621
|
-
|
|
8708
|
+
const value = lastHandedOut() + 1n;
|
|
8709
|
+
store.setItem(key, value.toString());
|
|
8710
|
+
return Promise.resolve(value);
|
|
8711
|
+
},
|
|
8712
|
+
advanceTo(floor) {
|
|
8713
|
+
const wanted = floor > 0n ? floor - 1n : 0n;
|
|
8714
|
+
if (wanted > lastHandedOut()) store.setItem(key, wanted.toString());
|
|
8715
|
+
return Promise.resolve();
|
|
8716
|
+
}
|
|
8717
|
+
};
|
|
8718
|
+
}
|
|
8719
|
+
function authorNonceLookup(options) {
|
|
8720
|
+
return {
|
|
8721
|
+
getWarrantNonce: (contextId, _authorDeviceKey) => options.client.getWarrantNonceAsAuthor(contextId, options.authorProof)
|
|
8722
|
+
};
|
|
8723
|
+
}
|
|
8724
|
+
var WarrantNonceExhaustedError = class extends Error {
|
|
8725
|
+
constructor(contextId, authorDeviceKey) {
|
|
8726
|
+
super(
|
|
8727
|
+
`author device ${authorDeviceKey} has spent every nonce in context ${contextId}; it must re-key to write again`
|
|
8728
|
+
);
|
|
8729
|
+
this.contextId = contextId;
|
|
8730
|
+
this.authorDeviceKey = authorDeviceKey;
|
|
8731
|
+
this.name = "WarrantNonceExhaustedError";
|
|
8732
|
+
}
|
|
8733
|
+
};
|
|
8734
|
+
function createRecoveringNonceSource(options) {
|
|
8735
|
+
const lookups = Array.isArray(options.lookups) ? options.lookups : [options.lookups];
|
|
8736
|
+
if (lookups.length === 0) {
|
|
8737
|
+
throw new Error("a recovering nonce source needs at least one node to ask");
|
|
8738
|
+
}
|
|
8739
|
+
const local = options.local ?? createMemoryNonceSource(1);
|
|
8740
|
+
const onLookupFailure = options.onLookupFailure ?? "throw";
|
|
8741
|
+
let floor = null;
|
|
8742
|
+
let pending = null;
|
|
8743
|
+
const resolveFloor = async () => {
|
|
8744
|
+
const answers = await Promise.allSettled(
|
|
8745
|
+
lookups.map(
|
|
8746
|
+
(lookup) => lookup.getWarrantNonce(options.contextId, options.authorDeviceKey)
|
|
8747
|
+
)
|
|
8748
|
+
);
|
|
8749
|
+
let highest = null;
|
|
8750
|
+
let exhausted = false;
|
|
8751
|
+
for (const answer of answers) {
|
|
8752
|
+
if (answer.status !== "fulfilled") continue;
|
|
8753
|
+
const state = answer.value;
|
|
8754
|
+
if (state.kind === "exhausted") {
|
|
8755
|
+
exhausted = true;
|
|
8756
|
+
continue;
|
|
8757
|
+
}
|
|
8758
|
+
if (highest === null || state.nextNonce > highest) highest = state.nextNonce;
|
|
8759
|
+
}
|
|
8760
|
+
if (highest === null && exhausted) {
|
|
8761
|
+
throw new WarrantNonceExhaustedError(options.contextId, options.authorDeviceKey);
|
|
8762
|
+
}
|
|
8763
|
+
if (highest === null) {
|
|
8764
|
+
const first = answers.find((a) => a.status === "rejected");
|
|
8765
|
+
const reason = first && first.status === "rejected" ? first.reason : void 0;
|
|
8766
|
+
if (onLookupFailure === "local") {
|
|
8767
|
+
floor = 0n;
|
|
8768
|
+
return;
|
|
8769
|
+
}
|
|
8770
|
+
throw reason instanceof Error ? reason : new Error(`no node could answer where the warrant-nonce sequence stands: ${String(reason)}`);
|
|
8771
|
+
}
|
|
8772
|
+
floor = highest;
|
|
8773
|
+
await local.advanceTo?.(highest);
|
|
8774
|
+
};
|
|
8775
|
+
return {
|
|
8776
|
+
async next() {
|
|
8777
|
+
if (floor === null) {
|
|
8778
|
+
pending ?? (pending = resolveFloor().finally(() => {
|
|
8779
|
+
pending = null;
|
|
8780
|
+
}));
|
|
8781
|
+
await pending;
|
|
7622
8782
|
}
|
|
7623
|
-
const
|
|
7624
|
-
|
|
7625
|
-
|
|
8783
|
+
const mine = await local.next();
|
|
8784
|
+
const value = floor !== null && floor > mine ? floor : mine;
|
|
8785
|
+
floor = value + 1n;
|
|
8786
|
+
return value;
|
|
8787
|
+
},
|
|
8788
|
+
async advanceTo(wanted) {
|
|
8789
|
+
if (floor === null || wanted > floor) floor = wanted;
|
|
8790
|
+
await local.advanceTo?.(wanted);
|
|
7626
8791
|
}
|
|
7627
8792
|
};
|
|
7628
8793
|
}
|
|
@@ -7795,7 +8960,7 @@ var MAX_HANDSHAKE_REPLY = 1024;
|
|
|
7795
8960
|
var RENEW_BEFORE_MS = 6e4;
|
|
7796
8961
|
var TRANSPORT_BINDING_DOMAIN = "calimero.tee-attest.transport-key.v1";
|
|
7797
8962
|
var NULL_BODY_STATUSES = [101, 204, 205, 304];
|
|
7798
|
-
var
|
|
8963
|
+
var utf82 = new TextEncoder();
|
|
7799
8964
|
var StaleTransportKeyError = class extends Error {
|
|
7800
8965
|
constructor() {
|
|
7801
8966
|
super("The node no longer holds the attested transport key; attest again for the current one");
|
|
@@ -7803,18 +8968,18 @@ var StaleTransportKeyError = class extends Error {
|
|
|
7803
8968
|
}
|
|
7804
8969
|
};
|
|
7805
8970
|
var SealedTransportError = class extends Error {
|
|
7806
|
-
constructor(status, code, message,
|
|
8971
|
+
constructor(status, code, message, retryAfterMs2) {
|
|
7807
8972
|
super(message);
|
|
7808
8973
|
this.status = status;
|
|
7809
8974
|
this.code = code;
|
|
7810
|
-
this.retryAfterMs =
|
|
8975
|
+
this.retryAfterMs = retryAfterMs2;
|
|
7811
8976
|
this.name = "SealedTransportError";
|
|
7812
8977
|
}
|
|
7813
8978
|
};
|
|
7814
8979
|
async function transportKeyBinding(inner, transportKey) {
|
|
7815
8980
|
const digest = await crypto.subtle.digest(
|
|
7816
8981
|
"SHA-256",
|
|
7817
|
-
concat(
|
|
8982
|
+
concat(utf82.encode(TRANSPORT_BINDING_DOMAIN), inner, transportKey)
|
|
7818
8983
|
);
|
|
7819
8984
|
return new Uint8Array(digest);
|
|
7820
8985
|
}
|
|
@@ -8018,7 +9183,7 @@ function nonce(requestId, index) {
|
|
|
8018
9183
|
return bytes;
|
|
8019
9184
|
}
|
|
8020
9185
|
function joinInner(head, body) {
|
|
8021
|
-
const encoded =
|
|
9186
|
+
const encoded = utf82.encode(JSON.stringify(head));
|
|
8022
9187
|
const len = new Uint8Array(4);
|
|
8023
9188
|
new DataView(len.buffer).setUint32(0, encoded.length, false);
|
|
8024
9189
|
return concat(len, encoded, body);
|
|
@@ -8127,8 +9292,8 @@ async function envelopeRefusal(response) {
|
|
|
8127
9292
|
}
|
|
8128
9293
|
if (code === "stale_transport_key") return new StaleTransportKeyError();
|
|
8129
9294
|
const retryAfter = Number(response.headers.get("retry-after"));
|
|
8130
|
-
const
|
|
8131
|
-
return new SealedTransportError(response.status, code, message,
|
|
9295
|
+
const retryAfterMs2 = Number.isFinite(retryAfter) && retryAfter > 0 ? Math.min(retryAfter, 5) * 1e3 : void 0;
|
|
9296
|
+
return new SealedTransportError(response.status, code, message, retryAfterMs2);
|
|
8132
9297
|
}
|
|
8133
9298
|
|
|
8134
9299
|
// src/cloud/connect.ts
|
|
@@ -8136,6 +9301,7 @@ async function connectCloud(options) {
|
|
|
8136
9301
|
if (!options.sessionToken && !options.googleIdToken) {
|
|
8137
9302
|
throw new Error("connectCloud needs either a sessionToken or a googleIdToken");
|
|
8138
9303
|
}
|
|
9304
|
+
const signer = await resolveSigner(options.deviceSecret, options.signer, "deviceSecret");
|
|
8139
9305
|
const cloud = new CloudClient({
|
|
8140
9306
|
cloudBaseUrl: options.cloudBaseUrl,
|
|
8141
9307
|
sessionToken: options.sessionToken,
|
|
@@ -8161,8 +9327,8 @@ async function connectCloud(options) {
|
|
|
8161
9327
|
executorAccount: relayInfo.executorAccount,
|
|
8162
9328
|
authorAccount: options.authorAccount,
|
|
8163
9329
|
authorProof: options.authorProof,
|
|
8164
|
-
|
|
8165
|
-
nonces: options.nonces ??
|
|
9330
|
+
signer,
|
|
9331
|
+
nonces: options.nonces ?? defaultNonceSource(signer.publicKey),
|
|
8166
9332
|
ttlSeconds: options.ttlSeconds,
|
|
8167
9333
|
// Only the relay is sealed: a sealed fetch is bound to one node and refuses
|
|
8168
9334
|
// any other URL, and the cloud calls above carry no intent.
|
|
@@ -8179,9 +9345,50 @@ async function connectCloud(options) {
|
|
|
8179
9345
|
namespaceId: namespace.namespaceId,
|
|
8180
9346
|
namespace,
|
|
8181
9347
|
relayInfo,
|
|
8182
|
-
execute: (contextId, method, argsJson) => relay.execute(contextId, method, argsJson)
|
|
9348
|
+
execute: (contextId, method, argsJson) => relay.execute(contextId, method, argsJson),
|
|
9349
|
+
// Built from the same `relay`, not a second one: one nonce sequence, one
|
|
9350
|
+
// warrant signer. Two clients over one relay would hand out the same nonce
|
|
9351
|
+
// twice and have the network refuse the loser as a replay.
|
|
9352
|
+
//
|
|
9353
|
+
// `observe` is always passed, with whatever key is known — the caller's if
|
|
9354
|
+
// they had one, otherwise the cloud's, which is `null` until mdma #312. One
|
|
9355
|
+
// code path rather than two, so the hosted case starts observing the day
|
|
9356
|
+
// the field appears, without a line changing here or at the call site.
|
|
9357
|
+
client: new MeroClient({
|
|
9358
|
+
transport: "relay",
|
|
9359
|
+
relay,
|
|
9360
|
+
observe: {
|
|
9361
|
+
nodeKey: options.relayNodeKey ?? relayInfo.nodeKey,
|
|
9362
|
+
audience: options.observeAudience,
|
|
9363
|
+
fetch: options.fetch,
|
|
9364
|
+
timeoutMs: options.timeoutMs
|
|
9365
|
+
}
|
|
9366
|
+
})
|
|
8183
9367
|
};
|
|
8184
9368
|
}
|
|
9369
|
+
async function connectCloudWithAccount(options) {
|
|
9370
|
+
const { root, authorAccount, ...rest } = options;
|
|
9371
|
+
const signer = await resolveSigner(rest.deviceSecret, rest.signer, "deviceSecret");
|
|
9372
|
+
const resolved = await resolveRoot(root);
|
|
9373
|
+
const cloud = new CloudClient({
|
|
9374
|
+
cloudBaseUrl: options.cloudBaseUrl,
|
|
9375
|
+
// Reported here as well as on the connection's own client: this is where
|
|
9376
|
+
// the session is first minted, and a caller persisting it would otherwise
|
|
9377
|
+
// only hear about the rolling refreshes of a token it never saw.
|
|
9378
|
+
onSession: options.onSession,
|
|
9379
|
+
fetch: options.fetch,
|
|
9380
|
+
timeoutMs: options.timeoutMs
|
|
9381
|
+
});
|
|
9382
|
+
const session = await cloud.signInWithAccount(resolved.signer);
|
|
9383
|
+
return connectCloud({
|
|
9384
|
+
...rest,
|
|
9385
|
+
// Already resolved above, and passing both forms on would be refused.
|
|
9386
|
+
deviceSecret: void 0,
|
|
9387
|
+
signer,
|
|
9388
|
+
sessionToken: session.sessionToken,
|
|
9389
|
+
authorAccount: authorAccount ?? resolved.accountId
|
|
9390
|
+
});
|
|
9391
|
+
}
|
|
8185
9392
|
function pickNamespace(namespaces, requested) {
|
|
8186
9393
|
if (requested) {
|
|
8187
9394
|
const found = namespaces.find((n) => n.namespaceId === requested);
|
|
@@ -8218,8 +9425,8 @@ function explainNoRelay(namespace, relays) {
|
|
|
8218
9425
|
}
|
|
8219
9426
|
return `namespace ${namespace.namespaceId} has ${relays.length} relay(s), but none is ready to relay yet (a URL, an executor account and a recent heartbeat) \u2014 retry shortly`;
|
|
8220
9427
|
}
|
|
8221
|
-
|
|
8222
|
-
const key = `mero-js:warrant-nonce:${
|
|
9428
|
+
function defaultNonceSource(devicePublicKey) {
|
|
9429
|
+
const key = `mero-js:warrant-nonce:${devicePublicKey}`;
|
|
8223
9430
|
try {
|
|
8224
9431
|
return createLocalStorageNonceSource(key);
|
|
8225
9432
|
} catch {
|
|
@@ -8227,6 +9434,168 @@ async function defaultNonceSource(deviceSecret) {
|
|
|
8227
9434
|
}
|
|
8228
9435
|
}
|
|
8229
9436
|
|
|
9437
|
+
// src/cloud/link-redirect.ts
|
|
9438
|
+
var DEFAULT_CLOUD_WEB_URL = "https://cloud.calimero.network";
|
|
9439
|
+
function cloudLinkUrl(options) {
|
|
9440
|
+
if (!/^[0-9a-f]{64}$/.test(options.accountId)) {
|
|
9441
|
+
throw new Error("accountId must be 64 lowercase hex characters");
|
|
9442
|
+
}
|
|
9443
|
+
const url = new URL(options.cloudWebUrl ?? DEFAULT_CLOUD_WEB_URL);
|
|
9444
|
+
url.searchParams.set("link-account", options.accountId);
|
|
9445
|
+
url.searchParams.set("callback-url", options.returnTo);
|
|
9446
|
+
return url.toString();
|
|
9447
|
+
}
|
|
9448
|
+
function beginCloudLink(options) {
|
|
9449
|
+
window.location.assign(cloudLinkUrl(options));
|
|
9450
|
+
throw new Error("redirecting to the cloud to authorise this link");
|
|
9451
|
+
}
|
|
9452
|
+
function readCloudLinkCallback(location2 = window.location) {
|
|
9453
|
+
const params = new URLSearchParams(location2.hash.replace(/^#/, ""));
|
|
9454
|
+
const error = params.get("error");
|
|
9455
|
+
if (error) {
|
|
9456
|
+
stripFragment();
|
|
9457
|
+
throw new Error(
|
|
9458
|
+
error === "denied" ? "the account link was declined at the cloud" : `the cloud refused this link: ${error}`
|
|
9459
|
+
);
|
|
9460
|
+
}
|
|
9461
|
+
const grant = params.get("grant");
|
|
9462
|
+
const account = params.get("account");
|
|
9463
|
+
if (!grant || !account) return null;
|
|
9464
|
+
stripFragment();
|
|
9465
|
+
return { grant, account };
|
|
9466
|
+
}
|
|
9467
|
+
function stripFragment() {
|
|
9468
|
+
if (typeof history === "undefined" || !history.replaceState) return;
|
|
9469
|
+
history.replaceState(null, "", location.pathname + location.search);
|
|
9470
|
+
}
|
|
9471
|
+
async function completeCloudLink(options) {
|
|
9472
|
+
const root = await resolveRoot(options.root);
|
|
9473
|
+
if (root.accountId !== options.account) {
|
|
9474
|
+
throw new Error(
|
|
9475
|
+
`this grant is for account ${options.account}, but the root here derives ${root.accountId} \u2014 it was minted for a different account`
|
|
9476
|
+
);
|
|
9477
|
+
}
|
|
9478
|
+
const cloud = new CloudClient({
|
|
9479
|
+
cloudBaseUrl: options.cloudBaseUrl,
|
|
9480
|
+
fetch: options.fetch
|
|
9481
|
+
});
|
|
9482
|
+
return cloud.linkAccountWithGrant({ grant: options.grant, signer: root.signer });
|
|
9483
|
+
}
|
|
9484
|
+
|
|
9485
|
+
// src/cloud/enrol-redirect.ts
|
|
9486
|
+
var DEVICE_PARAM = "enrol-device";
|
|
9487
|
+
var KEM_PARAM = "enrol-kem";
|
|
9488
|
+
var CALLBACK_PARAM = "callback-url";
|
|
9489
|
+
var STATE_PARAM = "state";
|
|
9490
|
+
function deviceEnrolmentUrl(options) {
|
|
9491
|
+
assertHex(options.devicePublicKey, "devicePublicKey");
|
|
9492
|
+
assertHex(options.kemPublicKey, "kemPublicKey");
|
|
9493
|
+
assertSafeReturnTo(options.returnTo);
|
|
9494
|
+
warnOnForeignCallback(options.returnTo);
|
|
9495
|
+
const url = new URL(options.walletUrl);
|
|
9496
|
+
url.searchParams.set(DEVICE_PARAM, options.devicePublicKey);
|
|
9497
|
+
url.searchParams.set(KEM_PARAM, options.kemPublicKey);
|
|
9498
|
+
url.searchParams.set(CALLBACK_PARAM, options.returnTo);
|
|
9499
|
+
if (options.state) url.searchParams.set(STATE_PARAM, options.state);
|
|
9500
|
+
return url.toString();
|
|
9501
|
+
}
|
|
9502
|
+
function beginDeviceEnrolment(options) {
|
|
9503
|
+
window.location.assign(deviceEnrolmentUrl(options));
|
|
9504
|
+
throw new Error("redirecting to the wallet to approve this device");
|
|
9505
|
+
}
|
|
9506
|
+
function readEnrolmentCallback(location2 = window.location) {
|
|
9507
|
+
const params = new URLSearchParams(location2.hash.replace(/^#/, ""));
|
|
9508
|
+
const error = params.get("error");
|
|
9509
|
+
if (error) {
|
|
9510
|
+
stripFragment2();
|
|
9511
|
+
throw new Error(
|
|
9512
|
+
error === "denied" || error === "cancelled" ? "the device was not approved at the wallet" : `the wallet refused this enrolment: ${error}`
|
|
9513
|
+
);
|
|
9514
|
+
}
|
|
9515
|
+
const credential = params.get("credential");
|
|
9516
|
+
const account = params.get("account");
|
|
9517
|
+
const device = params.get("device");
|
|
9518
|
+
if (!credential || !account || !device) return null;
|
|
9519
|
+
stripFragment2();
|
|
9520
|
+
const state = params.get("state");
|
|
9521
|
+
return { credential, account, device, ...state ? { state } : {} };
|
|
9522
|
+
}
|
|
9523
|
+
function stripFragment2() {
|
|
9524
|
+
if (typeof history === "undefined" || !history.replaceState) return;
|
|
9525
|
+
history.replaceState(null, "", location.pathname + location.search);
|
|
9526
|
+
}
|
|
9527
|
+
async function completeDeviceEnrolment(options) {
|
|
9528
|
+
if (options.expectState !== void 0 && options.expectState !== options.state) {
|
|
9529
|
+
throw new Error(
|
|
9530
|
+
"this enrolment answers a request this app did not make \u2014 the state it carries is not the one sent"
|
|
9531
|
+
);
|
|
9532
|
+
}
|
|
9533
|
+
const certificate = await verifyDeviceCredential(options.credential);
|
|
9534
|
+
if (certificate.signPublicKey !== normaliseHex(options.devicePublicKey)) {
|
|
9535
|
+
throw new Error(
|
|
9536
|
+
`this credential certifies device key ${certificate.signPublicKey}, but this app holds ${normaliseHex(options.devicePublicKey)} \u2014 it would sign with a key the certificate does not cover`
|
|
9537
|
+
);
|
|
9538
|
+
}
|
|
9539
|
+
if (options.kemPublicKey !== void 0 && certificate.kemPublicKey !== normaliseHex(options.kemPublicKey)) {
|
|
9540
|
+
throw new Error(
|
|
9541
|
+
`this credential names delivery key ${certificate.kemPublicKey}, not the one this app asked about \u2014 group keys would be sealed to somebody else`
|
|
9542
|
+
);
|
|
9543
|
+
}
|
|
9544
|
+
if (certificate.account !== normaliseHex(options.account)) {
|
|
9545
|
+
throw new Error(
|
|
9546
|
+
`the wallet reported account ${normaliseHex(options.account)}, but the credential names ${certificate.account}`
|
|
9547
|
+
);
|
|
9548
|
+
}
|
|
9549
|
+
if (certificate.device !== normaliseHex(options.device)) {
|
|
9550
|
+
throw new Error(
|
|
9551
|
+
`the wallet reported device ${normaliseHex(options.device)}, but the credential names ${certificate.device}`
|
|
9552
|
+
);
|
|
9553
|
+
}
|
|
9554
|
+
return {
|
|
9555
|
+
credential: options.credential,
|
|
9556
|
+
account: certificate.account,
|
|
9557
|
+
device: certificate.device,
|
|
9558
|
+
certificate
|
|
9559
|
+
};
|
|
9560
|
+
}
|
|
9561
|
+
function assertHex(value, label2) {
|
|
9562
|
+
if (!/^[0-9a-f]{64}$/.test(value)) {
|
|
9563
|
+
throw new Error(`${label2} must be 64 lowercase hex characters`);
|
|
9564
|
+
}
|
|
9565
|
+
}
|
|
9566
|
+
function normaliseHex(value) {
|
|
9567
|
+
return value.trim().toLowerCase();
|
|
9568
|
+
}
|
|
9569
|
+
function callbackIsForeign(returnTo, pageOrigin) {
|
|
9570
|
+
const here = pageOrigin ?? (typeof window === "undefined" ? void 0 : window.location.origin);
|
|
9571
|
+
if (!here) return false;
|
|
9572
|
+
try {
|
|
9573
|
+
return new URL(returnTo).origin !== here;
|
|
9574
|
+
} catch {
|
|
9575
|
+
return false;
|
|
9576
|
+
}
|
|
9577
|
+
}
|
|
9578
|
+
function warnOnForeignCallback(returnTo) {
|
|
9579
|
+
if (!callbackIsForeign(returnTo)) return;
|
|
9580
|
+
console.warn(
|
|
9581
|
+
`[calimero] enrolling a device but sending the credential to ${new URL(returnTo).origin}, which is not this page's origin. Legitimate when deliberate; worth a second look otherwise.`
|
|
9582
|
+
);
|
|
9583
|
+
}
|
|
9584
|
+
function assertSafeReturnTo(returnTo) {
|
|
9585
|
+
let url;
|
|
9586
|
+
try {
|
|
9587
|
+
url = new URL(returnTo);
|
|
9588
|
+
} catch {
|
|
9589
|
+
throw new Error(`returnTo must be an absolute URL, got ${returnTo}`);
|
|
9590
|
+
}
|
|
9591
|
+
const loopback = url.hostname === "localhost" || url.hostname === "127.0.0.1" || url.hostname === "[::1]" || url.hostname === "::1";
|
|
9592
|
+
if (url.protocol === "https:") return;
|
|
9593
|
+
if (url.protocol === "http:" && loopback) return;
|
|
9594
|
+
throw new Error(
|
|
9595
|
+
`returnTo must be https:, or http: on localhost \u2014 got ${url.protocol}//${url.hostname}`
|
|
9596
|
+
);
|
|
9597
|
+
}
|
|
9598
|
+
|
|
8230
9599
|
// src/sealed/verify.ts
|
|
8231
9600
|
var MEASUREMENT_HEX = /^[0-9a-f]{96}$/;
|
|
8232
9601
|
var REGISTERS = ["mrtd", "rtmr0", "rtmr1", "rtmr2", "rtmr3"];
|
|
@@ -8595,7 +9964,7 @@ var FULCIO_ISSUER_V1 = "1.3.6.1.4.1.57264.1.1";
|
|
|
8595
9964
|
var SAN_URI = 134;
|
|
8596
9965
|
var UTF8_STRING = 12;
|
|
8597
9966
|
var SEQUENCE2 = 48;
|
|
8598
|
-
var
|
|
9967
|
+
var utf83 = new TextEncoder();
|
|
8599
9968
|
var fromUtf8 = (bytes) => new TextDecoder().decode(bytes);
|
|
8600
9969
|
async function verifyCosignBundle(body, bundle, signer, signature) {
|
|
8601
9970
|
const { base64Signature, cert, rekorBundle } = cosignBundle(bundle);
|
|
@@ -8608,7 +9977,7 @@ async function verifyCosignBundle(body, bundle, signer, signature) {
|
|
|
8608
9977
|
const leaf = parseCertificate(leafDer);
|
|
8609
9978
|
const log = REKOR_LOGS.find((candidate) => candidate.logId === payload.logID.toLowerCase());
|
|
8610
9979
|
if (!log) throw new Error(`The bundle names Rekor log ${payload.logID}, which the Sigstore trust root does not know`);
|
|
8611
|
-
const canonical =
|
|
9980
|
+
const canonical = utf83.encode(
|
|
8612
9981
|
`{"body":${JSON.stringify(payload.body)},"integratedTime":${payload.integratedTime},"logID":${JSON.stringify(payload.logID)},"logIndex":${payload.logIndex}}`
|
|
8613
9982
|
);
|
|
8614
9983
|
const setVerifies = await verifyEcdsa(
|
|
@@ -9003,186 +10372,22 @@ async function signMemberJoinOp(input) {
|
|
|
9003
10372
|
credential
|
|
9004
10373
|
);
|
|
9005
10374
|
const op = concat(new Uint8Array([NAMESPACE_OP_ROOT]), rootOp);
|
|
9006
|
-
const
|
|
9007
|
-
const
|
|
10375
|
+
const signingKey = await resolveSigner(input.deviceSecret, input.signer, "deviceSecret");
|
|
10376
|
+
const signerPublicKey = fromHex(signingKey.publicKey, "signer.publicKey", 32);
|
|
9008
10377
|
const signable = concat(
|
|
9009
10378
|
new Uint8Array([SIGNED_NAMESPACE_OP_SCHEMA_VERSION]),
|
|
9010
10379
|
fromHex(input.namespaceId, "namespaceId", 32),
|
|
9011
10380
|
u32le(parents.length),
|
|
9012
10381
|
...parents.map((p, i) => fromHex(p, `parentOpHashes[${i}]`, 32)),
|
|
9013
|
-
|
|
10382
|
+
signerPublicKey,
|
|
9014
10383
|
u64le(input.nonce),
|
|
9015
10384
|
op
|
|
9016
10385
|
);
|
|
9017
|
-
const signature =
|
|
9018
|
-
await crypto.subtle.sign("Ed25519", key, concat(NAMESPACE_SIGN_DOMAIN, signable))
|
|
9019
|
-
);
|
|
10386
|
+
const signature = await signingKey.sign(concat(NAMESPACE_SIGN_DOMAIN, signable));
|
|
9020
10387
|
const unendorsed = new Uint8Array([0]);
|
|
9021
10388
|
return hex(concat(signable, signature, unendorsed));
|
|
9022
10389
|
}
|
|
9023
10390
|
|
|
9024
|
-
// src/login/login.ts
|
|
9025
|
-
var SIGN_DOMAIN5 = new TextEncoder().encode("calimero.auth.login.v1");
|
|
9026
|
-
var AUDIENCE_TAG = {
|
|
9027
|
-
webOrigin: 0,
|
|
9028
|
-
codeSigningId: 1,
|
|
9029
|
-
cli: 2
|
|
9030
|
-
};
|
|
9031
|
-
function audienceBody(audience) {
|
|
9032
|
-
const encoder = new TextEncoder();
|
|
9033
|
-
switch (audience.kind) {
|
|
9034
|
-
case "webOrigin":
|
|
9035
|
-
return encoder.encode(audience.origin);
|
|
9036
|
-
case "codeSigningId":
|
|
9037
|
-
return encoder.encode(audience.id);
|
|
9038
|
-
case "cli":
|
|
9039
|
-
return new Uint8Array(0);
|
|
9040
|
-
}
|
|
9041
|
-
}
|
|
9042
|
-
function audienceSigningBytes(audience) {
|
|
9043
|
-
return concat(
|
|
9044
|
-
Uint8Array.from([AUDIENCE_TAG[audience.kind]]),
|
|
9045
|
-
audienceBody(audience)
|
|
9046
|
-
);
|
|
9047
|
-
}
|
|
9048
|
-
function audienceWire(audience) {
|
|
9049
|
-
const tag2 = Uint8Array.from([AUDIENCE_TAG[audience.kind]]);
|
|
9050
|
-
if (audience.kind === "cli") {
|
|
9051
|
-
return tag2;
|
|
9052
|
-
}
|
|
9053
|
-
const body = audienceBody(audience);
|
|
9054
|
-
return concat(tag2, u32le(body.length), body);
|
|
9055
|
-
}
|
|
9056
|
-
async function signLoginStatement(input) {
|
|
9057
|
-
const node = fromHex(input.node, "node", 32);
|
|
9058
|
-
const challenge = fromHex(input.challenge, "challenge", 32);
|
|
9059
|
-
const sessionKey = fromHex(input.sessionKey, "sessionKey", 32);
|
|
9060
|
-
const key = await importSigningKey(input.deviceSecret);
|
|
9061
|
-
const deviceKey = await derivePublicKey(input.deviceSecret);
|
|
9062
|
-
const issuedAt = u64le(input.issuedAt);
|
|
9063
|
-
const expiresAt = u64le(input.expiresAt);
|
|
9064
|
-
const preimage = await domainHash(SIGN_DOMAIN5, [
|
|
9065
|
-
node,
|
|
9066
|
-
audienceSigningBytes(input.audience),
|
|
9067
|
-
challenge,
|
|
9068
|
-
sessionKey,
|
|
9069
|
-
deviceKey,
|
|
9070
|
-
issuedAt,
|
|
9071
|
-
expiresAt
|
|
9072
|
-
]);
|
|
9073
|
-
const signature = new Uint8Array(
|
|
9074
|
-
await crypto.subtle.sign({ name: "Ed25519" }, key, preimage)
|
|
9075
|
-
);
|
|
9076
|
-
return hex(
|
|
9077
|
-
concat(
|
|
9078
|
-
node,
|
|
9079
|
-
audienceWire(input.audience),
|
|
9080
|
-
challenge,
|
|
9081
|
-
sessionKey,
|
|
9082
|
-
deviceKey,
|
|
9083
|
-
issuedAt,
|
|
9084
|
-
expiresAt,
|
|
9085
|
-
signature
|
|
9086
|
-
)
|
|
9087
|
-
);
|
|
9088
|
-
}
|
|
9089
|
-
|
|
9090
|
-
// src/login/session.ts
|
|
9091
|
-
var DEFAULT_TTL_SECONDS2 = 300;
|
|
9092
|
-
async function generateSessionKey() {
|
|
9093
|
-
const secret = hex(crypto.getRandomValues(new Uint8Array(32)));
|
|
9094
|
-
return { publicKey: hex(await derivePublicKey(secret)), secret };
|
|
9095
|
-
}
|
|
9096
|
-
function audienceLabel(audience) {
|
|
9097
|
-
switch (audience.kind) {
|
|
9098
|
-
case "webOrigin":
|
|
9099
|
-
return audience.origin;
|
|
9100
|
-
case "codeSigningId":
|
|
9101
|
-
return audience.id;
|
|
9102
|
-
case "cli":
|
|
9103
|
-
return "cli";
|
|
9104
|
-
}
|
|
9105
|
-
}
|
|
9106
|
-
async function login(config) {
|
|
9107
|
-
const baseUrl = config.nodeUrl.replace(/\/+$/, "");
|
|
9108
|
-
const timeoutMs = config.timeoutMs ?? 1e4;
|
|
9109
|
-
const request = async (path, init) => {
|
|
9110
|
-
const controller = new AbortController();
|
|
9111
|
-
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
9112
|
-
let response;
|
|
9113
|
-
try {
|
|
9114
|
-
const requestInit = { ...init, signal: controller.signal };
|
|
9115
|
-
response = config.fetch ? await config.fetch(`${baseUrl}${path}`, requestInit) : await globalThis.fetch(`${baseUrl}${path}`, requestInit);
|
|
9116
|
-
} catch (err) {
|
|
9117
|
-
throw new HTTPError(
|
|
9118
|
-
0,
|
|
9119
|
-
err instanceof Error ? err.name : "NetworkError",
|
|
9120
|
-
`${baseUrl}${path}`,
|
|
9121
|
-
new Headers(),
|
|
9122
|
-
err instanceof Error ? err.message : String(err)
|
|
9123
|
-
);
|
|
9124
|
-
} finally {
|
|
9125
|
-
clearTimeout(timer);
|
|
9126
|
-
}
|
|
9127
|
-
const text = await response.text().catch(() => "");
|
|
9128
|
-
if (!response.ok) {
|
|
9129
|
-
throw new HTTPError(
|
|
9130
|
-
response.status,
|
|
9131
|
-
response.statusText,
|
|
9132
|
-
`${baseUrl}${path}`,
|
|
9133
|
-
response.headers,
|
|
9134
|
-
text
|
|
9135
|
-
);
|
|
9136
|
-
}
|
|
9137
|
-
return text ? JSON.parse(text) : {};
|
|
9138
|
-
};
|
|
9139
|
-
const challengeBody = await request("/auth/challenge", {
|
|
9140
|
-
method: "GET",
|
|
9141
|
-
headers: { Accept: "application/json" }
|
|
9142
|
-
});
|
|
9143
|
-
const challenge = challengeBody.challenge ?? challengeBody.data?.challenge;
|
|
9144
|
-
if (!challenge) {
|
|
9145
|
-
throw new Error("the node issued no challenge");
|
|
9146
|
-
}
|
|
9147
|
-
const session = await generateSessionKey();
|
|
9148
|
-
const issuedAt = Math.floor(Date.now() / 1e3);
|
|
9149
|
-
const statement = await signLoginStatement({
|
|
9150
|
-
node: config.node,
|
|
9151
|
-
audience: config.audience,
|
|
9152
|
-
challenge,
|
|
9153
|
-
sessionKey: session.publicKey,
|
|
9154
|
-
issuedAt,
|
|
9155
|
-
expiresAt: issuedAt + (config.ttlSeconds ?? DEFAULT_TTL_SECONDS2),
|
|
9156
|
-
deviceSecret: config.deviceSecret
|
|
9157
|
-
});
|
|
9158
|
-
const tokenBody = await request("/auth/token", {
|
|
9159
|
-
method: "POST",
|
|
9160
|
-
headers: { "Content-Type": "application/json", Accept: "application/json" },
|
|
9161
|
-
body: JSON.stringify({
|
|
9162
|
-
auth_method: "account_proof",
|
|
9163
|
-
public_key: session.publicKey,
|
|
9164
|
-
client_name: config.clientName ?? audienceLabel(config.audience),
|
|
9165
|
-
timestamp: issuedAt,
|
|
9166
|
-
provider_data: {
|
|
9167
|
-
challenge,
|
|
9168
|
-
login_statement: statement,
|
|
9169
|
-
account_proof: config.accountProof
|
|
9170
|
-
}
|
|
9171
|
-
})
|
|
9172
|
-
});
|
|
9173
|
-
const accessToken = tokenBody.access_token ?? tokenBody.data?.access_token;
|
|
9174
|
-
const refreshToken = tokenBody.refresh_token ?? tokenBody.data?.refresh_token;
|
|
9175
|
-
if (!accessToken) {
|
|
9176
|
-
throw new Error("the node minted no session");
|
|
9177
|
-
}
|
|
9178
|
-
return {
|
|
9179
|
-
accessToken,
|
|
9180
|
-
refreshToken: refreshToken ?? "",
|
|
9181
|
-
sessionKey: session.publicKey,
|
|
9182
|
-
sessionSecret: session.secret
|
|
9183
|
-
};
|
|
9184
|
-
}
|
|
9185
|
-
|
|
9186
10391
|
// src/nodeDiscovery.ts
|
|
9187
10392
|
var DEFAULT_LOCAL_NODE_PORTS = [2428, 2429, 2528, 2529];
|
|
9188
10393
|
var LOCAL_HOST = "localhost";
|
|
@@ -9235,4 +10440,100 @@ async function discoverLocalNodes(options = {}) {
|
|
|
9235
10440
|
);
|
|
9236
10441
|
return results.filter((url) => url !== null);
|
|
9237
10442
|
}
|
|
10443
|
+
|
|
10444
|
+
// src/relay-attestation/attest-node-key.ts
|
|
10445
|
+
var KEY_BINDING_DOMAIN = new TextEncoder().encode("calimero.tee-attest.key-binding.v1");
|
|
10446
|
+
var MOCK_QUOTE_HEADER = new TextEncoder().encode("MOCK_TDX_QUOTE_V1");
|
|
10447
|
+
var TDX_REPORT_DATA_OFFSET = 48 + 520;
|
|
10448
|
+
function concat2(...parts) {
|
|
10449
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
10450
|
+
let at = 0;
|
|
10451
|
+
for (const p of parts) {
|
|
10452
|
+
out.set(p, at);
|
|
10453
|
+
at += p.length;
|
|
10454
|
+
}
|
|
10455
|
+
return out;
|
|
10456
|
+
}
|
|
10457
|
+
function equal2(a, b) {
|
|
10458
|
+
if (a.length !== b.length) return false;
|
|
10459
|
+
let diff = 0;
|
|
10460
|
+
for (let i = 0; i < a.length; i += 1) diff |= a[i] ^ b[i];
|
|
10461
|
+
return diff === 0;
|
|
10462
|
+
}
|
|
10463
|
+
function fromBase643(b64) {
|
|
10464
|
+
const bin = atob(b64);
|
|
10465
|
+
const out = new Uint8Array(bin.length);
|
|
10466
|
+
for (let i = 0; i < bin.length; i += 1) out[i] = bin.charCodeAt(i);
|
|
10467
|
+
return out;
|
|
10468
|
+
}
|
|
10469
|
+
function fromHex32(value, label2) {
|
|
10470
|
+
if (typeof value !== "string" || !/^[0-9a-fA-F]{64}$/.test(value)) {
|
|
10471
|
+
throw new Error(`${label2} must be 64 hex characters`);
|
|
10472
|
+
}
|
|
10473
|
+
const out = new Uint8Array(32);
|
|
10474
|
+
for (let i = 0; i < 32; i += 1) out[i] = Number.parseInt(value.slice(i * 2, i * 2 + 2), 16);
|
|
10475
|
+
return out;
|
|
10476
|
+
}
|
|
10477
|
+
async function attestKeyBinding(nodeKey) {
|
|
10478
|
+
const digest = await crypto.subtle.digest(
|
|
10479
|
+
"SHA-256",
|
|
10480
|
+
concat2(KEY_BINDING_DOMAIN, new Uint8Array(32), nodeKey)
|
|
10481
|
+
);
|
|
10482
|
+
return new Uint8Array(digest);
|
|
10483
|
+
}
|
|
10484
|
+
function reportDataOf(quote) {
|
|
10485
|
+
const mock = quote.length >= MOCK_QUOTE_HEADER.length && equal2(quote.subarray(0, MOCK_QUOTE_HEADER.length), MOCK_QUOTE_HEADER);
|
|
10486
|
+
const at = mock ? MOCK_QUOTE_HEADER.length : TDX_REPORT_DATA_OFFSET;
|
|
10487
|
+
if (quote.length < at + 64) {
|
|
10488
|
+
throw new Error(`the quote is too short to carry report data (${quote.length} bytes)`);
|
|
10489
|
+
}
|
|
10490
|
+
return { reportData: quote.slice(at, at + 64), mock };
|
|
10491
|
+
}
|
|
10492
|
+
async function attestRelayNodeKey(opts) {
|
|
10493
|
+
const nonce2 = crypto.getRandomValues(new Uint8Array(32));
|
|
10494
|
+
const url = `${opts.relayUrl.replace(/\/+$/, "")}/admin-api/tee/attest`;
|
|
10495
|
+
const body = JSON.stringify({
|
|
10496
|
+
nonce: hex(nonce2),
|
|
10497
|
+
bindNodeKey: true,
|
|
10498
|
+
...opts.verify?.includeCollateral ? { includeCollateral: true } : {}
|
|
10499
|
+
});
|
|
10500
|
+
const init = { method: "POST", headers: { "Content-Type": "application/json" }, body };
|
|
10501
|
+
const response = opts.fetch ? await opts.fetch(url, init) : await globalThis.fetch(url, init);
|
|
10502
|
+
if (!response.ok) {
|
|
10503
|
+
const text = await response.text().catch(() => "");
|
|
10504
|
+
throw new Error(`the relay would not attest (HTTP ${response.status})${text ? `: ${text}` : ""}`);
|
|
10505
|
+
}
|
|
10506
|
+
const parsed = await response.json();
|
|
10507
|
+
const data = parsed.data ?? parsed;
|
|
10508
|
+
if (typeof data.quoteB64 !== "string") throw new Error("the relay returned no quote");
|
|
10509
|
+
const nodeKey = fromHex32(data.boundPublicKey, "boundPublicKey");
|
|
10510
|
+
const quote = fromBase643(data.quoteB64);
|
|
10511
|
+
const { reportData, mock } = reportDataOf(quote);
|
|
10512
|
+
if (mock) {
|
|
10513
|
+
if (!opts.allowMock) {
|
|
10514
|
+
throw new Error("the relay answered with a MOCK quote, which proves nothing about the hardware");
|
|
10515
|
+
}
|
|
10516
|
+
} else if (opts.verify) {
|
|
10517
|
+
const accepted = await opts.verify({
|
|
10518
|
+
quoteB64: data.quoteB64,
|
|
10519
|
+
nonce: hex(nonce2),
|
|
10520
|
+
reportDataSuffix: hex(await attestKeyBinding(nodeKey)),
|
|
10521
|
+
...data.collateral ? { collateral: data.collateral } : {}
|
|
10522
|
+
});
|
|
10523
|
+
if (!accepted) {
|
|
10524
|
+
throw new Error("the relay's quote did not verify: not a trusted image, or not bound to this request");
|
|
10525
|
+
}
|
|
10526
|
+
} else if (opts.verifyQuote) {
|
|
10527
|
+
await opts.verifyQuote(quote);
|
|
10528
|
+
} else {
|
|
10529
|
+
throw new Error("a real TDX quote needs a verify (or verifyQuote) to check its signature and measurements");
|
|
10530
|
+
}
|
|
10531
|
+
if (!equal2(reportData.subarray(0, 32), nonce2)) {
|
|
10532
|
+
throw new Error("the quote does not carry our nonce: it was not made for this request");
|
|
10533
|
+
}
|
|
10534
|
+
if (!equal2(reportData.subarray(32, 64), await attestKeyBinding(nodeKey))) {
|
|
10535
|
+
throw new Error("the quote does not bind the key the relay named: refusing it");
|
|
10536
|
+
}
|
|
10537
|
+
return { nodeKey: hex(nodeKey), mock };
|
|
10538
|
+
}
|
|
9238
10539
|
//# sourceMappingURL=index.cjs.map
|