-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathinterfaces.ts
More file actions
375 lines (319 loc) · 10.6 KB
/
interfaces.ts
File metadata and controls
375 lines (319 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import { Point as TkeyPoint, ShareDescriptionMap } from "@tkey-mpc/common-types";
import ThresholdKey from "@tkey-mpc/core";
import type {
AGGREGATE_VERIFIER_TYPE,
ExtraParams,
LoginWindowResponse,
SubVerifierDetails,
TorusVerifierResponse,
UX_MODE_TYPE,
WebAuthnExtraParams,
} from "@toruslabs/customauth";
import { CustomChainConfig, SafeEventEmitterProvider } from "@web3auth/base";
import BN from "bn.js";
import { FactorKeyTypeShareDescription, TssShareType, USER_PATH, WEB3AUTH_NETWORK } from "./constants";
export type CoreKitMode = UX_MODE_TYPE | "nodejs";
export interface IStorage {
getItem(key: string): string | null;
setItem(key: string, value: string): void;
}
export interface InitParams {
handleRedirectResult: boolean;
}
export interface BaseLoginParams {
// offset in seconds
serverTimeOffset?: number;
}
export interface SubVerifierDetailsParams extends BaseLoginParams {
subVerifierDetails: SubVerifierDetails;
}
export interface AggregateVerifierLoginParams extends BaseLoginParams {
aggregateVerifierIdentifier: string;
subVerifierDetailsArray: SubVerifierDetails[];
aggregateVerifierType?: AGGREGATE_VERIFIER_TYPE;
}
export interface IFactorKey {
factorKey: BN;
shareType: TssShareType;
}
export enum COREKIT_STATUS {
NOT_INITIALIZED = "NOT_INITIALIZED",
INITIALIZED = "INITIALIZED",
REQUIRED_SHARE = "REQUIRED_SHARE",
LOGGED_IN = "LOGGED_IN",
}
export type MPCKeyDetails = {
metadataPubKey: TkeyPoint;
threshold: number;
requiredFactors: number;
totalFactors: number;
shareDescriptions: ShareDescriptionMap;
tssPubKey?: TkeyPoint;
};
export type OauthLoginParams = SubVerifierDetailsParams | AggregateVerifierLoginParams;
export type UserInfo = TorusVerifierResponse & LoginWindowResponse;
export interface EnableMFAParams {
/**
* A BN used for encrypting your Device/ Recovery TSS Key Share. You can generate it using `generateFactorKey()` function or use an existing one.
*/
factorKey?: BN;
/**
* Setting the Description of Share - Security Questions, Device Share, Seed Phrase, Password Share, Social Share, Other. Default is Other.
*/
shareDescription?: FactorKeyTypeShareDescription;
/**
* Additional metadata information you want to be stored alongside this factor for easy identification.
*/
additionalMetadata?: Record<string, string>;
}
export interface CreateFactorParams extends EnableMFAParams {
/**
* Setting the Type of Share - Device or Recovery.
**/
shareType: TssShareType;
}
export interface IdTokenLoginParams {
/**
* Name of the verifier created on Web3Auth Dashboard. In case of Aggregate Verifier, the name of the top level aggregate verifier.
*/
verifier: string;
/**
* Unique Identifier for the User. The verifier identifier field set for the verifier/ sub verifier. E.g. "sub" field in your on jwt id token.
*/
verifierId: string;
/**
* The idToken received from the Auth Provider.
*/
idToken: string;
/**
* Name of the sub verifier in case of aggregate verifier setup. This field should only be provided in case of an aggregate verifier.
*/
subVerifier?: string;
/**
* Extra verifier params in case of a WebAuthn verifier type.
*/
extraVerifierParams?: WebAuthnExtraParams;
/**
* Any additional parameter (key value pair) you'd like to pass to the login function.
*/
additionalParams?: ExtraParams;
}
export interface IRemoteClientState {
remoteFactorPub: string;
remoteClientUrl: string;
remoteClientToken: string;
metadataShare: string;
}
export interface Web3AuthState {
oAuthKey?: string;
signatures?: string[];
userInfo?: UserInfo;
tssShareIndex?: number;
tssPubKey?: Buffer;
factorKey?: BN;
remoteClient?: IRemoteClientState;
}
export interface ICoreKit {
/**
* The tKey instance, if initialized.
* TKey is the core module on which this wrapper SDK sits for easy integration.
**/
tKey: ThresholdKey | null;
/**
* Provider for making the blockchain calls.
**/
provider: SafeEventEmitterProvider | null;
/**
* Signatures generated from the OAuth Login.
**/
signatures: string[] | null;
/**
* Status of the current MPC Core Kit Instance
**/
status: COREKIT_STATUS;
/**
* The current sdk state.
*/
state: Web3AuthState;
/**
* The current session id.
*/
sessionId: string;
/**
* The function used to initailise the state of MPCCoreKit
* Also is useful to resume an existing session.
* @param initParams - Contains flag for handleRedirectResult. Default is true.
*/
init(initParams?: InitParams): Promise<void>;
/**
* Login into the SDK in an implicit flow and initialize all relevant components.
* @param loginParams - Parameters for Implicit Login.
*/
loginWithOauth(loginParams: OauthLoginParams): Promise<void>;
/**
* Login into the SDK using ID Token based login and initialize all relevant components.
* @param idTokenLoginParams - Parameters with ID Token based Login.
*/
loginWithJWT(idTokenLoginParams: IdTokenLoginParams): Promise<void>;
/**
* Enable MFA for the user. Deletes the Cloud factor and generates a new
* factor key and a backup factor key. Recommended for Non Custodial Flow.
* Stores the factor key in browser storage and returns the backup factor key.
*
* ** NOTE before enableMFA, you will need to commitChanges if manualSync is true.
*
* @param enableMFAParams - Parameters for recovery factor for MFA.
* @param recoveryFactor - Default is true. If false, recovery factor will NOT be created.
* @returns The backup factor key if if recoveryFacort is true else empty string.
*/
enableMFA(enableMFAParams: EnableMFAParams, recoveryFactor?: boolean): Promise<string>;
/**
* Second step for login where the user inputs their factor key.
* @param factorKey - A BN used for encrypting your Device/ Recovery TSS Key
* Share. You can generate it using `generateFactorKey()` function or use an
* existing one.
*/
inputFactorKey(factorKey: BN): Promise<void>;
/**
* Returns the current Factor Key and TssShareType in MPC Core Kit State
**/
getCurrentFactorKey(): IFactorKey;
/**
* Creates a new factor for authentication. Generates and returns a new factor
* key if no factor key is provided in `params`.
* @param createFactorParams - Parameters for creating a new factor.
* @returns The factor key.
*/
createFactor(createFactorParams: CreateFactorParams): Promise<string>;
/**
* Deletes the factor identified by the given public key, including all
* associated metadata.
* @param factorPub - The public key of the factor to delete.
*/
deleteFactor(factorPub: TkeyPoint): Promise<void>;
/**
* Logs out the user, terminating the session.
*/
logout(): Promise<void>;
/**
* Get user information provided by the OAuth provider.
*/
getUserInfo(): UserInfo;
/**
* Get information about how the keys of the user is managed according to the information in the metadata server.
*/
getKeyDetails(): MPCKeyDetails;
/**
* Commit the changes made to the user's account when in manual sync mode.
*/
commitChanges(): Promise<void>;
/**
* Export the user's current TSS MPC account as a private key
*/
_UNSAFE_exportTssKey(): Promise<string>;
}
export type WEB3AUTH_NETWORK_TYPE = (typeof WEB3AUTH_NETWORK)[keyof typeof WEB3AUTH_NETWORK];
export type USER_PATH_TYPE = (typeof USER_PATH)[keyof typeof USER_PATH];
export interface Web3AuthOptions {
/**
* The Web3Auth Client ID for your application. Find one at https://dashboard.web3auth.io
*/
web3AuthClientId: string;
/**
* Chain Config for the chain you want to connect to. Currently supports only EVM based chains.
*/
chainConfig?: CustomChainConfig;
/**
* @defaultValue `false`
*/
manualSync?: boolean;
/**
* @defaultValue `${window.location.origin}/serviceworker`
*/
baseUrl?: string;
/**
*
* @defaultValue `'sapphire_mainnet'`
*/
web3AuthNetwork?: WEB3AUTH_NETWORK_TYPE;
/**
*
* @defaultValue `'local'`
*/
storageKey?: "session" | "local" | "memory" | IStorage;
/**
* @defaultValue 86400
*/
sessionTime?: number;
/**
* @defaultValue `'POPUP'`
*/
uxMode?: CoreKitMode;
/**
* @defaultValue `false`
* enables logging of the internal packages.
*/
enableLogging?: boolean;
/**
* This option is used to specify the url path where user will be
* redirected after login. Redirect Uri for OAuth is baseUrl/redirectPathName.
*
*
* @defaultValue `"redirect"`
*
* @remarks
* At verifier's interface (where you obtain client id), please use baseUrl/redirectPathName
* as the redirect_uri
*
* Torus Direct SDK installs a service worker relative to baseUrl to capture
* the auth redirect at `redirectPathName` path.
*
* For ex: While using serviceworker if `baseUrl` is "http://localhost:3000/serviceworker" and
* `redirectPathName` is 'redirect' (which is default)
* then user will be redirected to http://localhost:3000/serviceworker/redirect page after login
* where service worker will capture the results and send it back to original window where login
* was initiated.
*
* For browsers where service workers are not supported or if you wish to not use
* service workers,create and serve redirect page (i.e redirect.html file which is
* available in serviceworker folder of this package)
*
* If you are using redirect uxMode, you can get the results directly on your `redirectPathName`
* path using `getRedirectResult` function.
*
* For ex: if baseUrl is "http://localhost:3000" and `redirectPathName` is 'auth'
* then user will be redirected to http://localhost:3000/auth page after login
* where you can get login result by calling `getRedirectResult` on redirected page mount.
*
* Please refer to examples https://github.com/torusresearch/customauth/tree/master/examples
* for more understanding.
*
*/
redirectPathName?: string;
/**
* @defaultValue `false`
* Disables the cloud factor key, enabling the one key semi custodial flow.
* Recommended for Non Custodial Flow.
*/
disableHashedFactorKey?: boolean;
/**
* @defaultValue `null`
* Overwrite tss-lib for nodejs.
* Required for nodejs mode.
* Do not use this option for non nodejs mode.
*/
tssLib?: unknown;
}
export type Web3AuthOptionsWithDefaults = Required<Web3AuthOptions>;
export interface SessionData {
oAuthKey: string;
factorKey: string;
tssShareIndex: number;
tssPubKey: string;
signatures: string[];
userInfo: UserInfo;
remoteClient?: IRemoteClientState;
}
export interface TkeyLocalStoreData {
factorKey: string;
}