settings.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. import { SocialSharing } from "@ionic-native/social-sharing";
  10. @Component({
  11. selector: "page-settings",
  12. templateUrl: "settings.html"
  13. })
  14. export class SettingsPage {
  15. keywords: string;
  16. privateKey: string;
  17. publicKey: string;
  18. constructor(
  19. public navCtrl: NavController,
  20. public toastCtrl: ToastController,
  21. private cryptoUtils: CryptoProvider,
  22. private storage: Storage,
  23. private loadingCtrl: LoadingController,
  24. private sharing: SocialSharing
  25. ) {
  26. this.loadValuesFromStorage();
  27. }
  28. async loadValuesFromStorage() {
  29. this.privateKey = await this.storage.get("privateKey");
  30. this.publicKey = await this.storage.get("publicKey");
  31. this.keywords = await this.storage.get("keywords");
  32. }
  33. async generateKeys() {
  34. const keys = await this.cryptoUtils.generateRsaKeys();
  35. this.publicKey = await this.cryptoUtils.extractPublicKey(keys);
  36. this.privateKey = await this.cryptoUtils.extractPrivateKey(keys);
  37. }
  38. save() {
  39. this.storage.set("publicKey", this.publicKey);
  40. this.storage.set("privateKey", this.privateKey);
  41. this.storage.set("keywords", this.keywords);
  42. this.showToast("Successfully saved!");
  43. }
  44. async publishPublicKey() {
  45. if (this.publicKey.length) {
  46. const loading = this.loadingCtrl.create();
  47. loading.present();
  48. await this.cryptoUtils.publishPublicKey(this.publicKey);
  49. loading.dismiss();
  50. this.showToast("Public key has been published!");
  51. }
  52. }
  53. exportPrivateKey() {
  54. if (this.privateKey.length) {
  55. this.sharing
  56. .share(this.privateKey, null, null, null)
  57. .then(() => console.log("Private key was exported"))
  58. .catch(() =>
  59. this.showToast(
  60. "Sorry! Something went wrong trying to export the private key :("
  61. )
  62. );
  63. } else {
  64. this.showToast("There is nothing to share.");
  65. }
  66. }
  67. private showToast(message: string) {
  68. const toast = this.toastCtrl.create({
  69. message: message,
  70. position: "bottom",
  71. duration: 3000
  72. });
  73. toast.present();
  74. }
  75. }