@cubist-labs/cubesigner-sdk 0.4.241 → 0.4.246

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 (61) hide show
  1. package/dist/package.json +1 -1
  2. package/dist/spec/env/gamma.json +5 -0
  3. package/dist/src/audit_log.d.ts +5 -5
  4. package/dist/src/audit_log.js +1 -1
  5. package/dist/src/client/api_client.d.ts +22 -7
  6. package/dist/src/client/api_client.d.ts.map +1 -1
  7. package/dist/src/client/api_client.js +43 -13
  8. package/dist/src/client/base_client.d.ts +5 -1
  9. package/dist/src/client/base_client.d.ts.map +1 -1
  10. package/dist/src/client/base_client.js +16 -10
  11. package/dist/src/client/session.d.ts +3 -1
  12. package/dist/src/client/session.d.ts.map +1 -1
  13. package/dist/src/client/session.js +7 -3
  14. package/dist/src/client.d.ts +12 -5
  15. package/dist/src/client.d.ts.map +1 -1
  16. package/dist/src/client.js +15 -4
  17. package/dist/src/diffie_hellman.js +2 -2
  18. package/dist/src/env.d.ts +7 -0
  19. package/dist/src/env.d.ts.map +1 -1
  20. package/dist/src/env.js +14 -1
  21. package/dist/src/fetch.d.ts.map +1 -1
  22. package/dist/src/fetch.js +2 -3
  23. package/dist/src/key.d.ts +17 -1
  24. package/dist/src/key.d.ts.map +1 -1
  25. package/dist/src/key.js +19 -3
  26. package/dist/src/policy.d.ts +24 -7
  27. package/dist/src/policy.d.ts.map +1 -1
  28. package/dist/src/policy.js +34 -16
  29. package/dist/src/response.d.ts +29 -5
  30. package/dist/src/response.d.ts.map +1 -1
  31. package/dist/src/response.js +62 -24
  32. package/dist/src/schema.d.ts +975 -84
  33. package/dist/src/schema.d.ts.map +1 -1
  34. package/dist/src/schema.js +1 -1
  35. package/dist/src/schema_types.d.ts +10 -1
  36. package/dist/src/schema_types.d.ts.map +1 -1
  37. package/dist/src/schema_types.js +2 -1
  38. package/dist/src/scopes.d.ts.map +1 -1
  39. package/dist/src/scopes.js +23 -3
  40. package/dist/src/user_export.d.ts +1 -1
  41. package/dist/src/user_export.js +3 -3
  42. package/dist/src/util.d.ts +7 -0
  43. package/dist/src/util.d.ts.map +1 -1
  44. package/dist/src/util.js +30 -7
  45. package/package.json +1 -1
  46. package/src/audit_log.ts +3 -5
  47. package/src/client/api_client.ts +54 -14
  48. package/src/client/base_client.ts +13 -3
  49. package/src/client/session.ts +10 -3
  50. package/src/client.ts +17 -4
  51. package/src/diffie_hellman.ts +1 -1
  52. package/src/env.ts +17 -0
  53. package/src/fetch.ts +1 -2
  54. package/src/key.ts +26 -2
  55. package/src/policy.ts +34 -16
  56. package/src/response.ts +72 -25
  57. package/src/schema.ts +1034 -92
  58. package/src/schema_types.ts +12 -1
  59. package/src/scopes.ts +22 -2
  60. package/src/user_export.ts +2 -2
  61. package/src/util.ts +34 -8
package/src/policy.ts CHANGED
@@ -21,7 +21,7 @@ import type {
21
21
  PolicyInfo,
22
22
  } from ".";
23
23
 
24
- import { loadSubtleCrypto } from ".";
24
+ import { loadSubtleCrypto, encodeToHex, decodeFromHex } from ".";
25
25
 
26
26
  /**
27
27
  * Named policy rule type.
@@ -104,7 +104,7 @@ export async function uploadWasmFunction(
104
104
  // get the SHA-256 hash of the function to get the upload url.
105
105
  const subtle = await loadSubtleCrypto();
106
106
  const hashBytes = await subtle.digest("SHA-256", policy);
107
- const hash = "0x" + Buffer.from(hashBytes).toString("hex");
107
+ const hash = encodeToHex(new Uint8Array(hashBytes));
108
108
 
109
109
  // get the upload URL
110
110
  const { signed_url } = await apiClient.wasmPolicyUpload({ hash });
@@ -619,28 +619,46 @@ export class C2FInvocation {
619
619
  return this.#data.response;
620
620
  }
621
621
 
622
- /** @returns The standard output stream. Usually a UTF-8 encoded string. */
623
- get stdout(): Buffer {
624
- return this.fromHex(this.#data.stdout);
622
+ /**
623
+ * The standard output stream as raw bytes. Usually a UTF-8 encoded string,
624
+ * use {@link TextDecoder} to decode.
625
+ *
626
+ * @returns The standard output stream.
627
+ */
628
+ get stdoutBytes(): Uint8Array {
629
+ return decodeFromHex(this.#data.stdout);
625
630
  }
626
631
 
627
- /** @returns The standard error stream. Usually a UTF-8 encoded string. */
628
- get stderr(): Buffer {
629
- return this.fromHex(this.#data.stderr);
632
+ /**
633
+ * The standard error stream as raw bytes. Usually a UTF-8 encoded string, use
634
+ * {@link TextDecoder} to decode.
635
+ *
636
+ * @returns The standard error stream.
637
+ */
638
+ get stderrBytes(): Uint8Array {
639
+ return decodeFromHex(this.#data.stderr);
630
640
  }
631
641
 
632
- // --------------------------------------------------------------------------
633
- // -- INTERNAL --------------------------------------------------------------
634
- // --------------------------------------------------------------------------
642
+ /**
643
+ * The standard output stream as a Buffer. Usually a UTF-8 encoded string.
644
+ *
645
+ * @returns The standard output stream.
646
+ * @deprecated Use {@link stdoutBytes} instead for browser compatibility.
647
+ */
648
+ get stdout(): Buffer {
649
+ // eslint-disable-next-line no-restricted-globals -- Buffer return type preserved for backwards compatibility
650
+ return Buffer.from(this.stdoutBytes);
651
+ }
635
652
 
636
653
  /**
637
- * Helper method for converting hex-encoded data to a `Buffer`.
654
+ * The standard error stream as a Buffer. Usually a UTF-8 encoded string.
638
655
  *
639
- * @param hex The hex-encoded data.
640
- * @returns The data.
656
+ * @returns The standard error stream.
657
+ * @deprecated Use {@link stderrBytes} instead for browser compatibility.
641
658
  */
642
- private fromHex(hex: string): Buffer {
643
- return Buffer.from(hex.startsWith("0x") ? hex.slice(2) : hex, "hex");
659
+ get stderr(): Buffer {
660
+ // eslint-disable-next-line no-restricted-globals -- Buffer return type preserved for backwards compatibility
661
+ return Buffer.from(this.stderrBytes);
644
662
  }
645
663
 
646
664
  /**
package/src/response.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { MfaVote, EnvInterface, MfaReceipts, MfaRequired } from ".";
2
- import { CubeSignerClient, isManyMfaReceipts } from ".";
2
+ import { CubeSignerClient, MultiRegionEnv, isManyMfaReceipts } from ".";
3
3
  import { encodeToBase64Url } from "./util";
4
- import type { AcceptedResponse } from "./schema_types";
4
+ import type { AcceptedResponse, AcceptedValue, BinanceDryRun, SignDryRun } from "./schema_types";
5
5
 
6
6
  /**
7
7
  * Response type, which can be either a value of type {@link U}
@@ -29,54 +29,100 @@ export type MapFn<U, V> = (u: U) => V;
29
29
  * @returns Response whose value for status code 200 is mapped from U to V
30
30
  */
31
31
  export function mapResponse<U, V>(resp: Response<U>, mapFn: MapFn<U, V>): Response<V> {
32
- if ((resp as AcceptedResponse).accepted?.MfaRequired) {
32
+ if (asAccepted(resp) !== undefined) {
33
33
  return resp as AcceptedResponse;
34
34
  } else {
35
35
  return mapFn(resp as U);
36
36
  }
37
37
  }
38
38
 
39
+ /**
40
+ * @param resp The response to check
41
+ * @returns The {@link AcceptedValue} if the response status code is 202.
42
+ */
43
+ function asAccepted<U>(resp: Response<U>): AcceptedValue | undefined {
44
+ const acceptedResp = resp as AcceptedResponse;
45
+ return acceptedResp.error_code === "SignDryRun" || acceptedResp.error_code === "MfaRequired"
46
+ ? (acceptedResp.accepted ?? undefined)
47
+ : undefined;
48
+ }
49
+
39
50
  /**
40
51
  * A response of a CubeSigner request.
41
52
  */
42
53
  export class CubeSignerResponse<U> {
43
- readonly #env: EnvInterface;
54
+ readonly #env: MultiRegionEnv;
44
55
  readonly #requestFn: RequestFn<U>;
45
- readonly #resp: U | AcceptedResponse;
56
+ readonly #resp: Response<U>;
57
+
58
+ /**
59
+ * @returns The {@link AcceptedValue} if the response status code is 202.
60
+ */
61
+ asAccepted(): AcceptedValue | undefined {
62
+ return asAccepted(this.#resp);
63
+ }
64
+
65
+ /**
66
+ * @returns The associated {@link MfaRequired} value, if the response status code is 202 and the response indicates that MFA is required.
67
+ */
68
+ asMfaRequired(): MfaRequired | undefined {
69
+ return this.asAccepted()?.MfaRequired ?? undefined;
70
+ }
71
+
46
72
  /**
47
- * Optional MFA id. Only set if there is an MFA request associated with the request
73
+ * @returns The associated {@link SignDryRun} value, if the response status code is 202 and the response is a dry run of a sign operation.
48
74
  */
49
- readonly #mfaRequired?: MfaRequired;
75
+ asSignDryRun(): SignDryRun | undefined {
76
+ return this.asAccepted()?.SignDryRun ?? undefined;
77
+ }
78
+
79
+ /**
80
+ * @returns The associated {@link BinanceDryRun} value, if the response status code is 202 and the response is a dry run of a sign operation.
81
+ */
82
+ asBinanceDryRun(): BinanceDryRun | undefined {
83
+ return this.asAccepted()?.BinanceDryRun ?? undefined;
84
+ }
85
+
86
+ /**
87
+ * @returns Whether this response is a "200 Success" (in which case it is safe to call {@link data})
88
+ */
89
+ isSuccess(): boolean {
90
+ return this.asAccepted() === undefined;
91
+ }
92
+
93
+ /**
94
+ * @returns The underlying {@link MfaRequired} response (if any).
95
+ */
96
+ private get mfaRequired() {
97
+ return this.asAccepted()?.MfaRequired;
98
+ }
50
99
 
51
100
  /** @returns The first MFA id associated with this request (if any) */
52
101
  mfaId(): string | undefined {
53
- return this.#mfaRequired?.id;
102
+ return this.mfaRequired?.id;
54
103
  }
55
104
 
56
105
  /** @returns The MFA ids associated with this request (if any) */
57
106
  mfaIds(): string[] {
58
- return this.#mfaRequired?.ids ?? [];
107
+ return this.mfaRequired?.ids ?? [];
59
108
  }
60
109
 
61
110
  /** @returns True if this request requires an MFA approval */
62
111
  requiresMfa(): boolean {
63
- return this.#mfaRequired !== undefined;
112
+ return this.mfaRequired !== undefined;
64
113
  }
65
114
 
66
115
  /**
67
116
  * @returns Session information to use for any MFA approval requests (if any was included in the response).
68
117
  */
69
118
  async mfaClient(): Promise<CubeSignerClient | undefined> {
70
- if (this.#mfaRequired === undefined) return;
71
- const session = (this.#resp as AcceptedResponse).accepted?.MfaRequired?.session ?? undefined;
119
+ if (this.mfaRequired === undefined) return;
120
+ const session = this.asMfaRequired()?.session ?? undefined;
72
121
  if (session === undefined) return;
73
122
 
74
- const env = this.#env;
75
123
  return await CubeSignerClient.create({
76
- env: {
77
- "Dev-CubeSignerStack": env,
78
- },
79
- org_id: this.#mfaRequired.org_id,
124
+ env: this.#env.spec,
125
+ org_id: this.mfaRequired.org_id,
80
126
  session_exp: session.expiration,
81
127
  session_info: session.session_info,
82
128
  token: session.token,
@@ -86,8 +132,10 @@ export class CubeSignerResponse<U> {
86
132
 
87
133
  /** @returns The response data, if no MFA is required */
88
134
  data(): U {
89
- if (this.requiresMfa()) {
90
- throw new Error("Cannot call `data()` while MFA is required");
135
+ if (!this.isSuccess()) {
136
+ throw new Error(
137
+ "Cannot call `data()` on a 202 Accepted response; use `asMfaRequired()` or `asSignDryRun()`",
138
+ );
91
139
  }
92
140
  return this.#resp as U;
93
141
  }
@@ -131,7 +179,7 @@ export class CubeSignerResponse<U> {
131
179
  return this;
132
180
  }
133
181
 
134
- const mfaOrgId = this.#mfaRequired!.org_id;
182
+ const mfaOrgId = this.mfaRequired!.org_id;
135
183
  const mfaApproval = await client.apiClient.mfaVoteTotp(mfaId, code, vote);
136
184
  const mfaConf = mfaApproval.receipt?.confirmation;
137
185
 
@@ -174,7 +222,7 @@ export class CubeSignerResponse<U> {
174
222
  return this;
175
223
  }
176
224
 
177
- const mfaOrgId = this.#mfaRequired!.org_id;
225
+ const mfaOrgId = this.mfaRequired!.org_id;
178
226
 
179
227
  const mfaApproval = await client.apiClient.mfaVoteCs(mfaId, mfaVote);
180
228
  const mfaConf = mfaApproval.receipt?.confirmation;
@@ -211,11 +259,10 @@ export class CubeSignerResponse<U> {
211
259
  * @param resp The response as returned by the OpenAPI client.
212
260
  * @internal
213
261
  */
214
- protected constructor(env: EnvInterface, requestFn: RequestFn<U>, resp: U | AcceptedResponse) {
262
+ protected constructor(env: MultiRegionEnv, requestFn: RequestFn<U>, resp: Response<U>) {
215
263
  this.#env = env;
216
264
  this.#requestFn = requestFn;
217
265
  this.#resp = resp;
218
- this.#mfaRequired = (this.#resp as AcceptedResponse).accepted?.MfaRequired;
219
266
  }
220
267
 
221
268
  /**
@@ -230,12 +277,12 @@ export class CubeSignerResponse<U> {
230
277
  * @internal
231
278
  */
232
279
  static async create<U>(
233
- env: EnvInterface,
280
+ env: EnvInterface | MultiRegionEnv,
234
281
  requestFn: RequestFn<U>,
235
282
  mfaReceipt?: MfaReceipts,
236
283
  ): Promise<CubeSignerResponse<U>> {
237
284
  const seed = await requestFn(this.getMfaHeaders(mfaReceipt));
238
- return new CubeSignerResponse(env, requestFn, seed);
285
+ return new CubeSignerResponse(MultiRegionEnv.create(env), requestFn, seed);
239
286
  }
240
287
 
241
288
  /**