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 Header from "./components/Header";
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 Toggle from "./components/Toggle";
import RequestModal from "./modals/RequestModal";
@ -107,11 +113,11 @@ export default function App() {
};
return [
{ method: "eth_sendTransaction", callback: onSendTransaction },
{ method: "eth_signTransaction", callback: onSignTransaction },
{ method: "personal_sign", callback: onSignPersonalMessage },
{ method: "eth_sign (standard)", callback: onEthSign },
{ method: "eth_signTypedData", callback: onSignTypedData },
{ method: DEFAULT_EIP155_METHODS.ETH_SEND_TRANSACTION, callback: onSendTransaction },
{ method: DEFAULT_EIP155_METHODS.ETH_SIGN_TRANSACTION, callback: onSignTransaction },
{ method: DEFAULT_EIP155_METHODS.PERSONAL_SIGN, callback: onSignPersonalMessage },
{ method: DEFAULT_EIP155_METHODS.ETH_SIGN + " (standard)", callback: onEthSign },
{ method: DEFAULT_EIP155_METHODS.ETH_SIGN_TYPED_DATA, callback: onSignTypedData },
];
};
@ -125,8 +131,8 @@ export default function App() {
await cosmosRpc.testSignAmino(chainId, address);
};
return [
{ method: "cosmos_signDirect", callback: onSignDirect },
{ method: "cosmos_signAmino", callback: onSignAmino },
{ method: DEFAULT_COSMOS_METHODS.COSMOS_SIGN_DIRECT, callback: onSignDirect },
{ 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_EIP155_METHODS = [
"eth_sendTransaction",
"eth_signTransaction",
"eth_sign",
"personal_sign",
"eth_signTypedData",
];
export enum DEFAULT_EIP155_METHODS {
ETH_SEND_TRANSACTION = "eth_sendTransaction",
ETH_SIGN_TRANSACTION = "eth_signTransaction",
ETH_SIGN = "eth_sign",
PERSONAL_SIGN = "personal_sign",
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 {
SOL_SIGN_TRANSACTION = "solana_signTransaction",

View File

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

View File

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