Skip to content

Commit

Permalink
refactor: createRIFWallet function moved to operations where it is used
Browse files Browse the repository at this point in the history
  • Loading branch information
TravellerOnTheRun committed Oct 4, 2023
1 parent ab96167 commit 3b5fed6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 40 deletions.
57 changes: 55 additions & 2 deletions src/core/operations.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,61 @@
import { Wallet, providers } from 'ethers'
import { RifRelayConfig } from '@rsksmart/rif-relay-light-sdk'
import { OnRequest, RIFWallet } from '@rsksmart/rif-wallet-core'

import { KeyManagementSystem } from 'lib/core'

import { saveKeys } from 'storage/SecureStorage'
import { ChainTypesByIdType } from 'shared/constants/chainConstants'
import {
ChainTypesByIdType,
chainTypesById,
} from 'shared/constants/chainConstants'
import { MMKVStorage } from 'storage/MMKVStorage'
import { AppDispatch } from 'src/redux'
import { onRequest } from 'src/redux/slices/settingsSlice'

import { createRIFWallet } from './setup'
import { getWalletSetting } from './config'
import { SETTINGS } from './types'

// function creates RIF Wallet instance
// along with necessary confings
const createRIFWallet = async (
chainId: 30 | 31,
wallet: Wallet,
onRequestFn: OnRequest,
) => {
const jsonRpcProvider = new providers.StaticJsonRpcProvider(
getWalletSetting(SETTINGS.RPC_URL, chainTypesById[chainId]),
)

const rifRelayConfig: RifRelayConfig = {
smartWalletFactoryAddress: getWalletSetting(
SETTINGS.SMART_WALLET_FACTORY_ADDRESS,
chainTypesById[chainId],
),
relayVerifierAddress: getWalletSetting(
SETTINGS.RELAY_VERIFIER_ADDRESS,
chainTypesById[chainId],
),
deployVerifierAddress: getWalletSetting(
SETTINGS.DEPLOY_VERIFIER_ADDRESS,
chainTypesById[chainId],
),
relayServer: getWalletSetting(
SETTINGS.RIF_RELAY_SERVER,
chainTypesById[chainId],
),
}

return await RIFWallet.create(
wallet.connect(jsonRpcProvider),
onRequestFn,
rifRelayConfig,
)
}

// gets the wallet from KeyManagementSystem
// re-creates the RIFWallet out it
// return kms, rifWallet and rifWalletIsDeployed bool
export const loadExistingWallet = async (
serializedKeys: string,
chainId: ChainTypesByIdType,
Expand All @@ -31,6 +79,11 @@ export const loadExistingWallet = async (
return null
}

// creates KeyManagementSystem instance using mnemonic phrase
// using chainId gets save function and Wallet instance
// creates RIFWallet instance out of it
// saves the kms state in encrypted storage of the device
// returns rifWallet and rifWalletIsDeployed bool
export const createKMS = async (
chainId: ChainTypesByIdType,
mnemonic: string,
Expand Down
35 changes: 0 additions & 35 deletions src/core/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,6 @@ export const getRnsResolver = (chainId: ChainTypesByIdType) =>
? Resolver.forRskMainnet({})
: Resolver.forRskTestnet({})

export const createRIFWallet = async (
chainId: 30 | 31,
wallet: Wallet,
onRequest: OnRequest,
) => {
const jsonRpcProvider = new providers.StaticJsonRpcProvider(
getWalletSetting(SETTINGS.RPC_URL, chainTypesById[chainId]),
)

const rifRelayConfig: RifRelayConfig = {
smartWalletFactoryAddress: getWalletSetting(
SETTINGS.SMART_WALLET_FACTORY_ADDRESS,
chainTypesById[chainId],
),
relayVerifierAddress: getWalletSetting(
SETTINGS.RELAY_VERIFIER_ADDRESS,
chainTypesById[chainId],
),
deployVerifierAddress: getWalletSetting(
SETTINGS.DEPLOY_VERIFIER_ADDRESS,
chainTypesById[chainId],
),
relayServer: getWalletSetting(
SETTINGS.RIF_RELAY_SERVER,
chainTypesById[chainId],
),
}

return await RIFWallet.create(
wallet.connect(jsonRpcProvider),
onRequest,
rifRelayConfig,
)
}

const defaultMainnetTokens: ITokenWithoutLogo[] = Object.keys(mainnetContracts)
.filter(address => ['RDOC', 'RIF'].includes(mainnetContracts[address].symbol))
.map(address => {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/core/KeyManagementSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface LastDerivedAccountIndex {
[chainId: number]: number
}

type KeyManagementSystemState = {
interface KeyManagementSystemState {
lastDerivedAccountIndex: LastDerivedAccountIndex
derivedPaths: DerivedPaths
}
Expand All @@ -20,12 +20,12 @@ const createInitialState = (): KeyManagementSystemState => ({
derivedPaths: {}
})

type KeyManagementSystemSerialization = {
interface KeyManagementSystemSerialization {
mnemonic: Mnemonic
state: KeyManagementSystemState
}

export type SaveableWallet = {
export interface SaveableWallet {
derivationPath: string
wallet: Wallet
save(): void
Expand Down

0 comments on commit 3b5fed6

Please sign in to comment.