Refactor method to get cosmos accounts
This commit is contained in:
parent
b90c18b794
commit
e11eb3a1ee
@ -23,7 +23,7 @@ import {
|
||||
INVALID_URL_ERROR,
|
||||
IS_NUMBER_REGEX,
|
||||
} from "../utils/constants";
|
||||
import { getCosmosAccounts } from "../utils/accounts";
|
||||
import { getCosmosAccountByHDPath } from "../utils/accounts";
|
||||
import ETH_CHAINS from "../assets/ethereum-chains.json";
|
||||
import {
|
||||
getInternetCredentials,
|
||||
@ -163,7 +163,7 @@ const AddNetwork = () => {
|
||||
|
||||
case COSMOS:
|
||||
address = (
|
||||
await getCosmosAccounts(
|
||||
await getCosmosAccountByHDPath(
|
||||
mnemonic,
|
||||
hdPath,
|
||||
(newNetworkData as z.infer<typeof cosmosNetworkDataSchema>)
|
||||
|
@ -12,7 +12,7 @@ import { getHeaderTitle } from '@react-navigation/elements';
|
||||
import { Account, StackParamsList } from '../types';
|
||||
import AccountDetails from '../components/AccountDetails';
|
||||
import styles from '../styles/stylesheet';
|
||||
import { getCosmosAccounts, retrieveSingleAccount } from '../utils/accounts';
|
||||
import { getCosmosAccountByHDPath, retrieveSingleAccount } from '../utils/accounts';
|
||||
import { getMnemonic, getPathKey, sendMessage } from '../utils/misc';
|
||||
import { COSMOS, SIGN_MESSAGE, SIGNED_MESSAGE } from '../utils/constants';
|
||||
import { useNetworks } from '../context/NetworksContext';
|
||||
@ -50,7 +50,7 @@ const SignRequestEmbed = ({ route }: SignRequestProps) => {
|
||||
throw new Error("Requested network not found")
|
||||
}
|
||||
|
||||
const cosmosAccount = await getCosmosAccounts(mnemonic, path, requestedNetworkData?.addressPrefix);
|
||||
const cosmosAccount = await getCosmosAccountByHDPath(mnemonic, path, requestedNetworkData?.addressPrefix);
|
||||
|
||||
const cosmosAminoSignature = await cosmosAccount.cosmosWallet.signAmino(
|
||||
signerAddress,
|
||||
|
@ -11,11 +11,11 @@ import { DirectSecp256k1Wallet, Algo, TxBodyEncodeObject, decodeOptionalPubkey }
|
||||
import { SigningStargateClient } from '@cosmjs/stargate';
|
||||
import { toHex } from '@cosmjs/encoding';
|
||||
|
||||
import { getCosmosAccount, retrieveAccounts, retrieveSingleAccount } from '../utils/accounts';
|
||||
import { getCosmosAccountByHDPath, retrieveAccounts, retrieveSingleAccount } from '../utils/accounts';
|
||||
import AccountDetails from '../components/AccountDetails';
|
||||
import styles from '../styles/stylesheet';
|
||||
import DataBox from '../components/DataBox';
|
||||
import { getPathKey, sendMessage } from '../utils/misc';
|
||||
import { getMnemonic, getPathKey, sendMessage } from '../utils/misc';
|
||||
import { useNetworks } from '../context/NetworksContext';
|
||||
import TxErrorDialog from '../components/TxErrorDialog';
|
||||
import { Account, NetworksDataState } from '../types';
|
||||
@ -26,13 +26,13 @@ interface GetAccountsRequestData {
|
||||
chainId: string,
|
||||
}
|
||||
|
||||
interface SignOnboardTxRequestData {
|
||||
interface SignTxRequestData {
|
||||
address: string;
|
||||
signDoc: SignDoc;
|
||||
txBody: TxBodyEncodeObject;
|
||||
}
|
||||
|
||||
type IncomingMessageData = SignOnboardTxRequestData | GetAccountsRequestData;
|
||||
type IncomingMessageData = SignTxRequestData | GetAccountsRequestData;
|
||||
|
||||
interface IncomingMessageEventData {
|
||||
id: string;
|
||||
@ -76,6 +76,7 @@ export const SignTxEmbed = () => {
|
||||
const source = event.source as Window;
|
||||
const origin = event.origin;
|
||||
const requestData = data as GetAccountsRequestData;
|
||||
const mnemonic = await getMnemonic();
|
||||
|
||||
console.log(`Received ${REQUEST_COSMOS_ACCOUNTS_DATA}`, id);
|
||||
try {
|
||||
@ -91,10 +92,9 @@ export const SignTxEmbed = () => {
|
||||
throw new Error("Accounts not found for network")
|
||||
}
|
||||
|
||||
// TODO: Refactor getCosmosAccounts functions to return all accounts
|
||||
const responseAccounts = await Promise.all(
|
||||
allAccounts.map(async (acc) => {
|
||||
const cosmosAccount = await getCosmosAccount(acc.hdPath, requestedNetworkData.addressPrefix!);
|
||||
const cosmosAccount = (await getCosmosAccountByHDPath(mnemonic, acc.hdPath, requestedNetworkData.addressPrefix)).data;
|
||||
return {
|
||||
...cosmosAccount,
|
||||
pubkey: toHex(cosmosAccount.pubkey),
|
||||
@ -112,11 +112,11 @@ export const SignTxEmbed = () => {
|
||||
}
|
||||
}, [networksData]);
|
||||
|
||||
const handleSignOnboardTxRequest = useCallback(async (event: MessageEvent<IncomingMessageEventData>) => {
|
||||
const handleSignTxRequest = useCallback(async (event: MessageEvent<IncomingMessageEventData>) => {
|
||||
const { id, data } = event.data;
|
||||
const source = event.source as Window;
|
||||
const origin = event.origin;
|
||||
const requestData = data as SignOnboardTxRequestData;
|
||||
const requestData = data as SignTxRequestData;
|
||||
|
||||
console.log(`Received ${REQUEST_SIGN_TX}`, id);
|
||||
setIsTxApprovalVisible(false);
|
||||
@ -185,12 +185,12 @@ export const SignTxEmbed = () => {
|
||||
handleGetCosmosAccountsRequest(event as MessageEvent<IncomingMessageEventData>);
|
||||
break;
|
||||
case REQUEST_SIGN_TX:
|
||||
handleSignOnboardTxRequest(event as MessageEvent<IncomingMessageEventData>);
|
||||
handleSignTxRequest(event as MessageEvent<IncomingMessageEventData>);
|
||||
break;
|
||||
default:
|
||||
console.warn(`Received unknown message type: ${messageData.type}`);
|
||||
}
|
||||
}, [handleGetCosmosAccountsRequest, handleSignOnboardTxRequest]);
|
||||
}, [handleGetCosmosAccountsRequest, handleSignTxRequest]);
|
||||
|
||||
useEffect(() => {
|
||||
window.addEventListener('message', handleIncomingMessage);
|
||||
|
@ -56,7 +56,7 @@ const createWalletFromMnemonic = async (
|
||||
|
||||
case COSMOS:
|
||||
address = (
|
||||
await getCosmosAccounts(mnemonic, hdPath, network.addressPrefix)
|
||||
await getCosmosAccountByHDPath(mnemonic, hdPath, network.addressPrefix)
|
||||
).data.address;
|
||||
break;
|
||||
|
||||
@ -281,7 +281,7 @@ const accountInfoFromHDPath = async (
|
||||
break;
|
||||
case COSMOS:
|
||||
address = (
|
||||
await getCosmosAccounts(mnemonic, hdPath, networkData.addressPrefix)
|
||||
await getCosmosAccountByHDPath(mnemonic, hdPath, networkData.addressPrefix)
|
||||
).data.address;
|
||||
break;
|
||||
default:
|
||||
@ -290,17 +290,6 @@ const accountInfoFromHDPath = async (
|
||||
return { privKey, pubKey, address };
|
||||
};
|
||||
|
||||
const getCosmosAccount = async (hdPath: string, addressPrefix: string) => {
|
||||
const mnemonicStore = getInternetCredentials('mnemonicServer');
|
||||
if (!mnemonicStore) {
|
||||
throw new Error('Mnemonic not found!');
|
||||
}
|
||||
|
||||
const mnemonic = mnemonicStore;
|
||||
|
||||
return (await getCosmosAccounts(mnemonic, hdPath, addressPrefix)).data
|
||||
}
|
||||
|
||||
const getNextAccountId = async (namespaceChainId: string): Promise<number> => {
|
||||
const idStore = await getInternetCredentials(
|
||||
`addAccountCounter/${namespaceChainId}`,
|
||||
@ -334,7 +323,7 @@ const updateAccountCounter = async (
|
||||
);
|
||||
};
|
||||
|
||||
const getCosmosAccounts = async (
|
||||
const getCosmosAccountByHDPath = async (
|
||||
mnemonic: string,
|
||||
path: string,
|
||||
prefix: string = COSMOS,
|
||||
@ -362,6 +351,5 @@ export {
|
||||
accountInfoFromHDPath,
|
||||
getNextAccountId,
|
||||
updateAccountCounter,
|
||||
getCosmosAccounts,
|
||||
getCosmosAccount
|
||||
getCosmosAccountByHDPath,
|
||||
};
|
||||
|
@ -10,7 +10,7 @@ import { SignDoc } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
|
||||
|
||||
import { SignMessageParams } from '../types';
|
||||
import { getDirectWallet, getMnemonic, getPathKey } from './misc';
|
||||
import { getCosmosAccounts } from './accounts';
|
||||
import { getCosmosAccountByHDPath } from './accounts';
|
||||
import { COSMOS, EIP155 } from './constants';
|
||||
|
||||
const signMessage = async ({
|
||||
@ -58,7 +58,7 @@ const signCosmosMessage = async (
|
||||
const mnemonic = await getMnemonic();
|
||||
const addressPrefix = fromBech32(cosmosAddress).prefix
|
||||
|
||||
const cosmosAccount = await getCosmosAccounts(mnemonic, path, addressPrefix);
|
||||
const cosmosAccount = await getCosmosAccountByHDPath(mnemonic, path, addressPrefix);
|
||||
const address = cosmosAccount.data.address;
|
||||
const cosmosSignature = await cosmosAccount.cosmosWallet.signAmino(
|
||||
address,
|
||||
|
@ -18,7 +18,7 @@ import { EIP155_SIGNING_METHODS } from './EIP155Data';
|
||||
import { signDirectMessage, signEthMessage } from '../sign-message';
|
||||
import { Account } from '../../types';
|
||||
import { getMnemonic, getPathKey } from '../misc';
|
||||
import { getCosmosAccounts } from '../accounts';
|
||||
import { getCosmosAccountByHDPath } from '../accounts';
|
||||
import { COSMOS_METHODS } from './COSMOSData';
|
||||
import { COSMOS } from '../constants';
|
||||
|
||||
@ -88,7 +88,7 @@ export async function approveWalletConnectRequest(
|
||||
addressPrefix = fromBech32(account.address).prefix
|
||||
}
|
||||
|
||||
const cosmosAccount = await getCosmosAccounts(mnemonic, path, addressPrefix);
|
||||
const cosmosAccount = await getCosmosAccountByHDPath(mnemonic, path, addressPrefix);
|
||||
const address = account.address;
|
||||
|
||||
switch (request.method) {
|
||||
|
Loading…
Reference in New Issue
Block a user