p2p-storage-ipfs.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { HttpClient } from "@angular/common/http";
  2. import { Injectable } from "@angular/core";
  3. @Injectable()
  4. export class P2pStorageIpfsProvider {
  5. private infuraUrl = "https://ipfs.infura.io:5001/api/v0/";
  6. constructor(public http: HttpClient) {}
  7. public storeTweet(tweet) {
  8. return this.storeOnIPFS(tweet);
  9. }
  10. public storePublicKey(publicKeyHistory) {
  11. return this.storeOnIPFS(publicKeyHistory);
  12. }
  13. private storeOnIPFS(json) {
  14. const formData = new FormData();
  15. formData.append("data", JSON.stringify(json));
  16. return this.http.post(this.infuraUrl + "add", formData).toPromise();
  17. }
  18. public async fetchJson(hash: string) {
  19. const options = {
  20. params: { arg: hash }
  21. };
  22. return await this.http.get(this.infuraUrl + "cat", options).toPromise();
  23. }
  24. public fetchTweet(hash: string): Promise<string> {
  25. const options = {
  26. params: { arg: hash }
  27. };
  28. return this.http.get<string>(this.infuraUrl + "cat", options).toPromise();
  29. }
  30. public async fetchTweets(hashs: string[]): Promise<string[]> {
  31. return await Promise.all(hashs.map(hash => this.fetchTweet(hash)));
  32. }
  33. }