@cubist-labs/cubesigner-sdk 0.2.17 → 0.2.21

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/dist/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cubist-labs/cubesigner-sdk",
3
3
  "author": "Cubist, Inc.",
4
- "version": "0.2.17",
4
+ "version": "0.2.21",
5
5
  "description": "CubeSigner TypeScript SDK",
6
6
  "homepage": "https://github.com/cubist-labs/CubeSigner-TypeScript-SDK",
7
7
  "bugs": "https://github.com/cubist-labs/CubeSigner-TypeScript-SDK/issues",
@@ -33,19 +33,19 @@
33
33
  },
34
34
  "devDependencies": {
35
35
  "@hpke/core": "^1.2.5",
36
- "@types/chai": "^4.3.5",
36
+ "@types/chai": "^4.3.11",
37
37
  "@types/chai-as-promised": "^7.1.8",
38
38
  "@types/jest": "^29.5.10",
39
39
  "@types/node": "^20.9.2",
40
40
  "@types/node-fetch": "^2.6.9",
41
41
  "@types/tmp": "^0.2.3",
42
- "@typescript-eslint/eslint-plugin": "^6.12.0",
43
- "chai": "^4.3.7",
42
+ "@typescript-eslint/eslint-plugin": "^6.13.1",
43
+ "chai": "^4.3.10",
44
44
  "chai-as-promised": "^7.1.1",
45
45
  "dotenv": "^16.3.1",
46
- "eslint": "^8.54.0",
46
+ "eslint": "^8.55.0",
47
47
  "eslint-config-google": "^0.14.0",
48
- "eslint-config-prettier": "^8.8.0",
48
+ "eslint-config-prettier": "^9.1.0",
49
49
  "jest": "^29.7.0",
50
50
  "openapi-typescript": "^6.7.1",
51
51
  "otplib": "^12.0.1",
package/dist/src/api.d.ts CHANGED
@@ -1,16 +1,107 @@
1
- import createClient from "openapi-fetch";
2
- import { paths } from "./schema";
1
+ import createClient, { FetchOptions, FetchResponse, FilterKeys, HttpMethod, PathsWith } from "openapi-fetch";
2
+ import { paths, operations } from "./schema";
3
3
  import { SignerSessionData, SignerSessionLifetime, SignerSessionManager } from "./session/signer_session_manager";
4
- import { CreateOidcUserOptions, IdentityProof, KeyInRoleInfo, KeyInfoApi, ListKeysResponse, ListRoleKeysResponse, ListRoleUsersResponse, ListRolesResponse, OidcIdentity, SessionsResponse, PublicKeyCredential, RoleInfo, UpdateKeyRequest, UpdateOrgRequest, UpdateOrgResponse, UpdateRoleRequest, UserIdInfo, UserInRoleInfo, UserInfo, SessionInfo, OrgInfo, RatchetConfig, EvmSignRequest, EvmSignResponse, Eth2SignRequest, Eth2SignResponse, Eth2StakeRequest, Eth2StakeResponse, Eth2UnstakeRequest, Eth2UnstakeResponse, BlobSignRequest, BlobSignResponse, BtcSignResponse, BtcSignRequest, SolanaSignRequest, SolanaSignResponse, AvaSignResponse, AvaTx, MfaRequestInfo, MemberRole, UserExportCompleteResponse, UserExportInitResponse, UserExportListResponse } from "./schema_types";
4
+ import { CreateOidcUserOptions, IdentityProof, KeyInRoleInfo, KeyInfoApi, ListKeysResponse, ListRoleKeysResponse, ListRoleUsersResponse, ListRolesResponse, OidcIdentity, SessionsResponse, PublicKeyCredential, RoleInfo, UpdateKeyRequest, UpdateOrgRequest, UpdateOrgResponse, UpdateRoleRequest, UserIdInfo, UserInRoleInfo, UserInfo, SessionInfo, OrgInfo, RatchetConfig, EvmSignRequest, EvmSignResponse, Eth2SignRequest, Eth2SignResponse, Eth2StakeRequest, Eth2StakeResponse, Eth2UnstakeRequest, Eth2UnstakeResponse, BlobSignRequest, BlobSignResponse, BtcSignResponse, BtcSignRequest, SolanaSignRequest, SolanaSignResponse, AvaSignResponse, AvaTx, MfaRequestInfo, MemberRole, UserExportCompleteResponse, UserExportInitResponse, UserExportListResponse, Empty } from "./schema_types";
5
5
  import { AddFidoChallenge, MfaFidoChallenge, MfaReceipt, TotpChallenge } from "./mfa";
6
6
  import { CubeSignerResponse } from "./response";
7
7
  import { Key, KeyType } from "./key";
8
8
  import { PageOpts, Paginator } from "./paginator";
9
9
  import { KeyPolicy } from "./role";
10
10
  import { EnvInterface } from "./env";
11
+ import { EventEmitter } from "./events";
11
12
  /** @internal */
12
13
  export type Client = ReturnType<typeof createClient<paths>>;
13
- export { paths };
14
+ export { paths, operations };
15
+ /**
16
+ * Omit routes in {@link T} whose methods are all 'never'
17
+ */
18
+ type OmitNeverPaths<T extends paths> = {
19
+ [p in keyof T as T[p] extends {
20
+ [m in keyof T[p]]: never;
21
+ } ? never : p]: T[p];
22
+ };
23
+ /**
24
+ * Filter out methods that don't match operation {@link Op}
25
+ */
26
+ type FilterPaths<Op extends keyof operations> = {
27
+ [p in keyof paths]: {
28
+ [m in HttpMethod as m extends keyof paths[p] ? m : never]: m extends keyof paths[p] ? operations[Op] extends paths[p][m] ? paths[p][m] extends operations[Op] ? operations[Op] : never : never : never;
29
+ };
30
+ };
31
+ type Paths<Op extends keyof operations> = OmitNeverPaths<FilterPaths<Op>>;
32
+ /**
33
+ * Open-fetch client restricted to the route that corresponds to operation {@link Op}
34
+ */
35
+ export type FetchClient<Op extends keyof operations> = ReturnType<typeof createClient<Paths<Op>>>;
36
+ /**
37
+ * Type alias for the type of the response body (the "data" field of
38
+ * {@link FetchResponse<T>}) when that response is successful.
39
+ */
40
+ export type FetchResponseSuccessData<T> = Required<FetchResponse<T>>["data"];
41
+ /**
42
+ * Error response type, thrown on non-successful responses.
43
+ */
44
+ export declare class ErrResponse extends Error {
45
+ /** Operation that produced this error */
46
+ readonly operation?: keyof operations;
47
+ /** HTTP status code text (derived from `this.status`) */
48
+ readonly statusText?: string;
49
+ /** HTTP status code */
50
+ readonly status?: number;
51
+ /** HTTP response url */
52
+ readonly url?: string;
53
+ /**
54
+ * @param {Partial<ErrResponse>} init Initializer
55
+ */
56
+ constructor(init: Partial<ErrResponse>);
57
+ }
58
+ /**
59
+ * Wrapper around an open-fetch client restricted to a single operation.
60
+ * The restriction applies only when type checking, the actual
61
+ * client does not restrict anything at runtime.
62
+ * client does not restrict anything at runtime
63
+ */
64
+ export declare class OpClient<Op extends keyof operations> {
65
+ #private;
66
+ /**
67
+ * @param {Op} op The operation this client should be restricted to
68
+ * @param {FetchClient<Op> | Client} client open-fetch client (either restricted to {@link Op} or not)
69
+ * @param {EventEmitter} eventEmitter The client-local event dispatcher.
70
+ */
71
+ constructor(op: Op, client: FetchClient<Op> | Client, eventEmitter: EventEmitter);
72
+ /** The operation this client is restricted to */
73
+ get op(): Op;
74
+ /**
75
+ * Inspects the response and returns the response body if the request was successful.
76
+ * Otherwise, dispatches the error to event listeners, then throws {@link ErrResponse}.
77
+ *
78
+ * @param {FetchResponse<T>} resp The response to check
79
+ * @return {FetchResponseSuccessData<T>} The response data corresponding to response type {@link T}.
80
+ */
81
+ private assertOk;
82
+ /**
83
+ * Invoke HTTP GET
84
+ */
85
+ get(url: PathsWith<Paths<Op>, "get">, init: FetchOptions<FilterKeys<Paths<Op>[PathsWith<Paths<Op>, "get">], "get">>): Promise<("get" extends keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "get">] ? OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "get">][keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "get">] & "get"] : unknown) extends infer T ? T extends ("get" extends keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "get">] ? OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "get">][keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "get">] & "get"] : unknown) ? T extends {
86
+ responses: any;
87
+ } ? NonNullable<FilterKeys<import("openapi-fetch").Success<T["responses"]>, `${string}/${string}`>> : unknown : never : never>;
88
+ /** Invoke HTTP POST */
89
+ post(url: PathsWith<Paths<Op>, "post">, init: FetchOptions<FilterKeys<Paths<Op>[PathsWith<Paths<Op>, "post">], "post">>): Promise<("post" extends keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "post">] ? OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "post">][keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "post">] & "post"] : unknown) extends infer T ? T extends ("post" extends keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "post">] ? OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "post">][keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "post">] & "post"] : unknown) ? T extends {
90
+ responses: any;
91
+ } ? NonNullable<FilterKeys<import("openapi-fetch").Success<T["responses"]>, `${string}/${string}`>> : unknown : never : never>;
92
+ /** Invoke HTTP PATCH */
93
+ patch(url: PathsWith<Paths<Op>, "patch">, init: FetchOptions<FilterKeys<Paths<Op>[PathsWith<Paths<Op>, "patch">], "patch">>): Promise<("patch" extends keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "patch">] ? OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "patch">][keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "patch">] & "patch"] : unknown) extends infer T ? T extends ("patch" extends keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "patch">] ? OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "patch">][keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "patch">] & "patch"] : unknown) ? T extends {
94
+ responses: any;
95
+ } ? NonNullable<FilterKeys<import("openapi-fetch").Success<T["responses"]>, `${string}/${string}`>> : unknown : never : never>;
96
+ /** Invoke HTTP DELETE */
97
+ del(url: PathsWith<Paths<Op>, "delete">, init: FetchOptions<FilterKeys<Paths<Op>[PathsWith<Paths<Op>, "delete">], "delete">>): Promise<("delete" extends keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "delete">] ? OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "delete">][keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "delete">] & "delete"] : unknown) extends infer T ? T extends ("delete" extends keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "delete">] ? OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "delete">][keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "delete">] & "delete"] : unknown) ? T extends {
98
+ responses: any;
99
+ } ? NonNullable<FilterKeys<import("openapi-fetch").Success<T["responses"]>, `${string}/${string}`>> : unknown : never : never>;
100
+ /** Invoke HTTP PUT */
101
+ put(url: PathsWith<Paths<Op>, "put">, init: FetchOptions<FilterKeys<Paths<Op>[PathsWith<Paths<Op>, "put">], "put">>): Promise<("put" extends keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "put">] ? OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "put">][keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "put">] & "put"] : unknown) extends infer T ? T extends ("put" extends keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "put">] ? OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "put">][keyof OmitNeverPaths<FilterPaths<Op>>[PathsWith<OmitNeverPaths<FilterPaths<Op>>, "put">] & "put"] : unknown) ? T extends {
102
+ responses: any;
103
+ } ? NonNullable<FilterKeys<import("openapi-fetch").Success<T["responses"]>, `${string}/${string}`>> : unknown : never : never>;
104
+ }
14
105
  /**
15
106
  * Creates a new HTTP client, setting the "User-Agent" header to this package's {name}@{version}.
16
107
  *
@@ -44,6 +135,14 @@ export declare class CubeSignerApi {
44
135
  withOrg(orgId?: string): CubeSignerApi;
45
136
  /** Org id or name */
46
137
  get orgId(): string;
138
+ /**
139
+ * HTTP client restricted to a single operation. The restriction applies only
140
+ * when type checking, the actual client does not restrict anything at runtime.
141
+ *
142
+ * @param {Op} op The operation to restrict the client to
143
+ * @return {Promise<OpClient<Op>>} The client restricted to {@link op}
144
+ */
145
+ private client;
47
146
  /**
48
147
  * Obtain information about the current user.
49
148
  *
@@ -53,14 +152,14 @@ export declare class CubeSignerApi {
53
152
  /**
54
153
  * Creates a request to change user's TOTP. Returns a {@link TotpChallenge}
55
154
  * that must be answered either by calling {@link TotpChallenge.answer} (or
56
- * {@link CubeSignerApi.userResetTotpComplete}).
155
+ * {@link CubeSignerApi.userTotpResetComplete}).
57
156
  *
58
157
  * @param {string} issuer Optional issuer; defaults to "Cubist"
59
158
  * @param {MfaReceipt} mfaReceipt MFA receipt to include in HTTP headers
60
159
  */
61
- userResetTotpInit(issuer?: string, mfaReceipt?: MfaReceipt): Promise<CubeSignerResponse<TotpChallenge>>;
160
+ userTotpResetInit(issuer?: string, mfaReceipt?: MfaReceipt): Promise<CubeSignerResponse<TotpChallenge>>;
62
161
  /**
63
- * Answer the TOTP challenge issued by {@link userResetTotpInit}. If successful, user's
162
+ * Answer the TOTP challenge issued by {@link userTotpResetInit}. If successful, user's
64
163
  * TOTP configuration will be updated to that of the TOTP challenge.
65
164
  *
66
165
  * Instead of calling this method directly, prefer {@link TotpChallenge.answer}.
@@ -68,26 +167,34 @@ export declare class CubeSignerApi {
68
167
  * @param {string} totpId - The ID of the TOTP challenge
69
168
  * @param {string} code - The TOTP code that should verify against the TOTP configuration from the challenge.
70
169
  */
71
- userResetTotpComplete(totpId: string, code: string): Promise<void>;
170
+ userTotpResetComplete(totpId: string, code: string): Promise<void>;
72
171
  /**
73
172
  * Verifies a given TOTP code against the current user's TOTP configuration.
74
173
  * Throws an error if the verification fails.
75
174
  *
76
175
  * @param {string} code Current TOTP code
77
176
  */
78
- userVerifyTotp(code: string): Promise<void>;
177
+ userTotpVerify(code: string): Promise<void>;
178
+ /**
179
+ * Delete TOTP from the user's account.
180
+ * Allowed only if at least one FIDO key is registered with the user's account.
181
+ * MFA via FIDO is always required.
182
+ *
183
+ * @param {MfaReceipt} mfaReceipt Optional MFA receipt to include in HTTP headers
184
+ */
185
+ userTotpDelete(mfaReceipt?: MfaReceipt): Promise<CubeSignerResponse<Empty>>;
79
186
  /**
80
187
  * Initiate adding a new FIDO device. MFA may be required. This returns a {@link AddFidoChallenge}
81
- * that must be answered with {@link AddFidoChallenge.answer} or {@link userRegisterFidoComplete}
188
+ * that must be answered with {@link AddFidoChallenge.answer} or {@link userFidoRegisterComplete}
82
189
  * (after MFA approvals).
83
190
  *
84
191
  * @param {string} name The name of the new device.
85
192
  * @param {MfaReceipt} mfaReceipt Optional MFA receipt to include in HTTP headers
86
193
  * @return {Promise<CubeSignerResponse<AddFidoChallenge>>} A challenge that must be answered in order to complete FIDO registration.
87
194
  */
88
- userRegisterFidoInit(name: string, mfaReceipt?: MfaReceipt): Promise<CubeSignerResponse<AddFidoChallenge>>;
195
+ userFidoRegisterInit(name: string, mfaReceipt?: MfaReceipt): Promise<CubeSignerResponse<AddFidoChallenge>>;
89
196
  /**
90
- * Complete a previously initiated (via {@link userRegisterFidoInit}) request to add a new FIDO device.
197
+ * Complete a previously initiated (via {@link userFidoRegisterInit}) request to add a new FIDO device.
91
198
  *
92
199
  * Instead of calling this method directly, prefer {@link AddFidoChallenge.answer} or
93
200
  * {@link AddFidoChallenge.createCredentialAndAnswer}.
@@ -95,7 +202,16 @@ export declare class CubeSignerApi {
95
202
  * @param {string} challengeId The ID of the challenge returned by the remote end.
96
203
  * @param {PublicKeyCredential} credential The answer to the challenge.
97
204
  */
98
- userRegisterFidoComplete(challengeId: string, credential: PublicKeyCredential): Promise<void>;
205
+ userFidoRegisterComplete(challengeId: string, credential: PublicKeyCredential): Promise<void>;
206
+ /**
207
+ * Delete a FIDO key from the user's account.
208
+ * Allowed only if TOTP is also defined.
209
+ * MFA via TOTP is always required.
210
+ *
211
+ * @param {string} fidoId The ID of the desired FIDO key
212
+ * @param {MfaReceipt} mfaReceipt Optional MFA receipt to include in HTTP headers
213
+ */
214
+ userFidoDelete(fidoId: string, mfaReceipt?: MfaReceipt): Promise<CubeSignerResponse<Empty>>;
99
215
  /**
100
216
  * Obtain information about the current organization.
101
217
  * @return {OrgInfo} Information about the organization.
@@ -255,6 +371,15 @@ export declare class CubeSignerApi {
255
371
  * @return {Paginator<ListRoleUsersResponse, UserInRoleInfo>} Paginator for iterating over the users in the role.
256
372
  */
257
373
  roleUsersList(roleId: string, page?: PageOpts): Paginator<ListRoleUsersResponse, UserInRoleInfo>;
374
+ /**
375
+ * Create new user session (management and/or signing)
376
+ *
377
+ * @param {string} purpose The purpose of the session
378
+ * @param {string[]} scopes Session scopes.
379
+ * @param {SignerSessionLifetime} lifetimes Lifetime settings
380
+ * @return {Promise<SignerSessionData>} New signer session info.
381
+ */
382
+ sessionCreate(purpose: string, scopes: string[], lifetimes?: SignerSessionLifetime): Promise<SignerSessionData>;
258
383
  /**
259
384
  * Create a new signer session for a given role.
260
385
  *
@@ -428,8 +553,6 @@ export declare class CubeSignerApi {
428
553
  * @return {Promise<SolanaSignResponse | AcceptedResponse>} The response.
429
554
  */
430
555
  signSolana(key: Key | string, req: SolanaSignRequest, mfaReceipt?: MfaReceipt): Promise<CubeSignerResponse<SolanaSignResponse>>;
431
- /** HTTPS client */
432
- private client;
433
556
  /**
434
557
  * List outstanding user-export requests.
435
558
  *
@@ -476,6 +599,13 @@ export declare class OidcClient {
476
599
  * @param {string} oidcToken User's OIDC token
477
600
  */
478
601
  constructor(env: EnvInterface, orgId: string, oidcToken: string);
602
+ /**
603
+ * HTTP client restricted to a single operation.
604
+ *
605
+ * @param {Op} op The operation to restrict the client to
606
+ * @return {OpClient<Op>} The client restricted to {@link op}
607
+ */
608
+ private client;
479
609
  /**
480
610
  * Exchange an OIDC token for a CubeSigner session token.
481
611
  * @param {List<string>} scopes The scopes for the new session