|
1 | 1 | import { OpenloginSessionManager } from "@toruslabs/openlogin-session-manager"; |
2 | | -import { BaseLoginParams, BUILD_ENV, jsonToBase64, OPENLOGIN_ACTIONS, OPENLOGIN_NETWORK, OpenloginSessionConfig } from "@toruslabs/openlogin-utils"; |
| 2 | +import { |
| 3 | + BaseLoginParams, |
| 4 | + BUILD_ENV, |
| 5 | + jsonToBase64, |
| 6 | + MFA_LEVELS, |
| 7 | + OPENLOGIN_ACTIONS, |
| 8 | + OPENLOGIN_NETWORK, |
| 9 | + OpenloginSessionConfig, |
| 10 | +} from "@toruslabs/openlogin-utils"; |
3 | 11 | import log from "loglevel"; |
4 | 12 |
|
5 | 13 | import { InitializationError, LoginError } from "./errors"; |
@@ -88,7 +96,20 @@ class Web3Auth implements IWeb3Auth { |
88 | 96 | private get baseUrl(): string { |
89 | 97 | // testing and develop don't have versioning |
90 | 98 | if (this.initParams.buildEnv === BUILD_ENV.DEVELOPMENT || this.initParams.buildEnv === BUILD_ENV.TESTING) return `${this.initParams.sdkUrl}`; |
91 | | - return `${this.initParams.sdkUrl}/v6`; |
| 99 | + return `${this.initParams.sdkUrl}/v7`; |
| 100 | + } |
| 101 | + |
| 102 | + private get walletSdkUrl(): string { |
| 103 | + const walletServicesVersion: string = "v1"; |
| 104 | + let sdkUrl: string; |
| 105 | + if (this.initParams.buildEnv === BUILD_ENV.TESTING) { |
| 106 | + sdkUrl = "https://develop-wallet.web3auth.io"; |
| 107 | + } else if (this.initParams.buildEnv === BUILD_ENV.STAGING) { |
| 108 | + sdkUrl = `https://staging-wallet.web3auth.io/${walletServicesVersion}`; |
| 109 | + } else { |
| 110 | + sdkUrl = `https://wallet.web3auth.io/${walletServicesVersion}`; |
| 111 | + } |
| 112 | + return sdkUrl; |
92 | 113 | } |
93 | 114 |
|
94 | 115 | async init(): Promise<void> { |
@@ -194,6 +215,104 @@ class Web3Auth implements IWeb3Auth { |
194 | 215 | this._syncState({}); |
195 | 216 | } |
196 | 217 |
|
| 218 | + async launchWalletServices(loginParams: SdkLoginParams, path: string | null = "wallet"): Promise<void> { |
| 219 | + if (!this.ready) throw InitializationError.notInitialized("Please call init first."); |
| 220 | + if (!this.sessionManager.sessionId) { |
| 221 | + throw new Error("user should be logged in."); |
| 222 | + } |
| 223 | + |
| 224 | + const dataObject: OpenloginSessionConfig = { |
| 225 | + actionType: OPENLOGIN_ACTIONS.LOGIN, |
| 226 | + options: this.initParams, |
| 227 | + params: { |
| 228 | + ...loginParams, |
| 229 | + redirectUrl: loginParams.redirectUrl || this.initParams.redirectUrl, |
| 230 | + }, |
| 231 | + }; |
| 232 | + |
| 233 | + const url = `${this.walletSdkUrl}/${path}`; |
| 234 | + log.debug(`[Web3Auth] config passed: ${JSON.stringify(dataObject)}`); |
| 235 | + const loginId = await this.getLoginId(dataObject); |
| 236 | + |
| 237 | + const configParams: BaseLoginParams = { |
| 238 | + loginId, |
| 239 | + sessionNamespace: "", |
| 240 | + }; |
| 241 | + |
| 242 | + const loginUrl = constructURL({ |
| 243 | + baseURL: url, |
| 244 | + hash: { b64Params: jsonToBase64(configParams) }, |
| 245 | + }); |
| 246 | + |
| 247 | + this.webBrowser.openAuthSessionAsync(loginUrl, dataObject.params.redirectUrl); |
| 248 | + } |
| 249 | + |
| 250 | + async enableMFA(): Promise<void> { |
| 251 | + log.debug("enableMFA_1 starts"); |
| 252 | + if (!this.ready) throw InitializationError.notInitialized("Please call init first."); |
| 253 | + if (!this.sessionManager.sessionId) { |
| 254 | + throw new Error("user should be logged in."); |
| 255 | + } |
| 256 | + |
| 257 | + const loginParams: SdkLoginParams = { |
| 258 | + loginProvider: this.state.userInfo?.typeOfLogin, |
| 259 | + mfaLevel: MFA_LEVELS.MANDATORY, |
| 260 | + extraLoginOptions: { |
| 261 | + login_hint: this.state.userInfo?.verifierId, |
| 262 | + }, |
| 263 | + redirectUrl: this.initParams.redirectUrl, |
| 264 | + }; |
| 265 | + |
| 266 | + log.debug("enableMFA_2 starts"); |
| 267 | + const dataObject: OpenloginSessionConfig = { |
| 268 | + actionType: OPENLOGIN_ACTIONS.ENABLE_MFA, |
| 269 | + options: this.initParams, |
| 270 | + params: { |
| 271 | + ...loginParams, |
| 272 | + redirectUrl: loginParams.redirectUrl || this.initParams.redirectUrl, |
| 273 | + }, |
| 274 | + sessionId: this.sessionManager.sessionId, |
| 275 | + }; |
| 276 | + |
| 277 | + log.debug("enableMFA_3 starts"); |
| 278 | + const result = await this.openloginHandler(`${this.baseUrl}/start`, dataObject); |
| 279 | + |
| 280 | + log.debug("enableMFA_4", result); |
| 281 | + if (result.type !== "success" || !result.url) { |
| 282 | + log.error(`[Web3Auth] login flow failed with error type ${result.type}`); |
| 283 | + throw new Error(`login flow failed with error type ${result.type}`); |
| 284 | + } |
| 285 | + |
| 286 | + log.debug("enableMFA_5"); |
| 287 | + const { sessionId, sessionNamespace, error } = extractHashValues(result.url); |
| 288 | + if (error || !sessionId) { |
| 289 | + throw LoginError.loginFailed(error || "SessionId is missing"); |
| 290 | + } |
| 291 | + |
| 292 | + log.debug("enableMFA_6"); |
| 293 | + if (sessionId) { |
| 294 | + await this.keyStore.set("sessionId", sessionId); |
| 295 | + this.sessionManager.sessionId = sessionId; |
| 296 | + this.sessionManager.sessionNamespace = sessionNamespace || ""; |
| 297 | + } |
| 298 | + |
| 299 | + const sessionData = await this._authorizeSession(); |
| 300 | + |
| 301 | + if (sessionData.userInfo?.dappShare.length > 0) { |
| 302 | + const verifier = sessionData.userInfo?.aggregateVerifier || sessionData.userInfo?.verifier; |
| 303 | + await this.keyStore.set(verifier, sessionData.userInfo?.dappShare); |
| 304 | + } |
| 305 | + |
| 306 | + this._syncState({ |
| 307 | + privKey: sessionData.privKey, |
| 308 | + coreKitKey: sessionData.coreKitKey, |
| 309 | + coreKitEd25519PrivKey: sessionData.coreKitEd25519PrivKey, |
| 310 | + ed25519PrivKey: sessionData.ed25519PrivKey, |
| 311 | + sessionId: sessionData.sessionId, |
| 312 | + userInfo: sessionData.userInfo, |
| 313 | + }); |
| 314 | + } |
| 315 | + |
197 | 316 | public userInfo(): State["userInfo"] { |
198 | 317 | if (this.privKey) { |
199 | 318 | const storeData = this.state.userInfo; |
|
0 commit comments