Skip to content

Commit

Permalink
Merge pull request #313 from omnisat/fix/xverse-connect-popup
Browse files Browse the repository at this point in the history
fix: xverse connection popup on reload
  • Loading branch information
ufe-pr authored Feb 5, 2025
2 parents 8247d3a + 122a284 commit 09d0466
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
32 changes: 11 additions & 21 deletions packages/lasereyes-core/src/client/providers/magic-eden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ import {
import { listenKeys, MapStore } from 'nanostores'
import { persistentMap } from '@nanostores/persistent'
import { fromOutputScript } from 'bitcoinjs-lib/src/address'
import { keysToPersist, PersistedKey } from '../utils'
import {
handleStateChangePersistence,
keysToPersist,
PersistedKey,
} from '../utils'

const MAGIC_EDEN_WALLET_PERSISTENCE_KEY = 'MAGIC_EDEN_CONNECTED_WALLET_STATE'

Expand Down Expand Up @@ -76,26 +80,12 @@ export default class MagicEdenProvider extends WalletProvider {
_: LaserEyesStoreType | undefined,
changedKey: keyof LaserEyesStoreType | undefined
) {
if (newState.provider === MAGIC_EDEN) {
if (changedKey) {
if (changedKey === 'balance') {
this.$valueStore.setKey('balance', newState.balance?.toString() ?? '')
} else if ((keysToPersist as readonly string[]).includes(changedKey)) {
this.$valueStore.setKey(
changedKey as PersistedKey,
newState[changedKey]?.toString() ?? ''
)
}
} else {
this.$valueStore.set({
address: newState.address,
paymentAddress: newState.paymentAddress,
paymentPublicKey: newState.paymentPublicKey,
publicKey: newState.publicKey,
balance: newState.balance?.toString() ?? '',
})
}
}
handleStateChangePersistence(
MAGIC_EDEN,
newState,
changedKey,
this.$valueStore
)
}

initialize(): void {
Expand Down
13 changes: 2 additions & 11 deletions packages/lasereyes-core/src/client/providers/xverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from '../../lib/helpers'
import { MapStore, listenKeys } from 'nanostores'
import { persistentMap } from '@nanostores/persistent'
import { keysToPersist, PersistedKey } from '../utils'
import { handleStateChangePersistence, keysToPersist, PersistedKey } from '../utils'

const XVERSE_WALLET_PERSISTENCE_KEY = 'XVERSE_CONNECTED_WALLET_STATE'
export default class XVerseProvider extends WalletProvider {
Expand Down Expand Up @@ -73,16 +73,7 @@ export default class XVerseProvider extends WalletProvider {
_: LaserEyesStoreType | undefined,
changedKey: keyof LaserEyesStoreType | undefined
) {
if (changedKey && newState.provider === XVERSE) {
if (changedKey === 'balance') {
this.$valueStore.setKey('balance', newState.balance?.toString() ?? '')
} else if ((keysToPersist as readonly string[]).includes(changedKey)) {
this.$valueStore.setKey(
changedKey as PersistedKey,
newState[changedKey]?.toString() ?? ''
)
}
}
handleStateChangePersistence(XVERSE, newState, changedKey, this.$valueStore)
}

initialize(): void {
Expand Down
30 changes: 29 additions & 1 deletion packages/lasereyes-core/src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
OP_NET,
SPARROW,
} from '../constants/wallets'
import { NetworkType } from '../types'
import { NetworkType, ProviderType } from '../types'
import { LaserEyesStoreType } from './types'

export function triggerDOMShakeHack(callback: VoidFunction) {
Expand Down Expand Up @@ -79,6 +79,34 @@ export const keysToPersist = [

export type PersistedKey = (typeof keysToPersist)[number]

export function handleStateChangePersistence(
walletName: ProviderType,
newState: LaserEyesStoreType,
changedKey: keyof LaserEyesStoreType | undefined,
$valueStore: MapStore<Record<PersistedKey, string>>
) {
if (newState.provider === walletName) {
if (changedKey) {
if (changedKey === 'balance') {
$valueStore.setKey('balance', newState.balance?.toString() ?? '')
} else if ((keysToPersist as readonly string[]).includes(changedKey)) {
$valueStore.setKey(
changedKey as PersistedKey,
newState[changedKey]?.toString() ?? ''
)
}
} else {
$valueStore.set({
address: newState.address,
paymentAddress: newState.paymentAddress,
paymentPublicKey: newState.paymentPublicKey,
publicKey: newState.publicKey,
balance: newState.balance?.toString() ?? '',
})
}
}
}

export const fromHexString = (hexString: string): Uint8Array => {
const matches = hexString.match(/.{1,2}/g)
if (!matches) {
Expand Down

0 comments on commit 09d0466

Please sign in to comment.