Skip to content

Commit bbbe247

Browse files
authored
Merge pull request #315 from blocknative/enhancement/hardware-wallet-dynamic
Enhancement: Hardware Wallet Dynamic Import. Closes #313 - Fixes Rollup builds if Ledger, Trezor and Authereum wallet modules are not initialized
2 parents 80f9f79 + 094f74a commit bbbe247

File tree

4 files changed

+42
-29
lines changed

4 files changed

+42
-29
lines changed

src/modules/select/wallets/hd-wallet.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import HDKey from 'hdkey'
2-
import { publicToAddress, toChecksumAddress } from 'ethereumjs-util'
3-
import buffer from 'buffer'
2+
import * as ethUtil from 'ethereumjs-util'
3+
import * as buffer from 'buffer'
4+
5+
const { publicToAddress, toChecksumAddress } = ethUtil
46

57
const numberToGet = 5
68

src/modules/select/wallets/ledger.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,8 @@ import {
44
Helpers,
55
CommonWalletOptions
66
} from '../../../interfaces'
7-
import ledgerIcon from '../wallet-icons/icon-ledger'
8-
9-
import createProvider from './providerEngine'
10-
import { generateAddresses, isValidPath } from './hd-wallet'
11-
12-
import TransportU2F from '@ledgerhq/hw-transport-u2f'
13-
import Eth from '@ledgerhq/hw-app-eth'
14-
import * as EthereumTx from 'ethereumjs-tx'
15-
import * as ethUtil from 'ethereumjs-util'
167

17-
import buffer from 'buffer'
8+
import ledgerIcon from '../wallet-icons/icon-ledger'
189

1910
const LEDGER_LIVE_PATH = `m/44'/60'`
2011
const ACCOUNTS_TO_GET = 5
@@ -81,6 +72,15 @@ async function ledgerProvider(options: {
8172
BigNumber: any
8273
networkName: (id: number) => string
8374
}) {
75+
const { default: createProvider } = await import('./providerEngine')
76+
const { generateAddresses, isValidPath } = await import('./hd-wallet')
77+
const { default: TransportU2F } = await import('@ledgerhq/hw-transport-u2f')
78+
const { default: Eth } = await import('@ledgerhq/hw-app-eth')
79+
80+
const EthereumTx = await import('ethereumjs-tx')
81+
const ethUtil = await import('ethereumjs-util')
82+
const buffer = await import('buffer')
83+
8484
const { networkId, rpcUrl, LedgerTransport, BigNumber, networkName } = options
8585

8686
let dPath = ''

src/modules/select/wallets/trezor.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ import trezorIcon from '../wallet-icons/icon-trezor'
99
import createProvider from './providerEngine'
1010
import { generateAddresses, isValidPath } from './hd-wallet'
1111

12-
import * as TrezorConnectLibrary from 'trezor-connect'
13-
import * as EthereumTx from 'ethereumjs-tx'
14-
import * as ethUtil from 'ethereumjs-util'
15-
16-
const { default: TrezorConnect, DEVICE_EVENT, DEVICE } = TrezorConnectLibrary
17-
1812
const TREZOR_DEFAULT_PATH = "m/44'/60'/0'/0"
1913

2014
function trezor(options: TrezorOptions & CommonWalletOptions): WalletModule {
@@ -82,6 +76,12 @@ async function trezorProvider(options: {
8276
BigNumber: any
8377
networkName: (id: number) => string
8478
}) {
79+
const TrezorConnectLibrary = await import('trezor-connect')
80+
const EthereumTx = await import('ethereumjs-tx')
81+
const ethUtil = await import('ethereumjs-util')
82+
83+
const { default: TrezorConnect, DEVICE_EVENT, DEVICE } = TrezorConnectLibrary
84+
8585
const { networkId, email, appUrl, rpcUrl, BigNumber, networkName } = options
8686
let dPath: string = ''
8787

src/views/WalletSelect.svelte

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,6 @@
9898
deviceWallets.length > 4 ? deviceWallets.slice(4) : undefined
9999
}
100100
101-
modalData = {
102-
heading,
103-
description,
104-
explanation,
105-
primaryWallets,
106-
secondaryWallets
107-
}
108-
109101
if (appState.autoSelectWallet) {
110102
const module = deviceWallets.find(
111103
(m: WalletModule) => m.name === appState.autoSelectWallet
@@ -114,15 +106,23 @@
114106
app.update(store => ({ ...store, autoSelectWallet: '' }))
115107
116108
if (module) {
117-
handleWalletSelect(module)
109+
handleWalletSelect(module, true)
118110
return
119111
}
120112
}
121113
114+
modalData = {
115+
heading,
116+
description,
117+
explanation,
118+
primaryWallets,
119+
secondaryWallets
120+
}
121+
122122
app.update(store => ({ ...store, walletSelectDisplayedUI: true }))
123123
}
124124
125-
async function handleWalletSelect(module: WalletModule) {
125+
async function handleWalletSelect(module: WalletModule, autoSelected?: boolean) {
126126
const currentWalletInterface = get(walletInterface)
127127
128128
if (currentWalletInterface && currentWalletInterface.name === module.name) {
@@ -163,7 +163,18 @@
163163
selectedWallet: selectedWalletModule.name
164164
})
165165
166-
app.update(store => ({ ...store, walletSelectDisplayedUI: true }))
166+
// if it was autoSelected then we need to add modalData to show the modal
167+
if (autoSelected) {
168+
modalData = {
169+
heading,
170+
description,
171+
explanation,
172+
primaryWallets,
173+
secondaryWallets
174+
}
175+
176+
app.update(store => ({ ...store, walletSelectDisplayedUI: true }))
177+
}
167178
168179
return
169180
}

0 commit comments

Comments
 (0)