Browse Source

storing pvt key history

rohit.gowda 4 years ago
parent
commit
d3c8b10ca6

+ 1 - 1
app/src/pages/settings/settings.html

@@ -31,7 +31,7 @@
       <ion-label color="primary" stacked>Public Key:</ion-label>
       <ion-textarea [(ngModel)]="publicKey"></ion-textarea>
       <p>Under no circumstances share your private key with any other person!</p>
-      <button ion-button block (click)="publishPublicKey()">Publish public key</button>
+      <button ion-button block (click)="publishKeys()">Publish public key</button>
       <button ion-button block (click)="exportPrivateKey()">Export private key</button>
     </ion-card-content>
   </ion-card>

+ 15 - 12
app/src/pages/settings/settings.ts

@@ -24,6 +24,7 @@ export class SettingsPage {
   keyid;
   userId;
   email1: string;
+  privateKey_prev: string;
   constructor(
     public navCtrl: NavController,
     public toastCtrl: ToastController,
@@ -41,12 +42,16 @@ export class SettingsPage {
   }
   private async init() {
     this.userId = await this.storage.get("userId");
-    console.log("email of teh user is FIRST ONE", this.email);
     if(!this.email){
     const email = await this.gun.getEmail(this.userId);
     this.email1 = email.email;
-    console.log("email of the user is", this.email1);
-       await this.storage.set("email", this.email1);
+    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);
     }
   }
 
@@ -105,21 +110,19 @@ export class SettingsPage {
     this.showToast("Successfully saved!");
   }
 
-  async publishPublicKey() {
+  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);
-    //test if stored in gun
-    const email = await this.gun.getEmail(this.userId);
+    
+    //Saving private key in gun
+    await this.cryptoUtils.publishPrivateKey(this.privateKey);
+    console.log(" settings private keys is", this.privateKey);
   }
 
-  // async publishPrivateKey() {
-  //   console.log("publishing pvt key");
-  //   await this.cryptoUtils.publishPrivateKey(this.privateKey);
-  // }
-
   exportPrivateKey() {
     if (this.privateKey.length) {
       this.sharing

+ 41 - 48
app/src/providers/crypto/crypto.ts

@@ -1,6 +1,7 @@
 import { Injectable } from "@angular/core";
 import { TwitterApiProvider } from "../twitter-api/twitter-api";
 import { P2pStorageIpfsProvider } from "../p2p-storage-ipfs/p2p-storage-ipfs";
+import { P2pDatabaseGunProvider } from "../../providers/p2p-database-gun/p2p-database-gun";
 import { Storage } from "@ionic/storage";
 import NodeRSA from "node-rsa";
 import * as openpgp from 'openpgp';
@@ -11,84 +12,76 @@ export class CryptoProvider {
   ownUserId: string;
   IV_LENGTH = 12;
   HYBRID_OSN_AES_KEY = "Z1vxAULQnZdoWhJOvv+hWEvVpyUHzNjD/ichEE2c8i4=";
+  email: string;
 
   constructor(
     private twitter: TwitterApiProvider,
     private ipfs: P2pStorageIpfsProvider,
-    private storage: Storage
+    private storage: Storage,
+    private gun: P2pDatabaseGunProvider
   ) {
     this.init();
   }
 
   private async init() {
     this.ownUserId = await this.storage.get("userId");
+    this.email = await this.storage.get("email");
   }
 
   /**
    * Publishs the public key history with the latest key
    * @param key key to publish
    */
-  public async publishPublicKey(key: string) {
-    let publicKeyHistory = await this.getKeyHistory(this.ownUserId);
-
+  public async publishPrivateKey(key: string) {
+    let privateKeyHistory = await this.getKeyHistory(this.ownUserId);
+    console.log("get publishPrivateKey crypto ", privateKeyHistory);
     // Todo: avoid publishing the same public key twice - check if new key equals newest key in history
-
-    // Add new key to history
-    const newKey = {
-      key: key,
-      validFrom: Date.now()
-    };
-
-    if (publicKeyHistory) {
-      publicKeyHistory["keys"].push(newKey);
-    } else {
-      publicKeyHistory = {
-        keys: [newKey]
+    if ( key === privateKeyHistory[0])
+    {
+      return;
+    }
+    else
+    {
+      // Add new key to history
+      const newKey = {
+        key: key,
+        validFrom: Date.now()
       };
+
+      if (privateKeyHistory) {
+        privateKeyHistory["keys"].push(newKey);
+      } else {
+        privateKeyHistory = {
+          keys: [newKey]
+        };
+      }
     }
 
-    // Ecnrypt key history
-    const encryptedPublicKeyHistory = await this.aesEncrypt(
-      JSON.stringify(publicKeyHistory)
-    );
+   
+     // Ecnrypt key history
+    const encryptedPrivateKeyHistory = JSON.stringify(privateKeyHistory);
 
     // Publish updated key history...
-    const res = await this.ipfs.storePublicKey(encryptedPublicKeyHistory);
+    const res = await this.ipfs.storePrivateKey(encryptedPrivateKeyHistory);
 
-    // tweet ipfs link
-    const tweetResponse = await this.twitter.tweet(
-      "ipfs://" + res["Hash"] + " #hybridOSN"
-    );
+    // store ipfs link Of private tweet in gundb
+    // let ipfsLink = "ipfs://" + res["Hash"];
 
-    // ... and update description in user profile with tweet id
-    this.twitter.updateProfileDescription(
-      "tweet://" + tweetResponse["data"]["id_str"] + " #hybridOSN"
-    );
+    await this.gun.storePrivateKeyHistory(this.ownUserId, this.email, res["Hash"]);
   }
 
-  private async getKeyHistory(userId: string) {
-    // Get user description
-    const userData = await this.twitter.fetchUser(userId);
-    const profileDescription = userData["description"];
+  public async getKeyHistory(userId: string) {
 
-    // Get tweet with link to key history
-    const tweetId = this.extractTweetId(profileDescription);
-    if (tweetId.length === 0) {
-      return null;
-    }
-    const tweetWithKeyHistoryLink = await this.twitter.fetchTweet(tweetId);
-
-    // Extract link to public key
-    const link = this.extractLinkFromDescription(
-      tweetWithKeyHistoryLink["data"]["full_text"]
-    );
+    
+    //get private key history from gun
+    let link = await this.gun.getPvtKeyHistory(userId);
+    console.log("get link crypto ", link);
 
     // Fetch public key history
     if (link.length) {
-      const encryptedKeyHistory = await this.ipfs.fetchJson(link);
-      // Decrypt key history
-      const keyHistory = await this.aesDecrypt(encryptedKeyHistory.toString());
-      return JSON.parse(keyHistory);
+      const encryptedKeyHistory = await this.ipfs.fetchJson(link); 
+      console.log("get link private key crypto ", encryptedKeyHistory);
+      return JSON.parse(encryptedKeyHistory.toString());
     } else {
       return null;
     }

+ 33 - 0
app/src/providers/p2p-database-gun/p2p-database-gun.ts

@@ -161,6 +161,39 @@ export class P2pDatabaseGunProvider {
     }
   }
 
+  public async storePrivateKeyHistory(userId, email, ipfs) {
+    const privateKey = this.gun.get(userId).put({
+      key: ipfs
+    });
+
+    this.gun
+      .get(this.osnPrefix)
+      .get("privateKey")
+      .set(privateKey);
+
+  }
+
+  public async getPvtKeyHistory(userId) {
+    // console.log("user id looking for key hsitory",userId);
+    let entry = await this.gun
+      .get(this.osnPrefix)
+      .get("privateKey");
+      // console.log("entry:",entry);
+
+    const pvtKeyEntry = await this.gun
+      .get(this.osnPrefix)
+      .get("privateKey")
+      .get(userId)
+      .then();
+
+    console.log("private key entry:", pvtKeyEntry);
+    if (pvtKeyEntry === undefined) {
+      return null;
+    } else {
+      return pvtKeyEntry;
+    }
+  }
+
 
 
   

+ 9 - 0
app/src/providers/p2p-storage-ipfs/p2p-storage-ipfs.ts

@@ -29,6 +29,15 @@ export class P2pStorageIpfsProvider {
     return this.http.post(this.infuraUrl + "add", formData).toPromise();
   }
 
+  /**
+   * Store private key history on ipfs
+   * @param privateKeyHistory private key history object
+   */
+  public storePrivateKey(privateKeyHistory) {
+    return this.storeOnIPFS(privateKeyHistory);
+  }
+
+
   /**
    * fetch data from ipfs for hash
    * @param hash address hash

+ 1 - 0
app/src/providers/pgp-key-server/pgp-key-server.ts

@@ -1,6 +1,7 @@
 import { Injectable } from "@angular/core";
 import { Storage } from "@ionic/storage";
 import * as openpgp from 'openpgp';
+import { P2pDatabaseGunProvider } from "../../providers/p2p-database-gun/p2p-database-gun";
 
 @Injectable()
 export class PgpKeyServerProvider {