Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: walletconnect v2 #1758

Merged
merged 7 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@rabby-wallet/eth-lattice-keyring": "^1.0.5",
"@rabby-wallet/eth-simple-keyring": "^5.0.0",
"@rabby-wallet/eth-trezor-keyring": "^2.2.0",
"@rabby-wallet/eth-walletconnect-keyring": "^1.7.21",
"@rabby-wallet/eth-walletconnect-keyring": "^2.0.4-beta.0",
"@rabby-wallet/eth-watch-keyring": "^1.0.0",
"@rabby-wallet/gnosis-sdk": "^1.3.5",
"@rabby-wallet/page-provider": "^0.1.22",
Expand Down
137 changes: 60 additions & 77 deletions src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ethErrors } from 'eth-rpc-errors';
import * as bip39 from '@scure/bip39';
import { wordlist } from '@scure/bip39/wordlists/english';
import { ethers, Contract } from 'ethers';
import { groupBy, uniq } from 'lodash';
import { chain, groupBy, uniq } from 'lodash';
import abiCoder, { AbiCoder } from 'web3-eth-abi';
import * as optimismContracts from '@eth-optimism/contracts';
import {
Expand Down Expand Up @@ -61,7 +61,7 @@ import {
} from '@rabby-wallet/rabby-security-engine/dist/rules';
import DisplayKeyring from '../service/keyring/display';
import provider from './provider';
import WalletConnectKeyring from '@rabby-wallet/eth-walletconnect-keyring';
import { WalletConnectKeyring } from '@rabby-wallet/eth-walletconnect-keyring';
import eventBus from '@/eventBus';
import {
setPageStateCacheWhenPopupClose,
Expand Down Expand Up @@ -99,6 +99,7 @@ import { t } from 'i18next';
import { getWeb3Provider } from './utils';
import { CoboSafeAccount } from '@/utils/cobo-agrus-sdk/cobo-agrus-sdk';
import CoboArgusKeyring from '../service/keyring/eth-cobo-argus-keyring';
import { GET_WALLETCONNECT_CONFIG } from '@/utils/walletconnect';

const stashKeyrings: Record<string | number, any> = {};

Expand Down Expand Up @@ -1678,11 +1679,11 @@ export class WalletController extends BaseController {
return null;
};

resendWalletConnect = () => {
resendWalletConnect = (account: Account) => {
const keyringType = KEYRING_CLASS.WALLETCONNECT;
const keyring: WalletConnectKeyring = this._getKeyringByType(keyringType);
if (keyring) {
return keyring.resend();
return keyring.resend(account);
}
return null;
};
Expand Down Expand Up @@ -1725,7 +1726,16 @@ export class WalletController extends BaseController {
return null;
};

initWalletConnect = async (brandName: string, curStashId?: number | null) => {
_currentWalletConnectStashId?: undefined | null | number;

initWalletConnect = async (
brandName: string,
curStashId?: number | null,
chainId = 1
) => {
if (!curStashId && this._currentWalletConnectStashId) {
curStashId = this._currentWalletConnectStashId;
}
let keyring: WalletConnectKeyring, isNewKey;
const keyringType = KEYRING_CLASS.WALLETCONNECT;
try {
Expand All @@ -1737,28 +1747,21 @@ export class WalletController extends BaseController {
}
} catch {
const WalletConnect = keyringService.getKeyringClassForType(keyringType);
keyring = new WalletConnect({
accounts: [],
brandName: brandName,
// 1h
maxDuration: 3600000,
clientMeta: {
description: t('global.appDescription'),
url: 'https://rabby.io',
icons: ['https://rabby.io/assets/images/logo.png'],
name: 'Rabby',
},
});
keyring = new WalletConnect(GET_WALLETCONNECT_CONFIG());
isNewKey = true;
}
const { uri } = await keyring.initConnector(brandName);
keyring.initConnector(brandName, 1);
let stashId = curStashId;
if (isNewKey) {
stashId = this.addKeyringToStash(keyring);
eventBus.addEventListener(
EVENTS.WALLETCONNECT.INIT,
({ address, brandName }) => {
(keyring as WalletConnectKeyring).init(address, brandName);
({ address, brandName, chainId }) => {
(keyring as WalletConnectKeyring).init(
address,
brandName,
!chainId ? 1 : chainId
);
}
);
(keyring as WalletConnectKeyring).on('inited', (uri) => {
Expand Down Expand Up @@ -1807,46 +1810,12 @@ export class WalletController extends BaseController {
Sentry.captureException(error);
});
}
this._currentWalletConnectStashId = stashId;
return {
uri,
stashId,
};
};

getWalletConnectBridge = (address: string, brandName: string) => {
const keyringType = KEYRING_CLASS.WALLETCONNECT;
const keyring: WalletConnectKeyring = this._getKeyringByType(keyringType);
if (keyring) {
const target = keyring.accounts.find(
(account) =>
account.address.toLowerCase() === address.toLowerCase() &&
brandName === account.brandName
);

if (target) return target.bridge;

return null;
}
return null;
};

getWalletConnectConnectors = () => {
const keyringType = KEYRING_CLASS.WALLETCONNECT;
const keyring: WalletConnectKeyring = this._getKeyringByType(keyringType);
if (keyring) {
const result: { address: string; brandName: string }[] = [];
for (const key in keyring.connectors) {
const target = keyring.connectors[key];
result.push({
address: key.split('-')[1],
brandName: target.brandName,
});
}
return result;
}
return [];
};

killWalletConnectConnector = async (
address: string,
brandName: string,
Expand All @@ -1856,18 +1825,9 @@ export class WalletController extends BaseController {
const keyringType = KEYRING_CLASS.WALLETCONNECT;
const keyring: WalletConnectKeyring = this._getKeyringByType(keyringType);
if (keyring) {
const connector =
keyring.connectors[`${brandName}-${address.toLowerCase()}`];
if (connector) {
await keyring.closeConnector(
connector.connector,
address,
brandName,
silent
);
// reset onAfterConnect
if (resetConnect) keyring.onAfterConnect = null;
}
await keyring.closeConnector({ address, brandName }, silent);
// reset onAfterConnect
if (resetConnect) keyring.resetConnect();
}
};

Expand All @@ -1890,25 +1850,23 @@ export class WalletController extends BaseController {
) => {
let keyring: WalletConnectKeyring, isNewKey;
const keyringType = KEYRING_CLASS.WALLETCONNECT;
if (stashId !== null && stashId !== undefined) {
keyring = stashKeyrings[stashId];
isNewKey = true;
} else {
try {
keyring = this._getKeyringByType(keyringType);
} catch {
try {
keyring = this._getKeyringByType(keyringType);
} catch {
if (stashId !== null && stashId !== undefined) {
keyring = stashKeyrings[stashId];
} else {
const WalletConnectKeyring = keyringService.getKeyringClassForType(
keyringType
);
keyring = new WalletConnectKeyring();
isNewKey = true;
keyring = new WalletConnectKeyring(GET_WALLETCONNECT_CONFIG());
}
isNewKey = true;
}

keyring.setAccountToAdd({
address,
brandName,
bridge,
realBrandName,
realBrandUrl,
});
Expand Down Expand Up @@ -3322,6 +3280,31 @@ export class WalletController extends BaseController {
notificationService.blockedDapp();
this.rejectAllApprovals();
};

walletConnectScanAccount = async () => {
let keyring: WalletConnectKeyring, isNewKey;
const keyringType = KEYRING_CLASS.WALLETCONNECT;
try {
keyring = this._getKeyringByType(keyringType);
} catch {
const WalletConnect = keyringService.getKeyringClassForType(keyringType);
keyring = new WalletConnect(GET_WALLETCONNECT_CONFIG());
isNewKey = true;
}

if (isNewKey) {
this.addKeyringToStash(keyring);
}

keyring.on('scanAccount', (payload) => {
eventBus.emit(EVENTS.broadcastToUI, {
method: EVENTS.WALLETCONNECT.SCAN_ACCOUNT,
params: payload,
});
});

return await keyring.scanAccount();
};
}

const wallet = new WalletController();
Expand Down
18 changes: 12 additions & 6 deletions src/background/service/keyring/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import LatticeKeyring from './eth-lattice-keyring';
import WatchKeyring from '@rabby-wallet/eth-watch-keyring';
import KeystoneKeyring from './eth-keystone-keyring';
import CoboArgusKeyring from './eth-cobo-argus-keyring';
import WalletConnectKeyring, {
keyringType,
} from '@rabby-wallet/eth-walletconnect-keyring';
import { WalletConnectKeyring } from '@rabby-wallet/eth-walletconnect-keyring';
import GnosisKeyring, {
TransactionBuiltEvent,
TransactionConfirmedEvent,
Expand All @@ -38,6 +36,7 @@ import { isSameAddress } from 'background/utils';
import contactBook from '../contactBook';
import { generateAliasName } from '@/utils/account';
import * as Sentry from '@sentry/browser';
import { GET_WALLETCONNECT_CONFIG } from '@/utils/walletconnect';

export const KEYRING_SDK_TYPES = {
SimpleKeyring,
Expand Down Expand Up @@ -862,7 +861,10 @@ export class KeyringService extends EventEmitter {
async _restoreKeyring(serialized: any): Promise<any> {
const { type, data } = serialized;
const Keyring = this.getKeyringClassForType(type);
const keyring = new Keyring();
const keyring =
Keyring?.type === KEYRING_CLASS.WALLETCONNECT
? new Keyring(GET_WALLETCONNECT_CONFIG())
: new Keyring();
await keyring.deserialize(data);
if (
keyring.type === HARDWARE_KEYRING_TYPES.Ledger.type &&
Expand All @@ -873,8 +875,12 @@ export class KeyringService extends EventEmitter {
if (keyring.type === KEYRING_CLASS.WALLETCONNECT) {
eventBus.addEventListener(
EVENTS.WALLETCONNECT.INIT,
({ address, brandName }) => {
(keyring as WalletConnectKeyring).init(address, brandName);
({ address, brandName, chainId }) => {
(keyring as WalletConnectKeyring).init(
address,
brandName,
!chainId ? 1 : chainId
);
}
);
(keyring as WalletConnectKeyring).on('inited', (uri) => {
Expand Down
7 changes: 4 additions & 3 deletions src/constant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ export const EVENTS = {
INIT: 'WALLETCONNECT_INIT',
INITED: 'WALLETCONNECT_INITED',
TRANSPORT_ERROR: 'TRANSPORT_ERROR',
SCAN_ACCOUNT: 'SCAN_ACCOUNT',
},
GNOSIS: {
TX_BUILT: 'TransactionBuilt',
Expand Down Expand Up @@ -358,7 +359,7 @@ export enum WALLET_BRAND_TYPES {
WALLETCONNECT = 'WALLETCONNECT',
AIRGAP = 'AirGap',
Rainbow = 'Rainbow',
Bitkeep = 'Bitkeep',
Bitkeep = 'Bitget',
// Uniswap = 'Uniswap',
Zerion = 'Zerion',
CoboArgus = 'CoboArgus',
Expand All @@ -382,7 +383,7 @@ export type IWalletBrandContent = {
};

export const WALLET_BRAND_CONTENT: {
[K in WALLET_BRAND_TYPES]: IWalletBrandContent;
[K in string]: IWalletBrandContent;
} = {
[WALLET_BRAND_TYPES.AMBER]: {
id: 0,
Expand Down Expand Up @@ -577,7 +578,7 @@ export const WALLET_BRAND_CONTENT: {
},
[WALLET_BRAND_TYPES.Bitkeep]: {
id: 22,
name: 'Bitkeep',
name: 'Bitget Wallet',
brand: WALLET_BRAND_TYPES.Bitkeep,
icon: LogoBitkeep,
image: LogoBitkeep,
Expand Down
44 changes: 42 additions & 2 deletions src/ui/assets/walletlogo/bitkeep.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading