Skip to content

Commit

Permalink
Merge pull request #44 from Web3Auth/fix/tests
Browse files Browse the repository at this point in the history
fix solana signed messages
  • Loading branch information
chaitanyapotti authored May 23, 2024
2 parents 578db08 + eb93dd4 commit fe56da6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 41 deletions.
79 changes: 49 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 15 additions & 10 deletions src/Web3Auth.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -21,6 +21,7 @@ import {
WalletLoginError,
Web3AuthError,
} from "@web3auth/base";
import bs58 from "bs58";

import { PASSKEYS_PLUGIN } from "./constants";
import {
Expand Down Expand Up @@ -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<unknown, string[]>({
method: "eth_accounts",
method: chainNamespace === CHAIN_NAMESPACES.EIP155 ? "eth_accounts" : "getAccounts",
});
if (accounts && accounts.length > 0) {
const existingToken = getSavedToken(accounts[0] as string, "SFA");
Expand All @@ -186,15 +187,10 @@ class Web3Auth extends SafeEventEmitter implements IWeb3Auth {
};

const challenge = await signChallenge(payload, chainNamespace);

const signedMessage = await this.privKeyProvider.provider.request<string[], string>({
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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -472,6 +468,15 @@ class Web3Auth extends SafeEventEmitter implements IWeb3Auth {
});
});
}

private async _getSignedMessage(challenge: string, accounts: string[], chainNamespace: ChainNamespaceType): Promise<string> {
const signedMessage = await this.privKeyProvider.provider.request<string[] | { message: Uint8Array }, string | Uint8Array>({
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;

0 comments on commit fe56da6

Please sign in to comment.