|
| 1 | +import { LatticeOptions, WalletModule, Helpers } from '../../../interfaces' |
| 2 | +import latticeIcon from '../wallet-icons/icon-lattice' |
| 3 | + |
| 4 | +function lattice( |
| 5 | + options: LatticeOptions & { networkId: number } |
| 6 | +): WalletModule { |
| 7 | + const { appName, rpcUrl, networkId, preferred, label, iconSrc, svg } = options |
| 8 | + |
| 9 | + return { |
| 10 | + name: label || 'Lattice', |
| 11 | + svg: svg || latticeIcon, |
| 12 | + iconSrc, |
| 13 | + wallet: async (helpers: Helpers) => { |
| 14 | + const { BigNumber, networkName, resetWalletState } = helpers |
| 15 | + |
| 16 | + const provider = await latticeProvider({ |
| 17 | + appName, |
| 18 | + rpcUrl, |
| 19 | + networkId, |
| 20 | + BigNumber, |
| 21 | + networkName, |
| 22 | + resetWalletState |
| 23 | + }) |
| 24 | + |
| 25 | + return { |
| 26 | + provider, |
| 27 | + interface: { |
| 28 | + name: 'Lattice', |
| 29 | + connect: provider.enable, |
| 30 | + disconnect: provider.disconnect, |
| 31 | + address: { |
| 32 | + get: async () => provider.getPrimaryAddress() |
| 33 | + }, |
| 34 | + network: { |
| 35 | + get: async () => networkId |
| 36 | + }, |
| 37 | + balance: { |
| 38 | + get: async () => { |
| 39 | + const address = provider.getPrimaryAddress() |
| 40 | + return address && provider.getBalance(address) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + }, |
| 46 | + type: 'hardware', |
| 47 | + desktop: true, |
| 48 | + mobile: true, |
| 49 | + osExclusions: ['iOS'], |
| 50 | + preferred |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +async function latticeProvider(options: { |
| 55 | + appName: string |
| 56 | + networkId: number |
| 57 | + rpcUrl: string |
| 58 | + BigNumber: any |
| 59 | + networkName: (id: number) => string |
| 60 | + resetWalletState: (options?: { |
| 61 | + disconnected: boolean |
| 62 | + walletName: string |
| 63 | + }) => void |
| 64 | +}) { |
| 65 | + const { default: EthLatticeKeyring } = await import('eth-lattice-keyring') |
| 66 | + const EthereumTx = await import('ethereumjs-tx') |
| 67 | + const { default: createProvider } = await import('./providerEngine') |
| 68 | + |
| 69 | + const BASE_PATH = "m/44'/60'/0'/0" |
| 70 | + |
| 71 | + const { networkId, appName, rpcUrl, BigNumber, networkName } = options |
| 72 | + |
| 73 | + const params = { |
| 74 | + name: appName, |
| 75 | + network: networkName(networkId) |
| 76 | + } |
| 77 | + const Lattice = new EthLatticeKeyring(params) |
| 78 | + |
| 79 | + let dPath = '' |
| 80 | + |
| 81 | + let addressList = Array.from([]) |
| 82 | + let enabled = false |
| 83 | + let customPath = false |
| 84 | + |
| 85 | + const provider = createProvider({ |
| 86 | + getAccounts: (callback: any) => { |
| 87 | + getAccounts() |
| 88 | + .then((res: Array<string | undefined>) => callback(null, res)) |
| 89 | + .catch((err: any) => callback(err, null)) |
| 90 | + }, |
| 91 | + signTransaction: (transactionData: any, callback: any) => { |
| 92 | + signTransaction(transactionData) |
| 93 | + .then((res: string) => callback(null, res)) |
| 94 | + .catch(err => callback(err, null)) |
| 95 | + }, |
| 96 | + processMessage: (messageData: any, callback: any) => { |
| 97 | + signMessage(messageData) |
| 98 | + .then((res: string) => callback(null, res)) |
| 99 | + .catch(err => callback(err, null)) |
| 100 | + }, |
| 101 | + processPersonalMessage: (messageData: any, callback: any) => { |
| 102 | + signMessage(messageData) |
| 103 | + .then((res: string) => callback(null, res)) |
| 104 | + .catch(err => callback(err, null)) |
| 105 | + }, |
| 106 | + signMessage: (messageData: any, callback: any) => { |
| 107 | + signMessage(messageData) |
| 108 | + .then((res: string) => callback(null, res)) |
| 109 | + .catch(err => callback(err, null)) |
| 110 | + }, |
| 111 | + signPersonalMessage: (messageData: any, callback: any) => { |
| 112 | + signMessage(messageData) |
| 113 | + .then((res: string) => callback(null, res)) |
| 114 | + .catch(err => callback(err, null)) |
| 115 | + }, |
| 116 | + rpcUrl |
| 117 | + }) |
| 118 | + |
| 119 | + provider.setPath = setPath |
| 120 | + provider.dPath = dPath |
| 121 | + provider.enable = enable |
| 122 | + provider.setPrimaryAccount = setPrimaryAccount |
| 123 | + provider.getPrimaryAddress = getPrimaryAddress |
| 124 | + provider.getAccounts = getAccounts |
| 125 | + provider.getMoreAccounts = getMoreAccounts |
| 126 | + provider.getBalance = getBalance |
| 127 | + provider.getBalances = getBalances |
| 128 | + provider.send = provider.sendAsync |
| 129 | + provider.disconnect = disconnect |
| 130 | + provider.isCustomPath = isCustomPath |
| 131 | + |
| 132 | + function disconnect() { |
| 133 | + dPath = '' |
| 134 | + enabled = false |
| 135 | + provider.stop() |
| 136 | + } |
| 137 | + |
| 138 | + async function setPath(path: string) { |
| 139 | + if (path !== BASE_PATH) |
| 140 | + throw new Error("Lattice only supports standard path: m/44'/0'/0'/0/x") |
| 141 | + customPath = false |
| 142 | + dPath = path |
| 143 | + return true |
| 144 | + } |
| 145 | + |
| 146 | + function isCustomPath() { |
| 147 | + return customPath |
| 148 | + } |
| 149 | + |
| 150 | + function enable() { |
| 151 | + enabled = true |
| 152 | + return getAccounts() |
| 153 | + } |
| 154 | + |
| 155 | + function addresses() { |
| 156 | + return addressList |
| 157 | + } |
| 158 | + |
| 159 | + function setPrimaryAccount(address: string) { |
| 160 | + return |
| 161 | + } |
| 162 | + |
| 163 | + function getPrimaryAddress() { |
| 164 | + return enabled ? addresses()[0] : undefined |
| 165 | + } |
| 166 | + |
| 167 | + async function getMoreAccounts() { |
| 168 | + const m = `Lattice only supports one exported account per wallet. Checking for new wallet.` |
| 169 | + console.warn(m) |
| 170 | + const accounts = await getAccounts(true) |
| 171 | + return accounts && getBalances(accounts) |
| 172 | + } |
| 173 | + |
| 174 | + async function getAccounts(getMore?: boolean) { |
| 175 | + if (!enabled) { |
| 176 | + return [] |
| 177 | + } |
| 178 | + |
| 179 | + if (addressList.length > 0 && !getMore) { |
| 180 | + return addressList |
| 181 | + } |
| 182 | + |
| 183 | + try { |
| 184 | + addressList = await Lattice.addAccounts() |
| 185 | + } catch (error) { |
| 186 | + throw error |
| 187 | + } |
| 188 | + |
| 189 | + return addressList |
| 190 | + } |
| 191 | + |
| 192 | + function getBalances(addresses: Array<string>) { |
| 193 | + return Promise.all( |
| 194 | + addresses.map( |
| 195 | + address => |
| 196 | + new Promise(async resolve => { |
| 197 | + const balance = await getBalance(address) |
| 198 | + resolve({ address, balance }) |
| 199 | + }) |
| 200 | + ) |
| 201 | + ) |
| 202 | + } |
| 203 | + |
| 204 | + function getBalance(address: string) { |
| 205 | + return new Promise((resolve, reject) => { |
| 206 | + provider.sendAsync( |
| 207 | + { |
| 208 | + jsonrpc: '2.0', |
| 209 | + method: 'eth_getBalance', |
| 210 | + params: [address, 'latest'], |
| 211 | + id: 42 |
| 212 | + }, |
| 213 | + (e: any, res: any) => { |
| 214 | + e && reject(e) |
| 215 | + const result = res && res.result |
| 216 | + |
| 217 | + if (result != null) { |
| 218 | + resolve(new BigNumber(result).toString(10)) |
| 219 | + } else { |
| 220 | + resolve(null) |
| 221 | + } |
| 222 | + } |
| 223 | + ) |
| 224 | + }) |
| 225 | + } |
| 226 | + |
| 227 | + async function signTransaction(transactionData: any) { |
| 228 | + if (addressList.length === 0) { |
| 229 | + await enable() |
| 230 | + } |
| 231 | + |
| 232 | + const transaction = new EthereumTx.Transaction(transactionData, { |
| 233 | + chain: networkName(networkId) |
| 234 | + }) |
| 235 | + |
| 236 | + try { |
| 237 | + const signedTx = await Lattice.signTransaction( |
| 238 | + addressList[0], |
| 239 | + transaction |
| 240 | + ) |
| 241 | + return `0x${signedTx.serialize().toString('hex')}` |
| 242 | + } catch (err) { |
| 243 | + throw err |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + async function signMessage(message: { data: string }): Promise<string> { |
| 248 | + if (addressList.length === 0) { |
| 249 | + await enable() |
| 250 | + } |
| 251 | + |
| 252 | + try { |
| 253 | + const sig = await Lattice.signPersonalMessage( |
| 254 | + addressList[0], |
| 255 | + message.data |
| 256 | + ) |
| 257 | + return sig |
| 258 | + } catch (err) { |
| 259 | + throw err |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + return provider |
| 264 | +} |
| 265 | + |
| 266 | +export default lattice |
0 commit comments