settings.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 * as openpgp from 'openpgp';
  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. email: string;
  21. hkp = new openpgp.HKP('https://sks-keyservers.net/');
  22. constructor(
  23. public navCtrl: NavController,
  24. public toastCtrl: ToastController,
  25. private cryptoUtils: CryptoProvider,
  26. private storage: Storage,
  27. private loadingCtrl: LoadingController,
  28. private sharing: SocialSharing,
  29. private alertCtrl: AlertController
  30. ) {
  31. this.loadValuesFromStorage();
  32. }
  33. async loadValuesFromStorage() {
  34. this.privateKey = await this.storage.get("privateKey");
  35. this.publicKey = await this.storage.get("publicKey");
  36. this.keywords = await this.storage.get("keywords");
  37. this.email = await this.storage.get("email");
  38. }
  39. generateKeys() {
  40. if (!this.email){
  41. console.log("email is not provided or not valid");
  42. return;
  43. }
  44. else{
  45. this.storage.set("email", this.email);
  46. if (this.publicKey || this.privateKey) {
  47. const alert = this.alertCtrl.create({
  48. title: "Are you sure?",
  49. subTitle:
  50. "You already have keys entered. Do you want to overwrite them?",
  51. buttons: [
  52. {
  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.cryptoUtils.generatePgpKeys(this.email);
  72. this.privateKey = keys.privateKeyArmored;
  73. this.publicKey = keys.publicKeyArmored;
  74. console.log("public key", this.privateKey);
  75. console.log("public key", this.publicKey);
  76. // this.privateKey = await this.cryptoUtils.extractPrivateKey(keys);
  77. }
  78. save() {
  79. this.storage.set("publicKey", this.publicKey);
  80. this.storage.set("privateKey", this.privateKey);
  81. this.storage.set("keywords", this.keywords ? this.keywords.trim() : "");
  82. this.showToast("Successfully saved!");
  83. }
  84. async publishPublicKey() {
  85. await this.publishPublicKey2();
  86. await this.lookupKeys(this.email);
  87. }
  88. async publishPublicKey2() {
  89. const loading = this.loadingCtrl.create();
  90. loading.present();
  91. // console.log("Uploding publish public key", this.publicKey);
  92. if(!this.publicKey) return;
  93. this.hkp.upload(this.publicKey).then(function() {
  94. console.log("Uploding public key");
  95. });
  96. loading.dismiss();
  97. this.showToast("Public key has been published!");
  98. //lookup key to verify it has been pubblished
  99. // await this.lookupKeys(this.email);
  100. }
  101. public async lookupKeys(email:string){
  102. var options = {
  103. query: email
  104. };
  105. let armoredPubkey = await this.hkp.lookup(options);
  106. let pubkey = await openpgp.key.readArmored(armoredPubkey);
  107. console.log('Found public key:',pubkey);
  108. // if(! (email == 'rohit.hosn@gmail.com'))
  109. // this.pk.push(pubkey.publicKeyArmored);
  110. // return pubkey;
  111. }
  112. exportPrivateKey() {
  113. if (this.privateKey.length) {
  114. this.sharing
  115. .share(this.privateKey, null, null, null)
  116. .then(() => console.log("Private key was exported"))
  117. .catch(() =>
  118. this.showToast(
  119. "Sorry! Something went wrong trying to export the private key :("
  120. )
  121. );
  122. } else {
  123. this.showToast("There is nothing to share.");
  124. }
  125. }
  126. private showToast(message: string) {
  127. const toast = this.toastCtrl.create({
  128. message: message,
  129. position: "bottom",
  130. duration: 3000
  131. });
  132. toast.present();
  133. }
  134. }