Explorar o código

encrypt private tweets 🎉

Carsten Porth %!s(int64=5) %!d(string=hai) anos
pai
achega
00b11a7259

+ 6 - 1
app/src/pages/write-tweet/write-tweet.ts

@@ -97,7 +97,12 @@ export class WriteTweetPage {
 
   private async tweetPrivate() {
     const tweet = await this.buildPrivateTweet();
-    const res = await this.ipfs.storeTweet(tweet);
+    const privateKey = await this.storage.get("privateKey");
+    const encryptedTweet = this.cryptoUtils.encrypt(
+      JSON.stringify(tweet),
+      privateKey
+    );
+    const res = await this.ipfs.storeTweet(encryptedTweet);
 
     this.gun.storeLastTweetHashForUser(
       tweet.user_id,

+ 5 - 0
app/src/providers/crypto/crypto.ts

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

+ 23 - 5
app/src/providers/feed/feed.ts

@@ -2,6 +2,7 @@ import { Injectable } from "@angular/core";
 import { TwitterApiProvider } from "../twitter-api/twitter-api";
 import { P2pDatabaseGunProvider } from "../p2p-database-gun/p2p-database-gun";
 import { P2pStorageIpfsProvider } from "../p2p-storage-ipfs/p2p-storage-ipfs";
+import { CryptoProvider } from "../crypto/crypto";
 
 @Injectable()
 export class FeedProvider {
@@ -10,7 +11,8 @@ export class FeedProvider {
   constructor(
     private twitter: TwitterApiProvider,
     private gun: P2pDatabaseGunProvider,
-    private ipfs: P2pStorageIpfsProvider
+    private ipfs: P2pStorageIpfsProvider,
+    private cryptoUtils: CryptoProvider
   ) {}
 
   public async loadUserTimeline(
@@ -37,7 +39,10 @@ export class FeedProvider {
     );
 
     if (privateTweetHashs.length) {
-      const privateTweets = await this.fetchPrivateTweets(privateTweetHashs);
+      const privateTweets = await this.fetchPrivateTweets(
+        privateTweetHashs,
+        userId
+      );
 
       // Combine and sort tweets
       return tweets
@@ -88,7 +93,10 @@ export class FeedProvider {
     );
 
     if (privateTweetHashs.length) {
-      const privateTweets = await this.fetchPrivateTweets(privateTweetHashs);
+      const privateTweets = await this.fetchPrivateTweets(
+        privateTweetHashs,
+        userId
+      );
 
       // Combine and sort tweets
       return tweets
@@ -99,9 +107,19 @@ export class FeedProvider {
     }
   }
 
-  private async fetchPrivateTweets(privateTweetHashs) {
+  private async fetchPrivateTweets(privateTweetHashs, userId: string) {
     // Load private tweets from P2P storage
-    const privateTweets = await this.ipfs.fetchTweets(privateTweetHashs);
+    const privateEncryptedTweets = await this.ipfs.fetchTweets(
+      privateTweetHashs
+    );
+
+    // Fetch public key
+    const publicKey = await this.cryptoUtils.fetchPublicKeyForUser(userId);
+
+    // Decrypt tweets
+    const privateTweets = privateEncryptedTweets.map(encryptedTweet =>
+      JSON.parse(this.cryptoUtils.decrypt(encryptedTweet, publicKey))
+    );
 
     // Add user object to private tweets
     return await Promise.all(