Browse Source

publish link to key history in tweet and the tweet id to the profile desc

Carsten Porth 5 years ago
parent
commit
d9deabce96
1 changed files with 37 additions and 8 deletions
  1. 37 8
      app/src/providers/crypto/crypto.ts

+ 37 - 8
app/src/providers/crypto/crypto.ts

@@ -39,13 +39,20 @@ export class CryptoProvider {
       };
     }
 
+    // TODO: encrypt key history with AES
+
     // Publish updated key history...
     const res = await this.ipfs.storePublicKey(publicKeyHistory);
 
-    // ... and update description in user profile
-    this.twitter.updateProfileDescription(
+    // tweet ipfs link
+    const tweetResponse = await this.twitter.tweet(
       "ipfs://" + res["Hash"] + " #hybridOSN"
     );
+
+    // ... and update description in user profile with tweet id
+    this.twitter.updateProfileDescription(
+      "tweet://" + tweetResponse["data"]["id_str"] + " #hybridOSN"
+    );
   }
 
   private async getKeyHistory(userId: string) {
@@ -53,31 +60,53 @@ export class CryptoProvider {
     const userData = await this.twitter.fetchUser(userId);
     const profileDescription = userData["description"];
 
+    // Get tweet with link to key history
+    const tweetId = this.extractTweetId(profileDescription);
+    const tweetWithKeyHistoryLink = await this.twitter.fetchTweet(tweetId);
+
     // Extract link to public key
-    const link = this.extractLinkFromDescription(profileDescription);
+    const link = this.extractLinkFromDescription(
+      tweetWithKeyHistoryLink["data"]["full_text"]
+    );
 
     // Fetch public key history
     if (link.length) {
-      return await this.ipfs.fetchJson(link);
+      const keyHistory = await this.ipfs.fetchJson(link);
+      // TODO: dedcrypt key history with AES
+      return keyHistory;
     } else {
       return null;
     }
   }
 
-  private extractLinkFromDescription(profileDescription: string): string {
-    for (let word of profileDescription.split(" ")) {
-      if (this.isLink(word)) {
+  private extractTweetId(text: string) {
+    for (let word of text.split(" ")) {
+      if (this.isTweetId(word)) {
+        return word.substr(8);
+      }
+    }
+    return "";
+  }
+
+  private extractLinkFromDescription(text: string): string {
+    for (let word of text.split(" ")) {
+      if (this.isIpfsLink(word)) {
         return word.substr(7);
       }
     }
     return "";
   }
 
-  private isLink(word: string): boolean {
+  private isIpfsLink(word: string): boolean {
     const hits = word.match(/ipfs:\/\/Qm[a-zA-Z0-9]+/);
     return hits != null;
   }
 
+  private isTweetId(word: string): boolean {
+    const hits = word.match(/tweet:\/\/[0-9]+/);
+    return hits != null;
+  }
+
   public async generateRsaKeys() {
     return await crypto.subtle.generateKey(
       {