refactor(dapp-v2): use enums for default chain methods

This commit is contained in:
Ben Kremer 2022-03-17 16:21:47 +01:00
parent c4759249b6
commit 6b14e13eaf
4 changed files with 47 additions and 34 deletions

View File

@ -6,7 +6,13 @@ import Blockchain from "./components/Blockchain";
import Column from "./components/Column"; import Column from "./components/Column";
import Header from "./components/Header"; import Header from "./components/Header";
import Modal from "./components/Modal"; import Modal from "./components/Modal";
import { DEFAULT_MAIN_CHAINS, DEFAULT_SOLANA_METHODS, DEFAULT_TEST_CHAINS } from "./constants"; import {
DEFAULT_COSMOS_METHODS,
DEFAULT_EIP155_METHODS,
DEFAULT_MAIN_CHAINS,
DEFAULT_SOLANA_METHODS,
DEFAULT_TEST_CHAINS,
} from "./constants";
import { AccountAction, setLocaleStorageTestnetFlag } from "./helpers"; import { AccountAction, setLocaleStorageTestnetFlag } from "./helpers";
import Toggle from "./components/Toggle"; import Toggle from "./components/Toggle";
import RequestModal from "./modals/RequestModal"; import RequestModal from "./modals/RequestModal";
@ -107,11 +113,11 @@ export default function App() {
}; };
return [ return [
{ method: "eth_sendTransaction", callback: onSendTransaction }, { method: DEFAULT_EIP155_METHODS.ETH_SEND_TRANSACTION, callback: onSendTransaction },
{ method: "eth_signTransaction", callback: onSignTransaction }, { method: DEFAULT_EIP155_METHODS.ETH_SIGN_TRANSACTION, callback: onSignTransaction },
{ method: "personal_sign", callback: onSignPersonalMessage }, { method: DEFAULT_EIP155_METHODS.PERSONAL_SIGN, callback: onSignPersonalMessage },
{ method: "eth_sign (standard)", callback: onEthSign }, { method: DEFAULT_EIP155_METHODS.ETH_SIGN + " (standard)", callback: onEthSign },
{ method: "eth_signTypedData", callback: onSignTypedData }, { method: DEFAULT_EIP155_METHODS.ETH_SIGN_TYPED_DATA, callback: onSignTypedData },
]; ];
}; };
@ -125,8 +131,8 @@ export default function App() {
await cosmosRpc.testSignAmino(chainId, address); await cosmosRpc.testSignAmino(chainId, address);
}; };
return [ return [
{ method: "cosmos_signDirect", callback: onSignDirect }, { method: DEFAULT_COSMOS_METHODS.COSMOS_SIGN_DIRECT, callback: onSignDirect },
{ method: "cosmos_signAmino", callback: onSignAmino }, { method: DEFAULT_COSMOS_METHODS.COSMOS_SIGN_AMINO, callback: onSignAmino },
]; ];
}; };

View File

@ -26,15 +26,18 @@ export const DEFAULT_PROJECT_ID = process.env.REACT_APP_PROJECT_ID;
export const DEFAULT_RELAY_URL = process.env.REACT_APP_RELAY_URL; export const DEFAULT_RELAY_URL = process.env.REACT_APP_RELAY_URL;
export const DEFAULT_EIP155_METHODS = [ export enum DEFAULT_EIP155_METHODS {
"eth_sendTransaction", ETH_SEND_TRANSACTION = "eth_sendTransaction",
"eth_signTransaction", ETH_SIGN_TRANSACTION = "eth_signTransaction",
"eth_sign", ETH_SIGN = "eth_sign",
"personal_sign", PERSONAL_SIGN = "personal_sign",
"eth_signTypedData", ETH_SIGN_TYPED_DATA = "eth_signTypedData",
]; }
export const DEFAULT_COSMOS_METHODS = ["cosmos_signDirect", "cosmos_signAmino"]; export enum DEFAULT_COSMOS_METHODS {
COSMOS_SIGN_DIRECT = "cosmos_signDirect",
COSMOS_SIGN_AMINO = "cosmos_signAmino",
}
export enum DEFAULT_SOLANA_METHODS { export enum DEFAULT_SOLANA_METHODS {
SOL_SIGN_TRANSACTION = "solana_signTransaction", SOL_SIGN_TRANSACTION = "solana_signTransaction",

View File

@ -113,9 +113,9 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
.map(namespace => { .map(namespace => {
switch (namespace) { switch (namespace) {
case "eip155": case "eip155":
return DEFAULT_EIP155_METHODS; return Object.values(DEFAULT_EIP155_METHODS);
case "cosmos": case "cosmos":
return DEFAULT_COSMOS_METHODS; return Object.values(DEFAULT_COSMOS_METHODS);
case "solana": case "solana":
return Object.values(DEFAULT_SOLANA_METHODS); return Object.values(DEFAULT_SOLANA_METHODS);
default: default:

View File

@ -17,7 +17,11 @@ import {
import { useWalletConnectClient } from "./ClientContext"; import { useWalletConnectClient } from "./ClientContext";
import { apiGetChainNamespace, ChainsMap } from "caip-api"; import { apiGetChainNamespace, ChainsMap } from "caip-api";
import { TypedDataField } from "@ethersproject/abstract-signer"; import { TypedDataField } from "@ethersproject/abstract-signer";
import { DEFAULT_SOLANA_METHODS } from "../constants"; import {
DEFAULT_COSMOS_METHODS,
DEFAULT_EIP155_METHODS,
DEFAULT_SOLANA_METHODS,
} from "../constants";
import { clusterApiUrl, Connection, Keypair, SystemProgram, Transaction } from "@solana/web3.js"; import { clusterApiUrl, Connection, Keypair, SystemProgram, Transaction } from "@solana/web3.js";
import { SolanaChainData } from "../chains/solana"; import { SolanaChainData } from "../chains/solana";
@ -174,7 +178,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
const balance = BigNumber.from(balances[account][0].balance || "0"); const balance = BigNumber.from(balances[account][0].balance || "0");
if (balance.lt(BigNumber.from(tx.gasPrice).mul(tx.gasLimit))) { if (balance.lt(BigNumber.from(tx.gasPrice).mul(tx.gasLimit))) {
return { return {
method: "eth_sendTransaction", method: DEFAULT_EIP155_METHODS.ETH_SEND_TRANSACTION,
address, address,
valid: false, valid: false,
result: "Insufficient funds for intrinsic transaction cost", result: "Insufficient funds for intrinsic transaction cost",
@ -185,14 +189,14 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
topic: session!.topic, topic: session!.topic,
chainId, chainId,
request: { request: {
method: "eth_sendTransaction", method: DEFAULT_EIP155_METHODS.ETH_SEND_TRANSACTION,
params: [tx], params: [tx],
}, },
}); });
// format displayed result // format displayed result
return { return {
method: "eth_sendTransaction", method: DEFAULT_EIP155_METHODS.ETH_SEND_TRANSACTION,
address, address,
valid: true, valid: true,
result, result,
@ -209,13 +213,13 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
topic: session!.topic, topic: session!.topic,
chainId, chainId,
request: { request: {
method: "eth_signTransaction", method: DEFAULT_EIP155_METHODS.ETH_SIGN_TRANSACTION,
params: [tx], params: [tx],
}, },
}); });
return { return {
method: "eth_signTransaction", method: DEFAULT_EIP155_METHODS.ETH_SIGN_TRANSACTION,
address, address,
valid: true, valid: true,
result, result,
@ -237,7 +241,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
topic: session!.topic, topic: session!.topic,
chainId, chainId,
request: { request: {
method: "personal_sign", method: DEFAULT_EIP155_METHODS.PERSONAL_SIGN,
params, params,
}, },
}); });
@ -259,7 +263,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
// format displayed result // format displayed result
return { return {
method: "personal_sign", method: DEFAULT_EIP155_METHODS.PERSONAL_SIGN,
address, address,
valid, valid,
result, result,
@ -279,7 +283,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
topic: session!.topic, topic: session!.topic,
chainId, chainId,
request: { request: {
method: "eth_sign", method: DEFAULT_EIP155_METHODS.ETH_SIGN,
params, params,
}, },
}); });
@ -301,7 +305,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
// format displayed result // format displayed result
return { return {
method: "eth_sign (standard)", method: DEFAULT_EIP155_METHODS.ETH_SIGN + " (standard)",
address, address,
valid, valid,
result, result,
@ -318,7 +322,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
topic: session!.topic, topic: session!.topic,
chainId, chainId,
request: { request: {
method: "eth_signTypedData", method: DEFAULT_EIP155_METHODS.ETH_SIGN_TYPED_DATA,
params, params,
}, },
}); });
@ -337,7 +341,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
) === address; ) === address;
return { return {
method: "eth_signTypedData", method: DEFAULT_EIP155_METHODS.ETH_SIGN_TYPED_DATA,
address, address,
valid, valid,
result: signature, result: signature,
@ -387,7 +391,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
topic: session!.topic, topic: session!.topic,
chainId, chainId,
request: { request: {
method: "cosmos_signDirect", method: DEFAULT_COSMOS_METHODS.COSMOS_SIGN_DIRECT,
params, params,
}, },
}); });
@ -403,7 +407,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
// format displayed result // format displayed result
return { return {
method: "cosmos_signDirect", method: DEFAULT_COSMOS_METHODS.COSMOS_SIGN_DIRECT,
address, address,
valid, valid,
result: result.signature, result: result.signature,
@ -431,7 +435,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
topic: session!.topic, topic: session!.topic,
chainId, chainId,
request: { request: {
method: "cosmos_signAmino", method: DEFAULT_COSMOS_METHODS.COSMOS_SIGN_AMINO,
params, params,
}, },
}); });
@ -447,7 +451,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
// format displayed result // format displayed result
return { return {
method: "cosmos_signAmino", method: DEFAULT_COSMOS_METHODS.COSMOS_SIGN_AMINO,
address, address,
valid, valid,
result: result.signature, result: result.signature,