Browse Source

fetch correct key from key history for respective tweet

Carsten Porth 5 years ago
parent
commit
b9fe8ec047

+ 2 - 2
app/src/providers/crypto/crypto.ts

@@ -140,8 +140,8 @@ export class CryptoProvider {
     return key.decryptPublic(encryptedMessage).toString();
   }
 
-  public async fetchPublicKeyForUser(userId: string): Promise<string> {
+  public async fetchPublicKeyHistoryForUser(userId: string): Promise<object[]> {
     const keyHistory = await this.getKeyHistory(userId);
-    return keyHistory["keys"].reverse()[0]["key"];
+    return keyHistory["keys"].reverse();
   }
 }

+ 39 - 10
app/src/providers/feed/feed.ts

@@ -32,7 +32,7 @@ export class FeedProvider {
     const intervalEnd: Date = this.getOldestTweetTimestamp(tweets);
 
     // Fetch private tweet hashs from P2P DB for corresponding interval
-    const privateTweetHashs: string[] = await this.gun.fetchPrivateTweetHashsForUserInInterval(
+    const privateTweetHashs: object[] = await this.gun.fetchPrivateTweetHashsForUserInInterval(
       userId,
       intervalStart,
       intervalEnd
@@ -107,19 +107,33 @@ export class FeedProvider {
     }
   }
 
-  private async fetchPrivateTweets(privateTweetHashs, userId: string) {
+  private async fetchPrivateTweets(
+    privateTweetsData: object[],
+    userId: string
+  ) {
+    const privateTweets = [];
+
     // Load private tweets from P2P storage
-    const privateEncryptedTweets = await this.ipfs.fetchTweets(
-      privateTweetHashs
-    );
+    for (let i = 0; i < privateTweetsData.length; i++) {
+      const hash = privateTweetsData[i]["hash"];
+      privateTweetsData[i]["encryptedTweet"] = await this.ipfs.fetchTweet(hash);
+    }
 
-    // Fetch public key
-    const publicKey = await this.cryptoUtils.fetchPublicKeyForUser(userId);
+    // Fetch public key history for user
+    const publicKeyHistory: object[] = await this.cryptoUtils.fetchPublicKeyHistoryForUser(
+      userId
+    );
 
     // Decrypt tweets
-    const privateTweets = privateEncryptedTweets.map(encryptedTweet =>
-      JSON.parse(this.cryptoUtils.decrypt(encryptedTweet, publicKey))
-    );
+    for (let i = 0; i < privateTweetsData.length; i++) {
+      const encryptedTweet = privateTweetsData[i]["encryptedTweet"];
+      const timestamp = privateTweetsData[i]["created_at"];
+      const decryptedTweet = this.cryptoUtils.decrypt(
+        encryptedTweet,
+        this.getPublicKeyAt(timestamp, publicKeyHistory)
+      );
+      privateTweets.push(JSON.parse(decryptedTweet));
+    }
 
     // Add user object to private tweets
     return await Promise.all(
@@ -127,6 +141,21 @@ export class FeedProvider {
     );
   }
 
+  private getPublicKeyAt(
+    timestamp: string,
+    publicKeyHistory: object[]
+  ): string {
+    const timestampTweet = new Date(timestamp).getTime();
+    for (let key of publicKeyHistory) {
+      const timestampKey = new Date(key["validFrom"]).getTime();
+      if (timestampTweet > timestampKey) {
+        return key["key"];
+      }
+    }
+    // todo: throw error
+    return "";
+  }
+
   private async addUserToTweet(tweet) {
     tweet.user = await this.twitter.fetchUser(tweet.user_id);
     return tweet;

+ 6 - 8
app/src/providers/p2p-database-gun/p2p-database-gun.ts

@@ -40,7 +40,7 @@ export class P2pDatabaseGunProvider {
     userId,
     intervalStart,
     intervalEnd
-  ): Promise<string[]> {
+  ): Promise<object[]> {
     const gunIds = await this.gun
       .get(this.osnPrefix + "-" + userId)
       .get("tweets")
@@ -53,13 +53,11 @@ export class P2pDatabaseGunProvider {
           .map(key => this.gun.get(key).then())
       );
 
-      return entries
-        .filter(
-          entry =>
-            new Date(entry["created_at"]) < intervalStart &&
-            new Date(entry["created_at"]) >= intervalEnd
-        )
-        .map(entry => entry["hash"]);
+      return entries.filter(
+        entry =>
+          new Date(entry["created_at"]) < intervalStart &&
+          new Date(entry["created_at"]) >= intervalEnd
+      );
     } else {
       return [];
     }