settings.ts 3.8 KB

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