forked from AmazingAng/WTF-Solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signERC20Permit.html
140 lines (125 loc) · 5 KB
/
signERC20Permit.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ERC20Permit Signature Example</title>
</head>
<body>
<h1>ERC20Permit Signature Example</h1>
<label for="name">Name:</label>
<input id="name" value="WTFPermit">
<br>
<label for="chainId">Chain ID:</label>
<input id="chainId" value="1">
<br>
<label for="contractAddress">Contract Address:</label>
<input id="contractAddress" value="0xf8e81D47203A594245E36C48e151709F0C19fBe8">
<br>
<label for="spender">Spender:</label>
<input id="spender" value="0x5B38Da6a701c568545dCfcB03FcB875f56beddC4">
<br>
<label for="value">Amount:</label>
<input id="value" value="100">
<br>
<label for="nonce">Nonce:</label>
<input id="nonce" value="0">
<br>
<label for="deadline">Deadline:</label>
<input id="deadline" value="115792089237316195423570985008687907853269984665640564039457584007913129639935">
<br>
<button id="connectButton">Connect MetaMask</button>
<button id="signPermitButton" disabled>Sign ERC20Permit</button>
<br>
<pre id="signatureOutput"></pre>
<h5>钱包地址: <span class="showAccount"></span></h5>
<h5>ChainID: <span class="showChainID"></span></h5>
<h5>ETH 余额: <span class="showETHBalance"></span></h5>
<h5>签名数据: <span class="showSignature"></span></h5>
<h5>v: <span class="SignatureV"></span></h5>
<h5>r: <span class="SignatureR"></span></h5>
<h5>s: <span class="SignatureS"></span></h5>
<script type = "module">
//cdnjs.cloudflare.com/ajax/libs/ethers/6.3.0/ethers.js"; const ethereumButton = document.querySelector('.connect');
const showAccount = document.querySelector('.showAccount');
const showChainID = document.querySelector('.showChainID');
const showETHBalance = document.querySelector('.showETHBalance');
const showSignature = document.querySelector('.showSignature');
const SignatureV = document.querySelector('.SignatureV');
const SignatureR = document.querySelector('.SignatureR');
const SignatureS = document.querySelector('.SignatureS');
const connectButton = document.getElementById("connectButton");
const signPermitButton = document.getElementById("signPermitButton");
let provider;
let signer;
async function connectMetaMask() {
// Obter provedor
const provider = new ethers.BrowserProvider(window.ethereum)
// Ler endereço da carteira
const accounts = await provider.send("eth_requestAccounts", []);
const account = accounts[0]
console.log(`Endereço da carteira: ${account}`)
showAccount.innerHTML = account;
// Ler chainid
const { chainId } = await provider.getNetwork()
console.log(`chainid: ${chainId}`)
showChainID.innerHTML = chainId;
// Ler saldo ETH
const signer = await provider.getSigner()
const balance = await provider.getBalance(signer.getAddress());
console.log(`Saldo Ethereum: ${ethers.formatUnits(balance)}`)
showETHBalance.innerHTML = ethers.formatUnits(balance);
signPermitButton.disabled = false;
}
async function signPermit() {
const name = document.getElementById('name').value;
const version = "1";
const chainId = parseInt(document.getElementById('chainId').value);
const contractAddress = document.getElementById('contractAddress').value;
const spender = document.getElementById('spender').value;
const value = document.getElementById('value').value;
const nonce = document.getElementById('nonce').value;
const deadline = document.getElementById('deadline').value;
const provider = new ethers.BrowserProvider(window.ethereum)
const signer = await provider.getSigner()
const owner = await signer.getAddress();
const domain = {
name: name,
version: version,
chainId: chainId,
verifyingContract: contractAddress,
};
const types = {
Permit: [
{ name: "owner", type: "address" },
{ name: "spender", type: "address" },
{ name: "value", type: "uint256" },
{ name: "nonce", type: "uint256" },
{ name: "deadline", type: "uint256" },
],
};
const message = {
owner: owner,
spender: spender,
value: value,
nonce: nonce,
deadline: deadline,
};
try {
console.log(message)
const signature = await signer.signTypedData(domain, types, message);
const sig = ethers.Signature.from(signature);
console.log("Assinatura:", signature)
SignatureV.innerHTML = `${sig.v}`;
SignatureR.innerHTML = `${sig.r}`;
SignatureS.innerHTML = `${sig.s}`;
showSignature.innerHTML = `${signature}`;
} catch (error) {
console.error("Error signing permit:", error);
}
}
connectButton.addEventListener(`click`, connectMetaMask)
signPermitButton.addEventListener(`click`, signPermit)
</script>
</body>
</html>