Skip to content

Commit f145192

Browse files
authored
[FIX] : CORE - Handling of empty localStorage item, remove default of empty string and perform a falsey check (#2236)
* Fix handling of empty localStorage item, remove default of empty string and perform a falsey check * Revert changes to the demo app * Add additional handling for single wallet connect from older W3O version, add try to disconnect logic * Handle for connect({}) function with empty object passed
1 parent eb205fb commit f145192

File tree

9 files changed

+39
-24
lines changed

9 files changed

+39
-24
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/core",
3-
"version": "2.22.2-alpha.1",
3+
"version": "2.22.2-alpha.2",
44
"description": "Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardized spec compliant web3 providers for all supported wallets, framework agnostic modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
55
"keywords": [
66
"Ethereum",

packages/core/src/connect.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ async function connect(
3131
'At least one chain must be set before attempting to connect a wallet'
3232
)
3333

34-
const { autoSelect } = options || {
35-
autoSelect: { label: '', disableModals: false }
34+
let { autoSelect } = options || {}
35+
if (!autoSelect) {
36+
autoSelect = { label: '', disableModals: false }
3637
}
3738

3839
// if auto selecting, wait until next event loop
@@ -44,7 +45,6 @@ async function connect(
4445
if (!state.get().walletModules.length) {
4546
setWalletModules(configuration.initialWalletInit)
4647
}
47-
4848
connectWallet$.next({
4949
autoSelect:
5050
typeof autoSelect === 'string'

packages/core/src/disconnect.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,25 @@ async function disconnect(options: DisconnectOptions): Promise<WalletState[]> {
4444
disconnectWallet$.next(label)
4545
removeWallet(label)
4646

47-
const labels = JSON.parse(
48-
getLocalStore(STORAGE_KEYS.LAST_CONNECTED_WALLET) || ''
49-
)
47+
const lastConnectedWallets = getLocalStore(STORAGE_KEYS.LAST_CONNECTED_WALLET)
48+
if (lastConnectedWallets) {
49+
try {
50+
const labels = JSON.parse(lastConnectedWallets)
5051

51-
if (Array.isArray(labels) && labels.indexOf(label) >= 0) {
52-
setLocalStore(
53-
STORAGE_KEYS.LAST_CONNECTED_WALLET,
54-
JSON.stringify(labels.filter(walletLabel => walletLabel !== label))
55-
)
56-
}
57-
if (typeof labels === 'string' && labels === label) {
58-
delLocalStore(STORAGE_KEYS.LAST_CONNECTED_WALLET)
52+
if (Array.isArray(labels) && labels.indexOf(label) >= 0) {
53+
setLocalStore(
54+
STORAGE_KEYS.LAST_CONNECTED_WALLET,
55+
JSON.stringify(labels.filter(walletLabel => walletLabel !== label))
56+
)
57+
}
58+
if (typeof labels === 'string' && labels === label) {
59+
delLocalStore(STORAGE_KEYS.LAST_CONNECTED_WALLET)
60+
}
61+
} catch (e) {
62+
console.error(
63+
`There was an error disconnecting the last connected wallet from localStorage - Error: ${e}`
64+
)
65+
}
5966
}
6067

6168
return state.get().wallets

packages/core/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ function init(options: InitOptions): OnboardAPI {
290290
) {
291291
connectAllPreviousWallets(lastConnectedWalletsParsed, connect)
292292
}
293+
if (
294+
lastConnectedWalletsParsed &&
295+
typeof lastConnectedWalletsParsed === 'string'
296+
) {
297+
connectAllPreviousWallets([lastConnectedWalletsParsed], connect)
298+
}
293299
} catch (err) {
294300
// Handle for legacy single wallet approach
295301
// Above try will throw syntax error is local storage is not json
@@ -361,6 +367,7 @@ const connectAllPreviousWallets = async (
361367
}
362368
}
363369
}
370+
364371
setLocalStore(
365372
STORAGE_KEYS.LAST_CONNECTED_WALLET,
366373
JSON.stringify(activeWalletsList)

packages/demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"@web3-onboard/blocto": "^2.1.1",
3434
"@web3-onboard/capsule": "2.2.0-alpha.1",
3535
"@web3-onboard/cede-store": "^2.3.1",
36-
"@web3-onboard/core": "2.22.2-alpha.1",
36+
"@web3-onboard/core": "2.22.2-alpha.2",
3737
"@web3-onboard/coinbase": "^2.4.1",
3838
"@web3-onboard/dcent": "^2.2.7",
3939
"@web3-onboard/enkrypt": "^2.1.1",

packages/demo/src/App.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@
282282
const onboard = Onboard({
283283
wallets: [
284284
metamaskSDKWallet,
285-
injected,
286285
coinbaseWallet,
286+
injected,
287287
ledger,
288288
trezor,
289289
walletConnect,
@@ -415,7 +415,8 @@
415415
connect: {
416416
// disableClose: true,
417417
// removeWhereIsMyWalletWarning: true,
418-
autoConnectAllPreviousWallet: true
418+
// autoConnectLastWallet: false,
419+
autoConnectAllPreviousWallet: true,
419420
},
420421
appMetadata: {
421422
name: 'Blocknative',

packages/react/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/react",
3-
"version": "2.9.2-alpha.1",
3+
"version": "2.9.2-alpha.2",
44
"description": "A collection of React hooks for integrating Web3-Onboard in to React and Next.js projects. Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardised spec compliant web3 providers for all supported wallets, modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
55
"keywords": [
66
"Ethereum",
@@ -61,7 +61,7 @@
6161
"typescript": "^5.4.5"
6262
},
6363
"dependencies": {
64-
"@web3-onboard/core": "2.22.2-alpha.1",
64+
"@web3-onboard/core": "2.22.2-alpha.2",
6565
"@web3-onboard/common": "^2.4.1",
6666
"use-sync-external-store": "1.0.0"
6767
},

packages/solid/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/solid",
3-
"version": "2.1.2-alpha.1",
3+
"version": "2.1.2-alpha.2",
44
"description": "A collection of solid Composables for integrating Web3-Onboard in to a Solid project. Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardized spec compliant web3 providers for all supported wallets, modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
55
"keywords": [
66
"Ethereum",
@@ -63,7 +63,7 @@
6363
},
6464
"dependencies": {
6565
"@web3-onboard/common": "^2.4.1",
66-
"@web3-onboard/core": "2.22.2-alpha.1",
66+
"@web3-onboard/core": "2.22.2-alpha.2",
6767
"solid-js": "^1.8.1"
6868
}
6969
}

packages/vue/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/vue",
3-
"version": "2.8.2-alpha.1",
3+
"version": "2.8.2-alpha.2",
44
"description": "A collection of Vue Composables for integrating Web3-Onboard in to a Vue or Nuxt project. Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardized spec compliant web3 providers for all supported wallets, modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
55
"keywords": [
66
"Ethereum",
@@ -62,7 +62,7 @@
6262
"@vueuse/core": "^8.4.2",
6363
"@vueuse/rxjs": "^8.2.0",
6464
"@web3-onboard/common": "^2.4.1",
65-
"@web3-onboard/core": "2.22.2-alpha.1",
65+
"@web3-onboard/core": "2.22.2-alpha.2",
6666
"vue-demi": "^0.12.4"
6767
},
6868
"peerDependencies": {

0 commit comments

Comments
 (0)