@@ -93,7 +93,7 @@ async function keystoneProvider(options: {
93
93
94
94
let dPath = ''
95
95
96
- let addressList = Array . from < string > ( [ ] )
96
+ let addressToIndex = new Map < string , number > ( )
97
97
let enabled = false
98
98
let customPath = false
99
99
@@ -152,7 +152,7 @@ async function keystoneProvider(options: {
152
152
function disconnect ( ) {
153
153
dPath = ''
154
154
enabled = false
155
- addressList = [ ]
155
+ addressToIndex = new Map < string , number > ( )
156
156
provider . stop ( )
157
157
}
158
158
@@ -178,12 +178,32 @@ async function keystoneProvider(options: {
178
178
} )
179
179
}
180
180
181
+ function addresses ( ) {
182
+ return Array . from ( addressToIndex . keys ( ) )
183
+ }
184
+
185
+ function generateAccountsMap ( accounts : string [ ] ) {
186
+ const _map = new Map < string , number > ( )
187
+ accounts . forEach ( ( account , index ) => {
188
+ _map . set ( account , index )
189
+ } )
190
+ return _map
191
+ }
192
+
181
193
function setPrimaryAccount ( address : string ) {
182
- const index = addressList . findIndex ( addr => addr === address ) || 0
183
- keyring . setCurrentAccount ( index )
184
- const accounts = [ ...addressList ]
185
- accounts . unshift ( accounts . splice ( index , 1 ) [ 0 ] )
186
- addressList = accounts
194
+ // make a copy and put in an array
195
+ const accounts = [ ...addressToIndex . entries ( ) ]
196
+ const account = accounts . find (
197
+ ( [ accountAddress ] ) => accountAddress === address
198
+ ) !
199
+ const accountIndex = accounts . findIndex (
200
+ ( [ accountAddress ] ) => accountAddress === address
201
+ )
202
+ keyring . setCurrentAccount ( account ?. [ 1 ] || 0 )
203
+ // pull the item at the account index out of the array and place at the front
204
+ accounts . unshift ( accounts . splice ( accountIndex , 1 ) [ 0 ] )
205
+ // reassign addressToPath to new ordered accounts
206
+ addressToIndex = new Map ( accounts )
187
207
}
188
208
189
209
function getPrimaryAddress ( ) {
@@ -200,18 +220,21 @@ async function keystoneProvider(options: {
200
220
return [ ]
201
221
}
202
222
203
- if ( addressList . length > 0 && ! getMore ) {
204
- return addressList
223
+ if ( addressToIndex . size > 0 && ! getMore ) {
224
+ return addresses ( )
205
225
}
206
226
207
227
try {
208
- addressList = await keyring . addAccounts ( keyring . getAccounts ( ) . length + 5 )
228
+ const accounts = await keyring . addAccounts (
229
+ keyring . getAccounts ( ) . length + 5
230
+ )
231
+ addressToIndex = generateAccountsMap ( accounts )
209
232
const currentPrimary = getPrimaryAddress ( )
210
233
setPrimaryAccount ( currentPrimary )
211
234
} catch ( error ) {
212
235
throw error
213
236
}
214
- return addressList
237
+ return addresses ( )
215
238
}
216
239
217
240
function getBalances ( addresses : Array < string > ) {
@@ -250,7 +273,7 @@ async function keystoneProvider(options: {
250
273
}
251
274
252
275
async function signTransaction ( transactionData : any ) {
253
- if ( addressList . length === 0 ) {
276
+ if ( addressToIndex . size === 0 ) {
254
277
await enable ( )
255
278
}
256
279
@@ -278,7 +301,7 @@ async function keystoneProvider(options: {
278
301
}
279
302
280
303
async function signMessage ( message : { data : string } ) : Promise < string > {
281
- if ( addressList . length === 0 ) {
304
+ if ( addressToIndex . size === 0 ) {
282
305
await enable ( )
283
306
}
284
307
@@ -290,7 +313,7 @@ async function keystoneProvider(options: {
290
313
}
291
314
292
315
async function signTypedMessage ( { data } : { data : any } ) {
293
- if ( addressList . length === 0 ) {
316
+ if ( addressToIndex . size === 0 ) {
294
317
await enable ( )
295
318
}
296
319
0 commit comments