generated from MetaMask/template-snap
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.html
128 lines (114 loc) · 3.75 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
<!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="sendHello">Send Hello</button>
<button class="getAddresses">Get Addresses</button>
<form id="storeAddress">
<fieldset>
<legend>Save an address to your address book</legend>
<label for="nameToStore">Name</label>
<input type="text" id="nameToStore" name="nameToStore"><br>
<label for="addressToStore">Address</label>
<input type="text" id="addressToStore" name="addressToStore"><br>
<input type="submit" id="storeAddress" value="Save">
</fieldset>
</form>
<div>
<p>My address book</p>
<pre id="addressBook"></pre>
</div>
</body>
<script>
const snapId = `local:${window.location.href}`;
const connectButton = document.querySelector('button.connect')
const sendButton = document.querySelector('button.sendHello')
const getButton = document.querySelector('button.getAddresses');
connectButton.addEventListener('click', connect)
sendButton.addEventListener('click', send)
getButton.addEventListener('click',getAddresses);
// here we get permissions to interact with and install the snap
async function connect () {
await ethereum.request({
method: 'wallet_enable',
params: [{
wallet_snap: { [snapId]: {} },
}]
})
getAddresses()
}
// here we call the snap's "hello" method
async function send () {
try {
const response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'hello'
}]
})
} catch (err) {
console.error(err)
alert('Problem happened: ' + err.message || err)
}
}
async function getAddresses () {
let response = [];
try {
response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'retrieveAddresses'
}]
})
} catch (err) {
console.error(err)
alert('Problem happened: ' + err.message || err)
}
document.getElementById('addressBook').textContent = ''+response.map(function(item){
return `${item.name}: ${item.address}`;
}).join("\n");
}
const storeAddressForm = document.getElementById('storeAddress')
storeAddressForm.addEventListener('submit', storeAddress)
async function storeAddress (e) {
e.preventDefault() // to prevent default form behavior
const name = document.getElementById('nameToStore').value
const address = document.getElementById('addressToStore').value
try {
const response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'storeAddress',
nameToStore: name,
addressToStore: address
}]
})
getAddresses()
} catch (err) {
console.error(err)
alert('Problem happened: ' + err.message || err)
}
}
</script>
</html>