@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.
- package/README.md +66 -13
- package/dist/src/client.d.ts +434 -7
- package/dist/src/client.js +1022 -18
- package/dist/src/ethers/index.d.ts +2 -4
- package/dist/src/ethers/index.js +11 -9
- package/dist/src/fido.d.ts +76 -0
- package/dist/src/fido.js +148 -0
- package/dist/src/index.d.ts +102 -30
- package/dist/src/index.js +126 -72
- package/dist/src/key.d.ts +15 -45
- package/dist/src/key.js +31 -93
- package/dist/src/mfa.d.ts +85 -14
- package/dist/src/mfa.js +158 -40
- package/dist/src/org.d.ts +237 -123
- package/dist/src/org.js +108 -213
- package/dist/src/paginator.d.ts +76 -0
- package/dist/src/paginator.js +99 -0
- package/dist/src/role.d.ts +76 -74
- package/dist/src/role.js +79 -136
- package/dist/src/schema.d.ts +1672 -520
- package/dist/src/schema.js +1 -1
- package/dist/src/schema_types.d.ts +103 -0
- package/dist/src/schema_types.js +3 -0
- package/dist/src/session/session_manager.js +2 -2
- package/dist/src/session/session_storage.js +1 -1
- package/dist/src/session/signer_session_manager.d.ts +16 -29
- package/dist/src/session/signer_session_manager.js +27 -78
- package/dist/src/signer_session.d.ts +232 -125
- package/dist/src/signer_session.js +149 -250
- package/dist/src/util.d.ts +20 -0
- package/dist/src/util.js +31 -2
- package/package.json +13 -11
- package/src/client.ts +1217 -7
- package/src/ethers/index.ts +11 -18
- package/src/index.ts +149 -101
- package/src/key.ts +28 -121
- package/src/mfa.ts +202 -0
- package/src/org.ts +126 -275
- package/src/paginator.ts +122 -0
- package/src/role.ts +108 -181
- package/src/schema.ts +1673 -520
- package/src/schema_types.ts +103 -0
- package/src/session/session_manager.ts +2 -2
- package/src/session/session_storage.ts +1 -1
- package/src/session/signer_session_manager.ts +38 -108
- package/src/signer_session.ts +164 -323
- package/src/util.ts +41 -0
package/src/ethers/index.ts
CHANGED
|
@@ -7,15 +7,9 @@ import {
|
|
|
7
7
|
getBytes,
|
|
8
8
|
toBeHex,
|
|
9
9
|
} from "ethers";
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
EvmSignRequest,
|
|
13
|
-
MfaRequestInfo,
|
|
14
|
-
SignerSession,
|
|
15
|
-
SignResponse,
|
|
16
|
-
} from "../signer_session";
|
|
10
|
+
import { SignerSession, CubeSignerResponse } from "../signer_session";
|
|
11
|
+
import { BlobSignRequest, EvmSignRequest, MfaRequestInfo } from "../schema_types";
|
|
17
12
|
import { KeyInfo } from "../key";
|
|
18
|
-
import { CubeSigner } from "..";
|
|
19
13
|
|
|
20
14
|
/** Options for the signer */
|
|
21
15
|
interface SignerOptions {
|
|
@@ -31,8 +25,6 @@ interface SignerOptions {
|
|
|
31
25
|
* updates. Default is 1000ms
|
|
32
26
|
*/
|
|
33
27
|
mfaPollIntervalMs?: number;
|
|
34
|
-
/** Optional management session. Used to check for MFA updates */
|
|
35
|
-
managementSession?: CubeSigner;
|
|
36
28
|
}
|
|
37
29
|
|
|
38
30
|
/**
|
|
@@ -57,9 +49,6 @@ export class Signer extends ethers.AbstractSigner {
|
|
|
57
49
|
/** The amount of time to wait between checks for MFA updates */
|
|
58
50
|
readonly #mfaPollIntervalMs: number;
|
|
59
51
|
|
|
60
|
-
/** Optional management session, used for MFA flows */
|
|
61
|
-
readonly #managementSession?: CubeSigner;
|
|
62
|
-
|
|
63
52
|
/** Create new Signer instance
|
|
64
53
|
* @param {KeyInfo | string} address The key or the eth address of the account to use.
|
|
65
54
|
* @param {SignerSession} signerSession The underlying Signer session.
|
|
@@ -76,7 +65,6 @@ export class Signer extends ethers.AbstractSigner {
|
|
|
76
65
|
this.#signerSession = signerSession;
|
|
77
66
|
this.#onMfaPoll = options?.onMfaPoll ?? ((/* _mfaInfo: MfaRequestInfo */) => {}); // eslint-disable-line @typescript-eslint/no-empty-function
|
|
78
67
|
this.#mfaPollIntervalMs = options?.mfaPollIntervalMs ?? 1000;
|
|
79
|
-
this.#managementSession = options?.managementSession;
|
|
80
68
|
}
|
|
81
69
|
|
|
82
70
|
/** Resolves to the signer address. */
|
|
@@ -180,17 +168,22 @@ export class Signer extends ethers.AbstractSigner {
|
|
|
180
168
|
/**
|
|
181
169
|
* If the sign request requires MFA, this method waits for approvals
|
|
182
170
|
*
|
|
183
|
-
* @param {
|
|
171
|
+
* @param {CubeSignerResponse<U>} res The response of a sign request
|
|
184
172
|
* @return {Promise<U>} The sign data after MFA approvals
|
|
185
173
|
*/
|
|
186
|
-
async #handleMfa<U>(res:
|
|
174
|
+
async #handleMfa<U>(res: CubeSignerResponse<U>): Promise<U> {
|
|
187
175
|
while (res.requiresMfa()) {
|
|
188
176
|
await new Promise((resolve) => setTimeout(resolve, this.#mfaPollIntervalMs));
|
|
189
177
|
|
|
190
|
-
const
|
|
178
|
+
const mfaId = res.mfaId();
|
|
179
|
+
const mfaInfo = await this.#signerSession.getMfaInfo(mfaId);
|
|
191
180
|
this.#onMfaPoll(mfaInfo);
|
|
192
181
|
if (mfaInfo.receipt) {
|
|
193
|
-
res = await res.signWithMfaApproval(
|
|
182
|
+
res = await res.signWithMfaApproval({
|
|
183
|
+
mfaId,
|
|
184
|
+
mfaOrgId: this.#signerSession.orgId,
|
|
185
|
+
mfaConf: mfaInfo.receipt.confirmation,
|
|
186
|
+
});
|
|
194
187
|
}
|
|
195
188
|
}
|
|
196
189
|
return res.data();
|
package/src/index.ts
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
import { envs, EnvInterface } from "./env";
|
|
2
|
-
import {
|
|
2
|
+
import { Client, CubeSignerClient, OidcClient } from "./client";
|
|
3
3
|
import { Org } from "./org";
|
|
4
4
|
import { JsonFileSessionStorage } from "./session/session_storage";
|
|
5
5
|
|
|
6
6
|
import { SignerSessionStorage, SignerSessionManager } from "./session/signer_session_manager";
|
|
7
|
-
import {
|
|
7
|
+
import { CubeSignerResponse, SignerSession } from "./signer_session";
|
|
8
8
|
import { CognitoSessionManager, CognitoSessionStorage } from "./session/cognito_manager";
|
|
9
|
-
import {
|
|
9
|
+
import { configDir } from "./util";
|
|
10
10
|
import * as path from "path";
|
|
11
|
-
import
|
|
11
|
+
import { MfaReceipt } from "./mfa";
|
|
12
|
+
import {
|
|
13
|
+
IdentityProof,
|
|
14
|
+
MfaRequestInfo,
|
|
15
|
+
OidcAuthResponse,
|
|
16
|
+
RatchetConfig,
|
|
17
|
+
UserInfo,
|
|
18
|
+
} from "./schema_types";
|
|
12
19
|
|
|
13
20
|
/** CubeSigner constructor options */
|
|
14
21
|
export interface CubeSignerOptions {
|
|
@@ -16,28 +23,41 @@ export interface CubeSignerOptions {
|
|
|
16
23
|
env?: EnvInterface;
|
|
17
24
|
/** The management authorization token */
|
|
18
25
|
sessionMgr?: CognitoSessionManager | SignerSessionManager;
|
|
26
|
+
/** Optional organization id */
|
|
27
|
+
orgId?: string;
|
|
19
28
|
}
|
|
20
29
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
type OidcAuthResponse =
|
|
27
|
-
paths["/v0/org/{org_id}/oidc"]["post"]["responses"]["200"]["content"]["application/json"];
|
|
28
|
-
|
|
29
|
-
/** CubeSigner client */
|
|
30
|
+
/**
|
|
31
|
+
* CubeSigner client
|
|
32
|
+
*
|
|
33
|
+
* @deprecated Use {@link CubeSignerClient} instead.
|
|
34
|
+
*/
|
|
30
35
|
export class CubeSigner {
|
|
31
36
|
readonly #env: EnvInterface;
|
|
32
37
|
readonly sessionMgr?: CognitoSessionManager | SignerSessionManager;
|
|
38
|
+
#csc: CubeSignerClient;
|
|
33
39
|
|
|
34
40
|
/** @return {EnvInterface} The CubeSigner environment of this client */
|
|
35
41
|
get env(): EnvInterface {
|
|
36
42
|
return this.#env;
|
|
37
43
|
}
|
|
38
44
|
|
|
45
|
+
/** Organization ID */
|
|
46
|
+
get orgId() {
|
|
47
|
+
return this.#csc.orgId;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Set the organization ID
|
|
52
|
+
* @param {string} orgId The new organization id.
|
|
53
|
+
*/
|
|
54
|
+
setOrgId(orgId: string) {
|
|
55
|
+
this.#csc = this.#csc.withOrg(orgId);
|
|
56
|
+
}
|
|
57
|
+
|
|
39
58
|
/**
|
|
40
59
|
* Loads an existing management session and creates a CubeSigner instance.
|
|
60
|
+
*
|
|
41
61
|
* @param {CognitoSessionStorage} storage Optional session storage to load
|
|
42
62
|
* the session from. If not specified, the management session from the config
|
|
43
63
|
* directory will be loaded.
|
|
@@ -68,7 +88,7 @@ export class CubeSigner {
|
|
|
68
88
|
|
|
69
89
|
/**
|
|
70
90
|
* Create a new CubeSigner instance.
|
|
71
|
-
* @param {CubeSignerOptions} options The optional
|
|
91
|
+
* @param {CubeSignerOptions} options The optional configuration options for the CubeSigner instance.
|
|
72
92
|
*/
|
|
73
93
|
constructor(options?: CubeSignerOptions) {
|
|
74
94
|
let env = options?.env;
|
|
@@ -77,10 +97,21 @@ export class CubeSigner {
|
|
|
77
97
|
env = env ?? this.sessionMgr.env;
|
|
78
98
|
}
|
|
79
99
|
this.#env = env ?? envs["gamma"];
|
|
100
|
+
this.#csc = new CubeSignerClient(
|
|
101
|
+
// HACK: ignore that sessionMgr may be a CognitoSessionManager and pretend that it
|
|
102
|
+
// is a SignerSessionManager; that's fine because the CubeSignerClient will
|
|
103
|
+
// almost always just call `await token()` on it, which works in both cases.
|
|
104
|
+
//
|
|
105
|
+
// This is done here for backward compatibility reasons only; in the future,
|
|
106
|
+
// we should deprecate this class and people should start using `CubeSingerClient` directly.
|
|
107
|
+
options?.sessionMgr as unknown as SignerSessionManager,
|
|
108
|
+
options?.orgId,
|
|
109
|
+
);
|
|
80
110
|
}
|
|
81
111
|
|
|
82
112
|
/**
|
|
83
113
|
* Authenticate an OIDC user and create a new session manager for them.
|
|
114
|
+
*
|
|
84
115
|
* @param {string} oidcToken The OIDC token
|
|
85
116
|
* @param {string} orgId The id of the organization that the user is in
|
|
86
117
|
* @param {List<string>} scopes The scopes of the resulting session
|
|
@@ -99,30 +130,13 @@ export class CubeSigner {
|
|
|
99
130
|
return await SignerSessionManager.createFromSessionInfo(this.env, orgId, resp.data(), storage);
|
|
100
131
|
}
|
|
101
132
|
|
|
102
|
-
/** Retrieves information about the current user. */
|
|
103
|
-
async aboutMe(): Promise<UserInfo> {
|
|
104
|
-
const resp = await (
|
|
105
|
-
await this.management()
|
|
106
|
-
).get("/v0/about_me", {
|
|
107
|
-
parseAs: "json",
|
|
108
|
-
});
|
|
109
|
-
const data = assertOk(resp);
|
|
110
|
-
return data;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
133
|
/**
|
|
114
|
-
*
|
|
115
|
-
* if and only if no TOTP configuration is already set.
|
|
134
|
+
* Retrieves information about the current user.
|
|
116
135
|
*
|
|
117
|
-
* @return {Promise<
|
|
136
|
+
* @return {Promise<UserInfo>} User information.
|
|
118
137
|
*/
|
|
119
|
-
async
|
|
120
|
-
|
|
121
|
-
await this.management()
|
|
122
|
-
).put("/v0/totp", {
|
|
123
|
-
parseAs: "json",
|
|
124
|
-
});
|
|
125
|
-
return assertOk(resp);
|
|
138
|
+
async aboutMe(): Promise<UserInfo> {
|
|
139
|
+
return await this.#csc.userGet();
|
|
126
140
|
}
|
|
127
141
|
|
|
128
142
|
/**
|
|
@@ -133,56 +147,79 @@ export class CubeSigner {
|
|
|
133
147
|
* @return {Promise<MfaRequestInfo>} MFA request information
|
|
134
148
|
*/
|
|
135
149
|
async mfaGet(orgId: string, mfaId: string): Promise<MfaRequestInfo> {
|
|
136
|
-
|
|
137
|
-
await this.management()
|
|
138
|
-
).get("/v0/org/{org_id}/mfa/{mfa_id}", {
|
|
139
|
-
params: { path: { org_id: orgId, mfa_id: mfaId } },
|
|
140
|
-
});
|
|
141
|
-
return assertOk(resp);
|
|
150
|
+
return await this.#csc.withOrg(orgId).mfaGet(mfaId);
|
|
142
151
|
}
|
|
143
152
|
|
|
144
153
|
/**
|
|
145
|
-
*
|
|
146
|
-
*
|
|
154
|
+
* List pending MFA requests accessible to the current user.
|
|
155
|
+
* @param {string} orgId Organization ID
|
|
156
|
+
* @return {Promise<MfaRequestInfo[]>} The MFA requests.
|
|
147
157
|
*/
|
|
148
|
-
async
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
158
|
+
async mfaList(orgId: string): Promise<MfaRequestInfo[]> {
|
|
159
|
+
return await this.#csc.withOrg(orgId).mfaList();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Approve a pending MFA request.
|
|
164
|
+
*
|
|
165
|
+
* @param {string} orgId The org id of the MFA request
|
|
166
|
+
* @param {string} mfaId The id of the MFA request
|
|
167
|
+
* @return {Promise<MfaRequestInfo>} The result of the MFA request
|
|
168
|
+
*/
|
|
169
|
+
async mfaApprove(orgId: string, mfaId: string): Promise<MfaRequestInfo> {
|
|
170
|
+
return await this.#csc.withOrg(orgId).mfaApprove(mfaId);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Initiate adding a new FIDO device. MFA may be required. */
|
|
174
|
+
get addFidoStart() {
|
|
175
|
+
return this.#csc.userRegisterFidoInit.bind(this.#csc);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Complete a previously initiated request to add a new FIDO device. */
|
|
179
|
+
get addFidoComplete() {
|
|
180
|
+
return this.#csc.userRegisterFidoComplete.bind(this.#csc);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Creates a request to change user's TOTP. This request returns a new TOTP challenge
|
|
185
|
+
* that must be answered by calling `resetTotpComplete`
|
|
186
|
+
*/
|
|
187
|
+
get resetTotpStart() {
|
|
188
|
+
return this.#csc.userResetTotpInit.bind(this.#csc);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Answer the TOTP challenge issued by `resetTotpStart`. If successful, user's
|
|
193
|
+
* TOTP configuration will be updated to that of the TOTP challenge.he TOTP configuration from the challenge.
|
|
194
|
+
*/
|
|
195
|
+
get resetTotpComplete() {
|
|
196
|
+
return this.#csc.userResetTotpComplete.bind(this.#csc);
|
|
155
197
|
}
|
|
156
198
|
|
|
157
199
|
/**
|
|
158
200
|
* Verifies a given TOTP code against the current user's TOTP configuration.
|
|
159
201
|
* Throws an error if the verification fails.
|
|
160
|
-
* @param {string} code Current TOTP code
|
|
161
202
|
*/
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
await this.management()
|
|
165
|
-
).get("/v0/totp/verify/{code}", {
|
|
166
|
-
params: { path: { code } },
|
|
167
|
-
parseAs: "json",
|
|
168
|
-
});
|
|
169
|
-
assertOk(resp);
|
|
203
|
+
get verifyTotp() {
|
|
204
|
+
return this.#csc.userVerifyTotp.bind(this.#csc);
|
|
170
205
|
}
|
|
171
206
|
|
|
172
207
|
/** Retrieves information about an organization.
|
|
173
208
|
* @param {string} orgId The ID or name of the organization.
|
|
174
209
|
* @return {Org} The organization.
|
|
175
210
|
* */
|
|
176
|
-
async getOrg(orgId
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
params: { path: { org_id: orgId } },
|
|
181
|
-
parseAs: "json",
|
|
182
|
-
});
|
|
211
|
+
async getOrg(orgId?: string): Promise<Org> {
|
|
212
|
+
const orgInfo = await this.#csc.withOrg(orgId).orgGet();
|
|
213
|
+
return new Org(this.#csc, orgInfo);
|
|
214
|
+
}
|
|
183
215
|
|
|
184
|
-
|
|
185
|
-
|
|
216
|
+
/**
|
|
217
|
+
* Deletes a given key.
|
|
218
|
+
* @param {string} orgId - Organization id
|
|
219
|
+
* @param {string} keyId - Key id
|
|
220
|
+
*/
|
|
221
|
+
async deleteKey(orgId: string, keyId: string) {
|
|
222
|
+
await this.#csc.withOrg(orgId).keyDelete(keyId);
|
|
186
223
|
}
|
|
187
224
|
|
|
188
225
|
/** Get the management client.
|
|
@@ -196,6 +233,38 @@ export class CubeSigner {
|
|
|
196
233
|
return await this.sessionMgr.client();
|
|
197
234
|
}
|
|
198
235
|
|
|
236
|
+
/**
|
|
237
|
+
* Obtain a proof of authentication.
|
|
238
|
+
*
|
|
239
|
+
* @param {string} orgId The id of the organization that the user is in
|
|
240
|
+
* @return {Promise<IdentityProof>} Proof of authentication
|
|
241
|
+
*/
|
|
242
|
+
async proveIdentity(orgId: string): Promise<IdentityProof> {
|
|
243
|
+
return await this.#csc.withOrg(orgId).identityProve();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Exchange an OIDC token for a proof of authentication.
|
|
248
|
+
*
|
|
249
|
+
* @param {string} oidcToken The OIDC token
|
|
250
|
+
* @param {string} orgId The id of the organization that the user is in
|
|
251
|
+
* @return {Promise<IdentityProof>} Proof of authentication
|
|
252
|
+
*/
|
|
253
|
+
async oidcProveIdentity(oidcToken: string, orgId: string): Promise<IdentityProof> {
|
|
254
|
+
const oidcClient = new OidcClient(this.#env, orgId, oidcToken);
|
|
255
|
+
return await oidcClient.identityProve();
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Checks if a given identity proof is valid.
|
|
260
|
+
*
|
|
261
|
+
* @param {string} orgId The id of the organization that the user is in.
|
|
262
|
+
* @param {IdentityProof} identityProof The proof of authentication.
|
|
263
|
+
*/
|
|
264
|
+
async verifyIdentity(orgId: string, identityProof: IdentityProof) {
|
|
265
|
+
await this.#csc.withOrg(orgId).identityVerify(identityProof);
|
|
266
|
+
}
|
|
267
|
+
|
|
199
268
|
/**
|
|
200
269
|
* Exchange an OIDC token for a CubeSigner session token.
|
|
201
270
|
* @param {string} oidcToken The OIDC token
|
|
@@ -203,7 +272,7 @@ export class CubeSigner {
|
|
|
203
272
|
* @param {List<string>} scopes The scopes of the resulting session
|
|
204
273
|
* @param {RatchetConfig} lifetimes Lifetimes of the new session.
|
|
205
274
|
* @param {MfaReceipt} mfaReceipt Optional MFA receipt (id + confirmation code)
|
|
206
|
-
* @return {Promise<
|
|
275
|
+
* @return {Promise<CubeSignerResponse<OidcAuthResponse>>} The session data.
|
|
207
276
|
*/
|
|
208
277
|
async oidcLogin(
|
|
209
278
|
oidcToken: string,
|
|
@@ -211,41 +280,14 @@ export class CubeSigner {
|
|
|
211
280
|
scopes: Array<string>,
|
|
212
281
|
lifetimes?: RatchetConfig,
|
|
213
282
|
mfaReceipt?: MfaReceipt,
|
|
214
|
-
): Promise<
|
|
215
|
-
const
|
|
216
|
-
|
|
217
|
-
headers: {
|
|
218
|
-
Authorization: oidcToken,
|
|
219
|
-
},
|
|
220
|
-
});
|
|
221
|
-
const loginFn = async (headers?: HeadersInit) => {
|
|
222
|
-
const resp = await client.post("/v0/org/{org_id}/oidc", {
|
|
223
|
-
params: { path: { org_id: orgId } },
|
|
224
|
-
headers,
|
|
225
|
-
body: {
|
|
226
|
-
scopes,
|
|
227
|
-
tokens: lifetimes,
|
|
228
|
-
},
|
|
229
|
-
parseAs: "json",
|
|
230
|
-
});
|
|
231
|
-
return assertOk(resp);
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
const h1 = mfaReceipt
|
|
235
|
-
? SignResponse.getMfaHeaders(mfaReceipt.mfaId, mfaReceipt.mfaConf)
|
|
236
|
-
: undefined;
|
|
237
|
-
return new SignResponse(orgId, loginFn, await loginFn(h1));
|
|
283
|
+
): Promise<CubeSignerResponse<OidcAuthResponse>> {
|
|
284
|
+
const oidcClient = new OidcClient(this.#env, orgId, oidcToken);
|
|
285
|
+
return await oidcClient.sessionCreate(scopes, lifetimes, mfaReceipt);
|
|
238
286
|
}
|
|
239
287
|
}
|
|
240
288
|
|
|
241
|
-
/**
|
|
242
|
-
export
|
|
243
|
-
/** MFA request ID */
|
|
244
|
-
mfaId: string;
|
|
245
|
-
/** MFA confirmation code */
|
|
246
|
-
mfaConf: string;
|
|
247
|
-
}
|
|
248
|
-
|
|
289
|
+
/** Client */
|
|
290
|
+
export * from "./client";
|
|
249
291
|
/** Organizations */
|
|
250
292
|
export * from "./org";
|
|
251
293
|
/** Keys */
|
|
@@ -254,6 +296,12 @@ export * from "./key";
|
|
|
254
296
|
export * from "./role";
|
|
255
297
|
/** Env */
|
|
256
298
|
export * from "./env";
|
|
299
|
+
/** Fido */
|
|
300
|
+
export * from "./mfa";
|
|
301
|
+
/** Pagination */
|
|
302
|
+
export * from "./paginator";
|
|
303
|
+
/** Types */
|
|
304
|
+
export * from "./schema_types";
|
|
257
305
|
/** Sessions */
|
|
258
306
|
export * from "./signer_session";
|
|
259
307
|
/** Session storage */
|
package/src/key.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { KeyPolicy } from "./role";
|
|
2
|
+
import { KeyInfoApi, KeyTypeApi, UpdateKeyRequest, SchemaKeyType } from "./schema_types";
|
|
3
|
+
import { CubeSignerClient } from "./client";
|
|
4
4
|
|
|
5
5
|
/** Secp256k1 key type */
|
|
6
6
|
export enum Secp256k1 {
|
|
@@ -23,6 +23,7 @@ export enum Ed25519 {
|
|
|
23
23
|
Sui = "Ed25519SuiAddr", // eslint-disable-line no-unused-vars
|
|
24
24
|
Aptos = "Ed25519AptosAddr", // eslint-disable-line no-unused-vars
|
|
25
25
|
Cardano = "Ed25519CardanoAddrVk", // eslint-disable-line no-unused-vars
|
|
26
|
+
Stellar = "Ed25519StellarAddr", // eslint-disable-line no-unused-vars
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
/** Mnemonic key type */
|
|
@@ -36,13 +37,6 @@ export type Stark = typeof Stark;
|
|
|
36
37
|
/** Key type */
|
|
37
38
|
export type KeyType = Secp256k1 | Bls | Ed25519 | Mnemonic | Stark;
|
|
38
39
|
|
|
39
|
-
/** Schema key type (i.e., key type at the API level) */
|
|
40
|
-
type SchemaKeyType = components["schemas"]["KeyType"];
|
|
41
|
-
|
|
42
|
-
type UpdateKeyRequest = components["schemas"]["UpdateKeyRequest"];
|
|
43
|
-
type KeyInfoApi = components["schemas"]["KeyInfo"];
|
|
44
|
-
type KeyTypeApi = components["schemas"]["KeyType"];
|
|
45
|
-
|
|
46
40
|
/** Additional properties (for backward compatibility) */
|
|
47
41
|
export interface KeyInfo extends KeyInfoApi {
|
|
48
42
|
/** Alias for key_id */
|
|
@@ -75,9 +69,13 @@ export function toKeyInfo(key: KeyInfoApi): KeyInfo {
|
|
|
75
69
|
/** Signing keys. */
|
|
76
70
|
export class Key {
|
|
77
71
|
/** The CubeSigner instance that this key is associated with */
|
|
78
|
-
readonly #
|
|
72
|
+
readonly #csc: CubeSignerClient;
|
|
73
|
+
|
|
79
74
|
/** The organization that this key is in */
|
|
80
|
-
|
|
75
|
+
get orgId() {
|
|
76
|
+
return this.#csc.orgId;
|
|
77
|
+
}
|
|
78
|
+
|
|
81
79
|
/**
|
|
82
80
|
* The id of the key: "Key#" followed by a unique identifier specific to
|
|
83
81
|
* the type of key (such as a public key for BLS or an ethereum address for Secp)
|
|
@@ -163,124 +161,37 @@ export class Key {
|
|
|
163
161
|
await this.update({ owner });
|
|
164
162
|
}
|
|
165
163
|
|
|
164
|
+
/**
|
|
165
|
+
* Delete this key.
|
|
166
|
+
*/
|
|
167
|
+
async delete() {
|
|
168
|
+
await this.#csc.keyDelete(this.id);
|
|
169
|
+
}
|
|
170
|
+
|
|
166
171
|
// --------------------------------------------------------------------------
|
|
167
172
|
// -- INTERNAL --------------------------------------------------------------
|
|
168
173
|
// --------------------------------------------------------------------------
|
|
169
174
|
|
|
170
175
|
/** Create a new key.
|
|
171
|
-
* @param {
|
|
172
|
-
* @param {string} orgId The id of the organization to which the key belongs.
|
|
176
|
+
* @param {CubeSignerClient} csc The CubeSigner instance to use for signing.
|
|
173
177
|
* @param {KeyInfo} data The JSON response from the API server.
|
|
174
178
|
* @internal
|
|
175
179
|
* */
|
|
176
|
-
constructor(
|
|
177
|
-
this.#
|
|
178
|
-
this.orgId = orgId;
|
|
180
|
+
constructor(csc: CubeSignerClient, data: KeyInfoApi) {
|
|
181
|
+
this.#csc = csc;
|
|
179
182
|
this.id = data.key_id;
|
|
180
183
|
this.materialId = data.material_id;
|
|
181
184
|
this.publicKey = data.public_key;
|
|
182
185
|
}
|
|
183
186
|
|
|
184
|
-
/**
|
|
187
|
+
/**
|
|
188
|
+
* Update the key.
|
|
185
189
|
* @param {UpdateKeyRequest} request The JSON request to send to the API server.
|
|
186
190
|
* @return {KeyInfo} The JSON response from the API server.
|
|
187
|
-
* */
|
|
188
|
-
private async update(request: UpdateKeyRequest): Promise<KeyInfo> {
|
|
189
|
-
const resp = await (
|
|
190
|
-
await this.#cs.management()
|
|
191
|
-
).patch("/v0/org/{org_id}/keys/{key_id}", {
|
|
192
|
-
params: { path: { org_id: this.orgId, key_id: this.id } },
|
|
193
|
-
body: request,
|
|
194
|
-
parseAs: "json",
|
|
195
|
-
});
|
|
196
|
-
return toKeyInfo(assertOk(resp));
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/** Create new signing keys.
|
|
200
|
-
* @param {CubeSigner} cs The CubeSigner instance to use for signing.
|
|
201
|
-
* @param {string} orgId The id of the organization to which the key belongs.
|
|
202
|
-
* @param {KeyType} keyType The type of key to create.
|
|
203
|
-
* @param {number} count The number of keys to create.
|
|
204
|
-
* @param {string?} ownerId The owner of the keys. Defaults to the session's user.
|
|
205
|
-
* @return {Key[]} The new keys.
|
|
206
|
-
* @internal
|
|
207
|
-
* */
|
|
208
|
-
static async createKeys(
|
|
209
|
-
cs: CubeSigner,
|
|
210
|
-
orgId: string,
|
|
211
|
-
keyType: KeyType,
|
|
212
|
-
count: number,
|
|
213
|
-
ownerId?: string,
|
|
214
|
-
): Promise<Key[]> {
|
|
215
|
-
const chain_id = 0; // not used anymore
|
|
216
|
-
const resp = await (
|
|
217
|
-
await cs.management()
|
|
218
|
-
).post("/v0/org/{org_id}/keys", {
|
|
219
|
-
params: { path: { org_id: orgId } },
|
|
220
|
-
body: {
|
|
221
|
-
count,
|
|
222
|
-
chain_id,
|
|
223
|
-
key_type: keyType,
|
|
224
|
-
owner: ownerId || null,
|
|
225
|
-
},
|
|
226
|
-
parseAs: "json",
|
|
227
|
-
});
|
|
228
|
-
const data = assertOk(resp);
|
|
229
|
-
return data.keys.map((k) => new Key(cs, orgId, k));
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Derives a key of a specified type using a supplied derivation path and an existing long-lived mnemonic.
|
|
234
|
-
*
|
|
235
|
-
* @param {CubeSigner} cs The CubeSigner instance to use for key creation.
|
|
236
|
-
* @param {string} orgId The id of the organization to which the key belongs.
|
|
237
|
-
* @param {KeyType} keyType The type of key to create.
|
|
238
|
-
* @param {string[]} derivationPaths Derivation paths from which to derive new keys.
|
|
239
|
-
* @param {string} mnemonicId materialId of mnemonic key used to derive the new key.
|
|
240
|
-
* @param {string?} ownerId The owner of newly derived keys. Defaults to the session's user.
|
|
241
|
-
*
|
|
242
|
-
* @return {Key[]} The newly derived keys.
|
|
243
191
|
*/
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
keyType: KeyType,
|
|
248
|
-
derivationPaths: string[],
|
|
249
|
-
mnemonicId: string,
|
|
250
|
-
ownerId?: string,
|
|
251
|
-
): Promise<Key[]> {
|
|
252
|
-
const resp = await (
|
|
253
|
-
await cs.management()
|
|
254
|
-
).put("/v0/org/{org_id}/derive_key", {
|
|
255
|
-
params: { path: { org_id: orgId } },
|
|
256
|
-
body: {
|
|
257
|
-
derivation_path: derivationPaths,
|
|
258
|
-
mnemonic_id: mnemonicId,
|
|
259
|
-
key_type: keyType,
|
|
260
|
-
owner: ownerId || null,
|
|
261
|
-
},
|
|
262
|
-
parseAs: "json",
|
|
263
|
-
});
|
|
264
|
-
const data = assertOk(resp);
|
|
265
|
-
return data.keys.map((k) => new Key(cs, orgId, k));
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/** Get a key by id.
|
|
269
|
-
* @param {CubeSigner} cs The CubeSigner instance to use for signing.
|
|
270
|
-
* @param {string} orgId The id of the organization to which the key belongs.
|
|
271
|
-
* @param {string} keyId The id of the key to get.
|
|
272
|
-
* @return {Key} The key.
|
|
273
|
-
* @internal
|
|
274
|
-
* */
|
|
275
|
-
static async getKey(cs: CubeSigner, orgId: string, keyId: string): Promise<Key> {
|
|
276
|
-
const resp = await (
|
|
277
|
-
await cs.management()
|
|
278
|
-
).get("/v0/org/{org_id}/keys/{key_id}", {
|
|
279
|
-
params: { path: { org_id: orgId, key_id: keyId } },
|
|
280
|
-
parseAs: "json",
|
|
281
|
-
});
|
|
282
|
-
const data = assertOk(resp);
|
|
283
|
-
return new Key(cs, orgId, data);
|
|
192
|
+
private async update(request: UpdateKeyRequest): Promise<KeyInfo> {
|
|
193
|
+
const data = await this.#csc.keyUpdate(this.id, request);
|
|
194
|
+
return toKeyInfo(data);
|
|
284
195
|
}
|
|
285
196
|
|
|
286
197
|
/** Fetches the key information.
|
|
@@ -288,13 +199,7 @@ export class Key {
|
|
|
288
199
|
* @internal
|
|
289
200
|
* */
|
|
290
201
|
private async fetch(): Promise<KeyInfo> {
|
|
291
|
-
const
|
|
292
|
-
await this.#cs.management()
|
|
293
|
-
).get("/v0/org/{org_id}/keys/{key_id}", {
|
|
294
|
-
params: { path: { org_id: this.orgId, key_id: this.id } },
|
|
295
|
-
parseAs: "json",
|
|
296
|
-
});
|
|
297
|
-
const data = assertOk(resp);
|
|
202
|
+
const data = await this.#csc.keyGet(this.id);
|
|
298
203
|
return toKeyInfo(data);
|
|
299
204
|
}
|
|
300
205
|
}
|
|
@@ -328,6 +233,8 @@ export function fromSchemaKeyType(ty: SchemaKeyType): KeyType {
|
|
|
328
233
|
return Ed25519.Aptos;
|
|
329
234
|
case "Ed25519CardanoAddrVk":
|
|
330
235
|
return Ed25519.Cardano;
|
|
236
|
+
case "Ed25519StellarAddr":
|
|
237
|
+
return Ed25519.Stellar;
|
|
331
238
|
case "Stark":
|
|
332
239
|
return Stark;
|
|
333
240
|
case "Mnemonic":
|