import { Component } from "@angular/core"; import { NavController, ToastController, LoadingController, AlertController } from "ionic-angular"; import { Storage } from "@ionic/storage"; import { CryptoProvider } from "../../providers/crypto/crypto"; import { SocialSharing } from "@ionic-native/social-sharing"; import { P2pDatabaseGunProvider } from "../../providers/p2p-database-gun/p2p-database-gun"; import { PgpKeyServerProvider } from "../../providers/pgp-key-server/pgp-key-server"; @Component({ selector: "page-settings", templateUrl: "settings.html" }) export class SettingsPage { keywords: string; privateKey: string; publicKey: string; revocationCertificate:string; email: string; keyid; userId; email1: string; privateKey_prev: string; constructor( public navCtrl: NavController, public toastCtrl: ToastController, private cryptoUtils: CryptoProvider, private storage: Storage, private loadingCtrl: LoadingController, private sharing: SocialSharing, private alertCtrl: AlertController, private openpgp: PgpKeyServerProvider, private gun: P2pDatabaseGunProvider, ) { this.init(); this.loadValuesFromStorage(); } private async init() { this.userId = await this.storage.get("userId"); if(!this.email){ const email = await this.gun.getEmail(this.userId); this.email1 = email.email; await this.storage.set("email", this.email1); } if(!this.privateKey){ const privateKey = await this.cryptoUtils.getKeyHistory(this.userId);; this.privateKey_prev = privateKey[0]; console.log("private key prev in setttings", this.privateKey_prev); await this.storage.set("privateKey", this.privateKey_prev); } } async loadValuesFromStorage() { this.privateKey = await this.storage.get("privateKey"); this.publicKey = await this.storage.get("publicKey"); this.keywords = await this.storage.get("keywords"); this.email = await this.storage.get("email"); } generateKeys() { if (!this.email) { return; } else { this.storage.set("email", this.email); if (this.publicKey || this.privateKey) { const alert = this.alertCtrl.create({ title: "Are you sure?", subTitle: "You already have keys entered. Do you want to overwrite them?", buttons: [{ text: "No", role: "cancel" }, { text: "Yes", handler: () => { this.startKeyGeneration(); } } ] }); alert.present(); } else { this.startKeyGeneration(); } } } private async startKeyGeneration() { const keys = await this.openpgp.generateKey("passphrase",this.email); this.privateKey = keys.privateKeyArmored; this.publicKey = keys.publicKeyArmored; this.revocationCertificate = keys.revocationCertificate; this.keyid = keys.key.primaryKey.keyid; } save() { this.storage.set("publicKey", this.publicKey); this.storage.set("privateKey", this.privateKey); this.storage.set("keyid", this.keyid); this.storage.set("revocationCert", this.revocationCertificate); this.storage.set("keywords", this.keywords ? this.keywords.trim() : ""); this.showToast("Successfully saved!"); } async publishKeys() { // publishing public keys await this.openpgp.publishPubKey(this.publicKey); this.showToast("Publc key published"); // Saving email in gun await this.gun.setEmail(this.userId, this.email); //Saving private key in gun await this.cryptoUtils.publishPrivateKey(this.privateKey); console.log(" settings private keys is", this.privateKey); } exportPrivateKey() { if (this.privateKey.length) { this.sharing .share(this.privateKey, null, null, null) .then(() => console.log("Private key was exported")) .catch(() => this.showToast( "Sorry! Something went wrong trying to export the private key :(" ) ); } else { this.showToast("There is nothing to share."); } } private showToast(message: string) { const toast = this.toastCtrl.create({ message: message, position: "bottom", duration: 3000 }); toast.present(); } }