@cubist-labs/cubesigner-sdk 0.1.77 → 0.2.2

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/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,9 +69,13 @@ 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)
@@ -168,7 +165,7 @@ export class Key {
168
165
  * Delete this key.
169
166
  */
170
167
  async delete() {
171
- await this.#cs.deleteKey(this.orgId, this.id);
168
+ await this.#csc.keyDelete(this.id);
172
169
  }
173
170
 
174
171
  // --------------------------------------------------------------------------
@@ -176,118 +173,25 @@ export class Key {
176
173
  // --------------------------------------------------------------------------
177
174
 
178
175
  /** 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
+ * @param {CubeSignerClient} csc The CubeSigner instance to use for signing.
181
177
  * @param {KeyInfo} data The JSON response from the API server.
182
178
  * @internal
183
179
  * */
184
- constructor(cs: CubeSigner, orgId: string, data: KeyInfoApi) {
185
- this.#cs = cs;
186
- this.orgId = orgId;
180
+ constructor(csc: CubeSignerClient, data: KeyInfoApi) {
181
+ this.#csc = csc;
187
182
  this.id = data.key_id;
188
183
  this.materialId = data.material_id;
189
184
  this.publicKey = data.public_key;
190
185
  }
191
186
 
192
- /** Update the key.
187
+ /**
188
+ * Update the key.
193
189
  * @param {UpdateKeyRequest} request The JSON request to send to the API server.
194
190
  * @return {KeyInfo} The JSON response from the API server.
195
- * */
196
- 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));
238
- }
239
-
240
- /**
241
- * Derives a key of a specified type using a supplied derivation path and an existing long-lived mnemonic.
242
- *
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
191
  */
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);
192
+ private async update(request: UpdateKeyRequest): Promise<KeyInfo> {
193
+ const data = await this.#csc.keyUpdate(this.id, request);
194
+ return toKeyInfo(data);
291
195
  }
292
196
 
293
197
  /** Fetches the key information.
@@ -295,13 +199,7 @@ export class Key {
295
199
  * @internal
296
200
  * */
297
201
  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);
202
+ const data = await this.#csc.keyGet(this.id);
305
203
  return toKeyInfo(data);
306
204
  }
307
205
  }
@@ -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";
10
+ import { CubeSignerClient } from "./client";
5
11
  import { decodeBase64Url, encodeToBase64Url } from "./util";
6
12
 
7
- export type ApiAddFidoChallenge =
8
- components["responses"]["FidoCreateChallengeResponse"]["content"]["application/json"];
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 #csc: CubeSignerClient;
26
+ readonly #totpInfo: TotpInfo;
27
+
28
+ /** The id of the challenge */
29
+ get totpId() {
30
+ return this.#totpInfo.totp_id;
31
+ }
9
32
 
10
- export type ApiMfaFidoChallenge =
11
- components["responses"]["FidoAssertChallenge"]["content"]["application/json"];
33
+ /** The new TOTP configuration */
34
+ get totpUrl() {
35
+ return this.#totpInfo.totp_url;
36
+ }
12
37
 
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"];
38
+ /**
39
+ * @param {CubeSignerClient} csc Used when answering the challenge.
40
+ * @param {TotpInfo} totpInfo TOTP challenge information.
41
+ */
42
+ constructor(csc: CubeSignerClient, totpInfo: TotpInfo) {
43
+ this.#csc = csc;
44
+ this.#totpInfo = totpInfo;
45
+ }
46
+
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
+ }
55
+
56
+ await this.#csc.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 #csc: CubeSignerClient;
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 {CubeSignerClient} csc CubeSigner instance 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(csc: CubeSignerClient, challenge: ApiAddFidoChallenge) {
75
+ this.#csc = csc;
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[]
@@ -88,7 +124,7 @@ export class AddFidoChallenge {
88
124
  attestationObject: encodeToBase64Url(cred.response.attestationObject),
89
125
  },
90
126
  };
91
- await this.#cs.addFidoComplete(this.challengeId, answer);
127
+ await this.#csc.userRegisterFidoComplete(this.challengeId, answer);
92
128
  }
93
129
  }
94
130
 
@@ -97,18 +133,18 @@ export class AddFidoChallenge {
97
133
  * Provides some helper methods for answering this challenge.
98
134
  */
99
135
  export class MfaFidoChallenge {
100
- readonly #ss: SignerSession;
136
+ readonly #csc: CubeSignerClient;
101
137
  readonly mfaId: string;
102
138
  readonly challengeId: string;
103
139
  readonly options: any;
104
140
 
105
141
  /**
106
- * @param {SignerSession} ss The session used to initiate MFA approval using FIDO
142
+ * @param {CubeSignerClient} csc The session used to initiate MFA approval using FIDO
107
143
  * @param {string} mfaId The MFA request id.
108
144
  * @param {ApiMfaFidoChallenge} challenge The challenge returned by the remote end
109
145
  */
110
- constructor(ss: SignerSession, mfaId: string, challenge: ApiMfaFidoChallenge) {
111
- this.#ss = ss;
146
+ constructor(csc: CubeSignerClient, mfaId: string, challenge: ApiMfaFidoChallenge) {
147
+ this.#csc = csc;
112
148
  this.mfaId = mfaId;
113
149
  this.challengeId = challenge.challenge_id;
114
150
 
@@ -161,6 +197,6 @@ export class MfaFidoChallenge {
161
197
  signature: encodeToBase64Url(cred.response.signature),
162
198
  },
163
199
  };
164
- return await this.#ss.fidoApproveComplete(this.mfaId, this.challengeId, answer);
200
+ return await this.#csc.mfaApproveFidoComplete(this.mfaId, this.challengeId, answer);
165
201
  }
166
202
  }