Skip to content

Commit

Permalink
Bitcoin should only return the correct implementation depending on th… (
Browse files Browse the repository at this point in the history
#754)

* Bitcoin should only return the correct implementation depending on the chainId

* Created a separated ChainStorage as when the app restarts the MainStorage is lost - chainId should never change!
  • Loading branch information
Freshenext authored and jormelCoin committed Sep 28, 2023
1 parent 5970a35 commit 2e7a7fa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
14 changes: 13 additions & 1 deletion src/core/hooks/bitcoin/initializeBitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ const onNoNetworksPresent = (chainId: ChainTypesByIdType) => {
return BitcoinNetworkStore.getStoredNetworks()
}

const BITCOIN_CHAINID_MAP: Record<ChainTypesByIdType, string> = {
30: bitcoinMainnet.name,
31: bitcoinTestnet.name,
}

/**
* Will return networks as an array, networks as a map (networksMap) and a function to refresh networks from the storage
* This hook will also instantiate the bitcoin networks with a BIPWithRequest class that will handle the payments for the onRequest method
Expand All @@ -48,7 +53,6 @@ const onNoNetworksPresent = (chainId: ChainTypesByIdType) => {
* @param fetcher
* @param chainId
*/

export const initializeBitcoin = (
mnemonic: string,
dispatch: AppDispatch,
Expand All @@ -69,6 +73,14 @@ export const initializeBitcoin = (
networksMap = onNoNetworksPresent(chainId)
}

// if there is a network created but does not match current chainId - create it
if (!networksMap[BITCOIN_CHAINID_MAP[chainId]]) {
networksMap = onNoNetworksPresent(chainId)
}
// Due to how the bitcoin logic is implemented... I'll transform the networksMap to only include the btc network we need
networksMap = {
[BITCOIN_CHAINID_MAP[chainId]]: networksMap[BITCOIN_CHAINID_MAP[chainId]],
}
const transformNetwork = (
network: StoredBitcoinNetworkValue,
mnemonicText: string,
Expand Down
14 changes: 8 additions & 6 deletions src/redux/slices/settingsSlice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,15 @@ const initialState: SettingsSlice = {
usedBitcoinAddresses: {},
}

const createInitialState = () => ({
...initialState,
chainId: getCurrentChainId(),
chainType: chainTypesById[getCurrentChainId()],
})

const settingsSlice = createSlice({
name: 'settings',
initialState: () => ({
...initialState,
chainId: getCurrentChainId(),
chainType: chainTypesById[getCurrentChainId()],
}),
initialState: createInitialState,
reducers: {
setKeysExist: (state, { payload }: PayloadAction<boolean>) => {
state.keysExist = payload
Expand Down Expand Up @@ -440,7 +442,7 @@ const settingsSlice = createSlice({
deleteKeys()
deleteDomains()
deleteCache()
return initialState
return createInitialState()
},
setFullscreen: (state, { payload }: PayloadAction<boolean>) => {
state.fullscreen = payload
Expand Down
7 changes: 4 additions & 3 deletions src/storage/ChainStorage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ChainTypesByIdType } from 'shared/constants/chainConstants'
import { MMKVStorage } from 'storage/MMKVStorage'

import { MainStorage } from './MainStorage'
const ChainStorage = new MMKVStorage('chainStorage')

export const getCurrentChainId: () => ChainTypesByIdType = () =>
MainStorage.get('chainId') || 31
ChainStorage.get('chainId') || 31

export const setCurrentChainId = (chainId: ChainTypesByIdType) =>
MainStorage.set('chainId', chainId)
ChainStorage.set('chainId', chainId)

0 comments on commit 2e7a7fa

Please sign in to comment.