This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathpayment.html
470 lines (426 loc) · 15.5 KB
/
payment.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>AlgoSigner dApp | Payment</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
<link rel="shortcut icon" href="assets/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/tomorrow.min.css">
<script src="//cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js"></script>
<link rel="stylesheet" type="text/css" href="assets/style.css">
</head>
<body>
<nav class="navbar" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<a class="navbar-item" href="index.html"><img src="assets/logotype.png" height="28"></a>
</div>
<div class="navbar-menu">
<div class="navbar-start">
<a class="navbar-item" href="payment.html"><u>Payment Txn</u></a>
<a class="navbar-item" href="asset-create.html">Asset Create Txn</a>
<a class="navbar-item" href="asset-optin.html">Asset Opt-in Txn</a>
<a class="navbar-item" href="arcTransitionGuide.html">ARC Transition Guide</a>
</div>
<div class="navbar-end">
<a class="navbar-item" target="_blank" rel="noopener noreferrer" href="https://github.com/PureStake/algosigner/blob/develop/docs/dApp-integration.md">
Documentation
</a>
</div>
</div>
</nav>
<!-- SDKSETUP -->
<section class="section">
<div class="container">
<h1 class="title">
Clients Setup & Usage
<a class="button is-small is-pulled-right" target="_blank" rel="noopener noreferrer"
href="https://github.com/PureStake/algosigner-dapp-example/blob/master/payment.html#L346-L365">
<span class="icon is-small">
<i class="fab fa-github"></i>
</span>
<span>See code on GitHub</span>
</a>
</h1>
<p class="subtitle">
Setup Algod client and fetch transactions parameters
</p>
<div class="columns">
<div class="column">
<pre><code class="js">
const algodServer = 'https://testnet-algorand.api.purestake.io/ps2';
const token = { 'X-API-Key': 'YOUR API KEY HERE' };
const port = '';
algodClient = new algosdk.Algodv2(token, algodServer, port);
algodClient.getTransactionParams().do()
.then(d => {
console.log(d);
txnParams = d;
})
.catch(e => {
console.error(e);
});
</code></pre>
<button class="button is-primary is-fullwidth" id="sdksetup">Run code</button>
</div>
<div class="column">
<pre><code class="JSON" id="sdk-setup">code snippet output</code></pre>
</div>
</div>
</div>
</section>
<!-- /SDKSETUP -->
<!-- ENABLE -->
<section class="section">
<div class="container">
<h1 class="title">
Enable account discovery
<a class="button is-small is-pulled-right" target="_blank" rel="noopener noreferrer" href="https://github.com/PureStake/algosigner-dapp-example/blob/master/payment.html#L316-L344">
<span class="icon is-small">
<i class="fab fa-github"></i>
</span>
<span>See code on GitHub</span>
</a>
</h1>
<p class="subtitle">
Request from the user which accounts will be shared with the dApp
</p>
<div class="columns">
<div class="column">
<pre><code class="js">
<b>algorand</b>.enable({ genesisID: txnParams.genesisID })
.then((d) => {
console.log(d);
sharedAccounts = d.accounts;
})
.catch((e) => {
console.error(e);
});
</code></pre>
<button class="button is-primary is-fullwidth" id="enable">Run code</button>
</div>
<div class="column">
<pre><code class="JSON" id="enable-code">code snippet output</code></pre>
</div>
</div>
</div>
</section>
<!-- /ENABLE -->
<!-- SIGN -->
<section class="section">
<div class="container">
<h1 class="title">
Sign Payment Transaction
<a class="button is-small is-pulled-right" target="_blank" rel="noopener noreferrer"
href="https://github.com/PureStake/algosigner-dapp-example/blob/master/payment.html#L367-L404">
<span class="icon is-small">
<i class="fab fa-github"></i>
</span>
<span>See code on GitHub</span>
</a>
</h1>
<p class="subtitle">
Ask the user to sign a payment transaction using the <code>signTxns</code> method
</p>
<div class="columns">
<div class="column">
<pre><code class="js">
let unsignedTxn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: document.getElementById('from').value,
to: document.getElementById('to').value,
amount: +document.getElementById('amount').value,
note: <b>algorand.encoding</b>.stringToByteArray(document.getElementById('note').value),
suggestedParams: {...txnParams}
});
console.log(unsignedTxn._getDictForDisplay());
// Use the provided library to make the transaction base64-encoded
let b64EncodedTxn = <b>algorand.encoding</b>.msgpackToBase64(unsignedTxn.toByte());
<b>algorand.signTxns</b>([{txn: b64EncodedTxn}])
.then((d) => {
console.log(d);
signedTxns = d;
})
.catch((e) => {
console.error(e);
});
</code></pre>
<button class="button is-primary is-fullwidth" id="sign">Run code</button>
</div>
<div class="column">
<div class="field">
<div class="control has-icons-left">
<div class="select is-fullwidth">
<select id="from">
<option>Request accounts first</option>
</select>
</div>
<div class="icon is-small is-left">
<i class="fas fa-wallet"></i>
</div>
</div>
</div>
<div class="field">
<p class="control has-icons-left">
<input class="input" id="to" placeholder="To">
<span class="icon is-small is-left">
<i class="fas fa-share"></i>
</span>
</p>
</div>
<div class="field">
<p class="control has-icons-left">
<input class="input" id="amount" type="number" placeholder="Amount (µAlgos)">
<span class="icon is-small is-left">
<i class="fas fa-coins"></i>
</span>
</p>
</div>
<div class="field">
<p class="control has-icons-left">
<input class="input" id="note" placeholder="Note">
<span class="icon is-small is-left">
<i class="fas fa-sticky-note"></i>
</span>
</p>
</div>
<pre><code class="JSON" id="sign-code">code snippet output</code></pre>
</div>
</div>
</div>
</section>
<!-- /SIGN -->
<!-- POST -->
<section class="section">
<div class="container">
<h1 class="title">
Post Transaction to Network
<a class="button is-small is-pulled-right" target="_blank" rel="noopener noreferrer"
href="https://github.com/PureStake/algosigner-dapp-example/blob/master/payment.html#L406-L421">
<span class="icon is-small">
<i class="fab fa-github"></i>
</span>
<span>See code on GitHub</span>
</a>
</h1>
<p class="subtitle">
Post the previously signed transaction to the network
</p>
<div class="columns">
<div class="column">
<pre><code class="js">
console.log('Waiting for network confirmation...');
<b>algorand.postTxns</b>(signedTxns)
.then((d) => {
console.log(d);
})
.catch((e) => {
console.error(e);
});
</code></pre>
<button class="button is-primary is-fullwidth" id="post">Run code</button>
</div>
<div class="column">
<pre><code class="JSON" id="post-code">code snippet output</code></pre>
</div>
</div>
</div>
</section>
<!-- /POST -->
<!-- SIGNPOST -->
<section class="section">
<div class="container">
<h1 class="title">
Sign and Post transactions
<a class="button is-small is-pulled-right" target="_blank" rel="noopener noreferrer"
href="https://github.com/PureStake/algosigner-dapp-example/blob/master/payment.html#L423-L459">
<span class="icon is-small">
<i class="fab fa-github"></i>
</span>
<span>See code on GitHub</span>
</a>
</h1>
<p class="subtitle">
Request AlgoSigner to post the transaction to the network as soon as it's signed
</p>
<div class="columns">
<div class="column">
<pre><code class="js">
let unsignedTxn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: document.getElementById('from').value,
to: document.getElementById('to').value,
amount: +document.getElementById('amount').value,
note: <b>algorand.encoding</b>.stringToByteArray('sent without returning'),
suggestedParams: {...txnParams}
});
console.log(unsignedTxn._getDictForDisplay());
// Use the provided library to make the transaction base64-encoded
let b64EncodedTxn = <b>algorand.encoding</b>.msgpackToBase64(unsignedTxn.toByte());
<b>algorand.signAndPostTxns</b>([{txn: b64EncodedTxn}])
.then((d) => {
console.log(d);
})
.catch((e) => {
console.error(e);
});
</code></pre>
<button class="button is-primary is-fullwidth" id="signpost">Run code</button>
</div>
<div class="column">
<pre><code class="JSON" id="signpost-code">code snippet output</code></pre>
</div>
</div>
</div>
</section>
<!-- /SIGNPOST -->
<footer class="footer">
<div class="content has-text-centered">
by <a target="_blank" rel="noopener noreferrer" href="https://purestake.com">
<img src="assets/ps-logo.png" height="28">
</a>
</div>
</footer>
<script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"></script>
<script>
hljs.initHighlightingOnLoad();
let algodClient;
let txnParams;
let sharedAccounts;
let unsignedTxn;
let signedTxns;
let b64EncodedTxn;
function enable() {
let enableCodeElem = document.getElementById('enable-code');
if (txnParams === undefined) {
enableCodeElem.innerHTML = 'Please run the client setup code first.';
} else {
algorand.enable({ genesisID: txnParams.genesisID })
.then((d) => {
enableCodeElem.innerHTML = JSON.stringify(d, null, 2);
sharedAccounts = d.accounts;
// Append accounts to account select
let select = document.getElementById('from');
select.textContent = '';
for (const address of sharedAccounts) {
let option = document.createElement('option');
option.text = address;
option.value = address;
select.appendChild(option);
}
})
.catch((e) => {
console.error(e);
enableCodeElem.innerHTML = JSON.stringify(e, null, 2);
})
.finally(() => {
hljs.highlightBlock(enableCodeElem);
});
}
}
function sdkSetup() {
let sdkSetupCodeElem = document.getElementById('sdk-setup');
const algodServer = 'https://testnet-algorand.api.purestake.io/ps2';
const token = { 'X-API-Key': 'B3SU4KcVKi94Jap2VXkK83xx38bsv95K5UZm2lab' }
const port = '';
algodClient = new algosdk.Algodv2(token, algodServer, port);
// Fetch params
algodClient.getTransactionParams().do()
.then(d => {
txnParams = d;
sdkSetupCodeElem.innerHTML = JSON.stringify(d, null, 2);
})
.catch(e => { sdkSetupCodeElem.innerHTML = JSON.stringify(e, null, 2); })
.finally(() => {
hljs.highlightBlock(sdkSetupCodeElem);
});
}
function sign() {
let signCodeElem = document.getElementById('sign-code');
let from = document.getElementById('from').value;
let to = document.getElementById('to').value;
if (!from || !to) {
signCodeElem.innerHTML = 'Please fill the "from" and "to" fields.';
} else {
unsignedTxn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: from,
to: to,
amount: +document.getElementById('amount').value,
note: algorand.encoding.stringToByteArray(document.getElementById('note').value),
suggestedParams: {...txnParams}
});
let b64EncodedTxn = algorand.encoding.msgpackToBase64(unsignedTxn.toByte());
const readableTxn = {
...unsignedTxn._getDictForDisplay(),
note: unsignedTxn.note ? algorand.encoding.byteArrayToString(unsignedTxn.note) : null,
};
signCodeElem.innerHTML = JSON.stringify(readableTxn, null, 2);
algorand.signTxns([{txn: b64EncodedTxn}])
.then((d) => {
signedTxns = d;
signCodeElem.innerHTML = JSON.stringify(d, null, 2);
})
.catch((e) => {
console.error(e);
signCodeElem.innerHTML = JSON.stringify(e, null, 2);
})
.finally(() => {
hljs.highlightBlock(signCodeElem);
});
}
}
function post() {
let postCodeElem = document.getElementById('post-code');
postCodeElem.innerHTML = 'Waiting for network confirmation...';
algorand.postTxns(signedTxns)
.then((d) => {
postCodeElem.innerHTML = JSON.stringify(d, null, 2);
})
.catch((e) => {
console.error(e);
postCodeElem.innerHTML = JSON.stringify(e, null, 2);
})
.finally(() => {
hljs.highlightBlock(postCodeElem);
});
}
function signpost() {
let signpostCodeElem = document.getElementById('signpost-code');
let from = document.getElementById('from').value;
let to = document.getElementById('to').value;
if (!from || !to) {
signpostCodeElem.innerHTML = 'Please fill the "from" and "to" fields in the "sign" section.';
} else {
unsignedTxn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: from,
to: to,
amount: +document.getElementById('amount').value,
note: algorand.encoding.stringToByteArray('sent without returning'),
suggestedParams: {...txnParams}
});
let b64EncodedTxn = algorand.encoding.msgpackToBase64(unsignedTxn.toByte());
const readableTxn = {
...unsignedTxn._getDictForDisplay(),
note: unsignedTxn.note ? algorand.encoding.byteArrayToString(unsignedTxn.note) : null,
};
signpostCodeElem.innerHTML = JSON.stringify(readableTxn, null, 2);
algorand.signAndPostTxns([{txn: b64EncodedTxn}])
.then((d) => {
signpostCodeElem.innerHTML = JSON.stringify(d, null, 2);
})
.catch((e) => {
console.error(e);
signpostCodeElem.innerHTML = JSON.stringify(e, null, 2);
})
.finally(() => {
hljs.highlightBlock(signpostCodeElem);
});
}
}
document.getElementById('enable').addEventListener('click', enable);
document.getElementById('sdksetup').addEventListener('click', sdkSetup);
document.getElementById('sign').addEventListener('click', sign);
document.getElementById('post').addEventListener('click', post);
document.getElementById('signpost').addEventListener('click', signpost);
</script>
<script src="https://unpkg.com/[email protected]"></script>
</body>
</html>