|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import { WalletKitTypes } from '@reown/walletkit'; |
| 3 | + |
| 4 | +import { createThunk } from '@suite-common/redux-utils'; |
| 5 | +import { getNetwork } from '@suite-common/wallet-config'; |
| 6 | +import { selectAccounts, selectSelectedDevice } from '@suite-common/wallet-core'; |
| 7 | +import * as trezorConnectPopupActions from '@trezor/suite-desktop-connect-popup'; |
| 8 | +import TrezorConnect from '@trezor/connect'; |
| 9 | + |
| 10 | +import { WALLETCONNECT_MODULE } from '../walletConnectConstants'; |
| 11 | +import { WalletConnectAdapter } from '../walletConnectTypes'; |
| 12 | + |
| 13 | +const ethereumRequestThunk = createThunk< |
| 14 | + void, |
| 15 | + { |
| 16 | + event: WalletKitTypes.SessionRequest; |
| 17 | + } |
| 18 | +>(`${WALLETCONNECT_MODULE}/ethereumRequest`, async ({ event }, { dispatch, getState }) => { |
| 19 | + const device = selectSelectedDevice(getState()); |
| 20 | + const getAccount = (address: string, chainId?: number) => { |
| 21 | + const account = selectAccounts(getState()).find( |
| 22 | + a => |
| 23 | + a.descriptor.toLowerCase() === address.toLowerCase() && |
| 24 | + a.networkType === 'ethereum' && |
| 25 | + (!chainId || getNetwork(a.symbol).chainId === chainId), |
| 26 | + ); |
| 27 | + if (!account) { |
| 28 | + throw new Error('Account not found'); |
| 29 | + } |
| 30 | + |
| 31 | + return account; |
| 32 | + }; |
| 33 | + |
| 34 | + switch (event.params.request.method) { |
| 35 | + case 'personal_sign': { |
| 36 | + const [message, address] = event.params.request.params; |
| 37 | + const account = getAccount(address); |
| 38 | + const response = await dispatch( |
| 39 | + trezorConnectPopupActions.connectPopupCallThunk({ |
| 40 | + id: 0, |
| 41 | + method: 'ethereumSignMessage', |
| 42 | + payload: { |
| 43 | + path: account.path, |
| 44 | + message, |
| 45 | + hex: true, |
| 46 | + device, |
| 47 | + useEmptyPassphrase: device?.useEmptyPassphrase, |
| 48 | + }, |
| 49 | + processName: 'WalletConnect', |
| 50 | + origin: event.verifyContext.verified.origin, |
| 51 | + }), |
| 52 | + ).unwrap(); |
| 53 | + if (!response.success) { |
| 54 | + console.error('personal_sign error', response); |
| 55 | + throw new Error('personal_sign error'); |
| 56 | + } |
| 57 | + |
| 58 | + return response.payload.signature; |
| 59 | + } |
| 60 | + case 'eth_signTypedData_v4': { |
| 61 | + const [address, data] = event.params.request.params; |
| 62 | + const account = getAccount(address); |
| 63 | + const response = await dispatch( |
| 64 | + trezorConnectPopupActions.connectPopupCallThunk({ |
| 65 | + id: 0, |
| 66 | + method: 'ethereumSignTypedData', |
| 67 | + payload: { |
| 68 | + path: account.path, |
| 69 | + data: JSON.parse(data), |
| 70 | + metamask_v4_compat: true, |
| 71 | + device, |
| 72 | + useEmptyPassphrase: device?.useEmptyPassphrase, |
| 73 | + }, |
| 74 | + processName: 'WalletConnect', |
| 75 | + origin: event.verifyContext.verified.origin, |
| 76 | + }), |
| 77 | + ).unwrap(); |
| 78 | + if (!response.success) { |
| 79 | + console.error('eth_signTypedData_v4 error', response); |
| 80 | + throw new Error('eth_signTypedData_v4 error'); |
| 81 | + } |
| 82 | + |
| 83 | + return response.payload.signature; |
| 84 | + } |
| 85 | + case 'eth_sendTransaction': { |
| 86 | + const [transaction] = event.params.request.params; |
| 87 | + const chainId = Number(event.params.chainId.replace('eip155:', '')); |
| 88 | + const account = getAccount(transaction.from, chainId); |
| 89 | + if (account.networkType !== 'ethereum') { |
| 90 | + throw new Error('Account is not Ethereum'); |
| 91 | + } |
| 92 | + if (!transaction.gasPrice) { |
| 93 | + throw new Error('Gas price is not set'); |
| 94 | + } |
| 95 | + const signResponse = await dispatch( |
| 96 | + trezorConnectPopupActions.connectPopupCallThunk({ |
| 97 | + id: 0, |
| 98 | + method: 'ethereumSignTransaction', |
| 99 | + payload: { |
| 100 | + path: account.path, |
| 101 | + transaction: { |
| 102 | + ...transaction, |
| 103 | + gasLimit: transaction.gas, |
| 104 | + nonce: account.misc.nonce, |
| 105 | + chainId, |
| 106 | + push: true, |
| 107 | + }, |
| 108 | + device, |
| 109 | + useEmptyPassphrase: device?.useEmptyPassphrase, |
| 110 | + }, |
| 111 | + processName: 'WalletConnect', |
| 112 | + origin: event.verifyContext.verified.origin, |
| 113 | + }), |
| 114 | + ).unwrap(); |
| 115 | + if (!signResponse.success) { |
| 116 | + console.error('eth_sendTransaction error', signResponse); |
| 117 | + throw new Error('eth_sendTransaction error'); |
| 118 | + } |
| 119 | + |
| 120 | + console.log('pushTransaction', { |
| 121 | + tx: signResponse.payload.serializedTx, |
| 122 | + coin: account.symbol, |
| 123 | + }); |
| 124 | + const pushResponse = await TrezorConnect.pushTransaction({ |
| 125 | + tx: signResponse.payload.serializedTx, |
| 126 | + coin: account.symbol, |
| 127 | + }); |
| 128 | + if (!pushResponse.success) { |
| 129 | + console.error('eth_sendTransaction push error', pushResponse); |
| 130 | + throw new Error('eth_sendTransaction push error'); |
| 131 | + } |
| 132 | + |
| 133 | + return pushResponse.payload.txid; |
| 134 | + } |
| 135 | + case 'wallet_switchEthereumChain': { |
| 136 | + const [chainId] = event.params.request.params; |
| 137 | + |
| 138 | + return chainId; |
| 139 | + } |
| 140 | + } |
| 141 | +}); |
| 142 | + |
| 143 | +export const ethereumAdapter = { |
| 144 | + methods: [ |
| 145 | + 'eth_sendTransaction', |
| 146 | + 'eth_signTypedData_v4', |
| 147 | + 'personal_sign', |
| 148 | + 'wallet_switchEthereumChain', |
| 149 | + ], |
| 150 | + networkType: 'ethereum', |
| 151 | + requestThunk: ethereumRequestThunk, |
| 152 | +} satisfies WalletConnectAdapter; |
0 commit comments