@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.
@@ -1,85 +1,28 @@
1
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>;
2
+ import { CubeSigner, toKeyInfo, MfaReceipt, KeyInfo } from ".";
3
+ import { CubeSignerClient } from "./client";
4
+ import { AcceptedResponse, NewSessionResponse } from "./schema_types";
5
+ import { SignerSessionManager, SignerSessionStorage } from "./session/signer_session_manager";
6
+
7
+ type Response<U> = U | AcceptedResponse;
8
+ type RequestFn<U> = (headers?: HeadersInit) => Promise<Response<U>>;
9
+ type MapFn<U, V> = (u: U) => V;
10
+
11
+ /**
12
+ * Takes a {@link Response<U>} and a {@link MapFn<U, V>} function and returns
13
+ * a {@link Response<V>} that maps the value of the original response when its status code is 200.
14
+ *
15
+ * @param {Response<U>} resp Original response
16
+ * @param {Map<U, V>} mapFn Map to apply to the response value when its status code is 200.
17
+ * @return {Response<V>} Response whose value for status code 200 is mapped from U to V
18
+ */
19
+ export function mapResponse<U, V>(resp: Response<U>, mapFn: MapFn<U, V>): Response<V> {
20
+ if ((resp as AcceptedResponse).accepted?.MfaRequired) {
21
+ return resp as AcceptedResponse;
22
+ } else {
23
+ return mapFn(resp as U);
24
+ }
25
+ }
83
26
 
84
27
  export interface MfaRequired {
85
28
  /** Org id */
@@ -93,8 +36,8 @@ export interface MfaRequired {
93
36
  /**
94
37
  * A response of a CubeSigner request.
95
38
  */
96
- export class SignResponse<U> {
97
- readonly #signFn: SignFn<U>;
39
+ export class CubeSignerResponse<U> {
40
+ readonly #requestFn: RequestFn<U>;
98
41
  readonly #resp: U | AcceptedResponse;
99
42
  /**
100
43
  * Optional MFA id. Only set if there is an MFA request associated with the
@@ -120,8 +63,11 @@ export class SignResponse<U> {
120
63
  return (this.#resp as AcceptedResponse).accepted?.MfaRequired?.session ?? undefined;
121
64
  }
122
65
 
123
- /** @return {U} The signed data */
66
+ /** @return {U} The response data, if no MFA is required */
124
67
  data(): U {
68
+ if (this.requiresMfa()) {
69
+ throw new Error("Cannot call `data()` while MFA is required");
70
+ }
125
71
  return this.#resp as U;
126
72
  }
127
73
 
@@ -130,9 +76,9 @@ export class SignResponse<U> {
130
76
  *
131
77
  * @param {SignerSession} session Signer session to use
132
78
  * @param {string} code 6-digit TOTP code
133
- * @return {SignResponse<U>} The result of signing with the approval
79
+ * @return {CubeSignerResponse<U>} The result of signing with the approval
134
80
  */
135
- async approveTotp(session: SignerSession, code: string): Promise<SignResponse<U>> {
81
+ async approveTotp(session: SignerSession, code: string): Promise<CubeSignerResponse<U>> {
136
82
  assert(this.requiresMfa());
137
83
  const mfaId = this.mfaId();
138
84
  const mfaOrgId = this.#mfaRequired!.org_id;
@@ -148,17 +94,17 @@ export class SignResponse<U> {
148
94
  }
149
95
 
150
96
  /**
151
- * Approves the MFA request using a given `CubeSigner` instance (i.e., its management session).
97
+ * Approves the MFA request using a given `CubeSignerClient` instance (i.e., its session).
152
98
  *
153
99
  * @param {CubeSigner} cs CubeSigner whose session to use
154
- * @return {SignResponse<U>} The result of signing with the approval
100
+ * @return {CubeSignerResponse<U>} The result of signing with the approval
155
101
  */
156
- async approve(cs: CubeSigner): Promise<SignResponse<U>> {
102
+ async approve(cs: CubeSigner): Promise<CubeSignerResponse<U>> {
157
103
  assert(this.requiresMfa());
158
104
  const mfaId = this.#mfaRequired!.id;
159
105
  const mfaOrgId = this.#mfaRequired!.org_id;
160
106
 
161
- const mfaApproval = await Org.mfaApprove(cs, mfaOrgId, mfaId);
107
+ const mfaApproval = await cs.mfaApprove(mfaOrgId, mfaId);
162
108
  assert(mfaApproval.id === mfaId);
163
109
  const mfaConf = mfaApproval.receipt?.confirmation;
164
110
 
@@ -171,11 +117,11 @@ export class SignResponse<U> {
171
117
 
172
118
  /**
173
119
  * @param {MfaReceipt} mfaReceipt The MFA receipt
174
- * @return {Promise<SignResponse<U>>} The result of signing after MFA approval
120
+ * @return {Promise<CubeSignerResponse<U>>} The result of signing after MFA approval
175
121
  */
176
- async signWithMfaApproval(mfaReceipt: MfaReceipt): Promise<SignResponse<U>> {
177
- const headers = SignResponse.getMfaHeaders(mfaReceipt);
178
- return new SignResponse(this.#signFn, await this.#signFn(headers));
122
+ async signWithMfaApproval(mfaReceipt: MfaReceipt): Promise<CubeSignerResponse<U>> {
123
+ const headers = CubeSignerResponse.getMfaHeaders(mfaReceipt);
124
+ return new CubeSignerResponse(this.#requestFn, await this.#requestFn(headers));
179
125
  }
180
126
 
181
127
  // --------------------------------------------------------------------------
@@ -185,29 +131,31 @@ export class SignResponse<U> {
185
131
  /**
186
132
  * Constructor.
187
133
  *
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.
134
+ * @param {RequestFn} requestFn
135
+ * The signing function that this response is from.
136
+ * This argument is used to resend requests with different headers if needed.
137
+ * @param {U | AcceptedResponse} resp The response as returned by the OpenAPI client.
193
138
  */
194
- constructor(signFn: SignFn<U>, resp: U | AcceptedResponse) {
195
- this.#signFn = signFn;
139
+ constructor(requestFn: RequestFn<U>, resp: U | AcceptedResponse) {
140
+ this.#requestFn = requestFn;
196
141
  this.#resp = resp;
197
142
  this.#mfaRequired = (this.#resp as AcceptedResponse).accepted?.MfaRequired;
198
143
  }
199
144
 
200
145
  /**
201
146
  * 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.
147
+ * @param {RequestFn} requestFn
148
+ * The request function that this response is from.
149
+ * This argument is used to resend requests with different headers if needed.
205
150
  * @param {MfaReceipt} mfaReceipt Optional MFA receipt
206
- * @return {Promise<SignResponse<U>>} New instance of this class.
151
+ * @return {Promise<CubeSignerResponse<U>>} New instance of this class.
207
152
  */
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);
153
+ static async create<U>(
154
+ requestFn: RequestFn<U>,
155
+ mfaReceipt?: MfaReceipt,
156
+ ): Promise<CubeSignerResponse<U>> {
157
+ const seed = await requestFn(this.getMfaHeaders(mfaReceipt));
158
+ return new CubeSignerResponse(requestFn, seed);
211
159
  }
212
160
 
213
161
  /**
@@ -229,15 +177,13 @@ export class SignResponse<U> {
229
177
 
230
178
  /** Signer session info. Can only be used to revoke a token, but not for authentication. */
231
179
  export class SignerSessionInfo {
232
- readonly #cs: CubeSigner;
233
- readonly #orgId: string;
234
- readonly #roleId: string;
180
+ readonly #csc: CubeSignerClient;
235
181
  readonly #sessionId: string;
236
182
  public readonly purpose: string;
237
183
 
238
- /** Revoke this token */
184
+ /** Revoke this session */
239
185
  async revoke() {
240
- await SignerSession.revoke(this.#cs, this.#orgId, this.#roleId, this.#sessionId);
186
+ await this.#csc.sessionRevoke(this.#sessionId);
241
187
  }
242
188
 
243
189
  // --------------------------------------------------------------------------
@@ -246,351 +192,105 @@ export class SignerSessionInfo {
246
192
 
247
193
  /**
248
194
  * 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
195
+ * @param {CubeSignerClient} cs CubeSigner instance to use when calling `revoke`
196
+ * @param {string} sessionId The ID of the session; can be used for revocation but not for auth
253
197
  * @param {string} purpose Session purpose
254
198
  * @internal
255
199
  */
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;
200
+ constructor(cs: CubeSignerClient, sessionId: string, purpose: string) {
201
+ this.#csc = cs;
202
+ this.#sessionId = sessionId;
261
203
  this.purpose = purpose;
262
204
  }
263
205
  }
264
206
 
265
- /** Signer session. */
207
+ /**
208
+ * Signer session.
209
+ *
210
+ * @deprecated Use {@link CubeSignerClient} instead.
211
+ */
266
212
  export class SignerSession {
267
- sessionMgr: SignerSessionManager;
268
- readonly #orgId: string;
213
+ readonly #csc: CubeSignerClient;
214
+
215
+ /** Deprecated */
216
+ get sessionMgr() {
217
+ return this.#csc.sessionMgr;
218
+ }
269
219
 
270
220
  /** Org id */
271
221
  get orgId() {
272
- return this.#orgId;
222
+ return this.#csc.orgId;
273
223
  }
274
224
 
275
225
  /**
276
226
  * Returns the list of keys that this token grants access to.
277
- * @return {Key[]} The list of keys.
227
+ * @return {KeyInfo[]} The list of keys.
278
228
  */
279
229
  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));
230
+ const keys = await this.#csc.sessionKeysList();
231
+ return keys.map((k) => toKeyInfo(k));
288
232
  }
289
233
 
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);
234
+ /** Approve a pending MFA request using TOTP. */
235
+ get totpApprove() {
236
+ return this.#csc.mfaApproveTotp.bind(this.#csc);
306
237
  }
307
238
 
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);
239
+ /** Initiate approval of an existing MFA request using FIDO. */
240
+ get fidoApproveStart() {
241
+ return this.#csc.mfaApproveFidoInit.bind(this.#csc);
321
242
  }
322
243
 
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);
244
+ /** Get a pending MFA request by its id. */
245
+ get getMfaInfo() {
246
+ return this.#csc.mfaGet.bind(this.#csc);
345
247
  }
346
248
 
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);
249
+ /** Submit an EVM sign request. */
250
+ get signEvm() {
251
+ return this.#csc.signEvm.bind(this.#csc);
360
252
  }
361
253
 
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);
254
+ /** Submit an 'eth2' sign request. */
255
+ get signEth2() {
256
+ return this.#csc.signEth2.bind(this.#csc);
387
257
  }
388
258
 
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);
259
+ /** Sign a stake request. */
260
+ get stake() {
261
+ return this.#csc.signStake.bind(this.#csc);
414
262
  }
415
263
 
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);
264
+ /** Sign an unstake request. */
265
+ get unstake() {
266
+ return this.#csc.signUnstake.bind(this.#csc);
438
267
  }
439
268
 
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);
269
+ /** Sign a raw blob.*/
270
+ get signBlob() {
271
+ return this.#csc.signBlob.bind(this.#csc);
494
272
  }
495
273
 
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);
274
+ /** Sign a bitcoin message. */
275
+ get signBtc() {
276
+ return this.#csc.signBtc.bind(this.#csc);
523
277
  }
524
278
 
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);
279
+ /** Sign a solana message. */
280
+ get signSolana() {
281
+ return this.#csc.signSolana.bind(this.#csc);
550
282
  }
551
283
 
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);
284
+ /** Sign an Avalanche P- or X-chain message. */
285
+ get signAva() {
286
+ return this.#csc.signAva.bind(this.#csc);
580
287
  }
581
288
 
582
289
  /**
583
290
  * Obtain a proof of authentication.
584
- *
585
- * @return {Promise<IdentityProof>} Proof of authentication
586
291
  */
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);
292
+ get proveIdentity() {
293
+ return this.#csc.identityProve.bind(this.#csc);
594
294
  }
595
295
 
596
296
  /**
@@ -609,33 +309,6 @@ export class SignerSession {
609
309
  * @internal
610
310
  */
611
311
  constructor(sessionMgr: SignerSessionManager) {
612
- this.sessionMgr = sessionMgr;
613
- this.#orgId = sessionMgr.orgId;
614
- }
615
-
616
- // --------------------------------------------------------------------------
617
- // -- INTERNAL --------------------------------------------------------------
618
- // --------------------------------------------------------------------------
619
-
620
- /* eslint-disable require-jsdoc */
621
-
622
- /**
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
629
- */
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);
312
+ this.#csc = new CubeSignerClient(sessionMgr);
640
313
  }
641
314
  }