feat: adds eth_sign
and eth_signTransaction
examples
This commit is contained in:
parent
f5a0598ef1
commit
f5814d4d1a
@ -81,10 +81,18 @@ export default function App() {
|
|||||||
openRequestModal();
|
openRequestModal();
|
||||||
await ethereumRpc.testSendTransaction(chainId);
|
await ethereumRpc.testSendTransaction(chainId);
|
||||||
};
|
};
|
||||||
|
const onSignTransaction = async (chainId: string) => {
|
||||||
|
openRequestModal();
|
||||||
|
await ethereumRpc.testSignTransaction(chainId);
|
||||||
|
};
|
||||||
const onSignPersonalMessage = async (chainId: string) => {
|
const onSignPersonalMessage = async (chainId: string) => {
|
||||||
openRequestModal();
|
openRequestModal();
|
||||||
await ethereumRpc.testSignPersonalMessage(chainId);
|
await ethereumRpc.testSignPersonalMessage(chainId);
|
||||||
};
|
};
|
||||||
|
const onEthSign = async (chainId: string) => {
|
||||||
|
openRequestModal();
|
||||||
|
await ethereumRpc.testEthSign(chainId);
|
||||||
|
};
|
||||||
const onSignTypedData = async (chainId: string) => {
|
const onSignTypedData = async (chainId: string) => {
|
||||||
openRequestModal();
|
openRequestModal();
|
||||||
await ethereumRpc.testSignTypedData(chainId);
|
await ethereumRpc.testSignTypedData(chainId);
|
||||||
@ -92,7 +100,9 @@ export default function App() {
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
{ method: "eth_sendTransaction", callback: onSendTransaction },
|
{ method: "eth_sendTransaction", callback: onSendTransaction },
|
||||||
|
{ method: "eth_signTransaction", callback: onSignTransaction },
|
||||||
{ method: "personal_sign", callback: onSignPersonalMessage },
|
{ method: "personal_sign", callback: onSignPersonalMessage },
|
||||||
|
{ method: "eth_sign (standard)", callback: onEthSign },
|
||||||
{ method: "eth_signTypedData", callback: onSignTypedData },
|
{ method: "eth_signTypedData", callback: onSignTypedData },
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
@ -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_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"];
|
export const DEFAULT_COSMOS_METHODS = ["cosmos_signDirect", "cosmos_signAmino"];
|
||||||
|
|
||||||
|
@ -34,6 +34,8 @@ interface IContext {
|
|||||||
ping: () => Promise<void>;
|
ping: () => Promise<void>;
|
||||||
ethereumRpc: {
|
ethereumRpc: {
|
||||||
testSendTransaction: (chainId: string) => Promise<void>;
|
testSendTransaction: (chainId: string) => Promise<void>;
|
||||||
|
testSignTransaction: (chainId: string) => Promise<void>;
|
||||||
|
testEthSign: (chainId: string) => Promise<void>;
|
||||||
testSignPersonalMessage: (chainId: string) => Promise<void>;
|
testSignPersonalMessage: (chainId: string) => Promise<void>;
|
||||||
testSignTypedData: (chainId: string) => Promise<void>;
|
testSignTypedData: (chainId: string) => Promise<void>;
|
||||||
};
|
};
|
||||||
@ -187,6 +189,31 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
|
|||||||
result,
|
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) => {
|
testSignPersonalMessage: _createJsonRpcRequestHandler(async (chainId: string) => {
|
||||||
// test message
|
// test message
|
||||||
const message = `My email is john@doe.com - ${Date.now()}`;
|
const message = `My email is john@doe.com - ${Date.now()}`;
|
||||||
@ -232,6 +259,49 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea
|
|||||||
result,
|
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) => {
|
testSignTypedData: _createJsonRpcRequestHandler(async (chainId: string) => {
|
||||||
// test message
|
// test message
|
||||||
const message = JSON.stringify(eip712.example);
|
const message = JSON.stringify(eip712.example);
|
||||||
|
Loading…
Reference in New Issue
Block a user