Browse Source

Publish pulic key

Carsten Porth 5 years ago
parent
commit
b7b0c850b9

+ 3 - 1
app/src/app/app.module.ts

@@ -34,6 +34,7 @@ import { SearchResultsTweetsPopularPage } from "../pages/search-results-tweets-p
 import { SearchResultsTweetsRecentPage } from "../pages/search-results-tweets-recent/search-results-tweets-recent";
 import { SearchResultsTweetsTabsPage } from "../pages/search-results-tweets-tabs/search-results-tweets-tabs";
 import { AboutPage } from "../pages/about/about";
+import { CryptoProvider } from '../providers/crypto/crypto';
 
 @NgModule({
   declarations: [
@@ -90,7 +91,8 @@ import { AboutPage } from "../pages/about/about";
     TwitterApiProvider,
     P2pStorageIpfsProvider,
     P2pDatabaseGunProvider,
-    FeedProvider
+    FeedProvider,
+    CryptoProvider
   ]
 })
 export class AppModule {}

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

@@ -29,6 +29,7 @@
       <ion-label color="primary" stacked>Private 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>
     </ion-card-content>
   </ion-card>
 

+ 6 - 0
app/src/pages/settings/settings.ts

@@ -1,6 +1,7 @@
 import { Component } from "@angular/core";
 import { NavController, ToastController } from "ionic-angular";
 import { Storage } from "@ionic/storage";
+import { CryptoProvider } from "../../providers/crypto/crypto";
 
 @Component({
   selector: "page-settings",
@@ -14,6 +15,7 @@ export class SettingsPage {
   constructor(
     public navCtrl: NavController,
     public toastCtrl: ToastController,
+    private cryptoUtils: CryptoProvider,
     private storage: Storage
   ) {
     this.loadValuesFromStorage();
@@ -45,6 +47,10 @@ export class SettingsPage {
     toast.present();
   }
 
+  publishPublicKey() {
+    this.cryptoUtils.publishPublicKey(this.publicKey);
+  }
+
   private async generateRsaKeys() {
     return await crypto.subtle.generateKey(
       {

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

@@ -0,0 +1,75 @@
+import { Injectable } from "@angular/core";
+import { TwitterApiProvider } from "../twitter-api/twitter-api";
+import { P2pStorageIpfsProvider } from "../p2p-storage-ipfs/p2p-storage-ipfs";
+import { Storage } from "@ionic/storage";
+
+@Injectable()
+export class CryptoProvider {
+  ownUserId: string;
+
+  constructor(
+    private twitter: TwitterApiProvider,
+    private ipfs: P2pStorageIpfsProvider,
+    private storage: Storage
+  ) {
+    this.init();
+  }
+
+  private async init() {
+    this.ownUserId = await this.storage.get("userId");
+  }
+
+  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);
+    }
+
+    // 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 (link.length) {
+      publicKeyHistory["keys"].push(newKey);
+    } else {
+      publicKeyHistory = {
+        keys: [newKey]
+      };
+    }
+
+    // Publish updated key history...
+    const res = await this.ipfs.storePublicKey(publicKeyHistory);
+
+    // ... and update description in user profile
+    this.twitter.updateProfileDescription(
+      "ipfs://" + res["Hash"] + " #hybridOSN"
+    );
+  }
+
+  private extractLinkFromDescription(profileDescription: string): string {
+    for (let word of profileDescription.split(" ")) {
+      if (this.isLink(word)) {
+        return word.substr(7);
+      }
+    }
+    return "";
+  }
+
+  private isLink(word: string): boolean {
+    const hits = word.match(/ipfs:\/\/Qm[a-zA-Z0-9]+/);
+    return hits != null;
+  }
+}

+ 17 - 7
app/src/providers/p2p-storage-ipfs/p2p-storage-ipfs.ts

@@ -1,12 +1,6 @@
 import { HttpClient } from "@angular/common/http";
 import { Injectable } from "@angular/core";
 
-/*
-  Generated class for the P2pStorageIpfsProvider provider.
-
-  See https://angular.io/guide/dependency-injection for more info on providers
-  and Angular DI.
-*/
 @Injectable()
 export class P2pStorageIpfsProvider {
   private infuraUrl = "https://ipfs.infura.io:5001/api/v0/";
@@ -14,12 +8,28 @@ export class P2pStorageIpfsProvider {
   constructor(public http: HttpClient) {}
 
   public storeTweet(tweet) {
+    return this.storeOnIPFS(tweet);
+  }
+
+  public storePublicKey(publicKeyHistory) {
+    return this.storeOnIPFS(publicKeyHistory);
+  }
+
+  private storeOnIPFS(json) {
     const formData = new FormData();
-    formData.append("data", JSON.stringify(tweet));
+    formData.append("data", JSON.stringify(json));
 
     return this.http.post(this.infuraUrl + "add", formData).toPromise();
   }
 
+  public async fetchJson(hash: string) {
+    const options = {
+      params: { arg: hash }
+    };
+
+    return await this.http.get(this.infuraUrl + "cat", options).toPromise();
+  }
+
   public fetchTweet(hash: string) {
     const options = {
       params: { arg: hash }

+ 7 - 0
app/src/providers/twitter-api/twitter-api.ts

@@ -175,4 +175,11 @@ export class TwitterApiProvider {
     });
     return res.data;
   }
+
+  public async updateProfileDescription(description: string) {
+    const res = await this.client.post("account/update_profile", {
+      description: description
+    });
+    return res.data;
+  }
 }