forked from interlay/metamask-snap-bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
141 lines (117 loc) · 3.87 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
140
141
<!doctype html>
<html>
</head>
<title>Bitcoin Wallet</title>
</head>
<body>
<h1>Bitcoin Wallet</h1>
<section class="connect">
<button>Connect</button>
</section>
<section class="getAddress">
<button>Get Address</button>
<output></output>
</section>
<section class="signMessage">
<button>Sign Message</button>
<input type="text"/>
<output></output>
</section>
<section class="exportKeyPair">
<button>Export Keys</button>
<output></output>
</section>
<section class="getBlockCount">
<button>Get Block Count</button>
<output></output>
</section>
</body>
<script>
// we identify the Snap by the location of its package.json file
const origin = new URL('package.json', window.location.href).toString();
const snapId = `wallet_plugin_${origin}`;
const connectButton = document.querySelector('.connect').querySelector('button');
const getAddressButton = document.querySelector('.getAddress').querySelector('button');
const getAddressOutput = document.querySelector('.getAddress').querySelector('output');
const signMessageButton = document.querySelector('.signMessage').querySelector('button');
const signMessageInput = document.querySelector('.signMessage').querySelector('input');
const signMessageOutput = document.querySelector('.signMessage').querySelector('output');
const exportKeyPairButton = document.querySelector('.exportKeyPair').querySelector('button');
const exportKeyPairOutput = document.querySelector('.exportKeyPair').querySelector('output');
const getBlockCountButton = document.querySelector('.getBlockCount').querySelector('button');
const getBlockCountOutput = document.querySelector('.getBlockCount').querySelector('output');
connectButton.addEventListener('click', connect);
getAddressButton.addEventListener('click', getAddress);
signMessageButton.addEventListener('click', signMessage);
exportKeyPairButton.addEventListener('click', exportKeyPair);
getBlockCountButton.addEventListener('click', getBlockCount);
// here we get permissions to interact with and install the plugin
async function connect () {
await ethereum.send({
method: 'wallet_enable',
params: [{
[snapId]: {}
}]
})
}
async function getAddress() {
try {
const response = await ethereum.send({
method: snapId,
params: [{
method: 'getAddress'
}]
});
getAddressOutput.innerText = response;
} catch (err) {
console.error(err)
alert('Problem happened: ' + err.message || err)
}
}
async function signMessage() {
try {
const response = await ethereum.send({
method: snapId,
params: [{
method: 'signMessage',
params: {
payload: signMessageInput.value
}
}]
});
signMessageOutput.innerText = response;
} catch (err) {
console.error(err)
alert('Problem happened: ' + err.message || err)
}
}
async function exportKeyPair() {
try {
const response = await ethereum.send({
method: snapId,
params: [{
method: 'exportKeyPair',
}]
});
exportKeyPairOutput.innerText = response;
} catch (err) {
console.error(err)
alert('Problem happened: ' + err.message || err)
}
}
async function getBlockCount() {
try {
const response = await ethereum.send({
method: snapId,
params: [{
method: 'getBlockCount',
}]
});
getBlockCountOutput.innerText = response;
} catch (err) {
console.error(err)
alert('Problem happened: ' + err.message || err)
}
}
</script>
</html>