Browse Source

encryption workflow

rohit.gowda 4 years ago
parent
commit
23f9e2553a

+ 8 - 1
app/src/pages/settings/settings.ts

@@ -20,6 +20,7 @@ export class SettingsPage {
   publicKey: string;
   revocationCertificate:string;
   email: string;
+  keyid;
 
   constructor(
     public navCtrl: NavController,
@@ -32,11 +33,14 @@ export class SettingsPage {
     private openpgp: PgpKeyServerProvider
   ) {
     this.loadValuesFromStorage();
+
   }
 
   async loadValuesFromStorage() {
     this.privateKey = await this.storage.get("privateKey");
     this.publicKey = await this.storage.get("publicKey");
+    console.log("private key", this.privateKey);
+    console.log("public key", this.publicKey);
     this.keywords = await this.storage.get("keywords");
     this.email = await this.storage.get("email");
   }
@@ -77,7 +81,8 @@ export class SettingsPage {
     this.privateKey = keys.privateKeyArmored;
     this.publicKey = keys.publicKeyArmored;
     this.revocationCertificate = keys.revocationCertificate;
-
+    this.keyid = keys.key.primaryKey.keyid;
+    console.log('key id is:',this.keyid);
     console.log("private key", this.privateKey);
     console.log("public key", this.publicKey);
   }
@@ -86,6 +91,7 @@ export class SettingsPage {
   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() : "");
 
@@ -94,6 +100,7 @@ export class SettingsPage {
 
   async publishPublicKey() {
     await this.openpgp.publishPubKey(this.publicKey);
+    this.showToast("Publc key published");
   }
 
   exportPrivateKey() {

+ 16 - 15
app/src/providers/feed/feed.ts

@@ -140,9 +140,7 @@ export class FeedProvider {
       }
 
       // Fetch public key history for user
-      const publicKeyHistory: object[] = await this.cryptoUtils.fetchPublicKeyHistoryForUser(
-        userId
-      );
+    
 
       // Decrypt tweets
       // const decryptedTweet = this.cryptoUtils.decrypt(
@@ -150,24 +148,27 @@ export class FeedProvider {
       //   this.getPublicKeyAt(timestamp, publicKeyHistory)
       // );
       console.log('decrypting private tweets');
-    let pvtKey = await this.storage.get("privateKey");
+      let pvtKey = await this.storage.get("privateKey");
 
        const decryptedTweet = await this.opnpgp.decrypt(encryptedTweet,pvtKey);
-      privateTweets.push(JSON.parse(decryptedTweet));
+      // privateTweets.push(JSON.parse(decryptedTweet));
     }
 
-    // Add retweeted/quoted status
-    privateTweets.map(async tweet => await this.addQuotedStatusToTweet(tweet));
+    if(privateTweets.length>0){
+       // Add retweeted/quoted status
+      privateTweets.map(async tweet => await this.addQuotedStatusToTweet(tweet));
 
-    // Add original status (reply to)
-    privateTweets.map(
-      async tweet => await this.addOriginalStatusToTweet(tweet)
-    );
+      // Add original status (reply to)
+      privateTweets.map(
+        async tweet => await this.addOriginalStatusToTweet(tweet)
+      );
 
-    // Add user object to private tweets
-    return await Promise.all(
-      privateTweets.map(async tweet => await this.addUserToTweet(tweet))
-    );
+      // Add user object to private tweets
+      return await Promise.all(
+        privateTweets.map(async tweet => await this.addUserToTweet(tweet))
+      );
+    }
+   
   }
 
   private getPublicKeyAt(

+ 29 - 14
app/src/providers/pgp-key-server/pgp-key-server.ts

@@ -25,8 +25,8 @@ export class PgpKeyServerProvider {
 
   public async publishPubKey(pubkey) {
     console.log('passing pubkey to uplaoded : ', pubkey);
-    this.hkp.upload(pubkey).then(function() {
-      console.log('public key successfully uploaded');
+    this.hkp.upload(pubkey).then(function(result) {
+      console.log('public key successfully uploaded',result);
     });
   }
 
@@ -35,11 +35,18 @@ export class PgpKeyServerProvider {
     var options = {
       query: email
     };
-    let armoredPubkey = await this.hkp.lookup(options);
-    let pubkey = (await openpgp.key.readArmored(armoredPubkey)).keys[0];
-    console.log('Found latest public key:', pubkey);
-    this.pk.push(pubkey);
-    console.log('pk is:', this.pk);
+    try{
+      let armoredPubkey = await this.hkp.lookup(options);
+      let pubkey = (await openpgp.key.readArmored(armoredPubkey)).keys[0];
+      console.log('Found latest public key:', pubkey);
+      this.pk.push(pubkey);
+      return pubkey;
+    }
+    catch(err){
+      console.log("Error: key not found",err);
+      return "Key not found";
+    }
+    
   }
 
   /**
@@ -47,14 +54,16 @@ export class PgpKeyServerProvider {
    * @param plainText plain text
    * @param privateKey private key
    */
-  public async encrypt(plainText: string) {
+  public async encrypt(plainText) {
+    console.log('pk is:', this.pk);
+    if(!this.pk){ console.log("this.pk is empty"); return;}
     // this.lookupKeys('rohit.shiva.gowda@gmail.com');
     const options = {
       message: openpgp.message.fromText(plainText), // input as Message object
       publicKeys: await Promise.all(this.pk), // for encryption
       // privateKey s: [privKeyObj] // for signing (optional)
     }
-    console.log('options are:', options);
+    // console.log('options are:', options);
     const ciphertext = await openpgp.encrypt(options);
     console.log('encrypted text is:', ciphertext);
     return ciphertext.data;
@@ -64,15 +73,21 @@ export class PgpKeyServerProvider {
     const privKeyObj = (await openpgp.key.readArmored(a)).keys[0];
     console.log('privKeyObj', privKeyObj);
     await privKeyObj.decrypt(this.passphrase);
-    console.log('a is:',a);
+    // console.log('a is:',a);
     const options2 = {
       message: await openpgp.message.readArmored(encrypted), // parse armored message
       privateKeys: [privKeyObj] // for decryption
     }
-    console.log('options2 is: ', options2);
-    let plaintext = await openpgp.decrypt(options2);
-    console.log('decrypted text is:', plaintext, plaintext.data);
-    return plaintext.data // 'Hello, World!'
+    // console.log('options2 is: ', options2);
+    try{
+       let plaintext = await openpgp.decrypt(options2);
+      console.log('decrypted text is:', plaintext);
+      return plaintext.data // 'Hello, World!'
+    }
+     catch(err){
+      console.log('Error thrown:',err);
+    }
+   return null;
   }
 
   public async revokeKey() {