-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.html
836 lines (805 loc) · 41 KB
/
login.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
<html>
<head>
<title>WeaveID</title>
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@tailwindcss/ui@latest/dist/tailwind-ui.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.1/css/all.min.css" />
<link href="https://fonts.googleapis.com/css?family=Chivo&subset=latin" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="/img/favicon.png" type="image/x-icon" />
<link rel="icon" href="/img/favicon.png" type="image/x-icon" />
</head>
<body>
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="css/animate.min.css" />
<script src="https://unpkg.com/penpal/dist/penpal.min.js"></script>
<script type="module" src="js/bundle.js"></script>
<script src="/js/pem2jwk.bundle.js"></script>
<script src="https://unpkg.com/arweave/bundles/web.bundle.min.js"></script>
<script src="/js/ethereum-wallets.js"></script>
<!-- CommunityXYZ.js -->
<script src="https://arweave.net/6WvCKxIxCejO505AzeNkuw6NGgE0ySo5lDNN42J2TX0"></script>
<div id="app">
<!-- Main Template begins -->
<div class="min-h-screen flex items-center justify-center py-2 sm:py-12 px-4 sm:px-6 lg:px-8">
<!-- CONFIRM TRANSACTION -->
<div v-if="confirmAmount && confirmAmount > 0" id="confirm-view" class="w-full md:w-1/3">
<img src="/img/transfer2.svg" class="mx-auto h-24 mt-8 mb-4" />
<div class="prompt mt-8 mb-6">Are you sure you want to send <span v-if="confirmAmount" id="confirm-amount">{{ confirmAmount }} AR</span> to <span v-if="address" id="confirm-address">{{ address }}</span>?</div>
<div class="receipt">
<div class="flex row">
<div class="w-2/3">Amount</div>
<div class="w-1/3 text-right">${{ confirmAmountUSD }}</div>
</div>
<div class="flex row mt-2 mb-4">
<div class="w-2/3">Est. Network Fee</div>
<div class="w-1/3 text-right">${{ confirmAmountFeeUSD }}</div>
</div>
<div class="flex row mb-4">
<div class="w-2/3">Total</div>
<div class="w-1/3 text-right">${{ confirmAmountTotalUSD }}</div>
</div>
</div>
<button id="confirm-button" class="wbutton full mb-2">
Confirm
</button>
<button @click="closeAll()" class="wbutton outline full">
Cancel
</button>
</div>
<div v-else-if="screen == 'send'" id="send-view" class="w-full md:w-1/3">
SEND
</div>
<!-- SUCCESS -->
<div id="success" class="w-full md:w-1/3">
<img src="/img/success.svg" class="mx-auto h-24 mb-4" />
<h2 class="text-3xl font-bold text-center text-green-500 mb-6">Success!</h2>
</div>
<!-- RESULTS -->
<div v-if="!confirmAmount && loggedIn && screen == 'results'" id="results" class="w-full md:w-1/3">
<div class="header-row row">
<img src="/img/favicon.png" class="left-logo" />
<!-- <h3 class="font-bold text-center wallet-title">Wallet</h3> -->
<div id="wallet-text">
<!-- <i class="fas fa-user user-icon"></i> -->
<div id="public-key" class="">{{ address }}</div>
<a href="#" @click="copyToClipboard(address)" class="copy-link" title="Copy address to clipboard" alt="Copy address to clipboard"><i class="far fa-copy copy-icon"></i></a>
</div>
</div>
<div id="balance-holder" class="mt-20 text-center">
<div class="subtitle">Balance</div>
<span id="balance">{{ balanceFixed }}</span> <span class="units">AR</span>
<div class="subtitle">
<div class="inline-block" id="usd-value">${{ balanceUSD }} USD</div>
<div v-if="priceChange" :class="priceChange > 0 ? 'inline-block green' : 'inline-block red'" id="percentage-container" alt="24h Price Change" title="24h Price Change">
<i :class="priceChange > 0 ? 'fas fa-caret-up' : 'fas fa-caret-down'" id="price-arrow"></i> <span id="percentage">{{priceChange > 0 ? '+' : ''}}{{priceChange}}%</span>
</div>
</div>
</div>
<br />
<div class="row">
<button @click="showSend()" class="wbutton mr-1"><i class="fas fa-paper-plane mr-2"></i> Send</button>
<button class="wbutton outline" @click="closeAll()"><i class="fas fa-qrcode mr-2"></i> Receive</button>
</div>
<p class="subtitle mt-8 mb-4">Activity</p>
<div class="tx-holder w-full">
<!-- TX's Zero State -->
<div v-if="!transactions || transactions.length === 0" class="tx-zero-state">
<center><div id="usd-value">NO RECENT ACTIVITY</div></center>
</div>
<!-- TX -->
<div class="tx flex py-3" v-for="tx in transactions">
<div class="six-pct mr-4">
<i v-if="tx.type == 'send'" class="fas fa-share receive-icon"></i>
<svg v-else class="receive-icon" version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 285 285" style="enable-background: new 0 0 285 285;" xml:space="preserve">
<g>
<path
style="fill: #231f20;"
d="M217.256,157.998l-14.143-14.143c-1.406-1.407-3.314-2.197-5.303-2.197 c-1.989,0-3.897,0.79-5.303,2.197L160,176.362V7.5c0-4.142-3.358-7.5-7.5-7.5h-20c-4.142,0-7.5,3.358-7.5,7.5v168.862 l-32.507-32.507c-1.406-1.407-3.314-2.197-5.303-2.197c-1.989,0-3.897,0.79-5.303,2.197l-14.143,14.143 c-1.407,1.406-2.197,3.314-2.197,5.303c0,1.989,0.79,3.897,2.197,5.303l69.453,69.452c1.464,1.464,3.384,2.197,5.303,2.197 s3.839-0.732,5.303-2.197l69.453-69.452c1.407-1.406,2.197-3.314,2.197-5.303C219.453,161.311,218.663,159.404,217.256,157.998z"
/>
<path style="fill: #231f20;" d="M197.024,250H87.976c-4.142,0-7.5,3.358-7.5,7.5v20c0,4.142,3.358,7.5,7.5,7.5h109.048 c4.142,0,7.5-3.358,7.5-7.5v-20C204.524,253.358,201.166,250,197.024,250z" />
</g>
<g></g>
<g></g>
<g></g>
<g></g>
<g></g>
<g></g>
<g></g>
<g></g>
<g></g>
<g></g>
<g></g>
<g></g>
<g></g>
<g></g>
<g></g>
</svg>
</div>
<div class="w-1/2 tx-details">
<span v-if="tx.type == 'send'">
<p class="font-bold">Sent</p>
<p class="semigray">To: {{ tx.target }}</p>
</span>
<span v-else-if="tx.type == 'receive'">
<p class="font-bold">Received</p>
<p class="semigray">To: {{ tx.target }}</p>
</span>
</div>
<div class="w-1/2 tx-amount">
<p :class="tx.type == 'send' ? 'red' : 'green'">{{ tx.type == 'send' ? '-' : '+' }}{{ parseFloat(arweave.ar.winstonToAr(tx.quantity)).toFixed(4) }} AR</p>
</div>
</div>
</div>
</div>
<!-- LOGIN VIEW -->
<div v-if="!loggedIn" class="max-w-md w-full" id="login-view">
<div>
<img class="mx-auto h-24 w-auto" src="img/favicon.png" alt="Workflow" id="logo" />
<h2 class="mt-6 text-center text-3xl leading-9 font-extrabold text-gray-900">
Sign in to your WeaveID
</h2>
<p class="mt-2 text-center text-sm leading-5 text-gray-600">Login with any email/password.<br />If you don't have a wallet yet, we'll create one for you!</p>
</div>
<!-- LOADER -->
<div v-if="loggingIn" id="loader">
<img src="img/pulse.gif" />
</div>
<!-- LOGIN FORM -->
<div v-if="!loggingIn" class="mt-8" id="login-form">
<!-- ERRORS -->
<p v-if="error" class="mb-4 text-center text-red-500" id="error">{{ error }}</p>
<!-- FORM -->
<div class="rounded-md shadow-sm">
<div>
<input
v-model="email"
aria-label="Email address"
v-on:keyup.enter="generateKey()"
id="email"
name="email"
type="email"
required
class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-t-md focus:outline-none focus:shadow-outline-blue focus:border-blue-300 focus:z-10 sm:text-sm sm:leading-5"
placeholder="Email address"
/>
</div>
<div class="-mt-px">
<input
v-model="password"
aria-label="Password"
v-on:keyup.enter="generateKey()"
id="password"
name="password"
type="password"
required
class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-b-md focus:outline-none focus:shadow-outline-blue focus:border-blue-300 focus:z-10 sm:text-sm sm:leading-5"
placeholder="Password"
/>
</div>
</div>
<div class="mt-6 flex items-center justify-between">
<div class="flex items-center">
<input v-model="rememberMe" id="remember" type="checkbox" class="form-checkbox h-4 w-4 text-indigo-600 transition duration-150 ease-in-out" />
<label for="remember" class="ml-2 block text-sm leading-5 text-gray-900">
Remember me
</label>
</div>
<div class="text-sm leading-5">
<a href="https://weaveid.io" target="_blank" class="font-medium text-indigo-600 hover:text-indigo-500 focus:outline-none focus:underline transition ease-in-out duration-150">
Learn more
</a>
</div>
</div>
<div class="mt-6">
<button
@click="generateKey()"
class="group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm leading-5 font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-500 focus:outline-none focus:border-indigo-700 focus:shadow-outline-indigo active:bg-indigo-700 transition duration-150 ease-in-out"
>
<span class="absolute left-0 inset-y-0 flex items-center pl-3">
<svg class="h-5 w-5 text-indigo-500 group-hover:text-indigo-400 transition ease-in-out duration-150" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M5 9V7a5 5 0 0110 0v2a2 2 0 012 2v5a2 2 0 01-2 2H5a2 2 0 01-2-2v-5a2 2 0 012-2zm8-2v2H7V7a3 3 0 016 0z" clip-rule="evenodd" />
</svg>
</span>
Sign in
</button>
<div class="mt-4 text-center">
<div class="mb-2 text-sm text-gray-600">- OR -</div>
<div class="flex">
<input type="file" @change="setFiles" tabindex="-1" style="width: 100%;" accept="application/JSON" />
<button @click="importWallet" class="wbutton" style="height: unset; padding: unset; width: 30%;">Import</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- End of #app -->
<!-- <script src="https://unpkg.com/popper.js@1/dist/umd/popper.min.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.js"></script>
<!-- <script src="https://webmaker.app/app/lib/transpilers/babel-polyfill.min.js"></script> -->
<script>
var v = new Vue({
el: "#app",
data: function data() {
return {
email: "",
password: "",
fee: 0.001,
key_object: null,
promise_key: null,
encrypted_data: null,
encrypt_promise: null,
vector: 0,
decrypt_promise: null,
decrypted_data: null,
jwk: null,
arweave: null,
address: null,
balance: null,
iframeConnection: null,
transaction: null,
ar: null,
successLoaderFinished: false,
loggingIn: false,
error: "",
confirmAmount: 0,
rememberMe: false,
files: null,
screen: "results",
transactions: []
};
},
mounted: function mounted() {
// INIT:
this.vector = window.crypto.getRandomValues(new Uint8Array(16));
this.connectToParent();
var savedKey = localStorage.getItem("weaveid-wallet-pk");
if (savedKey) {
this.jwk = JSON.parse(savedKey);
this.gotWallet(this.jwk);
//showResults(JSON.parse(savedKey));
}
},
methods: {
returnAddy: function () {},
mainLogin: function (jwk) {
this.arweave.wallets.jwkToAddress(jwk).then((addy) => {
this.address = addy;
this.returnAddy(addy);
console.log("Address = ", this.address);
// Fetch balance
this.arweave.wallets.getBalance(this.address).then((bal) => {
this.balance = this.arweave.ar.winstonToAr(bal);
console.log("Balance = ", this.balance);
// Fetch USD price
fetch("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=250&page=1&sparkline=false")
.then((resp) => resp.json())
.then((data) => {
this.ar = data.filter((coin) => coin.id === "arweave");
if (this.ar && this.ar.length > 0) {
this.ar = this.ar[0];
console.log("Arweave USD Price = $", this.ar);
} else {
this.ar = 0;
console.log("Unable to fetch USD prices.");
}
this.animateCSS("#login-view", "fadeOut").then((msg) => {
document.getElementById("login-view").style.display = "none";
// Show success icon:
this.animateCSS("#success", "fadeIn").then((msg) => {
// Show results page:
this.animateCSS("#success", "fadeOut").then((msg) => {
this.successLoaderFinished = true;
// Expose Arweave methods (by overwriting):
if (this.iframeConnection) this.iframeConnection.overwriteArweaveMethods();
// Fetch transactions:
this.arweave
.arql({
op: "or",
expr1: {
op: "equals",
expr1: "from",
expr2: this.address
},
expr2: {
op: "equals",
expr1: "to",
expr2: this.address
}
})
.then((txids) => {
console.log("Your txs = ", txids);
for (var i in txids) {
this.arweave.transactions.get(txids[i]).then((data) => {
this.transactions.push({
id: txids[i],
quantity: data.quantity,
target: data.target,
type: data.target.toLowerCase() === this.address.toLowerCase() ? "receive" : "send"
});
});
}
});
});
});
});
});
});
});
},
generateKey: function () {
if (!this.email || this.email.trim() === "" || !this.password || this.password.trim() === "") {
this.error = "Please enter an email and password.";
} else {
this.loggingIn = true;
// Check wallet with ArweaveJS:
this.arweave = Arweave.init({
host: "arweave.net", // Hostname or IP address for a Arweave host
port: 443, // Port
protocol: "https", // Network protocol http or https
timeout: 20000, // Network request timeouts in milliseconds
logging: false // Enable network request logging
});
// Encrypt the user's email before we store it on the blockchain:
this.sha256(this.email).then((encryptedEmail) => {
encryptedEmail = btoa(encryptedEmail);
// Logging in :
this.arweave
.arql({
op: "and",
expr1: {
op: "equals",
expr1: "from",
expr2: "W46vSpKFlVnYyEzCn39bcwGzHslfucPZhcBsmed5uQU"
},
expr2: {
op: "equals",
expr1: "email",
expr2: encryptedEmail
}
})
.then((txids) => {
// --> If user already has a wallet, pull the encrypted JWK hash and decrypt it using decrypt_data()
console.log("txs = ", txids);
//exit;
if (txids && txids.length > 0) {
console.log("User already has wallet!");
// Get the data decode as string data
this.arweave.transactions.getData(txids[0], { decode: true, string: true }).then((data) => {
console.log(data);
data = atob(data);
data = data.split(",");
data = Uint8Array.from(data);
console.log("Encrypted wallet = ", data, this.password);
// <-- Do we need to JSON parse data here ?
this.encryptOrDecrypt("decrypt", data, this.password).then((decrypted) => {
var wallet = this.convertArrayBuffertoString(decrypted);
console.log("Decrypted wallet!!! = ", wallet); //decrypted);
// Hack to cut off weird characters added at beginning:
if (!this.isJson(wallet)) {
let temp = wallet.split('{"kty"');
temp = '{"kty"' + temp[1];
wallet = temp;
}
wallet = JSON.parse(wallet);
console.log("Decrypted wallet!!! = ", wallet); //decrypted);
this.jwk = wallet;
this.gotWallet(wallet);
});
});
} else {
// Generate private key + JWK wallet for Arweave: //var keyPair = module.getKeyPairFromSeed(btoa(email + "+" + password), { id: 'rsa', modulusLength: 4096 }, {privateKeyFormat: 'pkcs1-pem'}).then(function(response) {
var keyPair = this.arweave.wallets
.generate()
.then((key) => {
this.jwk = key;
this.encryptAndUploadWallet(this.jwk, encryptedEmail).then((data) => {
console.log("New wallet generated!", data);
this.gotWallet(this.jwk);
});
// TODO: Unset key AND response here
})
.catch(function (e) {
// Show errors:
this.error = e;
});
}
// Show loader:
console.log("Generating private key...", keyPair);
});
});
}
},
encryptAndUploadWallet: function (jwk, encryptedEmail = null) {
return new Promise((resolve, reject) => {
// JSON stringify the JWK:
var json = JSON.stringify(jwk);
console.log("Genereated new wallet, jwk = ", jwk);
console.log("json = ", json, this.password);
// Encrypt the JWK using the entered password:
return this.encryptOrDecrypt("encrypt", json, this.password).then((encrypted) => {
console.log("ENCRYPTED = ", encrypted);
var buffer = btoa(encrypted);
console.log("BUFFER = ", buffer);
// Fetch 'createWalletURL' from the WeaveID DAO:
var community = new Community(this.arweave);
community.setCommunityTx("lIzeO-Yb00iGm0oWK0BCEjxtluUbck1_sH3xeStAgcg").then((response) => {
community.getState().then((state) => {
console.log("STATE = ", state);
var settings = {};
state.settings.forEach((value, key) => {
settings[key] = value;
});
var createWalletURL = settings.createWalletURL ? settings.createWalletURL : "https://weaveid.io/arweave/save.php";
// Upload to Arweave tagged with entered Email and "weaveid" via API call and unique timestamp
return fetch(createWalletURL, {
method: "post",
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data'
},
mode: 'cors', // this cannot be 'no-cors'
body: btoa('{"hash": "' + buffer + '", "email": "' + encryptedEmail + '", "timestamp": "' + new Date() + '"}')
})
.then((response) => {
return response.json();
})
.then((data) => {
resolve(data);
});
});
});
});
});
},
setFiles: function () {
this.files = event.target.files;
},
getImportedWalletFile: function () {
return new Promise((resolve, reject) => {
// Get wallet file from frontend form and read contents
if (!this.files || this.files.length <= 0) reject("No file chosen!");
var fr = new FileReader();
fr.onload = function (e) {
var result = JSON.parse(e.target.result);
resolve(result);
};
return fr.readAsText(this.files.item(0));
});
},
importWallet: function () {
this.getImportedWalletFile()
.then((jwk) => {
// Set the current wallet file to the passed in JWK:
this.jwk = jwk;
// Form validation:
if (!this.email || this.email.trim() === "" || !this.password || this.password.trim() === "") {
this.error = "Please enter an email and password.";
} else {
this.loggingIn = true;
// Initialize ArweaveJS:
if (!this.arweave) {
this.arweave = Arweave.init({
host: "arweave.net", // Hostname or IP address for a Arweave host
port: 443, // Port
protocol: "https", // Network protocol http or https
timeout: 20000, // Network request timeouts in milliseconds
logging: false // Enable network request logging
});
}
// TODO: Get the user's balance
// if balance is > $2.00 USD, take fee
// --> calculate 0.1% fee using same code below (line ~600+ in arweavePost())
// Cap the fee
// if PST referrer was set on WeaveID init, split fee 50/50 between PST holders and referrer PST
// send fee transactions
// continue
// Encrypt the user's email before we store it on the blockchain:
this.sha256(this.email).then((encryptedEmail) => {
encryptedEmail = btoa(encryptedEmail);
// Encrypt & upload the wallet:
this.encryptAndUploadWallet(this.jwk, encryptedEmail).then((data) => {
console.log("New wallet generated!", data);
this.gotWallet(this.jwk);
});
});
}
})
.catch((e) => {
this.error = e;
});
},
gotWallet: function (jwk) {
// Main login:
this.mainLogin(jwk);
// Save the key if they checked "Remember Me":
// TODO: Save as encrypted JWK?
if (this.rememberMe) localStorage.setItem("weaveid-wallet-pk", JSON.stringify(jwk));
},
encryptOrDecrypt: function (action, message, password) {
promise_key = window.crypto.subtle.importKey("raw", this.convertStringToArrayBuffer(password), { name: "PBKDF2" }, false, ["deriveKey"]);
return promise_key
.then((importedPassword) => {
return window.crypto.subtle.deriveKey(
{
name: "PBKDF2",
salt: this.convertStringToArrayBuffer("WeaveID"),
iterations: 100000,
hash: "SHA-256"
},
importedPassword,
{
name: "AES-CBC",
length: 128
},
false,
["encrypt", "decrypt"]
);
})
.then((key) => {
key_object = key;
if (action === "encrypt") {
return this.encrypt_data(message);
} else {
return this.decrypt_data(message);
}
});
promise_key.catch = function (e) {
alert("Error while importing key: " + e.message);
};
},
encrypt_data: function (secretmessage) {
return new Promise((resolve, reject) => {
var abMessage = this.convertStringToArrayBuffer(this.vector + secretmessage);
this.encrypt_promise = window.crypto.subtle.encrypt({ name: "AES-CBC", iv: this.vector }, key_object, abMessage);
return this.encrypt_promise.then(
function (result) {
this.encrypted_data = new Uint8Array(result);
resolve(this.encrypted_data);
},
function (e) {
alert("Error while encrypting data: " + e.message);
}
);
});
},
decrypt_data: function (encrypted_text) {
return new Promise((resolve, reject) => {
var abMessage = encrypted_text;
this.decrypt_promise = window.crypto.subtle.decrypt({ name: "AES-CBC", iv: this.vector }, key_object, abMessage);
return this.decrypt_promise.then(
function (result) {
console.log(result);
resolve(new Uint8Array(result));
},
function (e) {
alert("Error while decrypting data: " + e.message);
console.log("Error while decrypting data: ", e);
reject(e);
}
);
});
},
sha256: function (message) {
return new Promise((resolve, reject) => {
// encode as UTF-8
const msgBuffer = new TextEncoder("utf-8").encode(message);
// hash the message
return crypto.subtle.digest("SHA-256", msgBuffer).then((hashBuffer) => {
// convert ArrayBuffer to Array
const hashArray = Array.from(new Uint8Array(hashBuffer));
// convert bytes to hex string
const hashHex = hashArray.map((b) => ("00" + b.toString(16)).slice(-2)).join("");
resolve(hashHex);
});
});
},
convertStringToArrayBuffer: function (str) {
//https://stackoverflow.com/a/45658748
var encoder = new TextEncoder("utf-8");
return encoder.encode(str);
},
convertArrayBuffertoString: function (buffer) {
var decoder = new TextDecoder("utf-8");
return decoder.decode(buffer);
},
isJson: function (str) {
try {
return JSON.parse(str);
} catch (e) {
return false;
}
},
copyToClipboard: function (str) {
var el = document.createElement("textarea");
el.value = str;
document.body.appendChild(el);
el.select();
document.execCommand("copy");
document.body.removeChild(el);
},
animateCSS: function (element, animation, prefix = "animate__", speed = "faster") {
// We create a Promise and return it
return new Promise((resolve, reject) => {
var animationName = `${prefix}${animation}`;
var animationSpeed = `${prefix}${speed}`;
var node = document.querySelector(element);
if (node) {
node.classList.add(`${prefix}animated`, animationName, animationSpeed);
// When the animation ends, we clean the classes and resolve the Promise
function handleAnimationEnd() {
node.classList.remove(`${prefix}animated`, animationName);
node.removeEventListener("animationend", handleAnimationEnd);
resolve("Animation ended");
}
node.addEventListener("animationend", handleAnimationEnd);
}
});
},
connectToParent: function () {
//if(parent!==top) { // Check if we're in an iFrame. TODO: Fix this
var connection = Penpal.connectToParent({
// Methods child is exposing to parent. TODO: ADD ETHEREUM METHODS IN HERE
methods: {
getAddress() {
return v.address;
},
getWallet() {
return v.jwk;
},
arweaveCreateTransaction(data, callback) {
var promise = new Promise(function (resolve, reject) {
v.arweave
.createTransaction(data, v.jwk)
.then(function (response) {
console.log("Before resolve = ", response);
resolve(response);
})
.catch(function (e) {
reject(e);
});
});
if (callback && typeof callback == "function") {
promise.then(callback.bind(null, null), callback);
}
return promise;
},
arweaveSign(tx, callback) {
return new Promise((resy, rejy) => {
return v.showConfirmation(tx.target, tx.quantity).then(function () {
console.log("Waiting for button push...");
document.getElementById("confirm-button").addEventListener(
"click",
function (e) {
// Dummy wallet for generating dummy object:
console.log("RUNNING REST OF THIS");
var test_jwk = {
d:
"ck9wAIHqTn7nCqKvK41Mf5BfqZduLjofOwJ5JM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MfoNNBCw6S1fREBnU08Pfzt4-GOGc85ptBSMH19KElVMJC7cNJruFjo1YLY30ZqGEv-NlPeG8ItjcAwdIIIDelSjr4TQdi2SIKGVx5ACDIYdTz3ea9hq4h2bj-OTdabRCnC-d69veP8bhCNP5OReXPRZN1WevVGl0Kqvad0k68PEXdqL_vQ4GPOgEt-PXhjnFVyVlOBsU8Kxq33JT1PXfeNWs9sTZm-6LnKm7LQ1JLKNQYieeJj-NP6ntO59c-Z4lqo33q2plFw",
q:
"wGwrK4OlRlY21hAZgJgt5-v9vukKYrSKuA1k-o3VA4i8m3WCYQCkBkICPQ3WGa1O6olgExYvjAV1MS3qbNEDBCCzi82tKz7vkcKvCE5GzXpq2DAW3H35PAt8vmGGnjukiZXgxDP3OtueRle6rcQs1pi1MwQmBhf9B-1LJZAXF0G0WuMObU9yTAf6n5fcbSf09AVnpiY8p2xizNG6jtLfZcgH_H2dZJ_Z4I1pudxViHyfMVvQgKU6POImV4u1o4C6ZpoehknN8hETR8p1eEIx7JMfqMyT9zYhXO8bKkj7dE9dyQsTiRhJ8NSs2UBlC9so63OVy1A2KhnzBoMwyTRT5Q",
qi:
"cXWAHLucCcopSVkIVmvOQ50MktM6MRtuap3CebXF1p2rARPfG8baNgrXb9itLFSGacQUrXcrHRGhAd0i4RrIG6W-OGzNpz-F7cv_9s5Fihw7e9P1DZ0tyYz8r1Efm0WZum_4Hdm1vbrjaHGxgJWrX-tvO_Ou4FTcFISHgMI74tYFs_FbTLgL0CCy2TCJ4UJvdBJk1UlFqLfdGm1y8zh1QNzht_H3okoptn6J4hLhEFdMTnDMcsxWorW7SQ4jfGeg-5NAfDb9EJRGKrzDM7YJSPr04HMFWpwhtF_XZ-nTgdyrCNe_kqSA8ogNKVRD-e0Occ5JIUx80JLsvzymXT_Tgg"
};
return v.arweave.createTransaction({ target: "1seRanklLU_1VTGkEk7P0xAwMJfA7owA1JHW5KyZKlY", quantity: v.arweave.ar.arToWinston("0.0005") }, test_jwk).then(function (dummy) {
Object.keys(tx).forEach(function (key) {
dummy[key] = tx[key];
});
console.log("My page got: ", dummy);
// Real code starts here:
return v.arweave.transactions
.sign(dummy, v.jwk)
.then(function (response) {
v.closeAll();
resy(dummy);
return dummy;
})
.catch(function (e) {
//reject(e);
});
});
},
{ once: true }
);
});
});
},
arweavePost(tx, callback) {
var promise = new Promise((resolve, reject) => {
v.arweave.transactions.post(tx).then(function (returnValue) {
console.log("Submitted main transaction = ", returnValue);
// SEND PST FEE:
var weightedHolder = "W46vSpKFlVnYyEzCn39bcwGzHslfucPZhcBsmed5uQU"; // Current 100% holder of PST. This will be changed in the future.
var quantity = tx.quantity * this.fee; // 0.1% transaction fee
v.arweave.createTransaction({ target: weightedHolder, quantity: quantity.toString() }, v.jwk).then(function (c_tx) {
console.log("Created PST fee TX: ", c_tx);
v.arweave.transactions.sign(c_tx, v.jwk).then(function (c_r) {
console.log("Signed PST fee TX: ", c_tx, c_r);
v.arweave.transactions.post(c_tx).then(function (c_r3) {
console.log("Submitted PST fee TX! ", c_tx, c_r3);
// Resolve promise:
resolve(returnValue);
});
});
});
});
});
}
}
});
connection.promise.then((parent) => {
this.iframeConnection = parent;
this.closeModal = this.iframeConnection.closeModal;
this.returnAddy = this.iframeConnection.returnAddress;
});
},
showConfirmation: function (address, amount) {
return new Promise((resolve, reject) => {
this.address = address;
this.confirmAmount = parseFloat(this.arweave.ar.winstonToAr(amount)).toFixed(6);
resolve(true);
});
},
hideConfirmation: function () {
this.confirmAmount = null;
},
showSend: function () {
this.screen = "send";
},
hideSend: function () {
this.screen = "results";
},
send: function () {
let amount = 1; // TODO: Get from UI
if (this.address && amount > 0) {
this.showConfirmation(this.address, amount);
this.screen = "results";
}
},
closeAll: function () {
this.hideConfirmation();
this.closeModal();
}
},
computed: {
loggedIn: function () {
return !!(this.jwk && this.address && this.successLoaderFinished);
},
balanceFixed: function () {
return parseFloat(this.balance).toFixed(4);
},
balanceUSD: function () {
if (!this.balance || !this.ar) return "0.000";
return (parseFloat(this.balance) * parseFloat(this.ar.current_price)).toFixed(2);
},
confirmAmountUSD: function () {
if (!this.confirmAmount || !this.ar) return "0.00";
return (parseFloat(this.confirmAmount) * parseFloat(this.ar.current_price)).toFixed(2);
},
confirmAmountFeeUSD: function () {
return (this.fee * parseFloat(this.confirmAmountUSD)).toFixed(2);
},
confirmAmountTotalUSD: function () {
return (parseFloat(this.confirmAmountUSD) + parseFloat(this.confirmAmountFeeUSD)).toFixed(2);
},
priceChange: function () {
if (!this.ar) return 0;
return parseFloat(this.ar.price_change_percentage_24h).toFixed(2);
}
}
});
</script>
</body>
</html>