settings.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { Component } from "@angular/core";
  2. import {
  3. NavController,
  4. ToastController,
  5. LoadingController,
  6. AlertController
  7. } from "ionic-angular";
  8. import { Storage } from "@ionic/storage";
  9. import { CryptoProvider } from "../../providers/crypto/crypto";
  10. import { SocialSharing } from "@ionic-native/social-sharing";
  11. @Component({
  12. selector: "page-settings",
  13. templateUrl: "settings.html"
  14. })
  15. export class SettingsPage {
  16. keywords: string;
  17. privateKey: string;
  18. publicKey: string;
  19. constructor(
  20. public navCtrl: NavController,
  21. public toastCtrl: ToastController,
  22. private cryptoUtils: CryptoProvider,
  23. private storage: Storage,
  24. private loadingCtrl: LoadingController,
  25. private sharing: SocialSharing,
  26. private alertCtrl: AlertController
  27. ) {
  28. this.loadValuesFromStorage();
  29. }
  30. async loadValuesFromStorage() {
  31. this.privateKey = await this.storage.get("privateKey");
  32. this.publicKey = await this.storage.get("publicKey");
  33. this.keywords = await this.storage.get("keywords");
  34. }
  35. generateKeys() {
  36. if (this.publicKey || this.privateKey) {
  37. const alert = this.alertCtrl.create({
  38. title: "Are you sure?",
  39. subTitle:
  40. "You already have keys entered. Do you want to overwrite them?",
  41. buttons: [
  42. {
  43. text: "No",
  44. role: "cancel"
  45. },
  46. {
  47. text: "Yes",
  48. handler: () => {
  49. this.startKeyGeneration();
  50. }
  51. }
  52. ]
  53. });
  54. alert.present();
  55. } else {
  56. this.startKeyGeneration();
  57. }
  58. }
  59. private async startKeyGeneration() {
  60. const keys = await this.cryptoUtils.generateRsaKeys();
  61. this.publicKey = await this.cryptoUtils.extractPublicKey(keys);
  62. this.privateKey = await this.cryptoUtils.extractPrivateKey(keys);
  63. }
  64. save() {
  65. this.storage.set("publicKey", this.publicKey);
  66. this.storage.set("privateKey", this.privateKey);
  67. this.storage.set("keywords", this.keywords);
  68. this.showToast("Successfully saved!");
  69. }
  70. async publishPublicKey() {
  71. if (this.publicKey.length) {
  72. const loading = this.loadingCtrl.create();
  73. loading.present();
  74. await this.cryptoUtils.publishPublicKey(this.publicKey);
  75. loading.dismiss();
  76. this.showToast("Public key has been published!");
  77. }
  78. }
  79. exportPrivateKey() {
  80. if (this.privateKey.length) {
  81. this.sharing
  82. .share(this.privateKey, null, null, null)
  83. .then(() => console.log("Private key was exported"))
  84. .catch(() =>
  85. this.showToast(
  86. "Sorry! Something went wrong trying to export the private key :("
  87. )
  88. );
  89. } else {
  90. this.showToast("There is nothing to share.");
  91. }
  92. }
  93. private showToast(message: string) {
  94. const toast = this.toastCtrl.create({
  95. message: message,
  96. position: "bottom",
  97. duration: 3000
  98. });
  99. toast.present();
  100. }
  101. }