feat: adds eth_sign and eth_signTransaction examples

This commit is contained in:
Ben Kremer 2022-02-16 14:05:12 +01:00
parent f5a0598ef1
commit f5814d4d1a
3 changed files with 87 additions and 1 deletions

View File

@ -81,10 +81,18 @@ export default function App() {
openRequestModal();
await ethereumRpc.testSendTransaction(chainId);
};
const onSignTransaction = async (chainId: string) => {
openRequestModal();
await ethereumRpc.testSignTransaction(chainId);
};
const onSignPersonalMessage = async (chainId: string) => {
openRequestModal();
await ethereumRpc.testSignPersonalMessage(chainId);
};
const onEthSign = async (chainId: string) => {
openRequestModal();
await ethereumRpc.testEthSign(chainId);
};
const onSignTypedData = async (chainId: string) => {
openRequestModal();
await ethereumRpc.testSignTypedData(chainId);
@ -92,7 +100,9 @@ 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 },
];
};

View File

@ -24,7 +24,13 @@ 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", "personal_sign", "eth_signTypedData"];
export const DEFAULT_EIP155_METHODS = [
"eth_sendTransaction",
"eth_signTransaction",
"eth_sign",
"personal_sign",
"eth_signTypedData",
];
export const DEFAULT_COSMOS_METHODS = ["cosmos_signDirect", "cosmos_signAmino"];

View File

@ -34,6 +34,8 @@ interface IContext {
ping: () => Promise<void>;
ethereumRpc: {
testSendTransaction: (chainId: string) => Promise<void>;
testSignTransaction: (chainId: string) => Promise<void>;
testEthSign: (chainId: string) => Promise<void>;
testSignPersonalMessage: (chainId: string) => Promise<void>;
testSignTypedData: (chainId: string) => Promise<void>;
};
@ -187,6 +189,31 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
result,
};
}),
testSignTransaction: _createJsonRpcRequestHandler(async (chainId: string) => {
// get ethereum address
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");
const tx = await formatTestTransaction(account);
const result: string = await client!.request({
topic: session!.topic,
chainId,
request: {
method: "eth_signTransaction",
params: [tx],
},
});
return {
method: "eth_signTransaction",
address,
valid: true,
result,
};
}),
testSignPersonalMessage: _createJsonRpcRequestHandler(async (chainId: string) => {
// test message
const message = `My email is john@doe.com - ${Date.now()}`;
@ -232,6 +259,49 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
result,
};
}),
testEthSign: _createJsonRpcRequestHandler(async (chainId: string) => {
const address = getAddressByChainId(chainId);
// test message
const message = `My email is john@doe.com - ${Date.now()}`;
// encode message (hex)
const hexMsg = encoding.utf8ToHex(message, true);
// eth_sign params
const params = [address, hexMsg];
// send message
const result: string = await client!.request({
topic: session!.topic,
chainId,
request: {
method: "eth_sign",
params,
},
});
// split chainId
const [namespace, reference] = chainId.split(":");
const targetChainData = chainData[namespace][reference];
if (typeof targetChainData === "undefined") {
throw new Error(`Missing chain data for chainId: ${chainId}`);
}
const rpcUrl = targetChainData.rpc[0];
// verify signature
const hash = hashPersonalMessage(message);
const valid = await verifySignature(address, result, hash, rpcUrl);
// format displayed result
return {
method: "eth_sign (standard)",
address,
valid,
result,
};
}),
testSignTypedData: _createJsonRpcRequestHandler(async (chainId: string) => {
// test message
const message = JSON.stringify(eip712.example);