Add account in all networks in wallet and prefill new network with accounts (#33)
Part of https://www.notion.so/Stage0-onboarding-flow-1e4a6b22d47280aba3b5da3ed1154ff5 Co-authored-by: Shreerang Kale <shreerangkale@gmail.com> Reviewed-on: LaconicNetwork/laconic-wallet-web#33 Co-authored-by: shreerang <shreerang@noreply.git.vdb.to> Co-committed-by: shreerang <shreerang@noreply.git.vdb.to>
This commit is contained in:
parent
f5b92af4f9
commit
fe3d2411a2
@ -85,7 +85,7 @@ const Accounts = () => {
|
|||||||
|
|
||||||
const addAccountHandler = async () => {
|
const addAccountHandler = async () => {
|
||||||
setIsAccountCreating(true);
|
setIsAccountCreating(true);
|
||||||
const newAccount = await addAccount(selectedNetwork!);
|
const newAccount = await addAccount(selectedNetwork!.chainId);
|
||||||
setIsAccountCreating(false);
|
setIsAccountCreating(false);
|
||||||
if (newAccount) {
|
if (newAccount) {
|
||||||
updateAccounts(newAccount);
|
updateAccounts(newAccount);
|
||||||
|
@ -16,7 +16,7 @@ const useAddAccountEmbed = () => {
|
|||||||
const { getAccountsData } = useAccountsData();
|
const { getAccountsData } = useAccountsData();
|
||||||
|
|
||||||
const addAccountHandler = useCallback(async (network: NetworksDataState) => {
|
const addAccountHandler = useCallback(async (network: NetworksDataState) => {
|
||||||
const newAccount = await addAccount(network);
|
const newAccount = await addAccount(network.chainId);
|
||||||
if (newAccount) {
|
if (newAccount) {
|
||||||
setAccounts(prev => [...prev, newAccount]);
|
setAccounts(prev => [...prev, newAccount]);
|
||||||
setCurrentIndex(newAccount.index);
|
setCurrentIndex(newAccount.index);
|
||||||
|
@ -13,7 +13,7 @@ import { zodResolver } from "@hookform/resolvers/zod";
|
|||||||
|
|
||||||
import { StackParamsList } from "../types";
|
import { StackParamsList } from "../types";
|
||||||
import { SelectNetworkType } from "../components/SelectNetworkType";
|
import { SelectNetworkType } from "../components/SelectNetworkType";
|
||||||
import { storeNetworkData } from "../utils/accounts";
|
import { addAccountsForNetwork, getNextAccountId, storeNetworkData } from "../utils/accounts";
|
||||||
import { useNetworks } from "../context/NetworksContext";
|
import { useNetworks } from "../context/NetworksContext";
|
||||||
import {
|
import {
|
||||||
COSMOS,
|
COSMOS,
|
||||||
@ -181,6 +181,15 @@ const AddNetwork = () => {
|
|||||||
const retrievedNetworksData = await storeNetworkData(newNetworkData);
|
const retrievedNetworksData = await storeNetworkData(newNetworkData);
|
||||||
setNetworksData(retrievedNetworksData);
|
setNetworksData(retrievedNetworksData);
|
||||||
|
|
||||||
|
// Get number of accounts in first network
|
||||||
|
const nextAccountId = await getNextAccountId(
|
||||||
|
`${retrievedNetworksData[0].namespace}:${retrievedNetworksData[0].chainId}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
const selectedNetwork = retrievedNetworksData.find(
|
||||||
|
(network) => network.chainId === newNetworkData.chainId,
|
||||||
|
);
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
setInternetCredentials(
|
setInternetCredentials(
|
||||||
`accounts/${newNetworkData.namespace}:${newNetworkData.chainId}/0`,
|
`accounts/${newNetworkData.namespace}:${newNetworkData.chainId}/0`,
|
||||||
@ -199,6 +208,8 @@ const AddNetwork = () => {
|
|||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
await addAccountsForNetwork(selectedNetwork!, nextAccountId - 1);
|
||||||
|
|
||||||
navigation.navigate("Home");
|
navigation.navigate("Home");
|
||||||
},
|
},
|
||||||
[navigation, namespace, setNetworksData],
|
[navigation, namespace, setNetworksData],
|
||||||
|
@ -87,15 +87,44 @@ const createWalletFromMnemonic = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
const addAccount = async (
|
const addAccount = async (
|
||||||
networkData: NetworksDataState,
|
chainId: string,
|
||||||
): Promise<Account | undefined> => {
|
): Promise<Account | undefined> => {
|
||||||
try {
|
try {
|
||||||
const namespaceChainId = `${networkData.namespace}:${networkData.chainId}`;
|
let selectedNetworkAccount
|
||||||
const id = await getNextAccountId(namespaceChainId);
|
const networksData = await retrieveNetworksData();
|
||||||
const hdPath = getHDPath(namespaceChainId, `0'/0/${id}`);
|
|
||||||
const accounts = await addAccountFromHDPath(hdPath, networkData);
|
// Add account to all networks and return account for selected network
|
||||||
await updateAccountCounter(namespaceChainId, id);
|
for (const network of networksData) {
|
||||||
return accounts;
|
const namespaceChainId = `${network.namespace}:${network.chainId}`;
|
||||||
|
const id = await getNextAccountId(namespaceChainId);
|
||||||
|
const hdPath = getHDPath(namespaceChainId, `0'/0/${id}`);
|
||||||
|
const account = await addAccountFromHDPath(hdPath, network);
|
||||||
|
await updateAccountCounter(namespaceChainId, id);
|
||||||
|
|
||||||
|
if (network.chainId === chainId) {
|
||||||
|
selectedNetworkAccount = account;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return selectedNetworkAccount;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error creating account:', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const addAccountsForNetwork = async (
|
||||||
|
network: NetworksDataState,
|
||||||
|
numberOfAccounts: number,
|
||||||
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const namespaceChainId = `${network.namespace}:${network.chainId}`;
|
||||||
|
|
||||||
|
for (let i = 0; i < numberOfAccounts; i++) {
|
||||||
|
const id = await getNextAccountId(namespaceChainId);
|
||||||
|
const hdPath = getHDPath(namespaceChainId, `0'/0/${id}`);
|
||||||
|
await addAccountFromHDPath(hdPath, network);
|
||||||
|
await updateAccountCounter(namespaceChainId, id);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error creating account:', error);
|
console.error('Error creating account:', error);
|
||||||
}
|
}
|
||||||
@ -343,6 +372,7 @@ export {
|
|||||||
createWallet,
|
createWallet,
|
||||||
addAccount,
|
addAccount,
|
||||||
addAccountFromHDPath,
|
addAccountFromHDPath,
|
||||||
|
addAccountsForNetwork,
|
||||||
storeNetworkData,
|
storeNetworkData,
|
||||||
retrieveNetworksData,
|
retrieveNetworksData,
|
||||||
retrieveAccounts,
|
retrieveAccounts,
|
||||||
|
Loading…
Reference in New Issue
Block a user