diff --git a/src/core/operations.ts b/src/core/operations.ts index 0b55f1600..5ef3139a3 100644 --- a/src/core/operations.ts +++ b/src/core/operations.ts @@ -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, @@ -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, diff --git a/src/core/setup.ts b/src/core/setup.ts index b88520a7c..94bb28cdc 100644 --- a/src/core/setup.ts +++ b/src/core/setup.ts @@ -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 => { diff --git a/src/lib/core/KeyManagementSystem.ts b/src/lib/core/KeyManagementSystem.ts index d2f80b042..830b53042 100644 --- a/src/lib/core/KeyManagementSystem.ts +++ b/src/lib/core/KeyManagementSystem.ts @@ -10,7 +10,7 @@ interface LastDerivedAccountIndex { [chainId: number]: number } -type KeyManagementSystemState = { +interface KeyManagementSystemState { lastDerivedAccountIndex: LastDerivedAccountIndex derivedPaths: DerivedPaths } @@ -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