refactor: creates getAddressByChainId
helper fn
This commit is contained in:
parent
601f16b171
commit
1a2e5098c3
@ -84,6 +84,15 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
|
|||||||
setChainData(chainData);
|
setChainData(chainData);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getAddressByChainId = (chainId: string) => {
|
||||||
|
const account = accounts.find(account => account.startsWith(chainId));
|
||||||
|
if (account === undefined) throw new Error(`Account for chainId ${chainId} not found.`);
|
||||||
|
const address = account.split(":").pop();
|
||||||
|
if (address === undefined) throw new Error(`Address for account ${account} is invalid`);
|
||||||
|
|
||||||
|
return address;
|
||||||
|
};
|
||||||
|
|
||||||
const _createJsonRpcRequestHandler =
|
const _createJsonRpcRequestHandler =
|
||||||
(rpcRequest: (...requestArgs: [any]) => Promise<IFormattedRpcResponse>) =>
|
(rpcRequest: (...requestArgs: [any]) => Promise<IFormattedRpcResponse>) =>
|
||||||
async (chainId: string) => {
|
async (chainId: string) => {
|
||||||
@ -185,11 +194,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
|
|||||||
// encode message (hex)
|
// encode message (hex)
|
||||||
const hexMsg = encoding.utf8ToHex(message, true);
|
const hexMsg = encoding.utf8ToHex(message, true);
|
||||||
|
|
||||||
// get ethereum address
|
const address = getAddressByChainId(chainId);
|
||||||
const account = accounts.find(account => account.startsWith(chainId));
|
|
||||||
if (account === undefined) throw new Error("Account is not found");
|
|
||||||
const address = account.split(":").pop();
|
|
||||||
if (address === undefined) throw new Error("Address is invalid");
|
|
||||||
|
|
||||||
// personal_sign params
|
// personal_sign params
|
||||||
const params = [hexMsg, address];
|
const params = [hexMsg, address];
|
||||||
@ -231,11 +236,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
|
|||||||
// test message
|
// test message
|
||||||
const message = JSON.stringify(eip712.example);
|
const message = JSON.stringify(eip712.example);
|
||||||
|
|
||||||
// get ethereum address
|
const address = getAddressByChainId(chainId);
|
||||||
const account = accounts.find(account => account.startsWith(chainId));
|
|
||||||
if (account === undefined) throw new Error("Account is not found");
|
|
||||||
const address = account.split(":").pop();
|
|
||||||
if (address === undefined) throw new Error("Address is invalid");
|
|
||||||
|
|
||||||
// eth_signTypedData params
|
// eth_signTypedData params
|
||||||
const params = [address, message];
|
const params = [address, message];
|
||||||
@ -306,11 +307,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
|
|||||||
reference,
|
reference,
|
||||||
);
|
);
|
||||||
|
|
||||||
// get cosmos address
|
const address = getAddressByChainId(chainId);
|
||||||
const account = accounts.find(account => account.startsWith(chainId));
|
|
||||||
if (account === undefined) throw new Error("Account is not found");
|
|
||||||
const address = account.split(":").pop();
|
|
||||||
if (address === undefined) throw new Error("Address is invalid");
|
|
||||||
|
|
||||||
// cosmos_signDirect params
|
// cosmos_signDirect params
|
||||||
const params = {
|
const params = {
|
||||||
@ -359,11 +356,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
|
|||||||
sequence: "54",
|
sequence: "54",
|
||||||
};
|
};
|
||||||
|
|
||||||
// get cosmos address
|
const address = getAddressByChainId(chainId);
|
||||||
const account = accounts.find(account => account.startsWith(chainId));
|
|
||||||
if (account === undefined) throw new Error("Account is not found");
|
|
||||||
const address = account.split(":").pop();
|
|
||||||
if (address === undefined) throw new Error("Address is invalid");
|
|
||||||
|
|
||||||
// cosmos_signAmino params
|
// cosmos_signAmino params
|
||||||
const params = { signerAddress: address, signDoc };
|
const params = { signerAddress: address, signDoc };
|
||||||
|
Loading…
Reference in New Issue
Block a user