@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
@@ -1,243 +1,16 @@
1
- import assert from "assert";
2
- import {
3
- CubeSigner,
4
- Key,
5
- toKeyInfo,
6
- Org,
7
- KeyInfo,
8
- MfaReceipt,
9
- IdentityProof,
10
- MfaFidoChallenge,
11
- } from ".";
12
- import { components, paths } from "./client";
13
- import { JsonMap, assertOk } from "./util";
14
- import { PublicKeyCredential } from "./fido";
15
- import {
16
- NewSessionResponse,
17
- SignerSessionManager,
18
- SignerSessionStorage,
19
- } from "./session/signer_session_manager";
20
-
21
- /* eslint-disable */
22
- export type EvmSignRequest =
23
- paths["/v1/org/{org_id}/eth1/sign/{pubkey}"]["post"]["requestBody"]["content"]["application/json"];
24
- export type Eth2SignRequest =
25
- paths["/v1/org/{org_id}/eth2/sign/{pubkey}"]["post"]["requestBody"]["content"]["application/json"];
26
- export type Eth2StakeRequest =
27
- paths["/v1/org/{org_id}/eth2/stake"]["post"]["requestBody"]["content"]["application/json"];
28
- export type Eth2UnstakeRequest =
29
- paths["/v1/org/{org_id}/eth2/unstake/{pubkey}"]["post"]["requestBody"]["content"]["application/json"];
30
- export type BlobSignRequest =
31
- paths["/v1/org/{org_id}/blob/sign/{key_id}"]["post"]["requestBody"]["content"]["application/json"];
32
- export type BtcSignRequest =
33
- paths["/v0/org/{org_id}/btc/sign/{pubkey}"]["post"]["requestBody"]["content"]["application/json"];
34
- export type SolanaSignRequest =
35
- paths["/v0/org/{org_id}/solana/sign/{pubkey}"]["post"]["requestBody"]["content"]["application/json"];
36
- export type AvaSignRequest =
37
- paths["/v0/org/{org_id}/ava/sign/{pubkey}"]["post"]["requestBody"]["content"]["application/json"];
38
-
39
- export type EvmSignResponse =
40
- components["responses"]["Eth1SignResponse"]["content"]["application/json"];
41
- export type Eth2SignResponse =
42
- components["responses"]["Eth2SignResponse"]["content"]["application/json"];
43
- export type Eth2StakeResponse =
44
- components["responses"]["StakeResponse"]["content"]["application/json"];
45
- export type Eth2UnstakeResponse =
46
- components["responses"]["UnstakeResponse"]["content"]["application/json"];
47
- export type BlobSignResponse =
48
- components["responses"]["BlobSignResponse"]["content"]["application/json"];
49
- export type BtcSignResponse =
50
- components["responses"]["BtcSignResponse"]["content"]["application/json"];
51
- export type SolanaSignResponse =
52
- components["responses"]["SolanaSignResponse"]["content"]["application/json"];
53
- export type MfaRequestInfo =
54
- components["responses"]["MfaRequestInfo"]["content"]["application/json"];
55
- export type AvaSignResponse =
56
- components["responses"]["AvaSignResponse"]["content"]["application/json"];
57
-
58
- export type AcceptedResponse = components["schemas"]["AcceptedResponse"];
59
- export type ErrorResponse = components["schemas"]["ErrorResponse"];
60
- export type BtcSignatureKind = components["schemas"]["BtcSignatureKind"];
61
- /* eslint-enable */
62
-
63
- /** MFA request kind */
64
- export type MfaType = components["schemas"]["MfaType"];
65
-
66
- /** Ava P- or X-chain transaction */
67
- export type AvaTx = { P: AvaPChainTx } | { X: AvaXChainTx };
68
-
69
- /** Ava P-chain transaction */
70
- export type AvaPChainTx =
71
- | { AddPermissionlessValidator: JsonMap }
72
- | { AddSubnetValidator: JsonMap }
73
- | { AddValidator: JsonMap }
74
- | { CreateChain: JsonMap }
75
- | { CreateSubnet: JsonMap }
76
- | { Export: JsonMap }
77
- | { Import: JsonMap };
78
-
79
- /** Ava X-chain transaction */
80
- export type AvaXChainTx = { Base: JsonMap } | { Export: JsonMap } | { Import: JsonMap };
81
-
82
- type SignFn<U> = (headers?: HeadersInit) => Promise<U | AcceptedResponse>;
83
-
84
- export interface MfaRequired {
85
- /** Org id */
86
- org_id: string;
87
- /** MFA request id */
88
- id: string;
89
- /** Optional MFA session */
90
- session?: NewSessionResponse | null;
91
- }
92
-
93
- /**
94
- * A response of a CubeSigner request.
95
- */
96
- export class SignResponse<U> {
97
- readonly #signFn: SignFn<U>;
98
- readonly #resp: U | AcceptedResponse;
99
- /**
100
- * Optional MFA id. Only set if there is an MFA request associated with the
101
- * signing request
102
- */
103
- readonly #mfaRequired?: MfaRequired;
104
-
105
- /** @return {string} The MFA id associated with this request */
106
- mfaId(): string {
107
- return this.#mfaRequired!.id;
108
- }
109
-
110
- /** @return {boolean} True if this request requires an MFA approval */
111
- requiresMfa(): boolean {
112
- return this.#mfaRequired !== undefined;
113
- }
114
-
115
- /**
116
- * Returns session information to use for any MFA approval requests (if any was included in the response).
117
- * @return {ClientSessionInfo | undefined}
118
- */
119
- mfaSessionInfo(): NewSessionResponse | undefined {
120
- return (this.#resp as AcceptedResponse).accepted?.MfaRequired?.session ?? undefined;
121
- }
122
-
123
- /** @return {U} The signed data */
124
- data(): U {
125
- return this.#resp as U;
126
- }
127
-
128
- /**
129
- * Approves the MFA request using a given session and a TOTP code.
130
- *
131
- * @param {SignerSession} session Signer session to use
132
- * @param {string} code 6-digit TOTP code
133
- * @return {SignResponse<U>} The result of signing with the approval
134
- */
135
- async approveTotp(session: SignerSession, code: string): Promise<SignResponse<U>> {
136
- assert(this.requiresMfa());
137
- const mfaId = this.mfaId();
138
- const mfaOrgId = this.#mfaRequired!.org_id;
139
- const mfaApproval = await session.totpApprove(mfaId, code);
140
- assert(mfaApproval.id === mfaId);
141
- const mfaConf = mfaApproval.receipt?.confirmation;
142
-
143
- if (!mfaConf) {
144
- return this;
145
- }
146
-
147
- return await this.signWithMfaApproval({ mfaId, mfaOrgId, mfaConf });
148
- }
149
-
150
- /**
151
- * Approves the MFA request using a given `CubeSigner` instance (i.e., its management session).
152
- *
153
- * @param {CubeSigner} cs CubeSigner whose session to use
154
- * @return {SignResponse<U>} The result of signing with the approval
155
- */
156
- async approve(cs: CubeSigner): Promise<SignResponse<U>> {
157
- assert(this.requiresMfa());
158
- const mfaId = this.#mfaRequired!.id;
159
- const mfaOrgId = this.#mfaRequired!.org_id;
160
-
161
- const mfaApproval = await Org.mfaApprove(cs, mfaOrgId, mfaId);
162
- assert(mfaApproval.id === mfaId);
163
- const mfaConf = mfaApproval.receipt?.confirmation;
164
-
165
- if (!mfaConf) {
166
- return this;
167
- }
168
-
169
- return await this.signWithMfaApproval({ mfaId, mfaOrgId, mfaConf });
170
- }
171
-
172
- /**
173
- * @param {MfaReceipt} mfaReceipt The MFA receipt
174
- * @return {Promise<SignResponse<U>>} The result of signing after MFA approval
175
- */
176
- async signWithMfaApproval(mfaReceipt: MfaReceipt): Promise<SignResponse<U>> {
177
- const headers = SignResponse.getMfaHeaders(mfaReceipt);
178
- return new SignResponse(this.#signFn, await this.#signFn(headers));
179
- }
180
-
181
- // --------------------------------------------------------------------------
182
- // -- INTERNAL --------------------------------------------------------------
183
- // --------------------------------------------------------------------------
184
-
185
- /**
186
- * Constructor.
187
- *
188
- * @param {SignFn} signFn The signing function that this response is from.
189
- * This argument is used to resend requests with
190
- * different headers if needed.
191
- * @param {U | AcceptedResponse} resp The response as returned by the OpenAPI
192
- * client.
193
- */
194
- constructor(signFn: SignFn<U>, resp: U | AcceptedResponse) {
195
- this.#signFn = signFn;
196
- this.#resp = resp;
197
- this.#mfaRequired = (this.#resp as AcceptedResponse).accepted?.MfaRequired;
198
- }
199
-
200
- /**
201
- * Static constructor.
202
- * @param {SignFn} signFn The signing function that this response is from.
203
- * This argument is used to resend requests with
204
- * different headers if needed.
205
- * @param {MfaReceipt} mfaReceipt Optional MFA receipt
206
- * @return {Promise<SignResponse<U>>} New instance of this class.
207
- */
208
- static async create<U>(signFn: SignFn<U>, mfaReceipt?: MfaReceipt): Promise<SignResponse<U>> {
209
- const seed = await signFn(this.getMfaHeaders(mfaReceipt));
210
- return new SignResponse(signFn, seed);
211
- }
212
-
213
- /**
214
- * Returns HTTP headers containing a given MFA receipt.
215
- *
216
- * @param {MfaReceipt} mfaReceipt MFA receipt
217
- * @return {HeadersInit} Headers including that receipt
218
- */
219
- static getMfaHeaders(mfaReceipt?: MfaReceipt): HeadersInit | undefined {
220
- return mfaReceipt
221
- ? {
222
- "x-cubist-mfa-id": mfaReceipt.mfaId,
223
- "x-cubist-mfa-org-id": mfaReceipt.mfaOrgId,
224
- "x-cubist-mfa-confirmation": mfaReceipt.mfaConf,
225
- }
226
- : undefined;
227
- }
228
- }
1
+ import { CubeSignerClient } from "./client";
2
+ import { KeyInfo, toKeyInfo } from "./key";
3
+ import { SignerSessionManager, SignerSessionStorage } from "./session/signer_session_manager";
229
4
 
230
5
  /** Signer session info. Can only be used to revoke a token, but not for authentication. */
231
6
  export class SignerSessionInfo {
232
- readonly #cs: CubeSigner;
233
- readonly #orgId: string;
234
- readonly #roleId: string;
7
+ readonly #csc: CubeSignerClient;
235
8
  readonly #sessionId: string;
236
9
  public readonly purpose: string;
237
10
 
238
- /** Revoke this token */
11
+ /** Revoke this session */
239
12
  async revoke() {
240
- await SignerSession.revoke(this.#cs, this.#orgId, this.#roleId, this.#sessionId);
13
+ await this.#csc.sessionRevoke(this.#sessionId);
241
14
  }
242
15
 
243
16
  // --------------------------------------------------------------------------
@@ -246,353 +19,23 @@ export class SignerSessionInfo {
246
19
 
247
20
  /**
248
21
  * Internal constructor.
249
- * @param {CubeSigner} cs CubeSigner instance to use when calling `revoke`
250
- * @param {string} orgId Organization ID
251
- * @param {string} roleId Role ID
252
- * @param {string} hash The hash of the token; can be used for revocation but not for auth
22
+ * @param {CubeSignerClient} cs CubeSigner instance to use when calling `revoke`
23
+ * @param {string} sessionId The ID of the session; can be used for revocation but not for auth
253
24
  * @param {string} purpose Session purpose
254
25
  * @internal
255
26
  */
256
- constructor(cs: CubeSigner, orgId: string, roleId: string, hash: string, purpose: string) {
257
- this.#cs = cs;
258
- this.#orgId = orgId;
259
- this.#roleId = roleId;
260
- this.#sessionId = hash;
27
+ constructor(cs: CubeSignerClient, sessionId: string, purpose: string) {
28
+ this.#csc = cs;
29
+ this.#sessionId = sessionId;
261
30
  this.purpose = purpose;
262
31
  }
263
32
  }
264
33
 
265
- /** Signer session. */
266
- export class SignerSession {
267
- sessionMgr: SignerSessionManager;
268
- readonly #orgId: string;
269
-
270
- /** Org id */
271
- get orgId() {
272
- return this.#orgId;
273
- }
274
-
275
- /**
276
- * Returns the list of keys that this token grants access to.
277
- * @return {Key[]} The list of keys.
278
- */
279
- async keys(): Promise<KeyInfo[]> {
280
- const resp = await (
281
- await this.sessionMgr.client()
282
- ).get("/v0/org/{org_id}/token/keys", {
283
- params: { path: { org_id: this.#orgId } },
284
- parseAs: "json",
285
- });
286
- const data = assertOk(resp);
287
- return data.keys.map((k) => toKeyInfo(k));
288
- }
289
-
290
- /**
291
- * Approve a pending MFA request using TOTP.
292
- *
293
- * @param {string} mfaId The MFA request to approve
294
- * @param {string} code The TOTP code
295
- * @return {Promise<MfaRequestInfo>} The current status of the MFA request
296
- */
297
- async totpApprove(mfaId: string, code: string): Promise<MfaRequestInfo> {
298
- const resp = await (
299
- await this.sessionMgr.client()
300
- ).patch("/v0/org/{org_id}/mfa/{mfa_id}/totp", {
301
- params: { path: { org_id: this.#orgId, mfa_id: mfaId } },
302
- body: { code },
303
- parseAs: "json",
304
- });
305
- return assertOk(resp);
306
- }
307
-
308
- /**
309
- * Initiate approval of an existing MFA request using FIDO.
310
- * @param {string} mfaId The MFA request ID.
311
- * @return {Promise<MfaFidoChallenge>} A challenge that needs to be answered to complete the approval.
312
- */
313
- async fidoApproveStart(mfaId: string): Promise<MfaFidoChallenge> {
314
- const client = await this.sessionMgr.client();
315
- const resp = await client.post("/v0/org/{org_id}/mfa/{mfa_id}/fido", {
316
- params: { path: { org_id: this.#orgId, mfa_id: mfaId } },
317
- parseAs: "json",
318
- });
319
- const challenge = assertOk(resp);
320
- return new MfaFidoChallenge(this, mfaId, challenge);
321
- }
322
-
323
- /**
324
- * Complete a previously initiated MFA request approval using FIDO.
325
- * @param {string} mfaId The MFA request ID
326
- * @param {string} challengeId The challenge ID
327
- * @param {PublicKeyCredential} credential The answer to the challenge
328
- * @return {Promise<MfaRequestInfo>} The current status of the MFA request.
329
- */
330
- async fidoApproveComplete(
331
- mfaId: string,
332
- challengeId: string,
333
- credential: PublicKeyCredential,
334
- ): Promise<MfaRequestInfo> {
335
- const client = await this.sessionMgr.client();
336
- const resp = await client.patch("/v0/org/{org_id}/mfa/{mfa_id}/fido", {
337
- params: { path: { org_id: this.#orgId, mfa_id: mfaId } },
338
- body: {
339
- challenge_id: challengeId,
340
- credential,
341
- },
342
- parseAs: "json",
343
- });
344
- return assertOk(resp);
345
- }
346
-
347
- /**
348
- * Get a pending MFA request by its id.
349
- * @param {CubeSigner} cs Management session to use (this argument will be removed in future versions)
350
- * @param {string} mfaId The id of the MFA request.
351
- * @return {Promise<MfaRequestInfo>} The MFA request.
352
- */
353
- async getMfaInfo(cs: CubeSigner, mfaId: string): Promise<MfaRequestInfo> {
354
- const resp = await (
355
- await cs.management()
356
- ).get("/v0/org/{org_id}/mfa/{mfa_id}", {
357
- params: { path: { org_id: this.#orgId, mfa_id: mfaId } },
358
- });
359
- return assertOk(resp);
360
- }
361
-
362
- /**
363
- * Submit an EVM sign request.
364
- * @param {Key | string} key The key to sign with (either {@link Key} or its material ID).
365
- * @param {EvmSignRequest} req What to sign.
366
- * @param {MfaReceipt} mfaReceipt Optional MFA receipt.
367
- * @return {Promise<EvmSignResponse | AcceptedResponse>} Signature
368
- */
369
- async signEvm(
370
- key: Key | string,
371
- req: EvmSignRequest,
372
- mfaReceipt?: MfaReceipt,
373
- ): Promise<SignResponse<EvmSignResponse>> {
374
- const pubkey = typeof key === "string" ? (key as string) : key.materialId;
375
- const sign = async (headers?: HeadersInit) => {
376
- const resp = await (
377
- await this.sessionMgr.client()
378
- ).post("/v1/org/{org_id}/eth1/sign/{pubkey}", {
379
- params: { path: { org_id: this.#orgId, pubkey } },
380
- body: req,
381
- headers,
382
- parseAs: "json",
383
- });
384
- return assertOk(resp);
385
- };
386
- return await SignResponse.create(sign, mfaReceipt);
387
- }
388
-
389
- /**
390
- * Submit an 'eth2' sign request.
391
- * @param {Key | string} key The key to sign with (either {@link Key} or its material ID).
392
- * @param {Eth2SignRequest} req What to sign.
393
- * @param {MfaReceipt} mfaReceipt Optional MFA receipt
394
- * @return {Promise<Eth2SignResponse | AcceptedResponse>} Signature
395
- */
396
- async signEth2(
397
- key: Key | string,
398
- req: Eth2SignRequest,
399
- mfaReceipt?: MfaReceipt,
400
- ): Promise<SignResponse<Eth2SignResponse>> {
401
- const pubkey = typeof key === "string" ? (key as string) : key.materialId;
402
- const sign = async (headers?: HeadersInit) => {
403
- const resp = await (
404
- await this.sessionMgr.client()
405
- ).post("/v1/org/{org_id}/eth2/sign/{pubkey}", {
406
- params: { path: { org_id: this.#orgId, pubkey } },
407
- body: req,
408
- headers,
409
- parseAs: "json",
410
- });
411
- return assertOk(resp);
412
- };
413
- return await SignResponse.create(sign, mfaReceipt);
414
- }
415
-
416
- /**
417
- * Sign a stake request.
418
- * @param {Eth2StakeRequest} req The request to sign.
419
- * @param {MfaReceipt} mfaReceipt Optional MFA receipt
420
- * @return {Promise<Eth2StakeResponse | AcceptedResponse>} The response.
421
- */
422
- async stake(
423
- req: Eth2StakeRequest,
424
- mfaReceipt?: MfaReceipt,
425
- ): Promise<SignResponse<Eth2StakeResponse>> {
426
- const sign = async (headers?: HeadersInit) => {
427
- const resp = await (
428
- await this.sessionMgr.client()
429
- ).post("/v1/org/{org_id}/eth2/stake", {
430
- params: { path: { org_id: this.#orgId } },
431
- body: req,
432
- headers,
433
- parseAs: "json",
434
- });
435
- return assertOk(resp);
436
- };
437
- return await SignResponse.create(sign, mfaReceipt);
438
- }
439
-
440
- /**
441
- * Sign an unstake request.
442
- * @param {Key | string} key The key to sign with (either {@link Key} or its material ID).
443
- * @param {Eth2UnstakeRequest} req The request to sign.
444
- * @param {MfaReceipt} mfaReceipt Optional MFA receipt
445
- * @return {Promise<Eth2UnstakeResponse | AcceptedResponse>} The response.
446
- */
447
- async unstake(
448
- key: Key | string,
449
- req: Eth2UnstakeRequest,
450
- mfaReceipt?: MfaReceipt,
451
- ): Promise<SignResponse<Eth2UnstakeResponse>> {
452
- const pubkey = typeof key === "string" ? (key as string) : key.materialId;
453
- const sign = async (headers?: HeadersInit) => {
454
- const resp = await (
455
- await this.sessionMgr.client()
456
- ).post("/v1/org/{org_id}/eth2/unstake/{pubkey}", {
457
- params: { path: { org_id: this.#orgId, pubkey } },
458
- body: req,
459
- headers,
460
- parseAs: "json",
461
- });
462
- return assertOk(resp);
463
- };
464
- return await SignResponse.create(sign, mfaReceipt);
465
- }
466
-
467
- /**
468
- * Sign a raw blob.
469
- * @param {Key | string} key The key to sign with (either {@link Key} or its ID).
470
- * @param {BlobSignRequest} req What to sign
471
- * @param {MfaReceipt} mfaReceipt Optional MFA receipt
472
- * @return {Promise<BlobSignResponse | AcceptedResponse>} The response.
473
- */
474
- async signBlob(
475
- key: Key | string,
476
- req: BlobSignRequest,
477
- mfaReceipt?: MfaReceipt,
478
- ): Promise<SignResponse<BlobSignResponse>> {
479
- const key_id = typeof key === "string" ? (key as string) : key.id;
480
- const sign = async (headers?: HeadersInit) => {
481
- const resp = await (
482
- await this.sessionMgr.client()
483
- ).post("/v1/org/{org_id}/blob/sign/{key_id}", {
484
- params: {
485
- path: { org_id: this.#orgId, key_id },
486
- },
487
- body: req,
488
- headers,
489
- parseAs: "json",
490
- });
491
- return assertOk(resp);
492
- };
493
- return await SignResponse.create(sign, mfaReceipt);
494
- }
495
-
496
- /**
497
- * Sign a bitcoin message.
498
- * @param {Key | string} key The key to sign with (either {@link Key} or its material ID).
499
- * @param {BtcSignRequest} req What to sign
500
- * @param {MfaReceipt} mfaReceipt Optional MFA receipt
501
- * @return {Promise<BtcSignResponse | AcceptedResponse>} The response.
502
- */
503
- async signBtc(
504
- key: Key | string,
505
- req: BtcSignRequest,
506
- mfaReceipt?: MfaReceipt,
507
- ): Promise<SignResponse<BtcSignResponse>> {
508
- const pubkey = typeof key === "string" ? (key as string) : key.materialId;
509
- const sign = async (headers?: HeadersInit) => {
510
- const resp = await (
511
- await this.sessionMgr.client()
512
- ).post("/v0/org/{org_id}/btc/sign/{pubkey}", {
513
- params: {
514
- path: { org_id: this.#orgId, pubkey },
515
- },
516
- body: req,
517
- headers: headers,
518
- parseAs: "json",
519
- });
520
- return assertOk(resp);
521
- };
522
- return await SignResponse.create(sign, mfaReceipt);
523
- }
524
-
525
- /**
526
- * Sign a solana message.
527
- * @param {Key | string} key The key to sign with (either {@link Key} or its material ID).
528
- * @param {SolanaSignRequest} req What to sign
529
- * @param {MfaReceipt} mfaReceipt Optional MFA receipt
530
- * @return {Promise<SolanaSignResponse | AcceptedResponse>} The response.
531
- */
532
- async signSolana(
533
- key: Key | string,
534
- req: SolanaSignRequest,
535
- mfaReceipt?: MfaReceipt,
536
- ): Promise<SignResponse<SolanaSignResponse>> {
537
- const pubkey = typeof key === "string" ? (key as string) : key.materialId;
538
- const sign = async (headers?: HeadersInit) => {
539
- const resp = await (
540
- await this.sessionMgr.client()
541
- ).post("/v0/org/{org_id}/solana/sign/{pubkey}", {
542
- params: { path: { org_id: this.#orgId, pubkey } },
543
- body: req,
544
- headers,
545
- parseAs: "json",
546
- });
547
- return assertOk(resp);
548
- };
549
- return await SignResponse.create(sign, mfaReceipt);
550
- }
551
-
552
- /**
553
- * Sign an Avalanche P- or X-chain message.
554
- * @param {Key | string} key The key to sign with (either {@link Key} or its material ID).
555
- * @param {AvaTx} tx Avalanche message (transaction) to sign
556
- * @param {MfaReceipt} mfaReceipt Optional MFA receipt
557
- * @return {Promise<AvaSignResponse | AcceptedResponse>} The response.
558
- */
559
- async signAva(
560
- key: Key | string,
561
- tx: AvaTx,
562
- mfaReceipt?: MfaReceipt,
563
- ): Promise<SignResponse<AvaSignResponse>> {
564
- const pubkey = typeof key === "string" ? (key as string) : key.materialId;
565
- const sign = async (headers?: HeadersInit) => {
566
- const req = <AvaSignRequest>{
567
- tx: tx as unknown,
568
- };
569
- const resp = await (
570
- await this.sessionMgr.client()
571
- ).post("/v0/org/{org_id}/ava/sign/{pubkey}", {
572
- params: { path: { org_id: this.#orgId, pubkey } },
573
- body: req,
574
- headers,
575
- parseAs: "json",
576
- });
577
- return assertOk(resp);
578
- };
579
- return await SignResponse.create(sign, mfaReceipt);
580
- }
581
-
582
- /**
583
- * Obtain a proof of authentication.
584
- *
585
- * @return {Promise<IdentityProof>} Proof of authentication
586
- */
587
- async proveIdentity(): Promise<IdentityProof> {
588
- const client = await this.sessionMgr.client();
589
- const resp = await client.post("/v0/org/{org_id}/identity/prove", {
590
- params: { path: { org_id: this.#orgId } },
591
- parseAs: "json",
592
- });
593
- return assertOk(resp);
594
- }
595
-
34
+ /**
35
+ * Signer session.
36
+ * Extends {@link CubeSignerClient} and provides a few convenience methods on top.
37
+ */
38
+ export class SignerSession extends CubeSignerClient {
596
39
  /**
597
40
  * Loads an existing signer session from storage.
598
41
  * @param {SignerSessionStorage} storage The session storage to use
@@ -609,33 +52,15 @@ export class SignerSession {
609
52
  * @internal
610
53
  */
611
54
  constructor(sessionMgr: SignerSessionManager) {
612
- this.sessionMgr = sessionMgr;
613
- this.#orgId = sessionMgr.orgId;
55
+ super(sessionMgr);
614
56
  }
615
57
 
616
- // --------------------------------------------------------------------------
617
- // -- INTERNAL --------------------------------------------------------------
618
- // --------------------------------------------------------------------------
619
-
620
- /* eslint-disable require-jsdoc */
621
-
622
58
  /**
623
- * Static method for revoking a token (used both from {SignerSession} and {SignerSessionInfo}).
624
- * @param {CubeSigner} cs CubeSigner instance
625
- * @param {string} orgId Organization ID
626
- * @param {string} roleId Role ID
627
- * @param {string} sessionId Signer session ID
628
- * @internal
59
+ * Returns the list of keys that this token grants access to.
60
+ * @return {KeyInfo[]} The list of keys.
629
61
  */
630
- static async revoke(cs: CubeSigner, orgId: string, roleId: string, sessionId: string) {
631
- const resp = await (
632
- await cs.management()
633
- ).del("/v0/org/{org_id}/roles/{role_id}/tokens/{session_id}", {
634
- params: {
635
- path: { org_id: orgId, role_id: roleId, session_id: sessionId },
636
- },
637
- parseAs: "json",
638
- });
639
- assertOk(resp);
62
+ async keys(): Promise<KeyInfo[]> {
63
+ const keys = await this.sessionKeysList();
64
+ return keys.map((k) => toKeyInfo(k));
640
65
  }
641
66
  }