Skip to content

Commit 55dcc8f

Browse files
1.34.1-0.1.2: [fix] keystone accounts (#674)
* fix keystone bug * version bump Co-authored-by: Soralit <soralitria@gmail.com>
1 parent fe4e4a9 commit 55dcc8f

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bnc-onboard",
3-
"version": "1.34.1-0.1.1",
3+
"version": "1.34.1-0.1.2",
44
"description": "Onboard users to web3 by allowing them to select a wallet, get that wallet ready to transact and have access to synced wallet state.",
55
"keywords": [
66
"ethereum",

src/modules/select/wallets/keystone.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ async function keystoneProvider(options: {
9393

9494
let dPath = ''
9595

96-
let addressList = Array.from<string>([])
96+
let addressToIndex = new Map<string, number>()
9797
let enabled = false
9898
let customPath = false
9999

@@ -152,7 +152,7 @@ async function keystoneProvider(options: {
152152
function disconnect() {
153153
dPath = ''
154154
enabled = false
155-
addressList = []
155+
addressToIndex = new Map<string, number>()
156156
provider.stop()
157157
}
158158

@@ -178,12 +178,32 @@ async function keystoneProvider(options: {
178178
})
179179
}
180180

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+
181193
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)
187207
}
188208

189209
function getPrimaryAddress() {
@@ -200,18 +220,21 @@ async function keystoneProvider(options: {
200220
return []
201221
}
202222

203-
if (addressList.length > 0 && !getMore) {
204-
return addressList
223+
if (addressToIndex.size > 0 && !getMore) {
224+
return addresses()
205225
}
206226

207227
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)
209232
const currentPrimary = getPrimaryAddress()
210233
setPrimaryAccount(currentPrimary)
211234
} catch (error) {
212235
throw error
213236
}
214-
return addressList
237+
return addresses()
215238
}
216239

217240
function getBalances(addresses: Array<string>) {
@@ -250,7 +273,7 @@ async function keystoneProvider(options: {
250273
}
251274

252275
async function signTransaction(transactionData: any) {
253-
if (addressList.length === 0) {
276+
if (addressToIndex.size === 0) {
254277
await enable()
255278
}
256279

@@ -278,7 +301,7 @@ async function keystoneProvider(options: {
278301
}
279302

280303
async function signMessage(message: { data: string }): Promise<string> {
281-
if (addressList.length === 0) {
304+
if (addressToIndex.size === 0) {
282305
await enable()
283306
}
284307

@@ -290,7 +313,7 @@ async function keystoneProvider(options: {
290313
}
291314

292315
async function signTypedMessage({ data }: { data: any }) {
293-
if (addressList.length === 0) {
316+
if (addressToIndex.size === 0) {
294317
await enable()
295318
}
296319

0 commit comments

Comments
 (0)