Browse Source

Verify that keys are set and public key is published before tweeting p2p

Carsten Porth 5 years ago
parent
commit
4b70d87bb9
2 changed files with 69 additions and 26 deletions
  1. 34 12
      app/src/pages/write-tweet/write-tweet.ts
  2. 35 14
      app/src/providers/crypto/crypto.ts

+ 34 - 12
app/src/pages/write-tweet/write-tweet.ts

@@ -3,7 +3,8 @@ import {
   IonicPage,
   NavController,
   NavParams,
-  LoadingController
+  LoadingController,
+  AlertController
 } from "ionic-angular";
 import { FormBuilder, Validators, FormGroup } from "@angular/forms";
 import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
@@ -11,6 +12,7 @@ import { Storage } from "@ionic/storage";
 import { P2pStorageIpfsProvider } from "../../providers/p2p-storage-ipfs/p2p-storage-ipfs";
 import { P2pDatabaseGunProvider } from "../../providers/p2p-database-gun/p2p-database-gun";
 import twittertext from "twitter-text";
+import { CryptoProvider } from "../../providers/crypto/crypto";
 
 @IonicPage()
 @Component({
@@ -32,7 +34,9 @@ export class WriteTweetPage {
     private loadingCtrl: LoadingController,
     private storage: Storage,
     private ipfs: P2pStorageIpfsProvider,
-    private gun: P2pDatabaseGunProvider
+    private gun: P2pDatabaseGunProvider,
+    private cryptoUtils: CryptoProvider,
+    private alertCtrl: AlertController
   ) {
     this.retweetId = this.navParams.get("tweetId");
     this.replyToStatusId = this.navParams.get("replyToStatus");
@@ -64,16 +68,21 @@ export class WriteTweetPage {
     loading.present();
 
     if (this.tweet.value.p2p) {
-      const tweet = await this.buildPrivateTweet();
-      const res = await this.ipfs.storeTweet(tweet);
-
-      this.gun.storeLastTweetHashForUser(
-        tweet.user_id,
-        res["Hash"],
-        tweet.created_at
-      );
-
-      this.gun.publishHashtags(tweet.entities.hashtags);
+      if (
+        (await this.cryptoUtils.isPrivateKeySet()) &&
+        (await this.cryptoUtils.isPublicKeyPublished())
+      ) {
+        await this.tweetPrivate();
+      } else {
+        const alert = this.alertCtrl.create({
+          title: "Oooops...",
+          message:
+            "Please verify that you have set a private and public key in the settings and that your latest public key was published."
+        });
+        loading.dismiss();
+        alert.present();
+        return;
+      }
     } else {
       await this.twitter.tweet(
         this.tweet.value["text"],
@@ -86,6 +95,19 @@ export class WriteTweetPage {
     this.navCtrl.pop();
   }
 
+  private async tweetPrivate() {
+    const tweet = await this.buildPrivateTweet();
+    const res = await this.ipfs.storeTweet(tweet);
+
+    this.gun.storeLastTweetHashForUser(
+      tweet.user_id,
+      res["Hash"],
+      tweet.created_at
+    );
+
+    this.gun.publishHashtags(tweet.entities.hashtags);
+  }
+
   private async buildPrivateTweet() {
     const status = this.tweet.value["text"].trim();
     const entities = await this.getEntities(status);

+ 35 - 14
app/src/providers/crypto/crypto.ts

@@ -20,19 +20,7 @@ export class CryptoProvider {
   }
 
   public async publishPublicKey(key: string) {
-    let publicKeyHistory;
-
-    // Get user description
-    const userData = await this.twitter.fetchUser(this.ownUserId);
-    const profileDescription = userData["description"];
-
-    // Extract link to public key
-    const link = this.extractLinkFromDescription(profileDescription);
-
-    // Fetch public key history
-    if (link.length) {
-      publicKeyHistory = await this.ipfs.fetchJson(link);
-    }
+    let publicKeyHistory = await this.getKeyHistory(this.ownUserId);
 
     // Todo: avoid publishing the same public key twice - check if new key equals newest key in history
 
@@ -42,7 +30,7 @@ export class CryptoProvider {
       validFrom: Date.now()
     };
 
-    if (link.length) {
+    if (publicKeyHistory) {
       publicKeyHistory["keys"].push(newKey);
     } else {
       publicKeyHistory = {
@@ -59,6 +47,22 @@ export class CryptoProvider {
     );
   }
 
+  private async getKeyHistory(userId: string) {
+    // Get user description
+    const userData = await this.twitter.fetchUser(userId);
+    const profileDescription = userData["description"];
+
+    // Extract link to public key
+    const link = this.extractLinkFromDescription(profileDescription);
+
+    // Fetch public key history
+    if (link.length) {
+      return await this.ipfs.fetchJson(link);
+    } else {
+      return null;
+    }
+  }
+
   private extractLinkFromDescription(profileDescription: string): string {
     for (let word of profileDescription.split(" ")) {
       if (this.isLink(word)) {
@@ -103,4 +107,21 @@ export class CryptoProvider {
       )
     );
   }
+
+  public async isPublicKeyPublished(): Promise<boolean> {
+    const publicKey = await this.storage.get("publicKey");
+    const keyHistory = await this.getKeyHistory(this.ownUserId);
+
+    if (keyHistory && publicKey.length) {
+      const newestPublicKey = keyHistory["keys"].reverse()[0]["key"];
+      return newestPublicKey === publicKey;
+    } else {
+      return false;
+    }
+  }
+
+  public async isPrivateKeySet(): Promise<boolean> {
+    const privateKey = await this.storage.get("privateKey");
+    return privateKey.length > 0;
+  }
 }