@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.
package/src/org.ts CHANGED
@@ -1,16 +1,8 @@
1
- import {
2
- CubeSigner,
3
- MfaRequestInfo,
4
- IdentityProof,
5
- PageOpts,
6
- Page,
7
- PageQueryArgs,
8
- Paginator,
9
- } from ".";
10
- import { components, paths } from "./client";
11
- import { assertOk } from "./util";
1
+ import { OrgInfo } from "./schema_types";
2
+ import { CubeSignerClient } from "./client";
12
3
  import { KeyType, Key } from "./key";
13
- import { MfaPolicy, Role, RoleInfo } from "./role";
4
+ import { Role } from "./role";
5
+ import { PageOpts } from "./paginator";
14
6
 
15
7
  /** Organization id */
16
8
  export type OrgId = string;
@@ -54,27 +46,10 @@ export interface MaxDailyUnstakePolicy {
54
46
  MaxDailyUnstake: number;
55
47
  }
56
48
 
57
- type OrgInfo = components["schemas"]["OrgInfo"];
58
- type UserIdInfo = components["schemas"]["UserIdInfo"];
59
- type UpdateOrgRequest =
60
- paths["/v0/org/{org_id}"]["patch"]["requestBody"]["content"]["application/json"];
61
- type UpdateOrgResponse =
62
- paths["/v0/org/{org_id}"]["patch"]["responses"]["200"]["content"]["application/json"];
63
-
64
- export type OidcIdentity = components["schemas"]["OIDCIdentity"];
65
- export type MemberRole = components["schemas"]["MemberRole"];
66
-
67
- /** Options for a new OIDC user */
68
- export interface CreateOidcUserOptions {
69
- /** The role of an OIDC user, default is "Alien" */
70
- memberRole?: MemberRole;
71
- /** Optional MFA policy to associate with the user account */
72
- mfaPolicy?: MfaPolicy;
73
- }
74
-
75
49
  /** An organization. */
76
50
  export class Org {
77
- readonly #cs: CubeSigner;
51
+ readonly #csc: CubeSignerClient;
52
+
78
53
  /**
79
54
  * The ID of the organization.
80
55
  * @example Org#124dfe3e-3bbd-487d-80c0-53c55e8ab87a
@@ -84,47 +59,48 @@ export class Org {
84
59
  /**
85
60
  * @description The org id
86
61
  * @example Org#c3b9379c-4e8c-4216-bd0a-65ace53cf98f
87
- * */
62
+ */
88
63
  get id(): OrgId {
89
64
  return this.#id;
90
65
  }
91
66
 
92
67
  /** Human-readable name for the org */
93
68
  async name(): Promise<string | undefined> {
94
- const data = await this.fetch();
69
+ const data = await this.#csc.orgGet();
95
70
  return data.name ?? undefined;
96
71
  }
97
72
 
98
- /** Set the human-readable name for the org.
73
+ /**
74
+ * Set the human-readable name for the org.
99
75
  * @param {string} name The new human-readable name for the org (must be alphanumeric).
100
76
  * @example my_org_name
101
- * */
77
+ */
102
78
  async setName(name: string) {
103
79
  if (!/^[a-zA-Z0-9_]{3,30}$/.test(name)) {
104
80
  throw new Error("Org name must be alphanumeric and between 3 and 30 characters");
105
81
  }
106
- await this.update({ name });
82
+ await this.#csc.orgUpdate({ name });
107
83
  }
108
84
 
109
85
  /** Is the org enabled? */
110
86
  async enabled(): Promise<boolean> {
111
- const data = await this.fetch();
87
+ const data = await this.#csc.orgGet();
112
88
  return data.enabled;
113
89
  }
114
90
 
115
91
  /** Enable the org. */
116
92
  async enable() {
117
- await this.update({ enabled: true });
93
+ await this.#csc.orgUpdate({ enabled: true });
118
94
  }
119
95
 
120
96
  /** Disable the org. */
121
97
  async disable() {
122
- await this.update({ enabled: false });
98
+ await this.#csc.orgUpdate({ enabled: false });
123
99
  }
124
100
 
125
101
  /** Get the policy for the org. */
126
102
  async policy(): Promise<OrgPolicy[]> {
127
- const data = await this.fetch();
103
+ const data = await this.#csc.orgGet();
128
104
  return (data.policy ?? []) as unknown as OrgPolicy[];
129
105
  }
130
106
 
@@ -133,30 +109,33 @@ export class Org {
133
109
  * */
134
110
  async setPolicy(policy: OrgPolicy[]) {
135
111
  const p = policy as unknown as Record<string, never>[];
136
- await this.update({ policy: p });
112
+ await this.#csc.orgUpdate({ policy: p });
137
113
  }
138
114
 
139
- /** Create a new signing key.
115
+ /**
116
+ * Create a new signing key.
140
117
  * @param {KeyType} type The type of key to create.
141
118
  * @param {string?} ownerId The owner of the key. Defaults to the session's user.
142
119
  * @return {Key[]} The new keys.
143
- * */
120
+ */
144
121
  async createKey(type: KeyType, ownerId?: string): Promise<Key> {
145
- return (await Key.createKeys(this.#cs, this.id, type, 1, ownerId))[0];
122
+ return (await this.createKeys(type, 1, ownerId))[0];
146
123
  }
147
124
 
148
- /** Create new signing keys.
125
+ /**
126
+ * Create new signing keys.
149
127
  * @param {KeyType} type The type of key to create.
150
- * @param {nummber} count The number of keys to create.
128
+ * @param {number} count The number of keys to create.
151
129
  * @param {string?} ownerId The owner of the keys. Defaults to the session's user.
152
130
  * @return {Key[]} The new keys.
153
- * */
131
+ */
154
132
  async createKeys(type: KeyType, count: number, ownerId?: string): Promise<Key[]> {
155
- return Key.createKeys(this.#cs, this.id, type, count, ownerId);
133
+ const keys = await this.#csc.keysCreate(type, count, ownerId);
134
+ return keys.map((k) => new Key(this.#csc, k));
156
135
  }
157
136
 
158
137
  /**
159
- * Derives a key of the given type using the given derivation path and mnemonic.
138
+ * Derive a key of the given type using the given derivation path and mnemonic.
160
139
  * The owner of the derived key will be the owner of the mnemonic.
161
140
  *
162
141
  * @param {KeyType} type Type of key to derive from the mnemonic.
@@ -166,11 +145,11 @@ export class Org {
166
145
  * @return {Key} newly derived key.
167
146
  */
168
147
  async deriveKey(type: KeyType, derivationPath: string, mnemonicId: string): Promise<Key> {
169
- return (await Key.deriveKeys(this.#cs, this.id, type, [derivationPath], mnemonicId))[0];
148
+ return (await this.deriveKeys(type, [derivationPath], mnemonicId))[0];
170
149
  }
171
150
 
172
151
  /**
173
- * Derives a set of keys of the given type using the given derivation paths and mnemonic.
152
+ * Derive a set of keys of the given type using the given derivation paths and mnemonic.
174
153
  *
175
154
  * The owner of the derived keys will be the owner of the mnemonic.
176
155
  *
@@ -181,147 +160,78 @@ export class Org {
181
160
  * @return {Key[]} newly derived keys.
182
161
  */
183
162
  async deriveKeys(type: KeyType, derivationPaths: string[], mnemonicId: string): Promise<Key[]> {
184
- return await Key.deriveKeys(this.#cs, this.#id, type, derivationPaths, mnemonicId);
163
+ const keys = await this.#csc.keysDerive(type, derivationPaths, mnemonicId);
164
+ return keys.map((k) => new Key(this.#csc, k));
185
165
  }
186
166
 
187
- /**
188
- * Create a new user in the organization and sends an invitation to that user
189
- * @param {string} email Email of the user
190
- * @param {string} name The full name of the user
191
- */
192
- async createUser(email: string, name: string): Promise<void> {
193
- const resp = await (
194
- await this.#cs.management()
195
- ).post("/v0/org/{org_id}/invite", {
196
- params: { path: { org_id: this.id } },
197
- body: {
198
- email,
199
- name,
200
- skip_email: false,
201
- },
202
- parseAs: "json",
203
- });
204
- assertOk(resp);
167
+ /** Create a new user in the organization and sends an invitation to that user. */
168
+ get createUser() {
169
+ return this.#csc.orgUserInvite.bind(this.#csc);
205
170
  }
206
171
 
207
- /**
208
- * Create a new OIDC user
209
- * @param {OidcIdentity} identity The identity of the OIDC user
210
- * @param {string} email Email of the OIDC user
211
- * @param {CreateOidcUserOptions} opts Additional options for new OIDC users
212
- * @return {string} User id of the new user
213
- */
214
- async createOidcUser(
215
- identity: OidcIdentity,
216
- email: string,
217
- opts: CreateOidcUserOptions = {},
218
- ): Promise<string> {
219
- const resp = await (
220
- await this.#cs.management()
221
- ).post("/v0/org/{org_id}/users", {
222
- params: { path: { org_id: this.id } },
223
- body: {
224
- identity,
225
- role: opts.memberRole ?? "Alien",
226
- email: email,
227
- mfa_policy: opts.mfaPolicy ?? null,
228
- },
229
- parseAs: "json",
230
- });
231
- return assertOk(resp).user_id;
172
+ /** Create a new OIDC user */
173
+ get createOidcUser() {
174
+ return this.#csc.orgUserCreateOidc.bind(this.#csc);
232
175
  }
233
176
 
234
- /**
235
- * Delete an existing OIDC user
236
- * @param {OidcIdentity} identity The identity of the OIDC user
237
- */
238
- async deleteOidcUser(identity: OidcIdentity) {
239
- const resp = await (
240
- await this.#cs.management()
241
- ).del("/v0/org/{org_id}/users/oidc", {
242
- params: { path: { org_id: this.id } },
243
- body: identity,
244
- parseAs: "json",
245
- });
246
- return assertOk(resp);
177
+ /** Delete an existing OIDC user */
178
+ get deleteOidcUser() {
179
+ return this.#csc.orgUserDeleteOidc.bind(this.#csc);
247
180
  }
248
181
 
249
- /**
250
- * Checks if a given proof of OIDC authentication is valid.
251
- *
252
- * @param {IdentityProof} proof The proof of authentication.
253
- */
254
- async verifyIdentity(proof: IdentityProof) {
255
- await this.#cs.verifyIdentity(this.id, proof);
182
+ /** Checks if a given proof of OIDC authentication is valid. */
183
+ get verifyIdentity() {
184
+ return this.#csc.identityVerify.bind(this.#csc);
256
185
  }
257
186
 
258
- /**
259
- * List users in the organization
260
- * @return {UserIdInfo[]} List of users
261
- */
262
- async users(): Promise<UserIdInfo[]> {
263
- const resp = await (
264
- await this.#cs.management()
265
- ).get("/v0/org/{org_id}/users", {
266
- params: { path: { org_id: this.id } },
267
- parseAs: "json",
268
- });
269
- return assertOk(resp).users;
187
+ /** List users in the organization */
188
+ get users() {
189
+ return this.#csc.orgUsersList.bind(this.#csc);
270
190
  }
271
191
 
272
- /** Get a key by id.
192
+ /**
193
+ * Get a key by id.
273
194
  * @param {string} keyId The id of the key to get.
274
195
  * @return {Key} The key.
275
- * */
196
+ */
276
197
  async getKey(keyId: string): Promise<Key> {
277
- return await Key.getKey(this.#cs, this.id, keyId);
198
+ const keyInfo = await this.#csc.keyGet(keyId);
199
+ return new Key(this.#csc, keyInfo);
278
200
  }
279
201
 
280
- /** Get all keys in the org.
202
+ /**
203
+ * Get all keys in the org.
281
204
  * @param {KeyType?} type Optional key type to filter list for.
282
205
  * @param {PageOpts} page Pagination options. Defaults to fetching the entire result set.
283
206
  * @return {Key} The key.
284
- * */
207
+ */
285
208
  async keys(type?: KeyType, page?: PageOpts): Promise<Key[]> {
286
- page ??= Page.default();
287
- const listFn = async (query: PageQueryArgs) => {
288
- const client = await this.#cs.management();
289
- const resp = await client.get("/v0/org/{org_id}/keys", {
290
- params: {
291
- path: { org_id: this.id },
292
- query: {
293
- key_type: type,
294
- ...query,
295
- },
296
- },
297
- parseAs: "json",
298
- });
299
- return assertOk(resp);
300
- };
301
- const p = new Paginator(
302
- page,
303
- listFn,
304
- (r) => r.keys,
305
- (r) => r.last_evaluated_key,
306
- );
307
- const keys = await p.fetch();
308
- return keys.map((k) => new Key(this.#cs, this.id, k));
209
+ const paginator = this.#csc.keysList(type, page);
210
+ const keys = await paginator.fetch();
211
+ return keys.map((k) => new Key(this.#csc, k));
309
212
  }
310
213
 
311
- /** Create a new role.
214
+ /**
215
+ * Create a new role.
216
+ *
312
217
  * @param {string?} name The name of the role.
313
218
  * @return {Role} The new role.
314
- * */
219
+ */
315
220
  async createRole(name?: string): Promise<Role> {
316
- return Role.createRole(this.#cs, this.id, name);
221
+ const roleId = await this.#csc.roleCreate(name);
222
+ const roleInfo = await this.#csc.roleGet(roleId);
223
+ return new Role(this.#csc, roleInfo);
317
224
  }
318
225
 
319
- /** Get a role by id or name.
226
+ /**
227
+ * Get a role by id or name.
228
+ *
320
229
  * @param {string} roleId The id or name of the role to get.
321
230
  * @return {Role} The role.
322
- * */
231
+ */
323
232
  async getRole(roleId: string): Promise<Role> {
324
- return Role.getRole(this.#cs, this.id, roleId);
233
+ const roleInfo = await this.#csc.roleGet(roleId);
234
+ return new Role(this.#csc, roleInfo);
325
235
  }
326
236
 
327
237
  /**
@@ -329,168 +239,62 @@ export class Org {
329
239
  *
330
240
  * @param {PageOpts} page Pagination options. Defaults to fetching the entire result set.
331
241
  * @return {Role[]} The roles.
332
- * */
242
+ */
333
243
  async listRoles(page?: PageOpts): Promise<Role[]> {
334
- return Org.roles(this.#cs, this.id, page);
244
+ const roles = await this.#csc.rolesList(page).fetch();
245
+ return roles.map((r) => new Role(this.#csc, r));
335
246
  }
336
247
 
337
- /** List all users in the org.
338
- * @return {User[]} The users.
339
- * */
340
- async listUsers(): Promise<UserIdInfo[]> {
341
- return Org.users(this.#cs, this.id);
248
+ /** List all users in the org. */
249
+ get listUsers() {
250
+ return this.#csc.orgUsersList.bind(this.#csc);
342
251
  }
343
252
 
344
253
  /**
345
254
  * Get a pending MFA request by its id.
346
- * @param {string} mfaId The id of the MFA request.
347
- * @return {Promise<MfaRequestInfo>} The MFA request.
348
255
  *
349
256
  * @deprecated Use {@link getMfaInfo()} instead.
350
257
  */
351
- async mfaGet(mfaId: string): Promise<MfaRequestInfo> {
352
- return await this.getMfaInfo(mfaId);
258
+ get mfaGet() {
259
+ return this.#csc.mfaGet.bind(this.#csc);
353
260
  }
354
261
 
355
262
  /**
356
263
  * Approve a pending MFA request.
357
264
  *
358
- * @param {string} mfaId The id of the MFA request.
359
- * @return {Promise<MfaRequestInfo>} The MFA request.
360
- *
361
265
  * @deprecated Use {@link approveMfaRequest()} instead.
362
266
  */
363
- async mfaApprove(mfaId: string): Promise<MfaRequestInfo> {
364
- return await this.approveMfaRequest(mfaId);
267
+ get mfaApprove() {
268
+ return this.#csc.mfaApprove.bind(this.#csc);
365
269
  }
366
270
 
367
- /**
368
- * Get a pending MFA request by its id.
369
- * @param {string} mfaId The id of the MFA request.
370
- * @return {Promise<MfaRequestInfo>} The MFA request.
371
- */
372
- async getMfaInfo(mfaId: string): Promise<MfaRequestInfo> {
373
- return await this.#cs.mfaGet(this.id, mfaId);
271
+ /** Get a pending MFA request by its id. */
272
+ get getMfaInfo() {
273
+ return this.#csc.mfaGet.bind(this.#csc);
374
274
  }
375
275
 
376
- /**
377
- * List pending MFA requests accessible to the current user.
378
- * @return {Promise<MfaRequestInfo[]>} The MFA requests.
379
- */
380
- async listMfaInfos(): Promise<MfaRequestInfo[]> {
381
- return await this.#cs.mfaList(this.id);
276
+ /** List pending MFA requests accessible to the current user. */
277
+ get listMfaInfos() {
278
+ return this.#csc.mfaList.bind(this.#csc);
382
279
  }
383
280
 
384
- /**
385
- * Approve a pending MFA request.
386
- *
387
- * @param {string} mfaId The id of the MFA request.
388
- * @return {Promise<MfaRequestInfo>} The MFA request.
389
- */
390
- async approveMfaRequest(mfaId: string): Promise<MfaRequestInfo> {
391
- return Org.mfaApprove(this.#cs, this.#id, mfaId);
281
+ /** Approve a pending MFA request. */
282
+ get approveMfaRequest() {
283
+ return this.#csc.mfaApprove.bind(this.#csc);
392
284
  }
393
285
 
394
286
  // --------------------------------------------------------------------------
395
287
  // -- INTERNAL --------------------------------------------------------------
396
288
  // --------------------------------------------------------------------------
397
289
 
398
- /** Create a new org.
399
- * @param {CubeSigner} cs The CubeSigner instance.
290
+ /**
291
+ * Create a new org.
292
+ * @param {CubeSignerClient} csc The CubeSigner instance.
400
293
  * @param {OrgInfo} data The JSON response from the API server.
401
294
  * @internal
402
- * */
403
- constructor(cs: CubeSigner, data: OrgInfo) {
404
- this.#cs = cs;
405
- this.#id = data.org_id;
406
- }
407
-
408
- /**
409
- * Approve a pending MFA request.
410
- *
411
- * @param {CubeSigner} cs The CubeSigner instance to use for requests
412
- * @param {string} orgId The org id of the MFA request
413
- * @param {string} mfaId The id of the MFA request
414
- * @return {Promise<MfaRequestInfo>} The result of the MFA request
415
295
  */
416
- static async mfaApprove(cs: CubeSigner, orgId: string, mfaId: string): Promise<MfaRequestInfo> {
417
- return await cs.mfaApprove(orgId, mfaId);
418
- }
419
-
420
- /** Fetch org info.
421
- * @return {OrgInfo} The org info.
422
- * */
423
- private async fetch(): Promise<OrgInfo> {
424
- const resp = await (
425
- await this.#cs.management()
426
- ).get("/v0/org/{org_id}", {
427
- params: { path: { org_id: this.id } },
428
- parseAs: "json",
429
- });
430
- const data = assertOk(resp);
431
- return data;
432
- }
433
-
434
- /** Update the org.
435
- * @param {UpdateOrgRequest} request The JSON request to send to the API server.
436
- * @return {UpdateOrgResponse} The JSON response from the API server.
437
- * */
438
- private async update(request: UpdateOrgRequest): Promise<UpdateOrgResponse> {
439
- const resp = await (
440
- await this.#cs.management()
441
- ).patch("/v0/org/{org_id}", {
442
- params: { path: { org_id: this.id } },
443
- body: request,
444
- parseAs: "json",
445
- });
446
- return assertOk(resp);
447
- }
448
-
449
- /** List roles.
450
- * @param {CubeSigner} cs The CubeSigner instance to use for signing.
451
- * @param {string} orgId The id of the organization to which the role belongs.
452
- * @param {PageOpts} page Pagination options. Defaults to fetching the entire result set.
453
- * @return {Role[]} Org roles.
454
- * @internal
455
- * */
456
- private static async roles(cs: CubeSigner, orgId: string, page?: PageOpts): Promise<Role[]> {
457
- page ??= Page.default();
458
- const listFn = async (query: PageQueryArgs) => {
459
- const resp = await (
460
- await cs.management()
461
- ).get("/v0/org/{org_id}/roles", {
462
- params: {
463
- path: { org_id: orgId },
464
- query,
465
- },
466
- parseAs: "json",
467
- });
468
- return assertOk(resp);
469
- };
470
- const p = new Paginator(
471
- page,
472
- listFn,
473
- (u) => u.roles,
474
- (u) => u.last_evaluated_key,
475
- );
476
- const roles = await p.fetch();
477
- return roles.map((r: RoleInfo) => new Role(cs, orgId, r));
478
- }
479
-
480
- /** List users.
481
- * @param {CubeSigner} cs The CubeSigner instance to use for signing.
482
- * @param {string} orgId The id of the organization to which the role belongs.
483
- * @return {User[]} Org users.
484
- * @internal
485
- * */
486
- private static async users(cs: CubeSigner, orgId: string): Promise<UserIdInfo[]> {
487
- const resp = await (
488
- await cs.management()
489
- ).get("/v0/org/{org_id}/users", {
490
- params: { path: { org_id: orgId } },
491
- parseAs: "json",
492
- });
493
- const data = assertOk(resp);
494
- return data.users;
296
+ constructor(csc: CubeSignerClient, data: OrgInfo) {
297
+ this.#csc = csc.withOrg(data.org_id);
298
+ this.#id = data.org_id;
495
299
  }
496
300
  }