Skip to content

Commit

Permalink
Will now have mainnet wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
Freshenext committed Sep 14, 2023
1 parent 4a99f45 commit 2a15d8d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
15 changes: 9 additions & 6 deletions src/core/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import { RIFWallet } from '@rsksmart/rif-wallet-core'
import { KeyManagementSystem } from 'lib/core'

import { saveKeys } from 'storage/SecureStorage'

import { MMKVStorage } from '../storage/MMKVStorage'
import { ChainTypesByIdType } from 'shared/constants/chainConstants'
import { MMKVStorage } from 'storage/MMKVStorage'

type CreateRIFWallet = (wallet: Wallet) => Promise<RIFWallet>

export const loadExistingWallet =
(createRIFWallet: CreateRIFWallet) => async (serializedKeys: string) => {
(createRIFWallet: CreateRIFWallet) =>
async (serializedKeys: string, chainId: ChainTypesByIdType) => {
try {
const { kms, wallets } =
KeyManagementSystem.fromSerialized(serializedKeys)
const { kms, wallets } = KeyManagementSystem.fromSerialized(
serializedKeys,
chainId,
)

const rifWallet = await createRIFWallet(Object.values(wallets)[0])
const rifWallet = await createRIFWallet(wallets[0])
const rifWalletIsDeployed =
await rifWallet.smartWalletFactory.isDeployed()

Expand Down
44 changes: 39 additions & 5 deletions src/lib/core/KeyManagementSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,45 @@ export class KeyManagementSystem implements IKeyManagementSystem {
/**
* Use this method to recover a stored serialized wallet
* @param serialized the serialized string
* @param chainId
* @returns the KeyManagementSystem that was serialized
*/
static fromSerialized (serialized: string): { kms: KeyManagementSystem, wallets: Wallet[] } {
static fromSerialized (serialized: string, chainId: number): { kms: KeyManagementSystem, wallets: Wallet[] } {
const { mnemonic, state }: KeyManagementSystemSerialization = JSON.parse(serialized)

const kms = new KeyManagementSystem(mnemonic, state)
// If for some reason the chainId that was passed has not been generated, generate and save it
if (!state.lastDerivedAccountIndex[chainId]) {
const newChain = kms.getWalletByChainIdAccountIndex(chainId, 0)
newChain.save()
}
// @TODO Save this state using saveKeys() function
// This is for incremental rollout, we should add both chains when an user creates a wallet

// Now, get the derivation path and use that one to return the wallet
const derivationPath = getDPathByChainId(chainId, 0)
// try to create the wallet using the private key which is faster, if it fails, the fallback
// is to use the derivedPath and mnemonic to regenerate the private key and then wallet
const wallets = Object.keys(state.derivedPaths)
let wallet
try {
wallet = new Wallet(state.derivedPaths[derivationPath])
} catch (_error) {
const derivedWalletContainer = kms.deriveWallet(derivationPath)
wallet = derivedWalletContainer.wallet
}
// Old logic, should be analyzed to see if it can be removed or the app modified
/*const wallets = Object.keys(state.derivedPaths)
.map((derivedPath: string) => {
try {
return new Wallet(state.derivedPaths[derivedPath])
} catch (_error) {
const { wallet } = kms.deriveWallet(derivedPath)
return wallet
}
})

})*/
return {
kms,
wallets
wallets: [wallet]
}
}

Expand Down Expand Up @@ -160,6 +177,23 @@ export class KeyManagementSystem implements IKeyManagementSystem {
}
}

/**
* This will get the wallet according to the chain id and accountIndex passed to the function
* @param chainId
* @param accountIndex
*/
getWalletByChainIdAccountIndex(chainId: number, accountIndex: number): SaveableWallet {
const derivationPath = getDPathByChainId(chainId, accountIndex)
const { wallet, privateKey } = this.deriveWallet(derivationPath)
return {
derivationPath,
wallet,
save: () => {
this.state.derivedPaths[derivationPath] = privateKey
}
}
}

/**
* Get the account for an arbitrary derivation path
* @param derivationPath an arbitrary derivation path
Expand Down
3 changes: 1 addition & 2 deletions src/redux/slices/settingsSlice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ export const unlockApp = createAsyncThunk<
request => thunkAPI.dispatch(onRequest({ request })),
chainId,
),
)(serializedKeys)

)(serializedKeys, chainId)
if (!existingWallet) {
return thunkAPI.rejectWithValue('No Existing Wallet')
}
Expand Down

0 comments on commit 2a15d8d

Please sign in to comment.