Skip to content

Commit 1ba13be

Browse files
authored
Merge pull request #236 from AElfProject/fix/night-elf
fix: 🐛 night elf not delect
2 parents e39f9e1 + c6e372b commit 1ba13be

File tree

13 files changed

+69
-13
lines changed

13 files changed

+69
-13
lines changed

packages/base/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aelf-web-login/wallet-adapter-base",
3-
"version": "0.4.0-alpha.19",
3+
"version": "0.4.0-alpha.20",
44
"type": "module",
55
"main": "dist/esm/index.js",
66
"module": "dist/esm/index.js",

packages/base/src/errors.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const ERR_CODE = {
2626
IN_FINISH_BUT_STATUS_IS_NOT_SUCCESS: 10023,
2727
UNKNOWN: 10024,
2828
DISCOVER_LOGIN_REQUIRED_APP: 10025,
29+
NIGHT_ELF_NOT_DETECTED: 10026,
2930
};
3031

3132
export const ERR_CODE_MSG: {
@@ -59,6 +60,7 @@ export const ERR_CODE_MSG: {
5960
10023: 'Login completed, but the isLoginStatus is not success. Please log in again',
6061
10024: 'Unknown error',
6162
10025: 'Please use the FairyVault app or the FairyVault browser extension on your computer.',
63+
10026: 'NightElf is not detected or has been disabled. Please connect in again.',
6264
};
6365

6466
export function makeError(code: number, nativeError?: any): TWalletError {

packages/base/src/utils/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ import { getOriginalAddress } from './getOriginalAddress';
33
import { isPortkeyApp } from './isPortkeyApp';
44
import { isFairyVaultApp } from './isFairyVaultApp';
55
import { sleep } from './sleep';
6+
import { checkConnectedWallet, checkNightElf, checkPrevLoginType } from './nightElf';
7+
68
export default {
79
isMobileDevices,
810
getOriginalAddress,
911
isPortkeyApp,
1012
isFairyVaultApp,
1113
sleep,
14+
checkConnectedWallet,
15+
checkNightElf,
16+
checkPrevLoginType,
1217
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Storage } from '../enhancedLocalStorage';
2+
import { WalletTypeEnum } from '../types';
3+
4+
export function checkPrevLoginType(type = WalletTypeEnum.aa, storage: Storage = localStorage) {
5+
return storage.getItem('connectedWallet') === type;
6+
}
7+
8+
export function checkConnectedWallet(type = WalletTypeEnum.aa, storage: Storage = localStorage) {
9+
try {
10+
if (checkPrevLoginType(type)) storage.removeItem('connectedWallet');
11+
} catch (error) {
12+
console.log(error, '====checkConnectedWallet');
13+
}
14+
}
15+
16+
export async function checkNightElf(): Promise<boolean> {
17+
let resolveTemp: ((arg0: boolean) => void) | undefined = undefined;
18+
async function check(): Promise<boolean> {
19+
return new Promise((resolve, reject) => {
20+
const win = window as any;
21+
if (win.NightElf) {
22+
console.log('There is NightElf');
23+
resolve(true);
24+
return;
25+
}
26+
setTimeout(() => {
27+
reject({
28+
error: 200001,
29+
message: 'timeout, please download and install the NightELF explorer extension',
30+
});
31+
}, 5000);
32+
resolveTemp = resolve;
33+
});
34+
}
35+
if (typeof window !== 'undefined') {
36+
document.addEventListener('NightElf', () => {
37+
resolveTemp?.(true);
38+
});
39+
}
40+
41+
try {
42+
return await check();
43+
} catch (error) {
44+
console.log(error, '====checkNightElf');
45+
return false;
46+
}
47+
}

packages/bridge/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aelf-web-login/wallet-adapter-bridge",
3-
"version": "0.4.0-alpha.19",
3+
"version": "0.4.0-alpha.20",
44
"type": "module",
55
"main": "dist/esm/index.js",
66
"module": "dist/esm/index.js",

packages/react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aelf-web-login/wallet-adapter-react",
3-
"version": "0.4.0-alpha.19",
3+
"version": "0.4.0-alpha.20",
44
"type": "module",
55
"main": "dist/esm/index.js",
66
"module": "dist/esm/index.js",

packages/utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aelf-web-login/utils",
3-
"version": "0.4.0-alpha.19",
3+
"version": "0.4.0-alpha.20",
44
"type": "module",
55
"main": "dist/esm/index.js",
66
"module": "dist/esm/index.js",

packages/wallets/fairy-vault-discover/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aelf-web-login/wallet-adapter-fairy-vault-discover",
3-
"version": "0.4.0-alpha.19",
3+
"version": "0.4.0-alpha.20",
44
"type": "module",
55
"main": "dist/esm/index.js",
66
"module": "dist/esm/index.js",

packages/wallets/night-elf/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aelf-web-login/wallet-adapter-night-elf",
3-
"version": "0.4.0-alpha.19",
3+
"version": "0.4.0-alpha.20",
44
"type": "module",
55
"main": "dist/esm/index.js",
66
"module": "dist/esm/index.js",

packages/wallets/night-elf/src/utils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { INightElfWalletAdapterConfig } from './index';
2-
import { makeError, utils, ERR_CODE } from '@aelf-web-login/wallet-adapter-base';
2+
import { makeError, utils, ERR_CODE, WalletTypeEnum } from '@aelf-web-login/wallet-adapter-base';
33
import type { AElfDappBridge, PublicKey } from '@aelf-react/types';
44
import AelfBridgeCheck from './AelfBridgeCheck';
55
import NightElfCheck from './NightElfCheck';
66

77
export const getBridges = async (nodes: INightElfWalletAdapterConfig['nodes'], appName: string) => {
8-
const { isMobileDevices } = utils;
8+
const { isMobileDevices, checkConnectedWallet } = utils;
99
try {
1010
const isAElfBridge = isMobileDevices() && !(window as any)?.NightElf;
1111
console.log('aelf-wallet-debug----------1');
@@ -24,7 +24,8 @@ export const getBridges = async (nodes: INightElfWalletAdapterConfig['nodes'], a
2424
const bridges: { [key: string]: AElfDappBridge } = {};
2525
console.log('nodes:', nodes);
2626
if (!nodes || Object.keys(nodes).length === 0) {
27-
throw makeError(ERR_CODE.INIT_BRIDGE_ERROR);
27+
checkConnectedWallet(WalletTypeEnum.elf);
28+
throw makeError(ERR_CODE.NIGHT_ELF_NOT_DETECTED);
2829
}
2930
Object.entries(nodes).forEach(([k, v]) => {
3031
if (!firstKey) firstKey = k;
@@ -35,7 +36,8 @@ export const getBridges = async (nodes: INightElfWalletAdapterConfig['nodes'], a
3536
const bridge = bridges[firstKey];
3637
return { bridge, node, bridges };
3738
} catch (e) {
38-
throw makeError(ERR_CODE.INIT_BRIDGE_ERROR);
39+
checkConnectedWallet(WalletTypeEnum.elf);
40+
throw makeError(ERR_CODE.NIGHT_ELF_NOT_DETECTED);
3941
}
4042
};
4143

0 commit comments

Comments
 (0)