@@ -56,8 +56,8 @@ function trezor(options: TrezorOptions & CommonWalletOptions): WalletModule {
56
56
} ,
57
57
balance : {
58
58
get : async ( ) => {
59
- const address = provider . getPrimaryAddress ( )
60
- return address && provider . getBalance ( address )
59
+ const accounts = provider . getPrimaryAddress ( )
60
+ return accounts [ 0 ] && provider . getBalance ( accounts [ 0 ] )
61
61
}
62
62
}
63
63
}
@@ -84,6 +84,10 @@ async function trezorProvider(options: {
84
84
let addressToPath = new Map ( )
85
85
let enabled : boolean = false
86
86
87
+ let account :
88
+ | undefined
89
+ | { publicKey : string ; chainCode : string ; path : string }
90
+
87
91
TrezorConnect . manifest ( {
88
92
email,
89
93
appUrl
@@ -110,17 +114,18 @@ async function trezorProvider(options: {
110
114
rpcUrl
111
115
} )
112
116
113
- provider . getPrimaryAddress = getPrimaryAddress
114
- provider . getAllAccountsAndBalances = getAllAccountsAndBalances
115
117
provider . setPath = setPath
116
118
provider . dPath = dPath
117
119
provider . enable = enable
118
120
provider . setPrimaryAccount = setPrimaryAccount
121
+ provider . getPrimaryAddress = getPrimaryAddress
122
+ provider . getAccounts = getAccounts
123
+ provider . getMoreAccounts = getMoreAccounts
119
124
provider . getBalance = getBalance
125
+ provider . getBalances = getBalances
120
126
provider . send = provider . sendAsync
121
127
122
128
function setPath ( path : string ) {
123
- console . log ( { path } )
124
129
dPath = path
125
130
}
126
131
@@ -133,23 +138,6 @@ async function trezorProvider(options: {
133
138
return Array . from ( addressToPath . keys ( ) )
134
139
}
135
140
136
- function getPrimaryAddress ( ) {
137
- return enabled ? addresses ( ) [ 0 ] : undefined
138
- }
139
-
140
- async function getAllAccountsAndBalances ( ) {
141
- const accounts = await getAccounts ( )
142
- return Promise . all (
143
- accounts . map (
144
- address =>
145
- new Promise ( async resolve => {
146
- const balance = await getBalance ( address )
147
- resolve ( { address, balance } )
148
- } )
149
- )
150
- )
151
- }
152
-
153
141
function setPrimaryAccount ( address : string ) {
154
142
// make a copy and put in an array
155
143
const accounts = [ ...addressToPath . entries ( ) ]
@@ -162,25 +150,44 @@ async function trezorProvider(options: {
162
150
addressToPath = new Map ( accounts )
163
151
}
164
152
165
- async function getAccounts ( ) {
153
+ async function getPublicKey ( ) {
154
+ const result = await TrezorConnect . getPublicKey ( {
155
+ path : dPath ,
156
+ coin : 'eth'
157
+ } )
158
+
159
+ account = {
160
+ publicKey : result . payload . publicKey ,
161
+ chainCode : result . payload . chainCode ,
162
+ path : result . payload . serializedPath
163
+ }
164
+
165
+ return account
166
+ }
167
+
168
+ function getPrimaryAddress ( ) {
169
+ return enabled ? addresses ( ) [ 0 ] : undefined
170
+ }
171
+
172
+ async function getMoreAccounts ( ) {
173
+ const accounts = await getAccounts ( true )
174
+ return getBalances ( accounts )
175
+ }
176
+
177
+ async function getAccounts ( getMore ?: boolean ) {
166
178
if ( ! enabled ) {
167
179
return [ undefined ]
168
180
}
169
181
170
- if ( addressToPath . size > 0 ) {
182
+ if ( addressToPath . size > 0 && ! getMore ) {
171
183
return addresses ( )
172
184
}
173
185
174
- const trezorAccount = await TrezorConnect . getPublicKey ( {
175
- path : dPath ,
176
- coin : 'eth'
177
- } )
186
+ const accountInfo = account || ( await getPublicKey ( ) )
178
187
179
- const addressInfo = generateAddresses (
180
- trezorAccount . payload . publicKey ,
181
- trezorAccount . payload . chainCode ,
182
- trezorAccount . payload . serializedPath
183
- )
188
+ const addressInfo = generateAddresses ( accountInfo , addressToPath . size )
189
+
190
+ console . log ( { addressInfo } )
184
191
185
192
addressInfo . forEach ( ( { dPath, address } ) => {
186
193
addressToPath . set ( address , dPath )
@@ -189,6 +196,18 @@ async function trezorProvider(options: {
189
196
return addresses ( )
190
197
}
191
198
199
+ function getBalances ( addresses : Array < string > ) {
200
+ return Promise . all (
201
+ addresses . map (
202
+ address =>
203
+ new Promise ( async resolve => {
204
+ const balance = await getBalance ( address )
205
+ resolve ( { address, balance } )
206
+ } )
207
+ )
208
+ )
209
+ }
210
+
192
211
function getBalance ( address : string ) {
193
212
return new Promise ( ( resolve , reject ) => {
194
213
provider . sendAsync (
@@ -253,11 +272,4 @@ async function trezorProvider(options: {
253
272
return provider
254
273
}
255
274
256
- function networkIdToDerivationPath ( networkId : number ) {
257
- switch ( networkId ) {
258
- default :
259
- return `m/44'/60'`
260
- }
261
- }
262
-
263
275
export default trezor
0 commit comments