|
@@ -1,6 +1,7 @@
|
|
import { Injectable } from "@angular/core";
|
|
import { Injectable } from "@angular/core";
|
|
import { TwitterApiProvider } from "../twitter-api/twitter-api";
|
|
import { TwitterApiProvider } from "../twitter-api/twitter-api";
|
|
import { P2pStorageIpfsProvider } from "../p2p-storage-ipfs/p2p-storage-ipfs";
|
|
import { P2pStorageIpfsProvider } from "../p2p-storage-ipfs/p2p-storage-ipfs";
|
|
|
|
+import { P2pDatabaseGunProvider } from "../../providers/p2p-database-gun/p2p-database-gun";
|
|
import { Storage } from "@ionic/storage";
|
|
import { Storage } from "@ionic/storage";
|
|
import NodeRSA from "node-rsa";
|
|
import NodeRSA from "node-rsa";
|
|
import * as openpgp from 'openpgp';
|
|
import * as openpgp from 'openpgp';
|
|
@@ -11,84 +12,76 @@ export class CryptoProvider {
|
|
ownUserId: string;
|
|
ownUserId: string;
|
|
IV_LENGTH = 12;
|
|
IV_LENGTH = 12;
|
|
HYBRID_OSN_AES_KEY = "Z1vxAULQnZdoWhJOvv+hWEvVpyUHzNjD/ichEE2c8i4=";
|
|
HYBRID_OSN_AES_KEY = "Z1vxAULQnZdoWhJOvv+hWEvVpyUHzNjD/ichEE2c8i4=";
|
|
|
|
+ email: string;
|
|
|
|
|
|
constructor(
|
|
constructor(
|
|
private twitter: TwitterApiProvider,
|
|
private twitter: TwitterApiProvider,
|
|
private ipfs: P2pStorageIpfsProvider,
|
|
private ipfs: P2pStorageIpfsProvider,
|
|
- private storage: Storage
|
|
|
|
|
|
+ private storage: Storage,
|
|
|
|
+ private gun: P2pDatabaseGunProvider
|
|
) {
|
|
) {
|
|
this.init();
|
|
this.init();
|
|
}
|
|
}
|
|
|
|
|
|
private async init() {
|
|
private async init() {
|
|
this.ownUserId = await this.storage.get("userId");
|
|
this.ownUserId = await this.storage.get("userId");
|
|
|
|
+ this.email = await this.storage.get("email");
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* Publishs the public key history with the latest key
|
|
* Publishs the public key history with the latest key
|
|
* @param key key to publish
|
|
* @param key key to publish
|
|
*/
|
|
*/
|
|
- public async publishPublicKey(key: string) {
|
|
|
|
- let publicKeyHistory = await this.getKeyHistory(this.ownUserId);
|
|
|
|
-
|
|
|
|
|
|
+ public async publishPrivateKey(key: string) {
|
|
|
|
+ let privateKeyHistory = await this.getKeyHistory(this.ownUserId);
|
|
|
|
+ console.log("get publishPrivateKey crypto ", privateKeyHistory);
|
|
// Todo: avoid publishing the same public key twice - check if new key equals newest key in history
|
|
// 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 (publicKeyHistory) {
|
|
|
|
- publicKeyHistory["keys"].push(newKey);
|
|
|
|
- } else {
|
|
|
|
- publicKeyHistory = {
|
|
|
|
- keys: [newKey]
|
|
|
|
|
|
+ if ( key === privateKeyHistory[0])
|
|
|
|
+ {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ // Add new key to history
|
|
|
|
+ const newKey = {
|
|
|
|
+ key: key,
|
|
|
|
+ validFrom: Date.now()
|
|
};
|
|
};
|
|
|
|
+
|
|
|
|
+ if (privateKeyHistory) {
|
|
|
|
+ privateKeyHistory["keys"].push(newKey);
|
|
|
|
+ } else {
|
|
|
|
+ privateKeyHistory = {
|
|
|
|
+ keys: [newKey]
|
|
|
|
+ };
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- // Ecnrypt key history
|
|
|
|
- const encryptedPublicKeyHistory = await this.aesEncrypt(
|
|
|
|
- JSON.stringify(publicKeyHistory)
|
|
|
|
- );
|
|
|
|
|
|
+
|
|
|
|
+ // Ecnrypt key history
|
|
|
|
+ const encryptedPrivateKeyHistory = JSON.stringify(privateKeyHistory);
|
|
|
|
|
|
// Publish updated key history...
|
|
// Publish updated key history...
|
|
- const res = await this.ipfs.storePublicKey(encryptedPublicKeyHistory);
|
|
|
|
|
|
+ const res = await this.ipfs.storePrivateKey(encryptedPrivateKeyHistory);
|
|
|
|
|
|
- // tweet ipfs link
|
|
|
|
- const tweetResponse = await this.twitter.tweet(
|
|
|
|
- "ipfs://" + res["Hash"] + " #hybridOSN"
|
|
|
|
- );
|
|
|
|
|
|
+ // store ipfs link Of private tweet in gundb
|
|
|
|
+ // let ipfsLink = "ipfs://" + res["Hash"];
|
|
|
|
|
|
- // ... and update description in user profile with tweet id
|
|
|
|
- this.twitter.updateProfileDescription(
|
|
|
|
- "tweet://" + tweetResponse["data"]["id_str"] + " #hybridOSN"
|
|
|
|
- );
|
|
|
|
|
|
+ await this.gun.storePrivateKeyHistory(this.ownUserId, this.email, res["Hash"]);
|
|
}
|
|
}
|
|
|
|
|
|
- private async getKeyHistory(userId: string) {
|
|
|
|
- // Get user description
|
|
|
|
- const userData = await this.twitter.fetchUser(userId);
|
|
|
|
- const profileDescription = userData["description"];
|
|
|
|
|
|
+ public async getKeyHistory(userId: string) {
|
|
|
|
|
|
- // Get tweet with link to key history
|
|
|
|
- const tweetId = this.extractTweetId(profileDescription);
|
|
|
|
- if (tweetId.length === 0) {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- const tweetWithKeyHistoryLink = await this.twitter.fetchTweet(tweetId);
|
|
|
|
-
|
|
|
|
- // Extract link to public key
|
|
|
|
- const link = this.extractLinkFromDescription(
|
|
|
|
- tweetWithKeyHistoryLink["data"]["full_text"]
|
|
|
|
- );
|
|
|
|
|
|
+
|
|
|
|
+ //get private key history from gun
|
|
|
|
+ let link = await this.gun.getPvtKeyHistory(userId);
|
|
|
|
+ console.log("get link crypto ", link);
|
|
|
|
|
|
// Fetch public key history
|
|
// Fetch public key history
|
|
if (link.length) {
|
|
if (link.length) {
|
|
- const encryptedKeyHistory = await this.ipfs.fetchJson(link);
|
|
|
|
- // Decrypt key history
|
|
|
|
- const keyHistory = await this.aesDecrypt(encryptedKeyHistory.toString());
|
|
|
|
- return JSON.parse(keyHistory);
|
|
|
|
|
|
+ const encryptedKeyHistory = await this.ipfs.fetchJson(link);
|
|
|
|
+ console.log("get link private key crypto ", encryptedKeyHistory);
|
|
|
|
+ return JSON.parse(encryptedKeyHistory.toString());
|
|
} else {
|
|
} else {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|