-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
252 lines (224 loc) · 7.88 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<html>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="name" content="Wallet Connection Example"/>
<title>Wallet Connection Example</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css"/>
<link rel="stylesheet" href="./index.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/js/all.min.js" data-auto-replace-svg="nest"></script>
<body id="body">
<div id="main_content">
<div id="headers">
<h2 class="wallet_options content" style='text-align:center;'>Connect a Wallet</h2>
</div>
<div class="wallet_options content">
<span>Network Provider:</span>
<select id='network_provider'>
<option value="Algonode">Algonode</option>
<option value="AlgoExplorer">AlgoExplorer</option>
</select>
<div id='wallet_list'>
<div ref="MyAlgo">
<span class="icon icon_myalgo"></span>
<span class="wallet_label">MyAlgo Wallet</span>
</div>
<div ref="Pera"><!-- use walletconnect, pera doesn't work in this example -->
<span class="icon icon_pera"></span>
<span class="wallet_label">PeraWallet</span>
</div>
<div ref="Defly">
<span class="icon icon_defly"></span>
<span class="wallet_label">Defly</span>
</div>
<div ref="AlgoSigner">
<span class="icon icon_algosigner"></span>
<span class="wallet_label">AlgoSigner</span>
</div>
<div ref="Wallet">
<span class="icon icon_walletconnect"></span>
<span class="wallet_label">WalletConnect</span>
</div>
</div>
</div>
<div class="accountinfo content">
<br/>
<div class="card_views">
<div>
<div class="wallet_detail">
<div>
<label>Connected Wallet:</label>
<span class="disp_wallet_id"></span>
<i class="fa fa-copy" title="Copy wallet ID to clipboard"></i>
<i class="fa fa-plug-circle-xmark" title="Disconnect Wallet"></i>
</div>
<div>
<label>Wallet Balance:</label>
<span class="disp_wallet_balance"></span>
<i class="fa fa-arrows-rotate" title="Refresh Balances"></i>
</div>
<div>
<label>Wallet Min Balance:</label>
<span class="disp_wallet_minbalance"></span>
</div>
</div>
<br/>
<div class="warning"></div>
</div>
</div>
</div>
<div class="footers">
<div>
This is a single page Javascript, jQuery, and Reach example borrowed from <a href='https://smartpoker.io' target="_new">SmartPoker.io</a> to demonstrate initiating a Wallet connection with Algorand. The repository with additional information can be found here:
<a href='https://github.com/xarmian/smartpoker_wallet_example' target="_new">https://github.com/xarmian/smartpoker_wallet_example</a>
</div>
</div>
</div>
<div class="ring">
<span></span>
</div>
</body>
<script src='https://code.jquery.com/jquery-3.6.0.min.js'></script>
<script src='https://code.jquery.com/ui/1.13.2/jquery-ui.min.js'></script>
<script src='./bundle.js'></script>
<script src="./ALGO_MakeAlgoSignerConnect.js"></script>
<script>
const reachsdk = require('@reach-sh/stdlib');
const providerNetwork = 'TestNet'; // or MainNet
let stdlib = reachsdk.loadStdlib('ALGO');
let acc = null;
let ctc = null;
const getWallet = (wallet) => {
delete window.algorand;
stdlib = reachsdk.loadStdlib('ALGO');
const node = $('#network_provider').val();
let provider = '';
let wf = null;
switch(node) {
case 'AlgoExplorer':
provider = 'randlabs/'+providerNetwork;
break;
default: // Algonode
provider = providerNetwork;
break;
}
switch(wallet) {
case 'PeraConnect':
const MakePeraConnect = reachsdk[`ALGO_MakePeraConnect`];
const PeraConnect = require('@perawallet/connect');
const PeraWalletConnect = PeraConnect['PeraWalletConnect'];
wf = stdlib.walletFallback({providerEnv: provider, WalletConnect: MakePeraConnect(PeraWalletConnect)});
break;
case 'WalletConnect':
case 'DeflyConnect':
const WalletConnect = reachsdk[`ALGO_WalletConnect`];
wf = stdlib.walletFallback({providerEnv: provider, WalletConnect});
break;
case 'MyAlgoConnect':
const MyAlgoConnect = reachsdk[`ALGO_MyAlgoConnect`];
wf = stdlib.walletFallback({providerEnv: provider, MyAlgoConnect});
break;
case 'AlgoSignerConnect':
wf = stdlib.walletFallback({providerEnv: provider, MyAlgoConnect: MakeAlgoSignerConnect(AlgoSigner,provider)});
break;
}
stdlib.setWalletFallback(wf);
console.debug('set wallet fallback: '+wallet);
};
const storeSession = (data) => {
localStorage.setItem('wex_acct',JSON.stringify(data));
localStorage.setItem('wex_provider',JSON.stringify({
id: $('#network_provider').val()
}));
};
const restoreSession = async () => {
const data = localStorage.getItem('wex_acct');
if (!data) {
return false;
}
try {
const networkProvider = JSON.parse(localStorage.getItem('wex_provider'));
if (networkProvider) {
$('#network_provider').val(networkProvider.id);
}
}
catch(err) {}
const dataObj = JSON.parse(data);
getWallet(dataObj.connector);
const addr = dataObj.address;
acc = await stdlib.connectAccount({addr});
}
// main app workflow
const mainFlow = async () => {
console.debug('mainFlow');
$('.warning').text('');
// try to restore wallet session if we don't have an account handle
if (!acc) await restoreSession();
if (acc) {
console.debug('has acc');
const userAddress = stdlib.formatAddress(acc.getAddress());
const bal = await stdlib.balanceOf(acc);
const minbal = stdlib.bigNumberToNumber(await stdlib.minimumBalanceOf(acc));
$('.disp_wallet_id').text(truncateAddr(userAddress)).attr('title',userAddress);
$('.disp_wallet_balance').text(stdlib.formatCurrency(bal)+' ALGO');
$('.disp_wallet_minbalance').text(stdlib.formatCurrency(minbal)+' ALGO');
// show accountinfo view
$('.accountinfo').show().siblings('.content').hide();
$('.accountinfo .card_views > div').eq(0).show().siblings().hide();
$('.wallet_options').hide();
}
else {
// show wallet connect options
$('.wallet_options').show().siblings('.content').hide();
}
$('.ring').hide();
$('#main_content > div').not('.content').show();
};
const truncateAddr = (addr) => {
return addr.substring(0,4)+'...'+addr.substring(addr.length-4);
}
$(function() {
$('#wallet_list > div').on('mousedown',function() {
$(this).addClass('mousedown');
}).on('mouseup',function() {
$(this).removeClass('mousedown');
}).click(async function() {
const connector = $(this).attr('ref')+'Connect';
getWallet(connector);
acc = await stdlib.getDefaultAccount();
const addr = stdlib.formatAddress(acc.getAddress());
console.debug(`connected address: ${addr}`);
storeSession({ address: addr, connector: connector}); // store so we can persist on refresh
mainFlow();
});
$('.wallet_detail .fa-copy').click(function() {
navigator.permissions.query({name: "clipboard-write"}).then((result) => {
if (result.state === "granted" || result.state === "prompt") {
navigator.clipboard.writeText($('.disp_wallet_id').attr('title'));
let toastmsg = $('<div class="toast_msg">Wallet ID Copied to Clipboard</div>')
toastmsg.appendTo('body');
setTimeout(function() {
$('.toast_msg').fadeOut(1000,function() {
$('.toast_msg').remove();
});
},2000);
}
});
});
$('#main_content').on('click','.fa-plug-circle-xmark,.reconnect_wallet_btn',function() {
ctc = null;
acc = null;
delete window.algorand;
stdlib = reachsdk.loadStdlib('ALGO');
localStorage.removeItem('wex_acct');
localStorage.removeItem('walletconnect');
localStorage.removeItem('PeraWallet.Wallet');
mainFlow();
});
$('.fa-arrows-rotate').click(function() {
$('.disp_wallet_balance').text('...');
mainFlow();
});
mainFlow();
});
</script>
</html>