@@ -14,6 +14,7 @@ import { Buffer } from 'buffer'
14
14
15
15
import type Transport from '@ledgerhq/hw-transport'
16
16
import type { providers } from 'ethers'
17
+ import type Eth from '@ledgerhq/hw-app-eth'
17
18
18
19
const LEDGER_LIVE_PATH = `m/44'/60'`
19
20
const LEDGER_DEFAULT_PATH = `m/44'/60'/0'`
@@ -54,38 +55,20 @@ const getTransport = async () =>
54
55
: ( await import ( '@ledgerhq/hw-transport-u2f' ) ) . default
55
56
) . create ( )
56
57
57
- interface LedgerAccount {
58
- publicKey : string
59
- derivationPath : string
60
- chainCode : string
61
- }
62
-
63
58
const getAccount = async (
64
- { publicKey , chainCode , derivationPath } : LedgerAccount ,
59
+ derivationPath : string ,
65
60
asset : Asset ,
66
61
index : number ,
67
- provider : providers . StaticJsonRpcProvider
62
+ provider : providers . StaticJsonRpcProvider ,
63
+ eth : Eth
68
64
) : Promise < Account > => {
69
- //@ts -ignore
70
- const { default : HDKey } = await import ( 'hdkey' )
71
- const ethUtil = await import ( 'ethereumjs-util' )
72
-
73
- // @ts -ignore - Commonjs importing weirdness
74
- const { publicToAddress, toChecksumAddress } = ethUtil . default || ethUtil
75
-
76
- const hdk = new HDKey ( )
77
-
78
- hdk . publicKey = Buffer . from ( publicKey , 'hex' )
79
- hdk . chainCode = Buffer . from ( chainCode , 'hex' )
80
-
81
- const dkey = hdk . deriveChild ( index )
82
-
83
- const address = toChecksumAddress (
84
- `0x${ publicToAddress ( dkey . publicKey , true ) . toString ( 'hex' ) } `
85
- )
86
-
65
+ const dPath =
66
+ derivationPath === LEDGER_LIVE_PATH
67
+ ? `${ derivationPath } /${ index } '/0/0`
68
+ : `${ derivationPath } /${ index } `
69
+ const { address } = await eth . getAddress ( dPath )
87
70
return {
88
- derivationPath : ` ${ derivationPath } / ${ index } ` ,
71
+ derivationPath : dPath ,
89
72
address,
90
73
balance : {
91
74
asset : asset . label ,
@@ -95,9 +78,10 @@ const getAccount = async (
95
78
}
96
79
97
80
const getAddresses = async (
98
- account : LedgerAccount ,
81
+ derivationPath : string ,
99
82
asset : Asset ,
100
- provider : providers . StaticJsonRpcProvider
83
+ provider : providers . StaticJsonRpcProvider ,
84
+ eth : Eth
101
85
) : Promise < Account [ ] > => {
102
86
const accounts = [ ]
103
87
let index = 0
@@ -106,7 +90,7 @@ const getAddresses = async (
106
90
// Iterates until a 0 balance account is found
107
91
// Then adds 4 more 0 balance accounts to the array
108
92
while ( zeroBalanceAccounts < 5 ) {
109
- const acc = await getAccount ( account , asset , index , provider )
93
+ const acc = await getAccount ( derivationPath , asset , index , provider , eth )
110
94
if ( acc . balance . value . isZero ( ) ) {
111
95
zeroBalanceAccounts ++
112
96
accounts . push ( acc )
@@ -135,7 +119,6 @@ function ledger({
135
119
getInterface : async ( { EventEmitter, chains } : GetInterfaceHelpers ) => {
136
120
const Eth = ( await import ( '@ledgerhq/hw-app-eth' ) ) . default
137
121
const { default : Common , Hardfork } = await import ( '@ethereumjs/common' )
138
- const { compress } = ( await import ( 'eth-crypto' ) ) . publicKey
139
122
const ethUtil = await import ( 'ethereumjs-util' )
140
123
141
124
const { SignTypedDataVersion } = await import ( '@metamask/eth-sig-util' )
@@ -165,18 +148,13 @@ function ledger({
165
148
chains . find ( ( { id } : Chain ) => id === chainId ) || currentChain
166
149
const provider = new StaticJsonRpcProvider ( currentChain . rpcUrl )
167
150
168
- const { publicKey, chainCode, address } = await eth . getAddress (
169
- derivationPath ,
170
- false ,
171
- true // set to true to return chainCode
172
- )
173
-
174
151
// Checks to see if this is a custom derivation path
175
152
// If it is then just return the single account
176
153
if (
177
154
derivationPath !== LEDGER_LIVE_PATH &&
178
155
derivationPath !== LEDGER_DEFAULT_PATH
179
156
) {
157
+ const { address } = await eth . getAddress ( derivationPath )
180
158
return [
181
159
{
182
160
derivationPath,
@@ -189,15 +167,7 @@ function ledger({
189
167
]
190
168
}
191
169
192
- return getAddresses (
193
- {
194
- publicKey : compress ( publicKey ) ,
195
- chainCode : chainCode || '' ,
196
- derivationPath
197
- } ,
198
- asset ,
199
- provider
200
- )
170
+ return getAddresses ( derivationPath , asset , provider , eth )
201
171
} catch ( error ) {
202
172
const { statusText } = error as { statusText : string }
203
173
throw new Error (
0 commit comments