@cubist-labs/cubesigner-sdk 0.1.50 → 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.
Files changed (47) hide show
  1. package/README.md +66 -13
  2. package/dist/src/client.d.ts +434 -7
  3. package/dist/src/client.js +1022 -18
  4. package/dist/src/ethers/index.d.ts +2 -4
  5. package/dist/src/ethers/index.js +11 -9
  6. package/dist/src/fido.d.ts +76 -0
  7. package/dist/src/fido.js +148 -0
  8. package/dist/src/index.d.ts +102 -30
  9. package/dist/src/index.js +126 -72
  10. package/dist/src/key.d.ts +15 -45
  11. package/dist/src/key.js +31 -93
  12. package/dist/src/mfa.d.ts +85 -14
  13. package/dist/src/mfa.js +158 -40
  14. package/dist/src/org.d.ts +237 -123
  15. package/dist/src/org.js +108 -213
  16. package/dist/src/paginator.d.ts +76 -0
  17. package/dist/src/paginator.js +99 -0
  18. package/dist/src/role.d.ts +76 -74
  19. package/dist/src/role.js +79 -136
  20. package/dist/src/schema.d.ts +1672 -520
  21. package/dist/src/schema.js +1 -1
  22. package/dist/src/schema_types.d.ts +103 -0
  23. package/dist/src/schema_types.js +3 -0
  24. package/dist/src/session/session_manager.js +2 -2
  25. package/dist/src/session/session_storage.js +1 -1
  26. package/dist/src/session/signer_session_manager.d.ts +16 -29
  27. package/dist/src/session/signer_session_manager.js +27 -78
  28. package/dist/src/signer_session.d.ts +232 -125
  29. package/dist/src/signer_session.js +149 -250
  30. package/dist/src/util.d.ts +20 -0
  31. package/dist/src/util.js +31 -2
  32. package/package.json +13 -11
  33. package/src/client.ts +1217 -7
  34. package/src/ethers/index.ts +11 -18
  35. package/src/index.ts +149 -101
  36. package/src/key.ts +28 -121
  37. package/src/mfa.ts +202 -0
  38. package/src/org.ts +126 -275
  39. package/src/paginator.ts +122 -0
  40. package/src/role.ts +108 -181
  41. package/src/schema.ts +1673 -520
  42. package/src/schema_types.ts +103 -0
  43. package/src/session/session_manager.ts +2 -2
  44. package/src/session/session_storage.ts +1 -1
  45. package/src/session/signer_session_manager.ts +38 -108
  46. package/src/signer_session.ts +164 -323
  47. package/src/util.ts +41 -0
@@ -1,31 +1,31 @@
1
- import { CubeSigner, Key, KeyInfo } from ".";
2
- import { components, paths } from "./client";
3
- import { NewSessionResponse, SignerSessionManager, SignerSessionStorage } from "./session/signer_session_manager";
4
- export type EvmSignRequest = paths["/v1/org/{org_id}/eth1/sign/{pubkey}"]["post"]["requestBody"]["content"]["application/json"];
5
- export type Eth2SignRequest = paths["/v1/org/{org_id}/eth2/sign/{pubkey}"]["post"]["requestBody"]["content"]["application/json"];
6
- export type Eth2StakeRequest = paths["/v1/org/{org_id}/eth2/stake"]["post"]["requestBody"]["content"]["application/json"];
7
- export type Eth2UnstakeRequest = paths["/v1/org/{org_id}/eth2/unstake/{pubkey}"]["post"]["requestBody"]["content"]["application/json"];
8
- export type BlobSignRequest = paths["/v1/org/{org_id}/blob/sign/{key_id}"]["post"]["requestBody"]["content"]["application/json"];
9
- export type BtcSignRequest = paths["/v0/org/{org_id}/btc/sign/{pubkey}"]["post"]["requestBody"]["content"]["application/json"];
10
- export type SolanaSignRequest = paths["/v1/org/{org_id}/solana/sign/{pubkey}"]["post"]["requestBody"]["content"]["application/json"];
11
- export type EvmSignResponse = components["responses"]["Eth1SignResponse"]["content"]["application/json"];
12
- export type Eth2SignResponse = components["responses"]["Eth2SignResponse"]["content"]["application/json"];
13
- export type Eth2StakeResponse = components["responses"]["StakeResponse"]["content"]["application/json"];
14
- export type Eth2UnstakeResponse = components["responses"]["UnstakeResponse"]["content"]["application/json"];
15
- export type BlobSignResponse = components["responses"]["BlobSignResponse"]["content"]["application/json"];
16
- export type BtcSignResponse = components["responses"]["BtcSignResponse"]["content"]["application/json"];
17
- export type SolanaSignResponse = components["responses"]["SolanaSignResponse"]["content"]["application/json"];
18
- export type MfaRequestInfo = components["responses"]["MfaRequestInfo"]["content"]["application/json"];
19
- export type AcceptedResponse = components["schemas"]["AcceptedResponse"];
20
- export type ErrorResponse = components["schemas"]["ErrorResponse"];
21
- export type BtcSignatureKind = components["schemas"]["BtcSignatureKind"];
22
- /** MFA request kind */
23
- export type MfaType = components["schemas"]["MfaType"];
24
- type SignFn<U> = (headers?: HeadersInit) => Promise<U | AcceptedResponse>;
1
+ import { CubeSigner, MfaReceipt, KeyInfo } from ".";
2
+ import { CubeSignerClient } from "./client";
3
+ import { AcceptedResponse, NewSessionResponse } from "./schema_types";
4
+ import { SignerSessionManager, SignerSessionStorage } from "./session/signer_session_manager";
5
+ type Response<U> = U | AcceptedResponse;
6
+ type RequestFn<U> = (headers?: HeadersInit) => Promise<Response<U>>;
7
+ type MapFn<U, V> = (u: U) => V;
8
+ /**
9
+ * Takes a {@link Response<U>} and a {@link MapFn<U, V>} function and returns
10
+ * a {@link Response<V>} that maps the value of the original response when its status code is 200.
11
+ *
12
+ * @param {Response<U>} resp Original response
13
+ * @param {Map<U, V>} mapFn Map to apply to the response value when its status code is 200.
14
+ * @return {Response<V>} Response whose value for status code 200 is mapped from U to V
15
+ */
16
+ export declare function mapResponse<U, V>(resp: Response<U>, mapFn: MapFn<U, V>): Response<V>;
17
+ export interface MfaRequired {
18
+ /** Org id */
19
+ org_id: string;
20
+ /** MFA request id */
21
+ id: string;
22
+ /** Optional MFA session */
23
+ session?: NewSessionResponse | null;
24
+ }
25
25
  /**
26
26
  * A response of a CubeSigner request.
27
27
  */
28
- export declare class SignResponse<U> {
28
+ export declare class CubeSignerResponse<U> {
29
29
  #private;
30
30
  /** @return {string} The MFA id associated with this request */
31
31
  mfaId(): string;
@@ -36,137 +36,253 @@ export declare class SignResponse<U> {
36
36
  * @return {ClientSessionInfo | undefined}
37
37
  */
38
38
  mfaSessionInfo(): NewSessionResponse | undefined;
39
- /** @return {U} The signed data */
39
+ /** @return {U} The response data, if no MFA is required */
40
40
  data(): U;
41
41
  /**
42
42
  * Approves the MFA request using a given session and a TOTP code.
43
43
  *
44
44
  * @param {SignerSession} session Signer session to use
45
45
  * @param {string} code 6-digit TOTP code
46
- * @return {SignResponse<U>} The result of signing with the approval
46
+ * @return {CubeSignerResponse<U>} The result of signing with the approval
47
47
  */
48
- approveTotp(session: SignerSession, code: string): Promise<SignResponse<U>>;
48
+ approveTotp(session: SignerSession, code: string): Promise<CubeSignerResponse<U>>;
49
49
  /**
50
- * Approves the MFA request using a given `CubeSigner` instance (i.e., its management session).
50
+ * Approves the MFA request using a given `CubeSignerClient` instance (i.e., its session).
51
51
  *
52
52
  * @param {CubeSigner} cs CubeSigner whose session to use
53
- * @return {SignResponse<U>} The result of signing with the approval
53
+ * @return {CubeSignerResponse<U>} The result of signing with the approval
54
54
  */
55
- approve(cs: CubeSigner): Promise<SignResponse<U>>;
55
+ approve(cs: CubeSigner): Promise<CubeSignerResponse<U>>;
56
56
  /**
57
- * @param {MfaRequestInfo} mfaInfo The MFA request info with the approval
58
- * @return {Promise<SignResponse<U>>} The result of signing after MFA approval
57
+ * @param {MfaReceipt} mfaReceipt The MFA receipt
58
+ * @return {Promise<CubeSignerResponse<U>>} The result of signing after MFA approval
59
59
  */
60
- signWithMfaApproval(mfaInfo: MfaRequestInfo): Promise<SignResponse<U>>;
60
+ signWithMfaApproval(mfaReceipt: MfaReceipt): Promise<CubeSignerResponse<U>>;
61
61
  /**
62
62
  * Constructor.
63
63
  *
64
- * @param {string} orgId The org id of the corresponding signing request
65
- * @param {SignFn} signFn The signing function that this response is from.
66
- * This argument is used to resend requests with
67
- * different headers if needed.
68
- * @param {U | AcceptedResponse} resp The response as returned by the OpenAPI
69
- * client.
64
+ * @param {RequestFn} requestFn
65
+ * The signing function that this response is from.
66
+ * This argument is used to resend requests with different headers if needed.
67
+ * @param {U | AcceptedResponse} resp The response as returned by the OpenAPI client.
70
68
  */
71
- constructor(orgId: string, signFn: SignFn<U>, resp: U | AcceptedResponse);
69
+ constructor(requestFn: RequestFn<U>, resp: U | AcceptedResponse);
72
70
  /**
73
- * MFA receipt to attach.
71
+ * Static constructor.
72
+ * @param {RequestFn} requestFn
73
+ * The request function that this response is from.
74
+ * This argument is used to resend requests with different headers if needed.
75
+ * @param {MfaReceipt} mfaReceipt Optional MFA receipt
76
+ * @return {Promise<CubeSignerResponse<U>>} New instance of this class.
77
+ */
78
+ static create<U>(requestFn: RequestFn<U>, mfaReceipt?: MfaReceipt): Promise<CubeSignerResponse<U>>;
79
+ /**
80
+ * Returns HTTP headers containing a given MFA receipt.
74
81
  *
75
- * @param {string} mfaId MFA request id
76
- * @param {string} mfaConf MFA receipt confirmation code
77
- * @return {HeadersInit} Headers
82
+ * @param {MfaReceipt} mfaReceipt MFA receipt
83
+ * @return {HeadersInit} Headers including that receipt
78
84
  */
79
- static getMfaHeaders(mfaId: string, mfaConf: string): HeadersInit;
85
+ static getMfaHeaders(mfaReceipt?: MfaReceipt): HeadersInit | undefined;
80
86
  }
81
87
  /** Signer session info. Can only be used to revoke a token, but not for authentication. */
82
88
  export declare class SignerSessionInfo {
83
89
  #private;
84
90
  readonly purpose: string;
85
- /** Revoke this token */
91
+ /** Revoke this session */
86
92
  revoke(): Promise<void>;
87
93
  /**
88
94
  * Internal constructor.
89
- * @param {CubeSigner} cs CubeSigner instance to use when calling `revoke`
90
- * @param {string} orgId Organization ID
91
- * @param {string} roleId Role ID
92
- * @param {string} hash The hash of the token; can be used for revocation but not for auth
95
+ * @param {CubeSignerClient} cs CubeSigner instance to use when calling `revoke`
96
+ * @param {string} sessionId The ID of the session; can be used for revocation but not for auth
93
97
  * @param {string} purpose Session purpose
94
98
  * @internal
95
99
  */
96
- constructor(cs: CubeSigner, orgId: string, roleId: string, hash: string, purpose: string);
100
+ constructor(cs: CubeSignerClient, sessionId: string, purpose: string);
97
101
  }
98
- /** Signer session. */
102
+ /**
103
+ * Signer session.
104
+ *
105
+ * @deprecated Use {@link CubeSignerClient} instead.
106
+ */
99
107
  export declare class SignerSession {
100
108
  #private;
101
- sessionMgr: SignerSessionManager;
109
+ /** Deprecated */
110
+ get sessionMgr(): SignerSessionManager;
111
+ /** Org id */
112
+ get orgId(): string;
102
113
  /**
103
114
  * Returns the list of keys that this token grants access to.
104
- * @return {Key[]} The list of keys.
115
+ * @return {KeyInfo[]} The list of keys.
105
116
  */
106
117
  keys(): Promise<KeyInfo[]>;
118
+ /** Approve a pending MFA request using TOTP. */
119
+ get totpApprove(): (mfaId: string, code: string) => Promise<{
120
+ expires_at: number;
121
+ id: string;
122
+ receipt?: {
123
+ confirmation: string;
124
+ final_approver: string;
125
+ timestamp: number;
126
+ } | null | undefined;
127
+ request: {
128
+ body?: Record<string, unknown> | null | undefined;
129
+ method: string;
130
+ path: string;
131
+ };
132
+ status: {
133
+ allowed_approvers: string[];
134
+ allowed_mfa_types?: ("CubeSigner" | "Totp" | "Fido")[] | null | undefined;
135
+ approved_by: {
136
+ [key: string]: {
137
+ [key: string]: {
138
+ timestamp: number;
139
+ };
140
+ };
141
+ };
142
+ count: number;
143
+ num_auth_factors: number;
144
+ };
145
+ }>;
146
+ /** Initiate approval of an existing MFA request using FIDO. */
147
+ get fidoApproveStart(): (mfaId: string) => Promise<import("./mfa").MfaFidoChallenge>;
148
+ /** Get a pending MFA request by its id. */
149
+ get getMfaInfo(): (mfaId: string) => Promise<{
150
+ expires_at: number;
151
+ id: string;
152
+ receipt?: {
153
+ confirmation: string;
154
+ final_approver: string;
155
+ timestamp: number;
156
+ } | null | undefined;
157
+ request: {
158
+ body?: Record<string, unknown> | null | undefined;
159
+ method: string;
160
+ path: string;
161
+ };
162
+ status: {
163
+ allowed_approvers: string[];
164
+ allowed_mfa_types?: ("CubeSigner" | "Totp" | "Fido")[] | null | undefined;
165
+ approved_by: {
166
+ [key: string]: {
167
+ [key: string]: {
168
+ timestamp: number;
169
+ };
170
+ };
171
+ };
172
+ count: number;
173
+ num_auth_factors: number;
174
+ };
175
+ }>;
176
+ /** Submit an EVM sign request. */
177
+ get signEvm(): (key: string | import("./key").Key, req: {
178
+ chain_id: number;
179
+ tx: Record<string, never>;
180
+ }, mfaReceipt?: MfaReceipt | undefined) => Promise<CubeSignerResponse<{
181
+ rlp_signed_tx: string;
182
+ }>>;
183
+ /** Submit an 'eth2' sign request. */
184
+ get signEth2(): (key: string | import("./key").Key, req: {
185
+ eth2_sign_request: Record<string, never>;
186
+ network: "mainnet" | "prater" | "goerli" | "holesky";
187
+ }, mfaReceipt?: MfaReceipt | undefined) => Promise<CubeSignerResponse<{
188
+ signature: string;
189
+ }>>;
190
+ /** Sign a stake request. */
191
+ get stake(): (req: {
192
+ chain_id: number;
193
+ deposit_type: "Canonical" | "Wrapper";
194
+ staking_amount_gwei?: number | undefined;
195
+ unsafe_conf?: {
196
+ deposit_contract_addr?: string | null | undefined;
197
+ genesis_fork_version?: string | null | undefined;
198
+ } | null | undefined;
199
+ validator_key?: string | null | undefined;
200
+ withdrawal_addr: string;
201
+ }, mfaReceipt?: MfaReceipt | undefined) => Promise<CubeSignerResponse<{
202
+ created_validator_key_id: string;
203
+ deposit_tx: {
204
+ chain_id: number;
205
+ deposit_txn: Record<string, never>;
206
+ new_validator_pk: string;
207
+ };
208
+ }>>;
209
+ /** Sign an unstake request. */
210
+ get unstake(): (key: string | import("./key").Key, req: {
211
+ epoch?: string | null | undefined;
212
+ fork: {
213
+ current_version: string;
214
+ epoch: string;
215
+ previous_version: string;
216
+ };
217
+ genesis_data: {
218
+ genesis_fork_version: string;
219
+ genesis_time: string;
220
+ genesis_validators_root: string;
221
+ };
222
+ network: "mainnet" | "prater" | "goerli" | "holesky";
223
+ validator_index: string;
224
+ }, mfaReceipt?: MfaReceipt | undefined) => Promise<CubeSignerResponse<{
225
+ message: {
226
+ epoch: string;
227
+ validator_index: string;
228
+ };
229
+ signature: string;
230
+ }>>;
231
+ /** Sign a raw blob.*/
232
+ get signBlob(): (key: string | import("./key").Key, req: {
233
+ message_base64: string;
234
+ }, mfaReceipt?: MfaReceipt | undefined) => Promise<CubeSignerResponse<{
235
+ signature: string;
236
+ }>>;
237
+ /** Sign a bitcoin message. */
238
+ get signBtc(): (key: string | import("./key").Key, req: {
239
+ sig_kind: {
240
+ Segwit: {
241
+ input_index: number;
242
+ script_code: string;
243
+ sighash_type: "All" | "None" | "Single" | "AllPlusAnyoneCanPay" | "NonePlusAnyoneCanPay" | "SinglePlusAnyoneCanPay";
244
+ value: number;
245
+ };
246
+ };
247
+ tx: Record<string, never>;
248
+ }, mfaReceipt?: MfaReceipt | undefined) => Promise<CubeSignerResponse<{
249
+ signature: string;
250
+ }>>;
251
+ /** Sign a solana message. */
252
+ get signSolana(): (key: string | import("./key").Key, req: {
253
+ message_base64: string;
254
+ }, mfaReceipt?: MfaReceipt | undefined) => Promise<CubeSignerResponse<{
255
+ signature: string;
256
+ }>>;
257
+ /** Sign an Avalanche P- or X-chain message. */
258
+ get signAva(): (key: string | import("./key").Key, tx: import("./schema_types").AvaTx, mfaReceipt?: MfaReceipt | undefined) => Promise<CubeSignerResponse<{
259
+ signature: string;
260
+ }>>;
107
261
  /**
108
- * Approve a pending MFA request using TOTP.
109
- *
110
- * @param {string} mfaId The MFA request to approve
111
- * @param {string} code The TOTP code
112
- * @return {Promise<MfaRequestInfo>} The current status of the MFA request
113
- */
114
- totpApprove(mfaId: string, code: string): Promise<MfaRequestInfo>;
115
- /**
116
- * Get a pending MFA request by its id.
117
- * @param {CubeSigner} cs Management session to use (this argument will be removed in future versions)
118
- * @param {string} mfaId The id of the MFA request.
119
- * @return {Promise<MfaRequestInfo>} The MFA request.
120
- */
121
- getMfaInfo(cs: CubeSigner, mfaId: string): Promise<MfaRequestInfo>;
122
- /**
123
- * Submit an EVM sign request.
124
- * @param {Key | string} key The key to sign with (either {@link Key} or its material ID).
125
- * @param {EvmSignRequest} req What to sign.
126
- * @return {Promise<EvmSignResponse | AcceptedResponse>} Signature
127
- */
128
- signEvm(key: Key | string, req: EvmSignRequest): Promise<SignResponse<EvmSignResponse>>;
129
- /**
130
- * Submit an 'eth2' sign request.
131
- * @param {Key | string} key The key to sign with (either {@link Key} or its material ID).
132
- * @param {Eth2SignRequest} req What to sign.
133
- * @return {Promise<Eth2SignResponse | AcceptedResponse>} Signature
262
+ * Obtain a proof of authentication.
134
263
  */
135
- signEth2(key: Key | string, req: Eth2SignRequest): Promise<SignResponse<Eth2SignResponse>>;
136
- /**
137
- * Sign a stake request.
138
- * @param {Eth2StakeRequest} req The request to sign.
139
- * @return {Promise<Eth2StakeResponse | AcceptedResponse>} The response.
140
- */
141
- stake(req: Eth2StakeRequest): Promise<SignResponse<Eth2StakeResponse>>;
142
- /**
143
- * Sign an unstake request.
144
- * @param {Key | string} key The key to sign with (either {@link Key} or its material ID).
145
- * @param {Eth2UnstakeRequest} req The request to sign.
146
- * @return {Promise<Eth2UnstakeResponse | AcceptedResponse>} The response.
147
- */
148
- unstake(key: Key | string, req: Eth2UnstakeRequest): Promise<SignResponse<Eth2UnstakeResponse>>;
149
- /**
150
- * Sign a raw blob.
151
- * @param {Key | string} key The key to sign with (either {@link Key} or its ID).
152
- * @param {BlobSignRequest} req What to sign
153
- * @return {Promise<BlobSignResponse | AcceptedResponse>} The response.
154
- */
155
- signBlob(key: Key | string, req: BlobSignRequest): Promise<SignResponse<BlobSignResponse>>;
156
- /**
157
- * Sign a bitcoin message.
158
- * @param {Key | string} key The key to sign with (either {@link Key} or its material ID).
159
- * @param {BtcSignRequest} req What to sign
160
- * @return {Promise<BtcSignResponse | AcceptedResponse>} The response.
161
- */
162
- signBtc(key: Key | string, req: BtcSignRequest): Promise<SignResponse<BtcSignResponse>>;
163
- /**
164
- * Sign a solana message.
165
- * @param {Key | string} key The key to sign with (either {@link Key} or its material ID).
166
- * @param {SolanaSignRequest} req What to sign
167
- * @return {Promise<SolanaSignResponse | AcceptedResponse>} The response.
168
- */
169
- signSolana(key: Key | string, req: SolanaSignRequest): Promise<SignResponse<SolanaSignResponse>>;
264
+ get proveIdentity(): () => Promise<{
265
+ aud?: string | null | undefined;
266
+ email: string;
267
+ exp_epoch: number;
268
+ identity?: {
269
+ iss: string;
270
+ sub: string;
271
+ } | null | undefined;
272
+ user_info?: {
273
+ configured_mfa: ({
274
+ type: "totp";
275
+ } | {
276
+ id: string;
277
+ name: string;
278
+ type: "fido";
279
+ })[];
280
+ initialized: boolean;
281
+ user_id: string;
282
+ } | null | undefined;
283
+ } & {
284
+ id: string;
285
+ }>;
170
286
  /**
171
287
  * Loads an existing signer session from storage.
172
288
  * @param {SignerSessionStorage} storage The session storage to use
@@ -179,14 +295,5 @@ export declare class SignerSession {
179
295
  * @internal
180
296
  */
181
297
  constructor(sessionMgr: SignerSessionManager);
182
- /**
183
- * Static method for revoking a token (used both from {SignerSession} and {SignerSessionInfo}).
184
- * @param {CubeSigner} cs CubeSigner instance
185
- * @param {string} orgId Organization ID
186
- * @param {string} roleId Role ID
187
- * @param {string} sessionId Signer session ID
188
- * @internal
189
- */
190
- static revoke(cs: CubeSigner, orgId: string, roleId: string, sessionId: string): Promise<void>;
191
298
  }
192
299
  export {};