-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcap.html
More file actions
129 lines (119 loc) · 5.23 KB
/
cap.html
File metadata and controls
129 lines (119 loc) · 5.23 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sign Typed Data v4</title>
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/eth-sig-util@2.5.2/dist/sigUtil.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/ethereumjs-util@7.1.3/dist/ethereumjs-util.min.js"></script>
</head>
<body>
<h3>Connect Wallet</h3>
<button type="button" id="connectWalletButton">Connect Wallet</button>
<h3>Sign typed data v4</h3>
<button type="button" id="signTypedDataV4Button" disabled>eth_signTypedData_v4</button>
<script>
let web3;
let userAccount;
document.getElementById('connectWalletButton').addEventListener('click', async function () {
if (typeof window.ethereum !== 'undefined' || (typeof window.web3 !== 'undefined')) {
const provider = window['ethereum'] || window.web3.currentProvider;
web3 = new Web3(provider);
try {
const accounts = await provider.request({ method: 'eth_requestAccounts' });
userAccount = accounts[0];
document.getElementById('signTypedDataV4Button').disabled = false;
alert('Wallet connected: ' + userAccount);
} catch (error) {
console.error('User denied account access', error);
alert('Failed to connect wallet');
}
} else {
alert('MetaMask is not installed. Please install it to use this feature.');
}
});
document.getElementById('signTypedDataV4Button').addEventListener('click', async function (event) {
event.preventDefault();
const msgParams = JSON.stringify({
domain: {
chainId: 1,
name: "Ether Mail",
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
version: "1",
},
message: {
contents: "Hello, Bob!",
attachedMoneyInEth: 4.2,
from: {
name: "Cow",
wallets: [
"0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
"0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF",
],
},
to: [
{
name: "hello",
wallets: [
"0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
"0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57",
"0xB0B0b0b0b0b0B000000000000000000000000000",
],
},
],
},
primaryType: "Mail",
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
],
Group: [
{ name: "name", type: "string" },
{ name: "members", type: "Person[]" },
],
Mail: [
{ name: "from", type: "Person" },
{ name: "to", type: "Person[]" },
{ name: "contents", type: "string" },
],
Person: [
{ name: "name", type: "string" },
{ name: "wallets", type: "address[]" },
],
},
});
try {
const params = [userAccount, msgParams];
const method = "personal_sign";
web3.currentProvider.sendAsync({
method,
params,
from: userAccount,
}, function (err, result) {
if (err) return console.error(err);
if (result.error) {
alert(result.error.message);
return console.error(result.error);
}
console.log("TYPED SIGNED:" + JSON.stringify(result.result));
const recovered = sigUtil.recoverTypedSignature_v4({
data: JSON.parse(msgParams),
sig: result.result,
});
if (ethereumjs.Util.toChecksumAddress(recovered) === ethereumjs.Util.toChecksumAddress(userAccount)) {
alert("Successfully recovered signer as " + userAccount);
} else {
alert("Failed to verify signer when comparing " + result + " to " + userAccount);
}
});
} catch (error) {
console.error(error);
}
});
</script>
</body>
</html>