settings.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. userId;
  25. email1: string;
  26. privateKey_prev: string;
  27. constructor(
  28. public navCtrl: NavController,
  29. public toastCtrl: ToastController,
  30. private cryptoUtils: CryptoProvider,
  31. private storage: Storage,
  32. private loadingCtrl: LoadingController,
  33. private sharing: SocialSharing,
  34. private alertCtrl: AlertController,
  35. private openpgp: PgpKeyServerProvider,
  36. private gun: P2pDatabaseGunProvider,
  37. ) {
  38. this.init();
  39. this.loadValuesFromStorage();
  40. }
  41. private async init() {
  42. this.userId = await this.storage.get("userId");
  43. if(!this.email){
  44. const email = await this.gun.getEmail(this.userId);
  45. this.email1 = email.email;
  46. await this.storage.set("email", this.email1);
  47. }
  48. if(!this.privateKey){
  49. const privateKey = await this.cryptoUtils.getKeyHistory(this.userId);;
  50. this.privateKey_prev = privateKey[0];
  51. console.log("private key prev in setttings", this.privateKey_prev);
  52. await this.storage.set("privateKey", this.privateKey_prev);
  53. }
  54. }
  55. async loadValuesFromStorage() {
  56. this.privateKey = await this.storage.get("privateKey");
  57. this.publicKey = await this.storage.get("publicKey");
  58. this.keywords = await this.storage.get("keywords");
  59. this.email = await this.storage.get("email");
  60. }
  61. generateKeys() {
  62. if (!this.email) {
  63. return;
  64. } else {
  65. this.storage.set("email", this.email);
  66. if (this.publicKey || this.privateKey) {
  67. const alert = this.alertCtrl.create({
  68. title: "Are you sure?",
  69. subTitle: "You already have keys entered. Do you want to overwrite them?",
  70. buttons: [{
  71. text: "No",
  72. role: "cancel"
  73. },
  74. {
  75. text: "Yes",
  76. handler: () => {
  77. this.startKeyGeneration();
  78. }
  79. }
  80. ]
  81. });
  82. alert.present();
  83. } else {
  84. this.startKeyGeneration();
  85. }
  86. }
  87. }
  88. private async startKeyGeneration() {
  89. const keys = await this.openpgp.generateKey("passphrase",this.email);
  90. this.privateKey = keys.privateKeyArmored;
  91. this.publicKey = keys.publicKeyArmored;
  92. this.revocationCertificate = keys.revocationCertificate;
  93. this.keyid = keys.key.primaryKey.keyid;
  94. }
  95. save() {
  96. this.storage.set("publicKey", this.publicKey);
  97. this.storage.set("privateKey", this.privateKey);
  98. this.storage.set("keyid", this.keyid);
  99. this.storage.set("revocationCert", this.revocationCertificate);
  100. this.storage.set("keywords", this.keywords ? this.keywords.trim() : "");
  101. this.showToast("Successfully saved!");
  102. }
  103. async publishKeys() {
  104. // publishing public keys
  105. await this.openpgp.publishPubKey(this.publicKey);
  106. this.showToast("Publc key published");
  107. // Saving email in gun
  108. await this.gun.setEmail(this.userId, this.email);
  109. //Saving private key in gun
  110. await this.cryptoUtils.publishPrivateKey(this.privateKey);
  111. console.log(" settings private keys is", this.privateKey);
  112. }
  113. exportPrivateKey() {
  114. if (this.privateKey.length) {
  115. this.sharing
  116. .share(this.privateKey, null, null, null)
  117. .then(() => console.log("Private key was exported"))
  118. .catch(() =>
  119. this.showToast(
  120. "Sorry! Something went wrong trying to export the private key :("
  121. )
  122. );
  123. } else {
  124. this.showToast("There is nothing to share.");
  125. }
  126. }
  127. private showToast(message: string) {
  128. const toast = this.toastCtrl.create({
  129. message: message,
  130. position: "bottom",
  131. duration: 3000
  132. });
  133. toast.present();
  134. }
  135. }