Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add request eip712 signature button #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 99 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function DApp() {
}

function Profile() {
const { address, chainId } = useAccount();
const { address, chainId, connector } = useAccount();
const { disconnect } = useDisconnect();
const { data: balance, refetch } = useBalance({
address,
Expand All @@ -74,6 +74,7 @@ function Profile() {
);
const [value, setValue] = useState<string>("0.01");
const [errorMessage, setErrorMessage] = useState<string>("");
const [eip712Signature, setEip712Signature] = useState("");

if (!address) {
return <div>Loading...</div>;
Expand All @@ -85,9 +86,12 @@ function Profile() {
<div>Balance: {formatUnits(balance.value, balance.decimals)} ETH</div>
)}
<button onClick={() => disconnect()}>Disconnect</button>
<div style={{ display: "flex", justifyContent: "space-between" }}>
<label htmlFor="toAddress" style={{ whiteSpace: "nowrap" }}>
To address:
<div>
<label
htmlFor="toAddress"
style={{ marginRight: "4px", whiteSpace: "nowrap" }}
>
To:
</label>
<input
id="toAddress"
Expand All @@ -105,8 +109,11 @@ function Profile() {
}}
/>
</div>
<div style={{ display: "flex", justifyContent: "space-between" }}>
<label htmlFor="value" style={{ whiteSpace: "nowrap" }}>
<div>
<label
htmlFor="value"
style={{ marginRight: "4px", whiteSpace: "nowrap" }}
>
Value:
</label>
<input
Expand Down Expand Up @@ -138,6 +145,92 @@ function Profile() {
>
Transfer
</button>
{connector && (
<div>
<button
onClick={async () => {
const provider = (await connector?.getProvider()) as {
request(args: { method: string; params: any[] }): any;
};
const signature = await provider.request({
method: "eth_signTypedData_v4",
params: [
address,
{
types: {
EIP712Domain: [
{
name: "name",
type: "string",
},
{
name: "version",
type: "string",
},
{
name: "chainId",
type: "uint256",
},
{
name: "verifyingContract",
type: "address",
},
],
Person: [
{
name: "name",
type: "string",
},
{
name: "wallet",
type: "address",
},
],
Mail: [
{
name: "from",
type: "Person",
},
{
name: "to",
type: "Person",
},
{
name: "contents",
type: "string",
},
],
},
primaryType: "Mail",
domain: {
name: "Ether Mail",
version: `${Math.floor(Math.random() * 100)}`,
chainId,
verifyingContract:
"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
},
message: {
from: {
name: "Cow",
wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
},
to: {
name: "Bob",
wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
},
contents: "Hello, Bob!",
},
},
],
});
setEip712Signature(signature);
}}
>
Request EIP712 signature
</button>
<div>{eip712Signature}</div>
</div>
)}
{errorMessage && (
<div style={{ color: "red", width: "350px" }}>{errorMessage}</div>
)}
Expand Down