diff --git a/package-lock.json b/package-lock.json index 44b3de4..0232e62 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,8 @@ "@toruslabs/openlogin-subkey": "^8.1.0", "@toruslabs/openlogin-utils": "^8.1.2", "@toruslabs/torus.js": "^12.3.5", - "@web3auth/base": "^8.6.1" + "@web3auth/base": "^8.6.1", + "bs58": "^5.0.0" }, "devDependencies": { "@babel/register": "^7.23.7", @@ -3234,6 +3235,26 @@ "superstruct": "^0.14.2" } }, + "node_modules/@solana/web3.js/node_modules/base-x": { + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/base-x/-/base-x-3.0.9.tgz", + "integrity": "sha512-H7JU6iBHTal1gp56aKoaa//YUxEaAOUiydvrV/pILqIHXTtqxSkATOnDA2u+jZ/61sD+L/412+7kzXRtWukhpQ==", + "dev": true, + "peer": true, + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, + "node_modules/@solana/web3.js/node_modules/bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha512-Ok3Wdf5vOIlBrgCvTq96gBkJw+JUEzdBgyaza5HLtPm7yTHkjRy8+JzNyHF7BHa0bNWOQIp3m5YF0nnFcOIKLw==", + "dev": true, + "peer": true, + "dependencies": { + "base-x": "^3.0.2" + } + }, "node_modules/@solana/web3.js/node_modules/node-fetch": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.9.tgz", @@ -4393,21 +4414,6 @@ "@solana/web3.js": "^1.x" } }, - "node_modules/@web3auth/solana-provider/node_modules/base-x": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/base-x/-/base-x-4.0.0.tgz", - "integrity": "sha512-FuwxlW4H5kh37X/oW59pwTzzTKRzfrrQwhmyspRM7swOEZcHtDZSCt45U6oKgtuFE+WYPblePMVIPR4RZrh/hw==", - "dev": true - }, - "node_modules/@web3auth/solana-provider/node_modules/bs58": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/bs58/-/bs58-5.0.0.tgz", - "integrity": "sha512-r+ihvQJvahgYT50JD05dyJNKlmmSlMoOGwn1lCcEzanPglg7TxYjioQUYehQ9mAR/+hOSd2jRc/Z2y5UxBymvQ==", - "dev": true, - "dependencies": { - "base-x": "^4.0.0" - } - }, "node_modules/@webassemblyjs/ast": { "version": "1.12.1", "resolved": "https://registry.npmjs.org/@webassemblyjs/ast/-/ast-1.12.1.tgz", @@ -5241,14 +5247,9 @@ "dev": true }, "node_modules/base-x": { - "version": "3.0.9", - "resolved": "https://registry.npmjs.org/base-x/-/base-x-3.0.9.tgz", - "integrity": "sha512-H7JU6iBHTal1gp56aKoaa//YUxEaAOUiydvrV/pILqIHXTtqxSkATOnDA2u+jZ/61sD+L/412+7kzXRtWukhpQ==", - "dev": true, - "peer": true, - "dependencies": { - "safe-buffer": "^5.0.1" - } + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/base-x/-/base-x-4.0.0.tgz", + "integrity": "sha512-FuwxlW4H5kh37X/oW59pwTzzTKRzfrrQwhmyspRM7swOEZcHtDZSCt45U6oKgtuFE+WYPblePMVIPR4RZrh/hw==" }, "node_modules/base64-js": { "version": "1.5.1", @@ -5385,6 +5386,26 @@ "text-encoding-utf-8": "^1.0.2" } }, + "node_modules/borsh/node_modules/base-x": { + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/base-x/-/base-x-3.0.9.tgz", + "integrity": "sha512-H7JU6iBHTal1gp56aKoaa//YUxEaAOUiydvrV/pILqIHXTtqxSkATOnDA2u+jZ/61sD+L/412+7kzXRtWukhpQ==", + "dev": true, + "peer": true, + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, + "node_modules/borsh/node_modules/bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha512-Ok3Wdf5vOIlBrgCvTq96gBkJw+JUEzdBgyaza5HLtPm7yTHkjRy8+JzNyHF7BHa0bNWOQIp3m5YF0nnFcOIKLw==", + "dev": true, + "peer": true, + "dependencies": { + "base-x": "^3.0.2" + } + }, "node_modules/bowser": { "version": "2.11.0", "resolved": "https://registry.npmjs.org/bowser/-/bowser-2.11.0.tgz", @@ -5618,13 +5639,11 @@ } }, "node_modules/bs58": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", - "integrity": "sha512-Ok3Wdf5vOIlBrgCvTq96gBkJw+JUEzdBgyaza5HLtPm7yTHkjRy8+JzNyHF7BHa0bNWOQIp3m5YF0nnFcOIKLw==", - "dev": true, - "peer": true, + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-5.0.0.tgz", + "integrity": "sha512-r+ihvQJvahgYT50JD05dyJNKlmmSlMoOGwn1lCcEzanPglg7TxYjioQUYehQ9mAR/+hOSd2jRc/Z2y5UxBymvQ==", "dependencies": { - "base-x": "^3.0.2" + "base-x": "^4.0.0" } }, "node_modules/buffer": { diff --git a/package.json b/package.json index 866d649..a82ac16 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,8 @@ "@toruslabs/openlogin-subkey": "^8.1.0", "@toruslabs/openlogin-utils": "^8.1.2", "@toruslabs/torus.js": "^12.3.5", - "@web3auth/base": "^8.6.1" + "@web3auth/base": "^8.6.1", + "bs58": "^5.0.0" }, "devDependencies": { "@babel/register": "^7.23.7", diff --git a/src/Web3Auth.ts b/src/Web3Auth.ts index 6829fc8..cd8b44c 100644 --- a/src/Web3Auth.ts +++ b/src/Web3Auth.ts @@ -1,4 +1,4 @@ -import { signChallenge, verifySignedChallenge } from "@toruslabs/base-controllers"; +import { ChainNamespaceType, signChallenge, verifySignedChallenge } from "@toruslabs/base-controllers"; import { NodeDetailManager } from "@toruslabs/fetch-node-details"; import { SafeEventEmitter } from "@toruslabs/openlogin-jrpc"; import { OpenloginSessionManager } from "@toruslabs/openlogin-session-manager"; @@ -21,6 +21,7 @@ import { WalletLoginError, Web3AuthError, } from "@web3auth/base"; +import bs58 from "bs58"; import { PASSKEYS_PLUGIN } from "./constants"; import { @@ -164,7 +165,7 @@ class Web3Auth extends SafeEventEmitter implements IWeb3Auth { const { chainNamespace, chainId } = this.coreOptions.chainConfig || {}; if (!this.authInstance || !this.privKeyProvider || !this.nodeDetailManagerInstance) throw WalletInitializationError.notReady(); const accounts = await this.privKeyProvider.provider.request({ - method: "eth_accounts", + method: chainNamespace === CHAIN_NAMESPACES.EIP155 ? "eth_accounts" : "getAccounts", }); if (accounts && accounts.length > 0) { const existingToken = getSavedToken(accounts[0] as string, "SFA"); @@ -186,15 +187,10 @@ class Web3Auth extends SafeEventEmitter implements IWeb3Auth { }; const challenge = await signChallenge(payload, chainNamespace); - - const signedMessage = await this.privKeyProvider.provider.request({ - method: "personal_sign", - params: [challenge, accounts[0]], - }); - + const signedMessage = await this._getSignedMessage(challenge, accounts, chainNamespace); const idToken = await verifySignedChallenge( chainNamespace, - signedMessage as string, + signedMessage, challenge, this.SFA_ISSUER, this.coreOptions.sessionTime, @@ -345,7 +341,7 @@ class Web3Auth extends SafeEventEmitter implements IWeb3Auth { const sessionId = OpenloginSessionManager.generateRandomSessionKey(); this.sessionManager.sessionId = sessionId; - const { idToken } = await this.authenticateUser(); + const { idToken } = await this.authenticateUser().catch((_) => ({ idToken: "" })); if (params.userInfo) { params.userInfo.idToken = idToken; } else { @@ -472,6 +468,15 @@ class Web3Auth extends SafeEventEmitter implements IWeb3Auth { }); }); } + + private async _getSignedMessage(challenge: string, accounts: string[], chainNamespace: ChainNamespaceType): Promise { + const signedMessage = await this.privKeyProvider.provider.request({ + method: chainNamespace === CHAIN_NAMESPACES.EIP155 ? "personal_sign" : "signMessage", + params: chainNamespace === CHAIN_NAMESPACES.EIP155 ? [challenge, accounts[0]] : { message: Buffer.from(challenge) }, + }); + if (chainNamespace === CHAIN_NAMESPACES.SOLANA) return bs58.encode(signedMessage as Uint8Array); + return signedMessage as string; + } } export default Web3Auth;