@cubist-labs/cubesigner-sdk 0.1.77 → 0.2.15

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.
Files changed (55) hide show
  1. package/dist/package.json +68 -0
  2. package/dist/src/api.d.ts +493 -0
  3. package/dist/src/api.js +1166 -0
  4. package/dist/src/client.d.ts +534 -10
  5. package/dist/src/client.js +355 -19
  6. package/dist/src/ethers/index.d.ts +34 -9
  7. package/dist/src/ethers/index.js +63 -19
  8. package/dist/src/index.d.ts +51 -70
  9. package/dist/src/index.js +83 -237
  10. package/dist/src/key.d.ts +35 -64
  11. package/dist/src/key.js +32 -96
  12. package/dist/src/mfa.d.ts +85 -14
  13. package/dist/src/mfa.js +146 -40
  14. package/dist/src/org.d.ts +42 -194
  15. package/dist/src/org.js +52 -336
  16. package/dist/src/paginator.js +1 -1
  17. package/dist/src/response.d.ts +101 -0
  18. package/dist/src/response.js +164 -0
  19. package/dist/src/role.d.ts +87 -83
  20. package/dist/src/role.js +79 -136
  21. package/dist/src/schema.d.ts +936 -28
  22. package/dist/src/schema.js +1 -1
  23. package/dist/src/schema_types.d.ts +109 -0
  24. package/dist/src/schema_types.js +3 -0
  25. package/dist/src/session/cognito_manager.d.ts +15 -3
  26. package/dist/src/session/cognito_manager.js +23 -5
  27. package/dist/src/session/session_manager.d.ts +1 -1
  28. package/dist/src/session/session_manager.js +3 -11
  29. package/dist/src/session/session_storage.js +1 -1
  30. package/dist/src/session/signer_session_manager.d.ts +10 -29
  31. package/dist/src/session/signer_session_manager.js +21 -80
  32. package/dist/src/signer_session.d.ts +15 -252
  33. package/dist/src/signer_session.js +25 -424
  34. package/dist/src/user_export.d.ts +52 -0
  35. package/dist/src/user_export.js +129 -0
  36. package/dist/src/util.d.ts +15 -0
  37. package/dist/src/util.js +33 -11
  38. package/package.json +13 -11
  39. package/src/api.ts +1395 -0
  40. package/src/client.ts +413 -12
  41. package/src/ethers/index.ts +74 -28
  42. package/src/index.ts +96 -273
  43. package/src/key.ts +36 -131
  44. package/src/{fido.ts → mfa.ts} +62 -38
  45. package/src/org.ts +54 -405
  46. package/src/response.ts +196 -0
  47. package/src/role.ts +113 -184
  48. package/src/schema.ts +936 -28
  49. package/src/schema_types.ts +110 -0
  50. package/src/session/cognito_manager.ts +33 -6
  51. package/src/session/session_manager.ts +2 -8
  52. package/src/session/signer_session_manager.ts +29 -110
  53. package/src/signer_session.ts +22 -597
  54. package/src/user_export.ts +116 -0
  55. package/src/util.ts +29 -10
package/src/key.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { CubeSigner, KeyPolicy } from ".";
2
- import { components } from "./client";
3
- import { assertOk } from "./util";
1
+ import { KeyPolicy } from "./role";
2
+ import { KeyInfoApi, KeyTypeApi, UpdateKeyRequest, SchemaKeyType } from "./schema_types";
3
+ import { CubeSignerClient } from "./client";
4
4
 
5
5
  /** Secp256k1 key type */
6
6
  export enum Secp256k1 {
@@ -37,13 +37,6 @@ export type Stark = typeof Stark;
37
37
  /** Key type */
38
38
  export type KeyType = Secp256k1 | Bls | Ed25519 | Mnemonic | Stark;
39
39
 
40
- /** Schema key type (i.e., key type at the API level) */
41
- type SchemaKeyType = components["schemas"]["KeyType"];
42
-
43
- type UpdateKeyRequest = components["schemas"]["UpdateKeyRequest"];
44
- type KeyInfoApi = components["schemas"]["KeyInfo"];
45
- type KeyTypeApi = components["schemas"]["KeyType"];
46
-
47
40
  /** Additional properties (for backward compatibility) */
48
41
  export interface KeyInfo extends KeyInfoApi {
49
42
  /** Alias for key_id */
@@ -76,20 +69,24 @@ export function toKeyInfo(key: KeyInfoApi): KeyInfo {
76
69
  /** Signing keys. */
77
70
  export class Key {
78
71
  /** The CubeSigner instance that this key is associated with */
79
- readonly #cs: CubeSigner;
72
+ readonly #csc: CubeSignerClient;
73
+
80
74
  /** The organization that this key is in */
81
- readonly orgId: string;
75
+ get orgId() {
76
+ return this.#csc.orgId;
77
+ }
78
+
82
79
  /**
83
80
  * The id of the key: "Key#" followed by a unique identifier specific to
84
81
  * the type of key (such as a public key for BLS or an ethereum address for Secp)
85
82
  * @example Key#0x8e3484687e66cdd26cf04c3647633ab4f3570148
86
- * */
83
+ */
87
84
  readonly id: string;
88
85
 
89
86
  /**
90
87
  * A unique identifier specific to the type of key, such as a public key or an ethereum address
91
88
  * @example 0x8e3484687e66cdd26cf04c3647633ab4f3570148
92
- * */
89
+ */
93
90
  readonly materialId: string;
94
91
 
95
92
  /**
@@ -97,7 +94,7 @@ export class Key {
97
94
  * - secp256k1 keys use 65-byte uncompressed SECG format
98
95
  * - BLS keys use 48-byte compressed BLS12-381 (ZCash) format
99
96
  * @example 0x04d2688b6bc2ce7f9879b9e745f3c4dc177908c5cef0c1b64cff19ae7ff27dee623c64fe9d9c325c7fbbc748bbd5f607ce14dd83e28ebbbb7d3e7f2ffb70a79431
100
- * */
97
+ */
101
98
  readonly publicKey: string;
102
99
 
103
100
  /** The type of key. */
@@ -151,15 +148,16 @@ export class Key {
151
148
  /**
152
149
  * @description Owner of the key
153
150
  * @example User#c3b9379c-4e8c-4216-bd0a-65ace53cf98f
154
- * */
151
+ */
155
152
  async owner(): Promise<string> {
156
153
  const data = await this.fetch();
157
154
  return data.owner;
158
155
  }
159
156
 
160
- /** Set the owner of the key. Only the key (or org) owner can change the owner of the key.
157
+ /**
158
+ * Set the owner of the key. Only the key (or org) owner can change the owner of the key.
161
159
  * @param {string} owner The user-id of the new owner of the key.
162
- * */
160
+ */
163
161
  async setOwner(owner: string) {
164
162
  await this.update({ owner });
165
163
  }
@@ -168,149 +166,56 @@ export class Key {
168
166
  * Delete this key.
169
167
  */
170
168
  async delete() {
171
- await this.#cs.deleteKey(this.orgId, this.id);
169
+ await this.#csc.keyDelete(this.id);
172
170
  }
173
171
 
174
172
  // --------------------------------------------------------------------------
175
173
  // -- INTERNAL --------------------------------------------------------------
176
174
  // --------------------------------------------------------------------------
177
175
 
178
- /** Create a new key.
179
- * @param {CubeSigner} cs The CubeSigner instance to use for signing.
180
- * @param {string} orgId The id of the organization to which the key belongs.
176
+ /**
177
+ * Create a new key.
178
+ *
179
+ * @param {CubeSignerClient} csc The CubeSigner instance to use for signing.
181
180
  * @param {KeyInfo} data The JSON response from the API server.
182
181
  * @internal
183
- * */
184
- constructor(cs: CubeSigner, orgId: string, data: KeyInfoApi) {
185
- this.#cs = cs;
186
- this.orgId = orgId;
182
+ */
183
+ constructor(csc: CubeSignerClient, data: KeyInfoApi) {
184
+ this.#csc = csc;
187
185
  this.id = data.key_id;
188
186
  this.materialId = data.material_id;
189
187
  this.publicKey = data.public_key;
190
188
  }
191
189
 
192
- /** Update the key.
190
+ /**
191
+ * Update the key.
193
192
  * @param {UpdateKeyRequest} request The JSON request to send to the API server.
194
193
  * @return {KeyInfo} The JSON response from the API server.
195
- * */
194
+ */
196
195
  private async update(request: UpdateKeyRequest): Promise<KeyInfo> {
197
- const resp = await (
198
- await this.#cs.management()
199
- ).patch("/v0/org/{org_id}/keys/{key_id}", {
200
- params: { path: { org_id: this.orgId, key_id: this.id } },
201
- body: request,
202
- parseAs: "json",
203
- });
204
- return toKeyInfo(assertOk(resp));
205
- }
206
-
207
- /** Create new signing keys.
208
- * @param {CubeSigner} cs The CubeSigner instance to use for signing.
209
- * @param {string} orgId The id of the organization to which the key belongs.
210
- * @param {KeyType} keyType The type of key to create.
211
- * @param {number} count The number of keys to create.
212
- * @param {string?} ownerId The owner of the keys. Defaults to the session's user.
213
- * @return {Key[]} The new keys.
214
- * @internal
215
- * */
216
- static async createKeys(
217
- cs: CubeSigner,
218
- orgId: string,
219
- keyType: KeyType,
220
- count: number,
221
- ownerId?: string,
222
- ): Promise<Key[]> {
223
- const chain_id = 0; // not used anymore
224
- const resp = await (
225
- await cs.management()
226
- ).post("/v0/org/{org_id}/keys", {
227
- params: { path: { org_id: orgId } },
228
- body: {
229
- count,
230
- chain_id,
231
- key_type: keyType,
232
- owner: ownerId || null,
233
- },
234
- parseAs: "json",
235
- });
236
- const data = assertOk(resp);
237
- return data.keys.map((k) => new Key(cs, orgId, k));
196
+ const data = await this.#csc.keyUpdate(this.id, request);
197
+ return toKeyInfo(data);
238
198
  }
239
199
 
240
200
  /**
241
- * Derives a key of a specified type using a supplied derivation path and an existing long-lived mnemonic.
201
+ * Fetch the key information.
242
202
  *
243
- * The owner of the derived key will be the owner of the mnemonic.
244
- *
245
- * @param {CubeSigner} cs The CubeSigner instance to use for key creation.
246
- * @param {string} orgId The id of the organization to which the key belongs.
247
- * @param {KeyType} keyType The type of key to create.
248
- * @param {string[]} derivationPaths Derivation paths from which to derive new keys.
249
- * @param {string} mnemonicId materialId of mnemonic key used to derive the new key.
250
- *
251
- * @return {Key[]} The newly derived keys.
252
- */
253
- static async deriveKeys(
254
- cs: CubeSigner,
255
- orgId: string,
256
- keyType: KeyType,
257
- derivationPaths: string[],
258
- mnemonicId: string,
259
- ): Promise<Key[]> {
260
- const resp = await (
261
- await cs.management()
262
- ).put("/v0/org/{org_id}/derive_key", {
263
- params: { path: { org_id: orgId } },
264
- body: {
265
- derivation_path: derivationPaths,
266
- mnemonic_id: mnemonicId,
267
- key_type: keyType,
268
- },
269
- parseAs: "json",
270
- });
271
- const data = assertOk(resp);
272
- return data.keys.map((k) => new Key(cs, orgId, k));
273
- }
274
-
275
- /** Get a key by id.
276
- * @param {CubeSigner} cs The CubeSigner instance to use for signing.
277
- * @param {string} orgId The id of the organization to which the key belongs.
278
- * @param {string} keyId The id of the key to get.
279
- * @return {Key} The key.
280
- * @internal
281
- * */
282
- static async getKey(cs: CubeSigner, orgId: string, keyId: string): Promise<Key> {
283
- const resp = await (
284
- await cs.management()
285
- ).get("/v0/org/{org_id}/keys/{key_id}", {
286
- params: { path: { org_id: orgId, key_id: keyId } },
287
- parseAs: "json",
288
- });
289
- const data = assertOk(resp);
290
- return new Key(cs, orgId, data);
291
- }
292
-
293
- /** Fetches the key information.
294
203
  * @return {KeyInfo} The key information.
295
204
  * @internal
296
- * */
205
+ */
297
206
  private async fetch(): Promise<KeyInfo> {
298
- const resp = await (
299
- await this.#cs.management()
300
- ).get("/v0/org/{org_id}/keys/{key_id}", {
301
- params: { path: { org_id: this.orgId, key_id: this.id } },
302
- parseAs: "json",
303
- });
304
- const data = assertOk(resp);
207
+ const data = await this.#csc.keyGet(this.id);
305
208
  return toKeyInfo(data);
306
209
  }
307
210
  }
308
211
 
309
- /** Convert a schema key type to a key type.
212
+ /**
213
+ * Convert a schema key type to a key type.
214
+ *
310
215
  * @param {SchemaKeyType} ty The schema key type.
311
216
  * @return {KeyType} The key type.
312
217
  * @internal
313
- * */
218
+ */
314
219
  export function fromSchemaKeyType(ty: SchemaKeyType): KeyType {
315
220
  switch (ty) {
316
221
  case "SecpEthAddr":
@@ -1,42 +1,78 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
 
3
- import { CubeSigner, MfaRequestInfo, SignerSession } from ".";
4
- import { components } from "./schema";
3
+ import {
4
+ ApiAddFidoChallenge,
5
+ ApiMfaFidoChallenge,
6
+ MfaRequestInfo,
7
+ PublicKeyCredential,
8
+ TotpInfo,
9
+ } from "./schema_types";
5
10
  import { decodeBase64Url, encodeToBase64Url } from "./util";
11
+ import { CubeSignerApi } from "./api";
12
+
13
+ /** MFA receipt */
14
+ export interface MfaReceipt {
15
+ /** MFA request ID */
16
+ mfaId: string;
17
+ /** Corresponding org ID */
18
+ mfaOrgId: string;
19
+ /** MFA confirmation code */
20
+ mfaConf: string;
21
+ }
22
+
23
+ /** TOTP challenge that must be answered before user's TOTP is updated */
24
+ export class TotpChallenge {
25
+ readonly #api: CubeSignerApi;
26
+ readonly #totpInfo: TotpInfo;
27
+
28
+ /** The id of the challenge */
29
+ get totpId() {
30
+ return this.#totpInfo.totp_id;
31
+ }
6
32
 
7
- export type ApiAddFidoChallenge =
8
- components["responses"]["FidoCreateChallengeResponse"]["content"]["application/json"];
33
+ /** The new TOTP configuration */
34
+ get totpUrl() {
35
+ return this.#totpInfo.totp_url;
36
+ }
37
+
38
+ /**
39
+ * @param {CubeSignerApi} api Used when answering the challenge.
40
+ * @param {TotpInfo} totpInfo TOTP challenge information.
41
+ */
42
+ constructor(api: CubeSignerApi, totpInfo: TotpInfo) {
43
+ this.#api = api;
44
+ this.#totpInfo = totpInfo;
45
+ }
9
46
 
10
- export type ApiMfaFidoChallenge =
11
- components["responses"]["FidoAssertChallenge"]["content"]["application/json"];
47
+ /**
48
+ * Answer the challenge with the code that corresponds to `this.totpUrl`.
49
+ * @param {string} code 6-digit code that corresponds to `this.totpUrl`.
50
+ */
51
+ async answer(code: string) {
52
+ if (!/^\d{1,6}$/.test(code)) {
53
+ throw new Error(`Invalid TOTP code: ${code}; it must be a 6-digit string`);
54
+ }
12
55
 
13
- export type PublicKeyCredentialCreationOptions =
14
- components["schemas"]["PublicKeyCredentialCreationOptions"];
15
- export type PublicKeyCredentialRequestOptions =
16
- components["schemas"]["PublicKeyCredentialRequestOptions"];
17
- export type PublicKeyCredentialParameters = components["schemas"]["PublicKeyCredentialParameters"];
18
- export type PublicKeyCredentialDescriptor = components["schemas"]["PublicKeyCredentialDescriptor"];
19
- export type AuthenticatorSelectionCriteria =
20
- components["schemas"]["AuthenticatorSelectionCriteria"];
21
- export type PublicKeyCredentialUserEntity = components["schemas"]["PublicKeyCredentialUserEntity"];
22
- export type PublicKeyCredential = components["schemas"]["PublicKeyCredential"];
56
+ await this.#api.userResetTotpComplete(this.totpId, code);
57
+ }
58
+ }
23
59
 
24
60
  /**
25
61
  * Returned after creating a request to add a new FIDO device.
26
62
  * Provides some helper methods for answering this challenge.
27
63
  */
28
64
  export class AddFidoChallenge {
29
- readonly #cs: CubeSigner;
65
+ readonly #api: CubeSignerApi;
30
66
  readonly challengeId: string;
31
67
  readonly options: any;
32
68
 
33
69
  /**
34
70
  * Constructor
35
- * @param {CubeSigner} cs CubeSigner instance used to request to add a FIDO device
71
+ * @param {CubeSignerApi} api The API client used to request to add a FIDO device
36
72
  * @param {ApiAddFidoChallenge} challenge The challenge returned by the remote end.
37
73
  */
38
- constructor(cs: CubeSigner, challenge: ApiAddFidoChallenge) {
39
- this.#cs = cs;
74
+ constructor(api: CubeSignerApi, challenge: ApiAddFidoChallenge) {
75
+ this.#api = api;
40
76
  this.challengeId = challenge.challenge_id;
41
77
 
42
78
  // fix options returned from the server: rename fields and decode base64 fields to uint8[]
@@ -44,12 +80,6 @@ export class AddFidoChallenge {
44
80
  ...challenge.options,
45
81
  challenge: decodeBase64Url(challenge.options.challenge),
46
82
  };
47
- this.options.pubKeyCredParams ??= challenge.options.pub_key_cred_params;
48
- this.options.excludeCredentials ??= challenge.options.exclude_credentials;
49
- this.options.authenticatorSelection ??= challenge.options.authenticator_selection;
50
- delete this.options.pub_key_cred_params;
51
- delete this.options.exclude_credentials;
52
- delete this.options.authenticator_selection;
53
83
 
54
84
  if (challenge.options.user) {
55
85
  this.options.user.id = decodeBase64Url(challenge.options.user.id);
@@ -88,7 +118,7 @@ export class AddFidoChallenge {
88
118
  attestationObject: encodeToBase64Url(cred.response.attestationObject),
89
119
  },
90
120
  };
91
- await this.#cs.addFidoComplete(this.challengeId, answer);
121
+ await this.#api.userRegisterFidoComplete(this.challengeId, answer);
92
122
  }
93
123
  }
94
124
 
@@ -97,18 +127,18 @@ export class AddFidoChallenge {
97
127
  * Provides some helper methods for answering this challenge.
98
128
  */
99
129
  export class MfaFidoChallenge {
100
- readonly #ss: SignerSession;
130
+ readonly #api: CubeSignerApi;
101
131
  readonly mfaId: string;
102
132
  readonly challengeId: string;
103
133
  readonly options: any;
104
134
 
105
135
  /**
106
- * @param {SignerSession} ss The session used to initiate MFA approval using FIDO
136
+ * @param {CubeSignerApi} api The API client used to initiate MFA approval using FIDO
107
137
  * @param {string} mfaId The MFA request id.
108
138
  * @param {ApiMfaFidoChallenge} challenge The challenge returned by the remote end
109
139
  */
110
- constructor(ss: SignerSession, mfaId: string, challenge: ApiMfaFidoChallenge) {
111
- this.#ss = ss;
140
+ constructor(api: CubeSignerApi, mfaId: string, challenge: ApiMfaFidoChallenge) {
141
+ this.#api = api;
112
142
  this.mfaId = mfaId;
113
143
  this.challengeId = challenge.challenge_id;
114
144
 
@@ -117,12 +147,6 @@ export class MfaFidoChallenge {
117
147
  ...challenge.options,
118
148
  challenge: decodeBase64Url(challenge.options.challenge),
119
149
  };
120
- this.options.rpId ??= challenge.options.rp_id;
121
- this.options.allowCredentials ??= challenge.options.allow_credentials;
122
- this.options.userVerification ??= challenge.options.user_verification;
123
- delete this.options.rp_id;
124
- delete this.options.allow_credentials;
125
- delete this.options.user_verification;
126
150
 
127
151
  for (const credential of this.options.allowCredentials ?? []) {
128
152
  credential.id = decodeBase64Url(credential.id);
@@ -161,6 +185,6 @@ export class MfaFidoChallenge {
161
185
  signature: encodeToBase64Url(cred.response.signature),
162
186
  },
163
187
  };
164
- return await this.#ss.fidoApproveComplete(this.mfaId, this.challengeId, answer);
188
+ return await this.#api.mfaApproveFidoComplete(this.mfaId, this.challengeId, answer);
165
189
  }
166
190
  }