-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm_sign_custom.html
56 lines (49 loc) · 1.86 KB
/
mm_sign_custom.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MetaMask Message Signing</title>
</head>
<body>
<h1>MetaMask Message Signing</h1>
<button id="connectButton">Connect MetaMask</button>
<button id="signButton" disabled>Sign Message</button>
<p id="account"></p>
<p id="signature"></p>
<script>
if (typeof window.ethereum !== 'undefined') {
console.log('MetaMask is installed!');
} else {
console.error('MetaMask is not installed!');
}
async function connectMetaMask() {
try {
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
const account = accounts[0];
document.getElementById('account').innerText = `Connected account: ${account}`;
document.getElementById('signButton').disabled = false;
} catch (error) {
console.error('Error connecting MetaMask:', error);
}
}
async function signMessage() {
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
const account = accounts[0];
const message = 'By signing this message I state that I read and agree with terms of Lumia Node Sale... bla-bla-bla';
const messageHex = '0x' + Array.from(message).map(c => c.charCodeAt(0).toString(16)).join('');
try {
const signature = await ethereum.request({
method: 'personal_sign',
params: [messageHex, account]
});
document.getElementById('signature').innerText = `Signature: ${signature}`;
} catch (error) {
console.error('Error signing message:', error);
}
}
document.getElementById('connectButton').addEventListener('click', connectMetaMask);
document.getElementById('signButton').addEventListener('click', signMessage);
</script>
</body>
</html>