@dakota-xyz/ts-sdk 3.2.0 → 3.4.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/README.md +95 -4
- package/dist/governance/index.d.mts +315 -0
- package/dist/governance/index.d.ts +315 -0
- package/dist/governance/index.js +379 -0
- package/dist/governance/index.js.map +1 -0
- package/dist/governance/index.mjs +365 -0
- package/dist/governance/index.mjs.map +1 -0
- package/dist/index.d.mts +983 -105
- package/dist/index.d.ts +983 -105
- package/dist/index.js +872 -123
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +852 -122
- package/dist/index.mjs.map +1 -1
- package/dist/webhook/index.d.mts +3 -2
- package/dist/webhook/index.d.ts +3 -2
- package/dist/webhook/index.js +1 -0
- package/dist/webhook/index.js.map +1 -1
- package/dist/webhook/index.mjs +1 -0
- package/dist/webhook/index.mjs.map +1 -1
- package/package.json +15 -3
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical intents for signer-group governance changes.
|
|
3
|
+
*
|
|
4
|
+
* A change to a governed signer group (adding or removing members, changing
|
|
5
|
+
* who governs it) needs signatures from the group's governing members. Each
|
|
6
|
+
* signature covers the RFC 8785 (JCS) canonical JSON of an "intent" that the
|
|
7
|
+
* policy engine rebuilds from the request and checks byte-for-byte. A wrong
|
|
8
|
+
* key, a missing `signer_group_version`, or an idempotency key that differs
|
|
9
|
+
* from the `X-Idempotency-Key` header gives a signature that never verifies,
|
|
10
|
+
* so build the intent here rather than by hand.
|
|
11
|
+
*
|
|
12
|
+
* The builders are pure (no I/O, no `node:` import), so they run in a
|
|
13
|
+
* browser or mobile app as well as on a server. The byte equality with the
|
|
14
|
+
* policy engine is pinned by golden vectors in the test suite.
|
|
15
|
+
*/
|
|
16
|
+
/** `add_signers_to_group`: endorses adding members to a signer group. */
|
|
17
|
+
interface AddSignersToGroupIntent {
|
|
18
|
+
readonly type: 'add_signers_to_group';
|
|
19
|
+
readonly signer_group_id: string;
|
|
20
|
+
readonly new_member_keys: readonly string[];
|
|
21
|
+
readonly signer_group_version: number;
|
|
22
|
+
readonly idempotency_key: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* `remove_signers_from_group`: endorses removing members. Note the group id
|
|
26
|
+
* key is `group_id` here, not `signer_group_id`.
|
|
27
|
+
*/
|
|
28
|
+
interface RemoveSignersFromGroupIntent {
|
|
29
|
+
readonly type: 'remove_signers_from_group';
|
|
30
|
+
readonly group_id: string;
|
|
31
|
+
readonly members_to_remove: readonly string[];
|
|
32
|
+
readonly signer_group_version: number;
|
|
33
|
+
readonly idempotency_key: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* `update_signer_group_governance`: endorses replacing a group's governance
|
|
37
|
+
* settings. An optional setting is ABSENT when not set (never null), and an
|
|
38
|
+
* absent setting is cleared by the change.
|
|
39
|
+
*/
|
|
40
|
+
interface UpdateSignerGroupGovernanceIntent {
|
|
41
|
+
readonly type: 'update_signer_group_governance';
|
|
42
|
+
readonly signer_group_id: string;
|
|
43
|
+
readonly signer_group_version: number;
|
|
44
|
+
readonly idempotency_key: string;
|
|
45
|
+
readonly governance_threshold?: number;
|
|
46
|
+
readonly governance_signer_group_id?: string;
|
|
47
|
+
}
|
|
48
|
+
/** `create_crypto_destination`: endorses creating a crypto destination. */
|
|
49
|
+
interface CreateCryptoDestinationIntent {
|
|
50
|
+
readonly type: 'create_crypto_destination';
|
|
51
|
+
readonly recipient_id: string;
|
|
52
|
+
readonly network_id: string;
|
|
53
|
+
readonly address: string;
|
|
54
|
+
readonly signer_group_id: string;
|
|
55
|
+
readonly idempotency_key: string;
|
|
56
|
+
}
|
|
57
|
+
/** Any intent this module builds. */
|
|
58
|
+
type GovernanceIntent = AddSignersToGroupIntent | RemoveSignersFromGroupIntent | UpdateSignerGroupGovernanceIntent | CreateCryptoDestinationIntent;
|
|
59
|
+
/**
|
|
60
|
+
* An intent together with the exact bytes its signatures cover.
|
|
61
|
+
*
|
|
62
|
+
* - `canonical` is the canonical JSON as a string (handy to store or log).
|
|
63
|
+
* - `bytes` is its UTF-8 encoding: what an ES256 signer signs, and what a
|
|
64
|
+
* WebAuthn `navigator.credentials.get()` call takes as its `challenge`.
|
|
65
|
+
*/
|
|
66
|
+
interface CanonicalIntent<T extends GovernanceIntent = GovernanceIntent> {
|
|
67
|
+
readonly intent: T;
|
|
68
|
+
readonly canonical: string;
|
|
69
|
+
readonly bytes: Uint8Array;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Check that `key` is a lowercase UUID, the only idempotency key the platform
|
|
73
|
+
* accepts on these endpoints. The same key goes into the intent and the
|
|
74
|
+
* `X-Idempotency-Key` header, so an uppercase key would be refused even
|
|
75
|
+
* though its signature is otherwise valid.
|
|
76
|
+
*
|
|
77
|
+
* @throws TypeError when it is not.
|
|
78
|
+
*/
|
|
79
|
+
declare function assertIdempotencyKey(key: string): void;
|
|
80
|
+
/**
|
|
81
|
+
* A fresh lowercase v4 UUID for a governance change, from the platform's
|
|
82
|
+
* cryptographic random source (`crypto.randomUUID`, else
|
|
83
|
+
* `crypto.getRandomValues`).
|
|
84
|
+
*/
|
|
85
|
+
declare function newIdempotencyKey(): string;
|
|
86
|
+
/**
|
|
87
|
+
* Intent for adding members to a signer group
|
|
88
|
+
* (`POST /signer-groups/{id}/signers`). That endpoint adds one `member_key`
|
|
89
|
+
* per request, so pass `[memberKey]`.
|
|
90
|
+
*
|
|
91
|
+
* @param params.signerGroupVersion - the group's current `version` (0 is valid
|
|
92
|
+
* and is signed as 0)
|
|
93
|
+
* @param params.idempotencyKey - a lowercase UUID; send the SAME value as the
|
|
94
|
+
* request's `X-Idempotency-Key`
|
|
95
|
+
*/
|
|
96
|
+
declare function addSignersToGroupIntent(params: {
|
|
97
|
+
signerGroupId: string;
|
|
98
|
+
newMemberKeys: readonly string[];
|
|
99
|
+
signerGroupVersion: number;
|
|
100
|
+
idempotencyKey: string;
|
|
101
|
+
}): CanonicalIntent<AddSignersToGroupIntent>;
|
|
102
|
+
/**
|
|
103
|
+
* Intent for removing members from a signer group
|
|
104
|
+
* (`POST /signer-groups/{id}/signer-removals`). `membersToRemove` is signed
|
|
105
|
+
* in the order given, which must be the order sent.
|
|
106
|
+
*/
|
|
107
|
+
declare function removeSignersFromGroupIntent(params: {
|
|
108
|
+
signerGroupId: string;
|
|
109
|
+
membersToRemove: readonly string[];
|
|
110
|
+
signerGroupVersion: number;
|
|
111
|
+
idempotencyKey: string;
|
|
112
|
+
}): CanonicalIntent<RemoveSignersFromGroupIntent>;
|
|
113
|
+
/**
|
|
114
|
+
* Intent for replacing a signer group's governance settings
|
|
115
|
+
* (`PATCH /signer-groups/{id}/governance`).
|
|
116
|
+
*
|
|
117
|
+
* The change replaces the WHOLE governance state: leave a setting out (or
|
|
118
|
+
* pass null) to clear it, which the intent expresses by omitting the key.
|
|
119
|
+
* No `governanceThreshold` means the default quorum; no
|
|
120
|
+
* `governanceSignerGroupId` means the group governs itself.
|
|
121
|
+
*/
|
|
122
|
+
declare function updateSignerGroupGovernanceIntent(params: {
|
|
123
|
+
signerGroupId: string;
|
|
124
|
+
signerGroupVersion: number;
|
|
125
|
+
idempotencyKey: string;
|
|
126
|
+
governanceThreshold?: number | null;
|
|
127
|
+
governanceSignerGroupId?: string | null;
|
|
128
|
+
}): CanonicalIntent<UpdateSignerGroupGovernanceIntent>;
|
|
129
|
+
/**
|
|
130
|
+
* Intent for creating a crypto destination endorsed by a signer group's
|
|
131
|
+
* governance (`POST /recipients/{recipient_id}/destinations` with
|
|
132
|
+
* `destination_type: 'crypto'`, `signatures` and `signer_group_id`). Send
|
|
133
|
+
* `crypto_address` = `address` and the same `network_id`.
|
|
134
|
+
*/
|
|
135
|
+
declare function createCryptoDestinationIntent(params: {
|
|
136
|
+
recipientId: string;
|
|
137
|
+
networkId: string;
|
|
138
|
+
address: string;
|
|
139
|
+
signerGroupId: string;
|
|
140
|
+
idempotencyKey: string;
|
|
141
|
+
}): CanonicalIntent<CreateCryptoDestinationIntent>;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Pluggable signers for governance intents.
|
|
145
|
+
*
|
|
146
|
+
* A signer turns the intent's canonical bytes into one `signatures[]` entry.
|
|
147
|
+
* The SDK ships three:
|
|
148
|
+
* - ES256 on a server: `ES256Signer` (also exported as `P256MandateSigner`) in the
|
|
149
|
+
* main entry, or your own HSM/KMS behind this interface.
|
|
150
|
+
* - ES256 in a browser: {@link webCryptoES256Signer} over a WebCrypto key.
|
|
151
|
+
* - Passkey: `webauthnSigner` from this entry point.
|
|
152
|
+
*/
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Produces one `signatures[]` entry over a governance intent's canonical
|
|
156
|
+
* bytes. For ES256 that is base64 of the ASN.1 DER ECDSA signature over
|
|
157
|
+
* SHA-256(payload); for a passkey it is the packed assertion.
|
|
158
|
+
*/
|
|
159
|
+
interface GovernanceSigner {
|
|
160
|
+
sign(payload: Uint8Array): Promise<string> | string;
|
|
161
|
+
}
|
|
162
|
+
/** A {@link GovernanceSigner} or a bare signing function. */
|
|
163
|
+
type GovernanceSignerLike = GovernanceSigner | ((payload: Uint8Array) => Promise<string> | string);
|
|
164
|
+
/**
|
|
165
|
+
* Sign an intent with each signer, one after another (passkey prompts must
|
|
166
|
+
* not overlap), and return the signatures in signer order.
|
|
167
|
+
*
|
|
168
|
+
* @throws when `signers` is empty or a signer returns an empty signature
|
|
169
|
+
*/
|
|
170
|
+
declare function signGovernanceIntent(intent: CanonicalIntent | Uint8Array, signers: readonly GovernanceSignerLike[]): Promise<string[]>;
|
|
171
|
+
/**
|
|
172
|
+
* The part of WebCrypto's `SubtleCrypto` the ES256 signer uses. Declared
|
|
173
|
+
* structurally so the SDK does not need the DOM type library; a real
|
|
174
|
+
* `crypto.subtle` (browser, Deno, Node's `webcrypto.subtle`) satisfies it.
|
|
175
|
+
*/
|
|
176
|
+
interface SubtleCryptoLike {
|
|
177
|
+
sign(algorithm: {
|
|
178
|
+
name: 'ECDSA';
|
|
179
|
+
hash: 'SHA-256';
|
|
180
|
+
}, key: never, data: ArrayBuffer): Promise<ArrayBuffer>;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* An ES256 {@link GovernanceSigner} over a WebCrypto P-256 private key, for
|
|
184
|
+
* browsers and other runtimes without `node:crypto`. The key can be
|
|
185
|
+
* non-extractable. WebCrypto returns raw `r || s`; this converts it to the
|
|
186
|
+
* ASN.1 DER form the platform verifies.
|
|
187
|
+
*
|
|
188
|
+
* @param privateKey - an ECDSA P-256 `CryptoKey` with the `sign` usage
|
|
189
|
+
* (typed loosely so the SDK does not require the DOM type library)
|
|
190
|
+
* @param subtle - the SubtleCrypto to use (defaults to `globalThis.crypto.subtle`)
|
|
191
|
+
*/
|
|
192
|
+
declare function webCryptoES256Signer(privateKey: object, subtle?: SubtleCryptoLike): GovernanceSigner;
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* RFC 8785 (JCS) canonical JSON — the exact bytes the platform signs.
|
|
196
|
+
*
|
|
197
|
+
* A payload canonicalized here matches the platform's `enc.C14nJSON`
|
|
198
|
+
* (json.Marshal + jsoncanonicalizer.Transform) byte-for-byte, so a
|
|
199
|
+
* signature produced from the result verifies server-side.
|
|
200
|
+
*
|
|
201
|
+
* This implementation covers the value shapes the SDK ever puts into a
|
|
202
|
+
* signed payload: nulls, booleans, finite integers/floats, strings,
|
|
203
|
+
* arrays, and plain object maps. It intentionally does NOT handle
|
|
204
|
+
* non-integer numbers with fractional parts differently from
|
|
205
|
+
* JSON.stringify, because every mandate/endorsement payload the SDK
|
|
206
|
+
* produces uses only integers, strings, arrays, and object maps — decimal
|
|
207
|
+
* amounts are always sent as strings.
|
|
208
|
+
*/
|
|
209
|
+
/**
|
|
210
|
+
* RFC 8785 (JCS) canonicalize `value` and return its bytes as a UTF-8 string.
|
|
211
|
+
*
|
|
212
|
+
* @throws if `value` contains a non-finite number, a Symbol, a bigint, or a
|
|
213
|
+
* circular reference — any of which cannot be encoded to canonical JSON.
|
|
214
|
+
*/
|
|
215
|
+
declare function canonicalJSON(value: unknown): string;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Passkey (WebAuthn) signatures for governance intents.
|
|
219
|
+
*
|
|
220
|
+
* The platform verifies a passkey signature like this: the assertion's
|
|
221
|
+
* `clientDataJSON.challenge` must decode to the intent's canonical JSON
|
|
222
|
+
* itself (NOT a hash of it), and the authenticator's signature over
|
|
223
|
+
* `authenticatorData || SHA-256(clientDataJSON)` must verify against the
|
|
224
|
+
* signer's registered COSE key. The `signatures[]` entry is base64url of the
|
|
225
|
+
* `CredentialAssertionResponse` JSON.
|
|
226
|
+
*
|
|
227
|
+
* So the SDK does two small things and never touches the credential:
|
|
228
|
+
* - {@link buildChallenge} gives you the bytes to pass as
|
|
229
|
+
* `navigator.credentials.get({ publicKey: { challenge } })`.
|
|
230
|
+
* - {@link packAssertion} turns the returned credential into the
|
|
231
|
+
* `signatures[]` entry.
|
|
232
|
+
*/
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Binary data as WebAuthn APIs hand it out: an ArrayBuffer or typed array
|
|
236
|
+
* (browsers), or a base64 / base64url string (most React Native passkey
|
|
237
|
+
* libraries, and `PublicKeyCredential.toJSON()`).
|
|
238
|
+
*/
|
|
239
|
+
type BinaryOrBase64 = ArrayBuffer | ArrayBufferView | string;
|
|
240
|
+
/**
|
|
241
|
+
* The parts of a `PublicKeyCredential` (with an `AuthenticatorAssertionResponse`)
|
|
242
|
+
* that the platform reads. A browser's `navigator.credentials.get()` result
|
|
243
|
+
* satisfies it as-is: the DOM types its `response` as the base
|
|
244
|
+
* `AuthenticatorResponse`, so `authenticatorData` and `signature` are
|
|
245
|
+
* optional here and checked at run time by {@link packAssertion}.
|
|
246
|
+
*/
|
|
247
|
+
interface AssertionCredential {
|
|
248
|
+
readonly id: string;
|
|
249
|
+
readonly rawId: BinaryOrBase64;
|
|
250
|
+
readonly type: string;
|
|
251
|
+
readonly authenticatorAttachment?: string | null;
|
|
252
|
+
readonly response: {
|
|
253
|
+
readonly clientDataJSON: BinaryOrBase64;
|
|
254
|
+
readonly authenticatorData?: BinaryOrBase64;
|
|
255
|
+
readonly signature?: BinaryOrBase64;
|
|
256
|
+
readonly userHandle?: BinaryOrBase64 | null;
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* What a challenge can be built from: a built intent, anything else that
|
|
261
|
+
* carries the `canonical` string (such as a `PreparedSignerGroupChange`),
|
|
262
|
+
* the `canonical` string itself (what a server would send to the browser),
|
|
263
|
+
* or its bytes.
|
|
264
|
+
*/
|
|
265
|
+
type ChallengeSource = CanonicalIntent | {
|
|
266
|
+
readonly canonical: string;
|
|
267
|
+
} | string | Uint8Array | ArrayBuffer;
|
|
268
|
+
/**
|
|
269
|
+
* The WebAuthn challenge for an intent: its raw canonical JSON bytes, as a
|
|
270
|
+
* fresh `ArrayBuffer`. Pass it as `publicKey.challenge`; the browser
|
|
271
|
+
* base64url-encodes it into `clientDataJSON`, which is exactly what the
|
|
272
|
+
* platform expects. (An `ArrayBuffer` is a `BufferSource` under every
|
|
273
|
+
* TypeScript version, unlike a `Uint8Array` on TypeScript 5.7+.)
|
|
274
|
+
*
|
|
275
|
+
* @throws TypeError when `intent` is none of the {@link ChallengeSource} forms
|
|
276
|
+
*/
|
|
277
|
+
declare function buildChallenge(intent: ChallengeSource): ArrayBuffer;
|
|
278
|
+
/**
|
|
279
|
+
* Package a passkey assertion as a `signatures[]` entry: base64url of the
|
|
280
|
+
* `CredentialAssertionResponse` JSON
|
|
281
|
+
* (`{id, rawId, type, authenticatorAttachment?, clientExtensionResults, response: {clientDataJSON, authenticatorData, signature, userHandle?}}`,
|
|
282
|
+
* every binary field base64url).
|
|
283
|
+
*
|
|
284
|
+
* @param credential - what `navigator.credentials.get()` returned (or a
|
|
285
|
+
* mobile passkey library's equivalent)
|
|
286
|
+
* @throws TypeError when it is not a public-key assertion or a field is
|
|
287
|
+
* missing
|
|
288
|
+
*/
|
|
289
|
+
declare function packAssertion(credential: AssertionCredential): string;
|
|
290
|
+
/**
|
|
291
|
+
* Whether a packed passkey signature was made over `intent` — its challenge
|
|
292
|
+
* equals the intent's canonical bytes. A cheap server-side check of what a
|
|
293
|
+
* browser sent back before you submit it. It does NOT verify the signature
|
|
294
|
+
* itself; the platform does that. Returns false for anything that is not a
|
|
295
|
+
* packed assertion, including one longer than 64 KiB.
|
|
296
|
+
*/
|
|
297
|
+
declare function assertionChallengeMatches(packed: string, intent: ChallengeSource): boolean;
|
|
298
|
+
/**
|
|
299
|
+
* A {@link GovernanceSigner} backed by a passkey. `getAssertion` receives
|
|
300
|
+
* the challenge and returns the credential; in a browser:
|
|
301
|
+
*
|
|
302
|
+
* ```typescript
|
|
303
|
+
* const signer = webauthnSigner((challenge) =>
|
|
304
|
+
* navigator.credentials.get({
|
|
305
|
+
* publicKey: { challenge, rpId: 'app.example.com', userVerification: 'required' },
|
|
306
|
+
* }) as Promise<PublicKeyCredential>
|
|
307
|
+
* );
|
|
308
|
+
* ```
|
|
309
|
+
*
|
|
310
|
+
* Every call prompts the user, so do not use it with automatic stale-version
|
|
311
|
+
* retries unless a second prompt is acceptable.
|
|
312
|
+
*/
|
|
313
|
+
declare function webauthnSigner(getAssertion: (challenge: ArrayBuffer) => Promise<AssertionCredential> | AssertionCredential): GovernanceSigner;
|
|
314
|
+
|
|
315
|
+
export { type AddSignersToGroupIntent, type AssertionCredential, type BinaryOrBase64, type CanonicalIntent, type ChallengeSource, type CreateCryptoDestinationIntent, type GovernanceIntent, type GovernanceSigner, type GovernanceSignerLike, type RemoveSignersFromGroupIntent, type SubtleCryptoLike, type UpdateSignerGroupGovernanceIntent, addSignersToGroupIntent, assertIdempotencyKey, assertionChallengeMatches, buildChallenge, canonicalJSON, createCryptoDestinationIntent, newIdempotencyKey, packAssertion, removeSignersFromGroupIntent, signGovernanceIntent, updateSignerGroupGovernanceIntent, webCryptoES256Signer, webauthnSigner };
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/agentic/canonicalize.ts
|
|
4
|
+
function canonicalJSON(value) {
|
|
5
|
+
return canonicalize(value);
|
|
6
|
+
}
|
|
7
|
+
function canonicalize(value) {
|
|
8
|
+
if (value === null || value === void 0) {
|
|
9
|
+
return "null";
|
|
10
|
+
}
|
|
11
|
+
if (typeof value === "boolean") {
|
|
12
|
+
return value ? "true" : "false";
|
|
13
|
+
}
|
|
14
|
+
if (typeof value === "number") {
|
|
15
|
+
if (!Number.isFinite(value)) {
|
|
16
|
+
throw new TypeError(`canonical JSON: non-finite number ${value}`);
|
|
17
|
+
}
|
|
18
|
+
return JSON.stringify(value);
|
|
19
|
+
}
|
|
20
|
+
if (typeof value === "string") {
|
|
21
|
+
return JSON.stringify(value);
|
|
22
|
+
}
|
|
23
|
+
if (typeof value === "bigint") {
|
|
24
|
+
throw new TypeError("canonical JSON: bigint values are not supported");
|
|
25
|
+
}
|
|
26
|
+
if (Array.isArray(value)) {
|
|
27
|
+
const parts = value.map((item) => canonicalize(item));
|
|
28
|
+
return `[${parts.join(",")}]`;
|
|
29
|
+
}
|
|
30
|
+
if (typeof value === "object") {
|
|
31
|
+
const obj = value;
|
|
32
|
+
const keys = Object.keys(obj).sort();
|
|
33
|
+
const parts = keys.map((key) => `${JSON.stringify(key)}:${canonicalize(obj[key])}`);
|
|
34
|
+
return `{${parts.join(",")}}`;
|
|
35
|
+
}
|
|
36
|
+
throw new TypeError(`canonical JSON: unsupported value type ${typeof value}`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/governance/encoding.ts
|
|
40
|
+
var B64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
41
|
+
function bytesToBase64(bytes) {
|
|
42
|
+
let out = "";
|
|
43
|
+
let i = 0;
|
|
44
|
+
for (; i + 2 < bytes.length; i += 3) {
|
|
45
|
+
const n = bytes[i] << 16 | bytes[i + 1] << 8 | bytes[i + 2];
|
|
46
|
+
out += B64_ALPHABET[n >> 18 & 63] + B64_ALPHABET[n >> 12 & 63] + B64_ALPHABET[n >> 6 & 63] + B64_ALPHABET[n & 63];
|
|
47
|
+
}
|
|
48
|
+
const rest = bytes.length - i;
|
|
49
|
+
if (rest === 1) {
|
|
50
|
+
const n = bytes[i] << 16;
|
|
51
|
+
out += B64_ALPHABET[n >> 18 & 63] + B64_ALPHABET[n >> 12 & 63] + "==";
|
|
52
|
+
} else if (rest === 2) {
|
|
53
|
+
const n = bytes[i] << 16 | bytes[i + 1] << 8;
|
|
54
|
+
out += B64_ALPHABET[n >> 18 & 63] + B64_ALPHABET[n >> 12 & 63] + B64_ALPHABET[n >> 6 & 63] + "=";
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
function bytesToBase64url(bytes) {
|
|
59
|
+
return stripPadding(bytesToBase64(bytes).replace(/\+/g, "-").replace(/\//g, "_"));
|
|
60
|
+
}
|
|
61
|
+
function stripPadding(s) {
|
|
62
|
+
let end = s.length;
|
|
63
|
+
while (end > 0 && s.charCodeAt(end - 1) === 61) end--;
|
|
64
|
+
return s.slice(0, end);
|
|
65
|
+
}
|
|
66
|
+
function base64ToBytes(input) {
|
|
67
|
+
const s = stripPadding(input.replace(/-/g, "+").replace(/_/g, "/"));
|
|
68
|
+
if (!/^[A-Za-z0-9+/]*$/.test(s) || s.length % 4 === 1) {
|
|
69
|
+
throw new TypeError("invalid base64 or base64url string");
|
|
70
|
+
}
|
|
71
|
+
const out = new Uint8Array(Math.floor(s.length * 3 / 4));
|
|
72
|
+
let o = 0;
|
|
73
|
+
for (let i = 0; i < s.length; i += 4) {
|
|
74
|
+
const a = B64_ALPHABET.indexOf(s[i]);
|
|
75
|
+
const b = B64_ALPHABET.indexOf(s[i + 1]);
|
|
76
|
+
const c = i + 2 < s.length ? B64_ALPHABET.indexOf(s[i + 2]) : 0;
|
|
77
|
+
const d = i + 3 < s.length ? B64_ALPHABET.indexOf(s[i + 3]) : 0;
|
|
78
|
+
const n = a << 18 | b << 12 | c << 6 | d;
|
|
79
|
+
out[o++] = n >> 16 & 255;
|
|
80
|
+
if (i + 2 < s.length) out[o++] = n >> 8 & 255;
|
|
81
|
+
if (i + 3 < s.length) out[o++] = n & 255;
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
function utf8(s) {
|
|
86
|
+
return new TextEncoder().encode(s);
|
|
87
|
+
}
|
|
88
|
+
function toBytes(source) {
|
|
89
|
+
if (ArrayBuffer.isView(source)) {
|
|
90
|
+
return new Uint8Array(source.buffer, source.byteOffset, source.byteLength).slice();
|
|
91
|
+
}
|
|
92
|
+
if (Object.prototype.toString.call(source) === "[object ArrayBuffer]") {
|
|
93
|
+
return new Uint8Array(source.slice(0));
|
|
94
|
+
}
|
|
95
|
+
throw new TypeError("expected an ArrayBuffer or a typed array");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// src/governance/intents.ts
|
|
99
|
+
var LOWERCASE_UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
100
|
+
function assertIdempotencyKey(key) {
|
|
101
|
+
if (typeof key !== "string" || !LOWERCASE_UUID.test(key)) {
|
|
102
|
+
throw new TypeError(
|
|
103
|
+
`idempotency key must be a lowercase UUID (got ${JSON.stringify(key)}); the platform refuses any other form`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function newIdempotencyKey() {
|
|
108
|
+
const c = globalThis.crypto;
|
|
109
|
+
if (c && typeof c.randomUUID === "function") {
|
|
110
|
+
return c.randomUUID().toLowerCase();
|
|
111
|
+
}
|
|
112
|
+
if (!c || typeof c.getRandomValues !== "function") {
|
|
113
|
+
throw new Error("no cryptographic random source: pass an idempotency key explicitly");
|
|
114
|
+
}
|
|
115
|
+
const b = c.getRandomValues(new Uint8Array(16));
|
|
116
|
+
b[6] = b[6] & 15 | 64;
|
|
117
|
+
b[8] = b[8] & 63 | 128;
|
|
118
|
+
const hex = Array.from(b, (x) => x.toString(16).padStart(2, "0")).join("");
|
|
119
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
120
|
+
}
|
|
121
|
+
function nonEmpty(name, value) {
|
|
122
|
+
if (typeof value !== "string" || value === "") {
|
|
123
|
+
throw new TypeError(`${name} is required`);
|
|
124
|
+
}
|
|
125
|
+
return value;
|
|
126
|
+
}
|
|
127
|
+
function keyList(name, keys) {
|
|
128
|
+
if (!Array.isArray(keys) || keys.length === 0) {
|
|
129
|
+
throw new TypeError(`${name} must list at least one public key`);
|
|
130
|
+
}
|
|
131
|
+
return keys.map((k, i) => {
|
|
132
|
+
const key = nonEmpty(`${name}[${i}]`, k);
|
|
133
|
+
if (key.trim() !== key) {
|
|
134
|
+
throw new TypeError(`${name}[${i}] has leading or trailing whitespace; trim it first`);
|
|
135
|
+
}
|
|
136
|
+
return key;
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
function version(v) {
|
|
140
|
+
if (!Number.isSafeInteger(v) || v < 0) {
|
|
141
|
+
throw new TypeError(
|
|
142
|
+
`signer_group_version must be a non-negative integer (got ${String(v)}); read it from the group's \`version\``
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
return v;
|
|
146
|
+
}
|
|
147
|
+
function finish(intent) {
|
|
148
|
+
const canonical = canonicalJSON(intent);
|
|
149
|
+
return { intent, canonical, bytes: utf8(canonical) };
|
|
150
|
+
}
|
|
151
|
+
function addSignersToGroupIntent(params) {
|
|
152
|
+
assertIdempotencyKey(params.idempotencyKey);
|
|
153
|
+
return finish({
|
|
154
|
+
type: "add_signers_to_group",
|
|
155
|
+
signer_group_id: nonEmpty("signerGroupId", params.signerGroupId),
|
|
156
|
+
new_member_keys: keyList("newMemberKeys", params.newMemberKeys),
|
|
157
|
+
signer_group_version: version(params.signerGroupVersion),
|
|
158
|
+
idempotency_key: params.idempotencyKey
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
function removeSignersFromGroupIntent(params) {
|
|
162
|
+
assertIdempotencyKey(params.idempotencyKey);
|
|
163
|
+
return finish({
|
|
164
|
+
type: "remove_signers_from_group",
|
|
165
|
+
group_id: nonEmpty("signerGroupId", params.signerGroupId),
|
|
166
|
+
members_to_remove: keyList("membersToRemove", params.membersToRemove),
|
|
167
|
+
signer_group_version: version(params.signerGroupVersion),
|
|
168
|
+
idempotency_key: params.idempotencyKey
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
function updateSignerGroupGovernanceIntent(params) {
|
|
172
|
+
assertIdempotencyKey(params.idempotencyKey);
|
|
173
|
+
const intent = {
|
|
174
|
+
type: "update_signer_group_governance",
|
|
175
|
+
signer_group_id: nonEmpty("signerGroupId", params.signerGroupId),
|
|
176
|
+
signer_group_version: version(params.signerGroupVersion),
|
|
177
|
+
idempotency_key: params.idempotencyKey
|
|
178
|
+
};
|
|
179
|
+
if (params.governanceThreshold !== void 0 && params.governanceThreshold !== null) {
|
|
180
|
+
const t = params.governanceThreshold;
|
|
181
|
+
if (!Number.isSafeInteger(t) || t < 1 || t > 2147483647) {
|
|
182
|
+
throw new TypeError(
|
|
183
|
+
"governanceThreshold must be an integer from 1 to 2^31-1, or null to clear it"
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
intent.governance_threshold = params.governanceThreshold;
|
|
187
|
+
}
|
|
188
|
+
if (params.governanceSignerGroupId !== void 0 && params.governanceSignerGroupId !== null) {
|
|
189
|
+
intent.governance_signer_group_id = nonEmpty(
|
|
190
|
+
"governanceSignerGroupId",
|
|
191
|
+
params.governanceSignerGroupId
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
return finish(intent);
|
|
195
|
+
}
|
|
196
|
+
function createCryptoDestinationIntent(params) {
|
|
197
|
+
assertIdempotencyKey(params.idempotencyKey);
|
|
198
|
+
return finish({
|
|
199
|
+
type: "create_crypto_destination",
|
|
200
|
+
recipient_id: nonEmpty("recipientId", params.recipientId),
|
|
201
|
+
network_id: nonEmpty("networkId", params.networkId),
|
|
202
|
+
address: nonEmpty("address", params.address),
|
|
203
|
+
signer_group_id: nonEmpty("signerGroupId", params.signerGroupId),
|
|
204
|
+
idempotency_key: params.idempotencyKey
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// src/governance/webauthn.ts
|
|
209
|
+
function buildChallenge(intent) {
|
|
210
|
+
const bytes = challengeBytes(intent);
|
|
211
|
+
const out = new ArrayBuffer(bytes.length);
|
|
212
|
+
new Uint8Array(out).set(bytes);
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
function challengeBytes(intent) {
|
|
216
|
+
if (typeof intent === "string") {
|
|
217
|
+
return utf8(intent);
|
|
218
|
+
}
|
|
219
|
+
if (isBinary(intent)) {
|
|
220
|
+
return toBytes(intent);
|
|
221
|
+
}
|
|
222
|
+
if (intent && typeof intent === "object") {
|
|
223
|
+
const canonical = intent.canonical;
|
|
224
|
+
if (typeof canonical === "string") {
|
|
225
|
+
return utf8(canonical);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
throw new TypeError(
|
|
229
|
+
"expected a built intent, a prepared change, its canonical string, or its bytes"
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
function isBinary(v) {
|
|
233
|
+
return ArrayBuffer.isView(v) || Object.prototype.toString.call(v) === "[object ArrayBuffer]";
|
|
234
|
+
}
|
|
235
|
+
function b64urlField(name, value) {
|
|
236
|
+
if (value === void 0 || value === null || value === "") {
|
|
237
|
+
throw new TypeError(`passkey assertion is missing ${name}`);
|
|
238
|
+
}
|
|
239
|
+
if (typeof value === "string") {
|
|
240
|
+
return bytesToBase64url(base64ToBytes(value));
|
|
241
|
+
}
|
|
242
|
+
return bytesToBase64url(toBytes(value));
|
|
243
|
+
}
|
|
244
|
+
function packAssertion(credential) {
|
|
245
|
+
if (!credential || credential.type !== "public-key") {
|
|
246
|
+
throw new TypeError('expected a WebAuthn assertion with type "public-key"');
|
|
247
|
+
}
|
|
248
|
+
if (typeof credential.id !== "string" || credential.id === "") {
|
|
249
|
+
throw new TypeError("passkey assertion is missing id");
|
|
250
|
+
}
|
|
251
|
+
const r = credential.response;
|
|
252
|
+
if (!r) {
|
|
253
|
+
throw new TypeError("passkey assertion is missing response");
|
|
254
|
+
}
|
|
255
|
+
const response = {
|
|
256
|
+
clientDataJSON: b64urlField("clientDataJSON", r.clientDataJSON),
|
|
257
|
+
authenticatorData: b64urlField("authenticatorData", r.authenticatorData),
|
|
258
|
+
signature: b64urlField("signature", r.signature)
|
|
259
|
+
};
|
|
260
|
+
if (r.userHandle !== void 0 && r.userHandle !== null && r.userHandle !== "") {
|
|
261
|
+
response.userHandle = b64urlField("userHandle", r.userHandle);
|
|
262
|
+
}
|
|
263
|
+
const envelope = {
|
|
264
|
+
// The verifier requires `id` to be base64url; some mobile libraries hand
|
|
265
|
+
// out standard base64, so normalize it like the binary fields.
|
|
266
|
+
id: b64urlField("id", credential.id),
|
|
267
|
+
rawId: b64urlField("rawId", credential.rawId),
|
|
268
|
+
type: "public-key",
|
|
269
|
+
clientExtensionResults: {},
|
|
270
|
+
response
|
|
271
|
+
};
|
|
272
|
+
if (credential.authenticatorAttachment) {
|
|
273
|
+
envelope.authenticatorAttachment = credential.authenticatorAttachment;
|
|
274
|
+
}
|
|
275
|
+
return bytesToBase64url(utf8(JSON.stringify(envelope)));
|
|
276
|
+
}
|
|
277
|
+
var MAX_PACKED_ASSERTION_LENGTH = 64 * 1024;
|
|
278
|
+
function assertionChallengeMatches(packed, intent) {
|
|
279
|
+
if (typeof packed !== "string" || packed.length > MAX_PACKED_ASSERTION_LENGTH) {
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
const envelope = JSON.parse(new TextDecoder().decode(base64ToBytes(packed)));
|
|
284
|
+
const clientDataB64 = envelope.response?.clientDataJSON;
|
|
285
|
+
if (typeof clientDataB64 !== "string") {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
const clientData = JSON.parse(new TextDecoder().decode(base64ToBytes(clientDataB64)));
|
|
289
|
+
if (typeof clientData.challenge !== "string") {
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
const got = base64ToBytes(clientData.challenge);
|
|
293
|
+
const want = new Uint8Array(buildChallenge(intent));
|
|
294
|
+
return got.length === want.length && got.every((b, i) => b === want[i]);
|
|
295
|
+
} catch {
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
function webauthnSigner(getAssertion) {
|
|
300
|
+
return {
|
|
301
|
+
async sign(payload) {
|
|
302
|
+
const credential = await getAssertion(buildChallenge(payload));
|
|
303
|
+
return packAssertion(credential);
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// src/governance/signer.ts
|
|
309
|
+
async function signGovernanceIntent(intent, signers) {
|
|
310
|
+
if (!Array.isArray(signers) || signers.length === 0) {
|
|
311
|
+
throw new TypeError("a governance change needs at least one signer");
|
|
312
|
+
}
|
|
313
|
+
const bytes = ArrayBuffer.isView(intent) ? toBytes(intent) : intent.bytes;
|
|
314
|
+
const out = [];
|
|
315
|
+
for (const s of signers) {
|
|
316
|
+
const sig = typeof s === "function" ? await s(bytes.slice()) : await s.sign(bytes.slice());
|
|
317
|
+
if (typeof sig !== "string" || sig === "") {
|
|
318
|
+
throw new Error("a governance signer returned an empty signature");
|
|
319
|
+
}
|
|
320
|
+
out.push(sig);
|
|
321
|
+
}
|
|
322
|
+
return out;
|
|
323
|
+
}
|
|
324
|
+
function webCryptoES256Signer(privateKey, subtle) {
|
|
325
|
+
return {
|
|
326
|
+
async sign(payload) {
|
|
327
|
+
const s = subtle ?? globalThis.crypto?.subtle;
|
|
328
|
+
if (!s) {
|
|
329
|
+
throw new Error(
|
|
330
|
+
"WebCrypto is not available: pass a SubtleCrypto or use ES256Signer on Node"
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
const raw = new Uint8Array(
|
|
334
|
+
await s.sign(
|
|
335
|
+
{ name: "ECDSA", hash: "SHA-256" },
|
|
336
|
+
privateKey,
|
|
337
|
+
toArrayBuffer(payload)
|
|
338
|
+
)
|
|
339
|
+
);
|
|
340
|
+
return bytesToBase64(p1363ToDer(raw));
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
function toArrayBuffer(bytes) {
|
|
345
|
+
const out = new ArrayBuffer(bytes.length);
|
|
346
|
+
new Uint8Array(out).set(bytes);
|
|
347
|
+
return out;
|
|
348
|
+
}
|
|
349
|
+
function p1363ToDer(sig) {
|
|
350
|
+
if (sig.length !== 64) {
|
|
351
|
+
throw new Error(`expected a 64-byte P-256 signature, got ${sig.length} bytes`);
|
|
352
|
+
}
|
|
353
|
+
const int = (bytes) => {
|
|
354
|
+
let i = 0;
|
|
355
|
+
while (i < bytes.length - 1 && bytes[i] === 0) i++;
|
|
356
|
+
const trimmed = bytes.slice(i);
|
|
357
|
+
const body = trimmed[0] & 128 ? Uint8Array.of(0, ...trimmed) : trimmed;
|
|
358
|
+
return Uint8Array.of(2, body.length, ...body);
|
|
359
|
+
};
|
|
360
|
+
const r = int(sig.slice(0, 32));
|
|
361
|
+
const s = int(sig.slice(32));
|
|
362
|
+
return Uint8Array.of(48, r.length + s.length, ...r, ...s);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
exports.addSignersToGroupIntent = addSignersToGroupIntent;
|
|
366
|
+
exports.assertIdempotencyKey = assertIdempotencyKey;
|
|
367
|
+
exports.assertionChallengeMatches = assertionChallengeMatches;
|
|
368
|
+
exports.buildChallenge = buildChallenge;
|
|
369
|
+
exports.canonicalJSON = canonicalJSON;
|
|
370
|
+
exports.createCryptoDestinationIntent = createCryptoDestinationIntent;
|
|
371
|
+
exports.newIdempotencyKey = newIdempotencyKey;
|
|
372
|
+
exports.packAssertion = packAssertion;
|
|
373
|
+
exports.removeSignersFromGroupIntent = removeSignersFromGroupIntent;
|
|
374
|
+
exports.signGovernanceIntent = signGovernanceIntent;
|
|
375
|
+
exports.updateSignerGroupGovernanceIntent = updateSignerGroupGovernanceIntent;
|
|
376
|
+
exports.webCryptoES256Signer = webCryptoES256Signer;
|
|
377
|
+
exports.webauthnSigner = webauthnSigner;
|
|
378
|
+
//# sourceMappingURL=index.js.map
|
|
379
|
+
//# sourceMappingURL=index.js.map
|