Fabcoin Core  0.16.2
P2P Digital Currency
Personal.cpp
Go to the documentation of this file.
1 #include <jsonrpccpp/common/exception.h>
4 #include <libethcore/CommonJS.h>
6 #include <libethereum/Client.h>
7 
8 #include "Personal.h"
9 
10 using namespace std;
11 using namespace dev;
12 using namespace dev::rpc;
13 using namespace dev::eth;
14 using namespace jsonrpc;
15 
16 Personal::Personal(KeyManager& _keyManager, AccountHolder& _accountHolder, eth::Interface& _eth):
17  m_keyManager(_keyManager),
18  m_accountHolder(_accountHolder),
19  m_eth(_eth)
20 {
21 }
22 
23 std::string Personal::personal_newAccount(std::string const& _password)
24 {
25  KeyPair p = KeyManager::newKeyPair(KeyManager::NewKeyType::NoVanity);
26  m_keyManager.import(p.secret(), std::string(), _password, std::string());
27  return toJS(p.address());
28 }
29 
30 string Personal::personal_sendTransaction(Json::Value const& _transaction, string const& _password)
31 {
33  try
34  {
35  t = toTransactionSkeleton(_transaction);
36  }
37  catch (...)
38  {
39  BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
40  }
41 
42  if (Secret s = m_keyManager.secret(t.from, [&](){ return _password; }, false))
43  {
44  // return the tx hash
45  return toJS(m_eth.submitTransaction(t, s).first);
46  }
47  BOOST_THROW_EXCEPTION(JsonRpcException("Invalid password or account."));
48 }
49 
50 string Personal::personal_signAndSendTransaction(Json::Value const& _transaction, string const& _password)
51 {
52  return personal_sendTransaction(_transaction, _password);
53 }
54 
55 bool Personal::personal_unlockAccount(std::string const& _address, std::string const& _password, int _duration)
56 {
57  return m_accountHolder.unlockAccount(Address(fromHex(_address, WhenError::Throw)), _password, _duration);
58 }
59 
61 {
62  return toJson(m_keyManager.accounts());
63 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
virtual bool personal_unlockAccount(std::string const &_address, std::string const &_password, int _duration) override
Definition: Personal.cpp:55
Manages real accounts (where we know the secret key) and proxy accounts (where transactions to be sen...
Definition: AccountHolder.h:64
Simple class that represents a "key pair".
Definition: Common.h:150
h160 Address
An Ethereum address: 20 bytes.
Definition: Common.h:62
virtual std::pair< h256, Address > submitTransaction(TransactionSkeleton const &_t, Secret const &_secret)=0
Submits a new transaction.
virtual bool unlockAccount(Address const &, std::string const &, unsigned)
Automatically authenticate all transactions for the given account for the next _duration seconds...
Definition: AccountHolder.h:82
std::hash for asio::adress
Definition: Common.h:323
Secret const & secret() const
Definition: Common.h:167
TransactionSkeleton toTransactionSkeleton(Json::Value const &_json)
Definition: JsonHelper.cpp:354
Secret secret(Address const &_address, std::function< std::string()> const &_pass=DontKnowThrow, bool _usePasswordCache=true) const
h128 import(Secret const &_s, std::string const &_accountName, std::string const &_pass, std::string const &_passwordHint)
dev::eth::KeyManager & m_keyManager
Definition: Personal.h:32
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
Definition: CommonData.cpp:99
dev::eth::AccountHolder & m_accountHolder
Definition: Personal.h:33
Config::Value_type Value
High-level manager of password-encrypted keys for Ethereum.
Definition: KeyManager.h:72
std::string toJS(FixedHash< S > const &_h)
Definition: CommonJS.h:34
dev::eth::Interface & m_eth
Definition: Personal.h:34
virtual std::string personal_signAndSendTransaction(Json::Value const &_transaction, std::string const &_password) override
Definition: Personal.cpp:50
Main API hub for interfacing with Ethereum.
Definition: Interface.h:67
Address const & address() const
Retrieve the associated address of the public key.
Definition: Common.h:173
Json::Value toJson(unordered_map< u256, u256 > const &_storage)
Definition: JsonHelper.cpp:41
virtual std::string personal_sendTransaction(Json::Value const &_transaction, std::string const &_password) override
Definition: Personal.cpp:30
Addresses accounts() const
Definition: KeyManager.cpp:328
virtual std::string personal_newAccount(std::string const &_password) override
Definition: Personal.cpp:23
virtual Json::Value personal_listAccounts() override
Definition: Personal.cpp:60