CommonJS.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 CommonJS.h
  15. * @authors:
  16. * Gav Wood <i@gavwood.com>
  17. * Marek Kotewicz <marek@ethdev.com>
  18. * @date 2014
  19. */
  20. #pragma once
  21. #include <string>
  22. #include <libdevcore/CommonJS.h>
  23. #include <libdevcrypto/Common.h>
  24. #include "Common.h"
  25. // devcrypto
  26. namespace dev
  27. {
  28. /// Leniently convert string to Public (h512). Accepts integers, "0x" prefixing, non-exact length.
  29. inline Public jsToPublic(std::string const& _s) { return jsToFixed<sizeof(dev::Public)>(_s); }
  30. /// Leniently convert string to Secret (h256). Accepts integers, "0x" prefixing, non-exact length.
  31. inline Secret jsToSecret(std::string const& _s) { h256 d = jsToFixed<sizeof(dev::Secret)>(_s); Secret ret(d); d.ref().cleanse(); return ret; }
  32. /// Leniently convert string to Address (h160). Accepts integers, "0x" prefixing, non-exact length.
  33. inline Address jsToAddress(std::string const& _s) { return eth::toAddress(_s); }
  34. /// Convert u256 into user-readable string. Returns int/hex value of 64 bits int, hex of 160 bits FixedHash. As a fallback try to handle input as h256.
  35. std::string prettyU256(u256 _n, bool _abridged = true);
  36. }
  37. // ethcore
  38. namespace dev
  39. {
  40. namespace eth
  41. {
  42. /// Convert to a block number, a bit like jsToInt, except that it correctly recognises "pending" and "latest".
  43. BlockNumber jsToBlockNumber(std::string const& _js);
  44. }
  45. }