Skip to content

Commit

Permalink
feat: protect state
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Sep 8, 2023
1 parent 082927e commit ad1ecbf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
17 changes: 8 additions & 9 deletions packages/connectors/src/injected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ export function injected(parameters: InjectedParameters = {}) {
const canSelectAccount = getWindowProvider().features?.includes(
'wallet_requestPermissions',
)
let accounts: readonly Address[] | null = null
if (
canSelectAccount &&
const isDisconnected =
shimDisconnect &&
!config.storage?.getItem(this.shimDisconnectStorageKey)
) {

let accounts: readonly Address[] | null = null
if (canSelectAccount && isDisconnected) {
accounts = await this.getAccounts().catch(() => null)
const isConnected = !!accounts?.length
if (isConnected)
const isAuthorized = !!accounts?.length
if (isAuthorized)
// Attempt to show another prompt for selecting wallet if already connected
try {
const permissions = await provider.request({
Expand Down Expand Up @@ -248,12 +248,11 @@ export function injected(parameters: InjectedParameters = {}) {
},
async isAuthorized() {
try {
if (
const isDisconnected =
shimDisconnect &&
// If shim does not exist in storage, wallet is disconnected
!config.storage?.getItem(this.shimDisconnectStorageKey)
)
return false
if (isDisconnected) return false

const provider = await this.getProvider()
if (!provider) {
Expand Down
12 changes: 9 additions & 3 deletions packages/core/src/createConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,15 @@ export function createConfig<

getClient,
setState(value) {
// TODO: Add check to make sure state isn't completely nuked (e.g. set to non-object) and important keys exist
const newState =
typeof value === 'function' ? value(store.getState() as any) : value
let newState: State
if (typeof value === 'function') newState = value(store.getState() as any)
else newState = value

// Reset state if it got set to something not matching the base state
if (typeof newState !== 'object') newState = initialState
const isCorrupt = Object.keys(initialState).some((x) => !(x in newState))
if (isCorrupt) newState = initialState

store.setState(newState, true)
},
subscribe(selector, listener, options) {
Expand Down

1 comment on commit ad1ecbf

@vercel
Copy link

@vercel vercel bot commented on ad1ecbf Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

wagmi-v2 – ./docs

wagmi-v2-wagmi-dev.vercel.app
wagmi-v2.vercel.app
alpha.wagmi.sh
wagmi-v2-git-alpha-wagmi-dev.vercel.app

Please sign in to comment.