IPFS.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file IPFS.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2015
  17. */
  18. #include "IPFS.h"
  19. #include <libdevcore/Hash.h>
  20. #include <libdevcore/Base58.h>
  21. using namespace std;
  22. using namespace dev;
  23. #include "libexecstream/exec-stream.h"
  24. static bytes exec(string const& _args)
  25. {
  26. string output;
  27. try
  28. {
  29. exec_stream_t es("ipfs", _args);
  30. do
  31. {
  32. string s;
  33. getline(es.out(), s);
  34. output += s;
  35. } while(es.out().good());
  36. }
  37. catch (exception const &e)
  38. {
  39. throw IPFSCommunicationError(e.what());
  40. }
  41. return bytes(output.begin(), output.end());
  42. }
  43. static void exec(string const& _args, bytesConstRef _in)
  44. {
  45. try
  46. {
  47. exec_stream_t es("ipfs", _args);
  48. es.in() << string(_in.begin(), _in.end());
  49. }
  50. catch (exception const &e)
  51. {
  52. throw IPFSCommunicationError(e.what());
  53. }
  54. }
  55. h256 IPFS::putBlockForSHA256(bytesConstRef _data)
  56. {
  57. exec("block put", _data);
  58. return sha256(_data);
  59. }
  60. bytes IPFS::putBlock(bytesConstRef _data)
  61. {
  62. return sha256AsMultihash(putBlockForSHA256(_data));
  63. }
  64. bytes IPFS::getBlockForSHA256(h256 const& _sha256)
  65. {
  66. auto b = sha256AsMultihash(_sha256);
  67. return getBlock(&b);
  68. }
  69. bytes IPFS::getBlock(bytesConstRef _multihash)
  70. {
  71. return exec("block get " + toBase58(_multihash));
  72. }