-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
190 lines (166 loc) · 5.3 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html>
<head>
<title>MetaMask Snap connect example</title>
<link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre.min.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=DM+Sans:[email protected]&display=swap" rel="stylesheet">
<style type="text/css">
body {
background-color:#223;
font-family:'DM Sans', sans-serif;
}
#page {
width:436px;
margin:2em auto;
padding:18px 24px;
border:2px solid #668;
border-radius:8px;
}
.icon {
width:24px;
height:24px;
margin-right:12px;
vertical-align:bottom;
position:relative;
top:-1px;
}
.found {
display:none;
}
#page p {
padding-top:8px;
}
#page p .btn {
float:right;
vertical-align:bottom;
position:relative;
top:-1px;
}
#explanation {
width:412px;
margin:1em auto;
}
#explanation a {
text-decoration:underline;
}
</style>
</head>
<body>
<div id="page" class="text-light">
<h1 class="h3">Detected Wallets</h1>
<div id="loading" class="loading loading-lg">Detecting...</div>
</div>
<div id="explanation" class="text-light">This demonstrates how to cleanly detect MetaMask, MetaMask Flask, or MetaMask Institutional and connect to a Snap by using EIP-6963: Multi Injected Provider Discovery. It avoids issues caused by provider pollution when running multiple wallets in the same browser profile. <a class="text-light" href="https://github.com/Montoya/snap-connect-test#readme">View code on GitHub</a></div>
<script type="text/javascript">
const snapId = 'npm:@consensys/starknet-snap';
const snapName = 'Starknet';
/*
* Use EIP-6963 to detect MetaMask
*/
const MetaMaskFound = async (providerDetail) => {
document.getElementById('loading').className = "found";
const row = document.createElement("p");
const icon = document.createElement("img");
icon.className = "icon";
icon.src = providerDetail.info.icon;
row.appendChild(icon);
const label = document.createElement("span");
label.textContent = providerDetail.info.name;
row.appendChild(label);
const btn = document.createElement("button");
btn.className = "btn btn-primary btn-sm";
btn.textContent = "Connect "+snapName+" Snap";
const provider = providerDetail.provider;
btn.onclick = async (event) => {
event.preventDefault();
try {
const result = await provider.request({
method: 'wallet_requestSnaps',
params:
{
[snapId]: { }
},
});
if(result) {
try {
const snaps = await provider.request({
method: 'wallet_getSnaps',
});
if( Object.keys(snaps).includes(snapId) ) {
// the snap is installed and connected
btn.textContent = "Connected!";
btn.onclick = null;
setTimeout(()=> {
btn.textContent = "Get account deploy fee";
btn.onclick = async (event) => {
const result = await provider.request({
method: 'wallet_invokeSnap',
params:
{
snapId: snapId,
request: {
method: 'starkNet_estimateAccountDeployFee',
params: {
accountIndex: 0
}
}
}
});
alert("Got fee: "+JSON.stringify(result));
};
}, 1000);
}
else {
// the snap was not installed
}
}
catch {
}
}
}
catch {
}
};
row.appendChild(btn);
document.getElementById('page').appendChild(row);
};
window.onload = function() {
window.addEventListener(
"eip6963:announceProvider",
(event) => {
/* event.detail contains the discovered provider interface */
const providerDetail = event.detail;
/*
* You could take one of these cases and use it for your needs,
* or set up a conditional that takes any of these possibilities,
* or prompt the user to choose which MetaMask flavor they want to use
* in case they have multiple MetaMask extensions installed at the same time
*/
if(providerDetail.info.rdns == "io.metamask") {
/* this is MetaMask */
MetaMaskFound(providerDetail);
}
else if(providerDetail.info.rdns == "io.metamask.flask") {
/* this is MetaMask Flask */
MetaMaskFound(providerDetail);
}
else if(providerDetail.info.rdns == "io.metamask.mmi") {
/* this is MetaMask Institutional */
MetaMaskFound(providerDetail);
}
}
);
window.dispatchEvent(new Event("eip6963:requestProvider"));
setTimeout(() => {
if("found"!==document.getElementById('loading').className) {
/* Assume MetaMask was not detected */
document.getElementById('loading').className = "none";
document.getElementById('loading').textContent = "MetaMask was not found.";
}
}, 1000)
}
</script>
</body>
</html>