settings.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. import { PgpKeyServerProvider } from "../../providers/pgp-key-server/pgp-key-server";
  12. @Component({
  13. selector: "page-settings",
  14. templateUrl: "settings.html"
  15. })
  16. export class SettingsPage {
  17. keywords: string;
  18. privateKey: string;
  19. publicKey: string;
  20. revocationCertificate:string;
  21. email: string;
  22. keyid;
  23. constructor(
  24. public navCtrl: NavController,
  25. public toastCtrl: ToastController,
  26. private cryptoUtils: CryptoProvider,
  27. private storage: Storage,
  28. private loadingCtrl: LoadingController,
  29. private sharing: SocialSharing,
  30. private alertCtrl: AlertController,
  31. private openpgp: PgpKeyServerProvider
  32. ) {
  33. this.loadValuesFromStorage();
  34. }
  35. async loadValuesFromStorage() {
  36. this.privateKey = await this.storage.get("privateKey");
  37. this.publicKey = await this.storage.get("publicKey");
  38. console.log("private key", this.privateKey);
  39. console.log("public key", this.publicKey);
  40. this.keywords = await this.storage.get("keywords");
  41. this.email = await this.storage.get("email");
  42. }
  43. generateKeys() {
  44. if (!this.email) {
  45. console.log("email is not provided or not valid");
  46. return;
  47. } else {
  48. this.storage.set("email", this.email);
  49. if (this.publicKey || this.privateKey) {
  50. const alert = this.alertCtrl.create({
  51. title: "Are you sure?",
  52. subTitle: "You already have keys entered. Do you want to overwrite them?",
  53. buttons: [{
  54. text: "No",
  55. role: "cancel"
  56. },
  57. {
  58. text: "Yes",
  59. handler: () => {
  60. this.startKeyGeneration();
  61. }
  62. }
  63. ]
  64. });
  65. alert.present();
  66. } else {
  67. this.startKeyGeneration();
  68. }
  69. }
  70. }
  71. private async startKeyGeneration() {
  72. const keys = await this.openpgp.generateKey("passphrase",this.email);
  73. this.privateKey = keys.privateKeyArmored;
  74. this.publicKey = keys.publicKeyArmored;
  75. this.revocationCertificate = keys.revocationCertificate;
  76. this.keyid = keys.key.primaryKey.keyid;
  77. console.log('key id is:',this.keyid);
  78. console.log("private key", this.privateKey);
  79. console.log("public key", this.publicKey);
  80. }
  81. save() {
  82. this.storage.set("publicKey", this.publicKey);
  83. this.storage.set("privateKey", this.privateKey);
  84. this.storage.set("keyid", this.keyid);
  85. this.storage.set("revocationCert", this.revocationCertificate);
  86. this.storage.set("keywords", this.keywords ? this.keywords.trim() : "");
  87. this.showToast("Successfully saved!");
  88. }
  89. async publishPublicKey() {
  90. await this.openpgp.publishPubKey(this.publicKey);
  91. this.showToast("Publc key published");
  92. }
  93. exportPrivateKey() {
  94. if (this.privateKey.length) {
  95. this.sharing
  96. .share(this.privateKey, null, null, null)
  97. .then(() => console.log("Private key was exported"))
  98. .catch(() =>
  99. this.showToast(
  100. "Sorry! Something went wrong trying to export the private key :("
  101. )
  102. );
  103. } else {
  104. this.showToast("There is nothing to share.");
  105. }
  106. }
  107. private showToast(message: string) {
  108. const toast = this.toastCtrl.create({
  109. message: message,
  110. position: "bottom",
  111. duration: 3000
  112. });
  113. toast.present();
  114. }
  115. }