Personal.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <jsonrpccpp/common/exception.h>
  2. #include <libethcore/KeyManager.h>
  3. #include <libweb3jsonrpc/AccountHolder.h>
  4. #include <libethcore/CommonJS.h>
  5. #include <libweb3jsonrpc/JsonHelper.h>
  6. #include <libethereum/Client.h>
  7. #include "Personal.h"
  8. using namespace std;
  9. using namespace dev;
  10. using namespace dev::rpc;
  11. using namespace dev::eth;
  12. using namespace jsonrpc;
  13. Personal::Personal(KeyManager& _keyManager, AccountHolder& _accountHolder, eth::Interface& _eth):
  14. m_keyManager(_keyManager),
  15. m_accountHolder(_accountHolder),
  16. m_eth(_eth)
  17. {
  18. }
  19. std::string Personal::personal_newAccount(std::string const& _password)
  20. {
  21. KeyPair p = KeyManager::newKeyPair(KeyManager::NewKeyType::NoVanity);
  22. m_keyManager.import(p.secret(), std::string(), _password, std::string());
  23. return toJS(p.address());
  24. }
  25. string Personal::personal_sendTransaction(Json::Value const& _transaction, string const& _password)
  26. {
  27. TransactionSkeleton t;
  28. try
  29. {
  30. t = toTransactionSkeleton(_transaction);
  31. }
  32. catch (...)
  33. {
  34. BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
  35. return string();
  36. }
  37. if (Secret s = m_keyManager.secret(t.from, [&](){ return _password; }, false))
  38. {
  39. // return the tx hash
  40. return toJS(m_eth.submitTransaction(t, s).first);
  41. }
  42. else
  43. BOOST_THROW_EXCEPTION(JsonRpcException("Invalid password or account."));
  44. return string();
  45. }
  46. string Personal::personal_signAndSendTransaction(Json::Value const& _transaction, string const& _password)
  47. {
  48. return personal_sendTransaction(_transaction, _password);
  49. }
  50. bool Personal::personal_unlockAccount(std::string const& _address, std::string const& _password, int _duration)
  51. {
  52. return m_accountHolder.unlockAccount(Address(fromHex(_address, WhenError::Throw)), _password, _duration);
  53. }
  54. Json::Value Personal::personal_listAccounts()
  55. {
  56. return toJson(m_keyManager.accounts());
  57. }