crypto.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { Injectable } from "@angular/core";
  2. import { TwitterApiProvider } from "../twitter-api/twitter-api";
  3. import { P2pStorageIpfsProvider } from "../p2p-storage-ipfs/p2p-storage-ipfs";
  4. import { Storage } from "@ionic/storage";
  5. @Injectable()
  6. export class CryptoProvider {
  7. ownUserId: string;
  8. constructor(
  9. private twitter: TwitterApiProvider,
  10. private ipfs: P2pStorageIpfsProvider,
  11. private storage: Storage
  12. ) {
  13. this.init();
  14. }
  15. private async init() {
  16. this.ownUserId = await this.storage.get("userId");
  17. }
  18. public async publishPublicKey(key: string) {
  19. let publicKeyHistory = await this.getKeyHistory(this.ownUserId);
  20. // Todo: avoid publishing the same public key twice - check if new key equals newest key in history
  21. // Add new key to history
  22. const newKey = {
  23. key: key,
  24. validFrom: Date.now()
  25. };
  26. if (publicKeyHistory) {
  27. publicKeyHistory["keys"].push(newKey);
  28. } else {
  29. publicKeyHistory = {
  30. keys: [newKey]
  31. };
  32. }
  33. // Publish updated key history...
  34. const res = await this.ipfs.storePublicKey(publicKeyHistory);
  35. // ... and update description in user profile
  36. this.twitter.updateProfileDescription(
  37. "ipfs://" + res["Hash"] + " #hybridOSN"
  38. );
  39. }
  40. private async getKeyHistory(userId: string) {
  41. // Get user description
  42. const userData = await this.twitter.fetchUser(userId);
  43. const profileDescription = userData["description"];
  44. // Extract link to public key
  45. const link = this.extractLinkFromDescription(profileDescription);
  46. // Fetch public key history
  47. if (link.length) {
  48. return await this.ipfs.fetchJson(link);
  49. } else {
  50. return null;
  51. }
  52. }
  53. private extractLinkFromDescription(profileDescription: string): string {
  54. for (let word of profileDescription.split(" ")) {
  55. if (this.isLink(word)) {
  56. return word.substr(7);
  57. }
  58. }
  59. return "";
  60. }
  61. private isLink(word: string): boolean {
  62. const hits = word.match(/ipfs:\/\/Qm[a-zA-Z0-9]+/);
  63. return hits != null;
  64. }
  65. public async generateRsaKeys() {
  66. return await crypto.subtle.generateKey(
  67. {
  68. name: "RSA-OAEP",
  69. modulusLength: 1024,
  70. publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
  71. hash: { name: "SHA-256" }
  72. },
  73. true,
  74. ["encrypt", "decrypt"]
  75. );
  76. }
  77. public async extractPrivateKey(keys): Promise<string> {
  78. return btoa(
  79. String.fromCharCode.apply(
  80. null,
  81. new Uint8Array(await crypto.subtle.exportKey("pkcs8", keys.privateKey))
  82. )
  83. );
  84. }
  85. public async extractPublicKey(keys): Promise<string> {
  86. return btoa(
  87. String.fromCharCode.apply(
  88. null,
  89. new Uint8Array(await crypto.subtle.exportKey("spki", keys.publicKey))
  90. )
  91. );
  92. }
  93. public async isPublicKeyPublished(): Promise<boolean> {
  94. const publicKey = await this.storage.get("publicKey");
  95. const keyHistory = await this.getKeyHistory(this.ownUserId);
  96. if (keyHistory && publicKey.length) {
  97. const newestPublicKey = keyHistory["keys"].reverse()[0]["key"];
  98. return newestPublicKey === publicKey;
  99. } else {
  100. return false;
  101. }
  102. }
  103. public async isPrivateKeySet(): Promise<boolean> {
  104. const privateKey = await this.storage.get("privateKey");
  105. return privateKey.length > 0;
  106. }
  107. }