Skip to content

Commit

Permalink
autologin does not relay anymore on the injected provider poiting to …
Browse files Browse the repository at this point in the history
…the correct network
  • Loading branch information
Viterbo authored and donnyquixotic committed Feb 4, 2024
1 parent 259baa5 commit 65c6b8f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
51 changes: 20 additions & 31 deletions src/antelope/mocks/AccountStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ let currentAccount = null as addressString | null;
interface LoginEVMActionData {
authenticator: EVMAuthenticator
network: string,
autoLogAccount?: string,
}

class AccountStore {
Expand All @@ -54,9 +55,10 @@ class AccountStore {
} as AccountModel;
}

async loginEVM({ authenticator, network }: LoginEVMActionData, trackAnalyticsEvents: boolean): Promise<boolean> {
async loginEVM({ authenticator, network, autoLogAccount }: LoginEVMActionData, trackAnalyticsEvents: boolean): Promise<boolean> {
currentAuthenticator = authenticator;
currentAccount = await authenticator.login(network, trackAnalyticsEvents);
currentAccount = autoLogAccount ? await authenticator.autoLogin(network, autoLogAccount, trackAnalyticsEvents) : await authenticator.login(network, trackAnalyticsEvents);

const account = useAccountStore().getAccount(authenticator.label);
getAntelope().events.onLoggedIn.next(account);
return true;
Expand Down Expand Up @@ -153,37 +155,24 @@ class AccountStore {

async assertNetworkConnection(label: string): Promise<boolean> {
if (!await useAccountStore().isConnectedToCorrectNetwork(label)) {
return new Promise<boolean>((resolve) => {
// eslint-disable-next-line no-async-promise-executor
return new Promise<boolean>(async (resolve) => {
const ant = getAntelope();
const authenticator = useAccountStore().loggedAccount.authenticator as EVMAuthenticator;
const networkName = useChainStore().loggedChain.settings.getDisplay();
const errorMessage = ant.config.localizationHandler('evm_wallet.incorrect_network', { networkName });
let userClickedSwitch = false;
ant.config.notifyFailureWithAction(errorMessage, {
label: ant.config.localizationHandler('evm_wallet.switch'),
handler: async () => {
userClickedSwitch = true;
try {
await authenticator.ensureCorrectChain();
if (!await useAccountStore().isConnectedToCorrectNetwork(label)) {
resolve(false);
} else {
resolve(true);
}
} catch (error) {
const message = (error as Error).message;
if (message === 'antelope.evm.error_switch_chain_rejected') {
ant.config.notifyNeutralMessageHandler(message);
}
resolve(false);
}
},
onDismiss: () => {
if (!userClickedSwitch) {
resolve(false);
}
},
});
try {
await authenticator.ensureCorrectChain();
if (!await useAccountStore().isConnectedToCorrectNetwork(label)) {
resolve(false);
} else {
resolve(true);
}
} catch (error) {
const message = (error as Error).message;
if (message === 'antelope.evm.error_switch_chain_rejected') {
ant.config.notifyNeutralMessageHandler(message);
}
resolve(false);
}
});
} else {
return true;
Expand Down
18 changes: 18 additions & 0 deletions src/antelope/wallets/authenticators/EVMAuthenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ export abstract class EVMAuthenticator {
}
}

async autoLogin(network: string, account: string, trackAnalyticsEvents?: boolean): Promise<addressString> {
this.trace('autoLogin', network, account);
this.trace('AutoLogin analytics enabled =', trackAnalyticsEvents);

const chain = useChainStore();
try {
chain.setChain(CURRENT_CONTEXT, network);
return account as addressString;
} catch (error) {
if ((error as unknown as ExceptionError).code === 4001) {
throw new AntelopeError('antelope.evm.error_connect_rejected');
} else {
console.error('Error:', error);
throw new AntelopeError('antelope.evm.error_login');
}
}
}

async ensureCorrectChain(): Promise<ethers.providers.Web3Provider> {
this.trace('ensureCorrectChain');
if (usePlatformStore().isMobile) {
Expand Down
10 changes: 6 additions & 4 deletions src/components/LoginModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export default defineComponent({
localStorage.setItem(LOGIN_DATA_KEY, JSON.stringify({
type: LOGIN_EVM,
provider: pr_name,
account: address,
}));
});
Expand All @@ -80,7 +81,7 @@ export default defineComponent({
const loginObj = JSON.parse(loginData);
if (loginObj.type === LOGIN_EVM) {
this.loginWithAntelope(loginObj.provider);
this.loginWithAntelope(loginObj.provider, loginObj.account);
} else if (loginObj.type === LOGIN_NATIVE) {
const wallet = this.authenticators.find((a: { getName: () => any; }) => a.getName() === loginObj.provider);
if (wallet) {
Expand Down Expand Up @@ -144,7 +145,7 @@ export default defineComponent({
nativeAccount: accountName,
});
this.$providerManager.setProvider(account);
localStorage.setItem(LOGIN_DATA_KEY, JSON.stringify({ type: LOGIN_NATIVE, provider: wallet.getName() }));
localStorage.setItem(LOGIN_DATA_KEY, JSON.stringify({ type: LOGIN_NATIVE, provider: wallet.getName(), account: evmAccount.address }));
}
this.$emit('hide');
},
Expand All @@ -156,7 +157,7 @@ export default defineComponent({
return wallet.getStyle().icon;
},
async loginWithAntelope(name:string) {
async loginWithAntelope(name:string, autoLogAccount?: string) {
const label = CURRENT_CONTEXT;
const auth = getAntelope().wallets.getAuthenticator(name);
if (!auth) {
Expand All @@ -165,12 +166,13 @@ export default defineComponent({
}
const authenticator = auth.newInstance(label);
const network = useChainStore().currentChain.settings.getNetwork();
useAccountStore().loginEVM({ authenticator, network }, true).then(() => {
useAccountStore().loginEVM({ authenticator, network, autoLogAccount }, true).then(() => {
const address = useAccountStore().getAccount(label).account;
this.setLogin({ address });
localStorage.setItem(LOGIN_DATA_KEY, JSON.stringify({
type: LOGIN_EVM,
provider: name,
account: address,
}));
});
this.$emit('hide');
Expand Down

0 comments on commit 65c6b8f

Please sign in to comment.