@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
91 lines (88 loc) • 3.55 kB
JavaScript
import { signChallenge, verifySignedChallenge } from '@toruslabs/base-controllers';
import { EVM_METHOD_TYPES } from '@web3auth/ws-embed';
import { getSavedToken, checkIfTokenIsExpired, saveToken, clearToken } from '../../base/connector/utils.js';
import { BaseConnector } from '../../base/connector/baseConnector.js';
import { WalletLoginError, WalletInitializationError } from '../../base/errors/index.js';
import { CONNECTOR_STATUS, CONNECTOR_EVENTS } from '../../base/connector/constants.js';
class BaseEvmConnector extends BaseConnector {
async init(_) {}
async getIdentityToken() {
if (!this.provider || !this.canAuthorize) throw WalletLoginError.notConnectedError();
if (!this.coreOptions) throw WalletInitializationError.invalidParams("Please initialize Web3Auth with valid options");
this.status = CONNECTOR_STATUS.AUTHORIZING;
this.emit(CONNECTOR_EVENTS.AUTHORIZING, {
connector: this.name
});
const accounts = await this.provider.request({
method: EVM_METHOD_TYPES.GET_ACCOUNTS
});
if (accounts && accounts.length > 0) {
const existingToken = getSavedToken(accounts[0], this.name);
if (existingToken) {
const isExpired = checkIfTokenIsExpired(existingToken);
if (!isExpired) {
this.status = CONNECTOR_STATUS.AUTHORIZED;
this.emit(CONNECTOR_EVENTS.AUTHORIZED, {
connector: this.name,
identityTokenInfo: {
idToken: existingToken
}
});
return {
idToken: existingToken
};
}
}
const chainId = await this.provider.request({
method: "eth_chainId"
});
const currentChainConfig = this.coreOptions.chains.find(x => x.chainId === chainId);
if (!currentChainConfig) throw WalletInitializationError.invalidParams("chainConfig is required before authentication");
const {
chainNamespace
} = currentChainConfig;
const payload = {
domain: window.location.origin,
uri: window.location.href,
address: accounts[0],
chainId: parseInt(chainId, 16),
version: "1",
nonce: Math.random().toString(36).slice(2),
issuedAt: new Date().toISOString()
};
const challenge = await signChallenge(payload, chainNamespace);
const hexChallenge = `0x${Buffer.from(challenge, "utf8").toString("hex")}`;
const signedMessage = await this.provider.request({
method: EVM_METHOD_TYPES.PERSONAL_SIGN,
params: [hexChallenge, accounts[0]]
});
const idToken = await verifySignedChallenge(chainNamespace, signedMessage, challenge, this.name, this.coreOptions.sessionTime, this.coreOptions.clientId, this.coreOptions.web3AuthNetwork);
saveToken(accounts[0], this.name, idToken);
this.status = CONNECTOR_STATUS.AUTHORIZED;
this.emit(CONNECTOR_EVENTS.AUTHORIZED, {
connector: this.name,
identityTokenInfo: {
idToken
}
});
return {
idToken
};
}
throw WalletLoginError.notConnectedError("Not connected with wallet, Please login/connect first");
}
async disconnectSession() {
super.checkDisconnectionRequirements();
const accounts = await this.provider.request({
method: "eth_accounts"
});
if (accounts && accounts.length > 0) {
clearToken(accounts[0], this.name);
}
}
async disconnect() {
this.rehydrated = false;
this.emit(CONNECTOR_EVENTS.DISCONNECTED);
}
}
export { BaseEvmConnector };