generated from MetaMask/template-snap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
139 lines (122 loc) · 3.64 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Hello, Snaps!</title>
<link rel="icon" type="image/svg" href="./images/icon.svg" />
</head>
<body>
<h1>Hello, Snaps!</h1>
<details>
<summary>Instructions</summary>
<ul>
<li>First, click "Connect". Then, try out the other buttons!</li>
<li>Please note that:</li>
<ul>
<li>
The <code>snap.manifest.json</code> and
<code>package.json</code> must be located in the server root
directory..
</li>
<li>
The Snap bundle must be hosted at the location specified by the
<code>location</code> field of <code>snap.manifest.json</code>.
</li>
</ul>
</ul>
</details>
<br />
<button class="connect">Connect</button>
<button class="sendPing">Send Ping</button>
<form id="storeSeed">
<fieldset>
<label for="seed">Seed:</label>
<input type="text" id="seed" name="seed" /><br />
<input type="submit" id="storeSeed" value="Save" />
</fieldset>
</form>
<form id="signMessage">
<fieldset>
<label for="message">Message:</label>
<input type="text" id="message" name="message" /><br />
<input type="submit" id="signMessage" value="Save" />
</fieldset>
</form>
</body>
<script>
const snapId = `local:${window.location.href}`;
const connectButton = document.querySelector('button.connect');
const sendButton = document.querySelector('button.sendPing');
connectButton.addEventListener('click', connect);
sendButton.addEventListener('click', sendPing);
// here we get permissions to interact with and install the snap
async function connect() {
await ethereum.request({
method: 'wallet_enable',
params: [
{
wallet_snap: { [snapId]: {} },
},
],
});
}
// here we call the snap's "ping" method
async function sendPing() {
try {
const response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [
snapId,
{
method: 'ping',
},
],
});
} catch (err) {
console.error(err);
alert('Problem happened: ' + err.message || err);
}
}
const storeSeedForm = document.getElementById('storeSeed');
storeSeedForm.addEventListener('submit', storeSeed);
async function storeSeed(e) {
e.preventDefault(); // to prevent default form behavior
const seed = document.getElementById('seed').value;
try {
const response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [
snapId,
{
method: 'storeSeed',
seed: seed,
},
],
});
} catch (err) {
console.error(err);
alert('Problem happened: ' + err.message || err);
}
}
const signMessageForm = document.getElementById('signMessage');
signMessageForm.addEventListener('submit', signMessage);
async function signMessage(e) {
e.preventDefault(); // to prevent default form behavior
const message = document.getElementById('message').value;
try {
const response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [
snapId,
{
method: 'signMessage',
message: message,
},
],
});
} catch (err) {
console.error(err);
alert('Problem happened: ' + err.message || err);
}
}
</script>
</html>