settings.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { Component } from "@angular/core";
  2. import {
  3. NavController,
  4. ToastController,
  5. LoadingController
  6. } from "ionic-angular";
  7. import { Storage } from "@ionic/storage";
  8. import { CryptoProvider } from "../../providers/crypto/crypto";
  9. @Component({
  10. selector: "page-settings",
  11. templateUrl: "settings.html"
  12. })
  13. export class SettingsPage {
  14. keywords: string;
  15. privateKey: string;
  16. publicKey: string;
  17. constructor(
  18. public navCtrl: NavController,
  19. public toastCtrl: ToastController,
  20. private cryptoUtils: CryptoProvider,
  21. private storage: Storage,
  22. private loadingCtrl: LoadingController
  23. ) {
  24. this.loadValuesFromStorage();
  25. }
  26. async loadValuesFromStorage() {
  27. this.privateKey = await this.storage.get("privateKey");
  28. this.publicKey = await this.storage.get("publicKey");
  29. this.keywords = await this.storage.get("keywords");
  30. }
  31. async generateKeys() {
  32. const keys = await this.cryptoUtils.generateRsaKeys();
  33. this.publicKey = await this.cryptoUtils.extractPublicKey(keys);
  34. this.privateKey = await this.cryptoUtils.extractPrivateKey(keys);
  35. }
  36. save() {
  37. this.storage.set("publicKey", this.publicKey);
  38. this.storage.set("privateKey", this.privateKey);
  39. this.storage.set("keywords", this.keywords);
  40. this.showToast("Successfully saved!");
  41. }
  42. async publishPublicKey() {
  43. if (this.publicKey.length) {
  44. const loading = this.loadingCtrl.create();
  45. loading.present();
  46. await this.cryptoUtils.publishPublicKey(this.publicKey);
  47. loading.dismiss();
  48. this.showToast("Public key has been published!");
  49. }
  50. }
  51. private showToast(message: string) {
  52. const toast = this.toastCtrl.create({
  53. message: message,
  54. position: "bottom",
  55. duration: 3000
  56. });
  57. toast.present();
  58. }
  59. }