IoTeX client
account.h
1#ifndef IOTEX_ACCOUNT_H
2#define IOTEX_ACCOUNT_H
3
4#include "connection/connection.h"
5#include "encoder/encoder.h"
6#include "IoTeXConstants.h"
7#include "protobuf/pb_api.h"
8#include "random/random.h"
9#include "signer/signer.h"
10#include <stddef.h>
11#include <stdint.h>
12
13namespace iotex
14{
16{
17 public:
22 Account();
23
29 Account(uint8_t privateKey[IOTEX_PRIVATE_KEY_SIZE]);
30
31 /**************************************************************************/
32 /* Getters */
33 /**************************************************************************/
34
40 void getIotexAddress(char buffer[IOTEX_ADDRESS_C_STRING_SIZE]);
41
47 void getEthereumAddress(char buffer[ETH_ADDRESS_C_STRING_SIZE]);
48
54 void getEthereumAddressBytes(uint8_t buffer[ETH_ADDRESS_SIZE]);
55
61 void getPublicKey(uint8_t buffer[IOTEX_PUBLIC_KEY_SIZE]);
62
68 void getPublicKeyString(char buffer[IOTEX_PUBLIC_KEY_C_STRING_SIZE]);
69
75 void getPrivateKey(uint8_t buffer[IOTEX_PRIVATE_KEY_SIZE]);
76
82 void getPrivateKeyString(char buffer[IOTEX_PRIVATE_KEY_C_STRING_SIZE]);
83
84 /**************************************************************************/
85 /* Signing */
86 /**************************************************************************/
87
95 void signMessage(const uint8_t* message, size_t size, uint8_t signature[IOTEX_SIGNATURE_SIZE]);
96
104 uint8_t signature[IOTEX_SIGNATURE_SIZE]);
105
114 uint8_t signature[IOTEX_SIGNATURE_SIZE],
115 uint8_t hash[IOTEX_HASH_SIZE] = nullptr);
116
117 /**************************************************************************/
118 /* Action execution */
119 /**************************************************************************/
120
134 template<typename TAPI>
135 ResultCode sendTokenTransferAction(Connection<TAPI>& conn, uint64_t nonce, uint64_t gasLimit,
136 const char* gasPrice, const char* amount,
137 const char* recipient, uint8_t hash[IOTEX_HASH_SIZE])
138 {
140 core.version = 1;
141 core.gasLimit = gasLimit;
142 core.nonce = nonce;
143 strcpy(core.gasPrice, gasPrice);
144 core.chainId = 0;
145 strcpy(core.transfer.amount, amount);
146 core.transfer.payload = "";
147 strcpy(core.transfer.recipient, recipient);
148
149 // Sign
150 uint8_t signature[IOTEX_SIGNATURE_SIZE];
151 signTokenTransferAction(core, signature);
152
153 return conn.api.wallets.sendTokenTransfer(_publicKey, signature, core, hash);
154 }
155
170 template<typename TAPI>
171 ResultCode sendExecutionAction(Connection<TAPI>& conn, uint64_t nonce, uint64_t gasLimit,
172 const char* gasPrice, const char* amount,
173 const char contract[IOTEX_ADDRESS_C_STRING_SIZE],
174 IotexString data, uint8_t hash[IOTEX_HASH_SIZE])
175 {
177 core.version = 1;
178 core.gasLimit = gasLimit;
179 core.nonce = nonce;
180 strcpy(core.gasPrice, gasPrice);
181 strcpy(core.execution.amount, amount);
182 strcpy(core.execution.contract, contract);
183 core.execution.data = data;
184
185 // Sign
186 uint8_t signature[IOTEX_SIGNATURE_SIZE];
187 signExecutionAction(core, signature);
188
189 return conn.api.wallets.sendExecution(_publicKey, signature, core, hash);
190 }
191
192 private:
193 IotexString _iotexAddr;
194 IotexString _ethAddr;
195 uint8_t _publicKey[IOTEX_PUBLIC_KEY_SIZE];
196 uint8_t _privateKey[IOTEX_PRIVATE_KEY_SIZE];
197
198 //**************************************************************************/
199 /* Dependencies */
200 /**************************************************************************/
201 Random* _pRandomGenerator;
202 Encoder* _pEncoder;
203 Signer* _pSigner;
204
205 /**************************************************************************/
206 /* Private methods */
207 /**************************************************************************/
208 private:
209 void GenerateAddressesFromPrivateKey();
210 void generateIotexAddress();
211 void generateEthAddress();
212 void setDepsFromGlobals();
213
214 /**************************************************************************/
215 /* Unit testing methods */
216 /**************************************************************************/
217 public:
218 // Allow to inject mock dependencies in constructor
219 Account(Random* pRandomGenerator, Encoder* pEncoder, Signer* pSigner,
220 uint8_t privateKey[IOTEX_PRIVATE_KEY_SIZE] = nullptr);
221};
222} // namespace iotex
223
224#endif
Definition: account.h:16
void signTokenTransferAction(iotex::ResponseTypes::ActionCore_Transfer &transfer, uint8_t signature[IOTEX_SIGNATURE_SIZE])
Signs a token transfer action.
Definition: account.cpp:99
void getIotexAddress(char buffer[IOTEX_ADDRESS_C_STRING_SIZE])
Get the IoTeX address as a null terminated string.
Definition: account.cpp:54
void getPublicKeyString(char buffer[IOTEX_PUBLIC_KEY_C_STRING_SIZE])
Get the public key as a null terminated string.
Definition: account.cpp:74
Account()
Construct a new Account object with a new randomly generated address.
Definition: account.cpp:16
ResultCode sendTokenTransferAction(Connection< TAPI > &conn, uint64_t nonce, uint64_t gasLimit, const char *gasPrice, const char *amount, const char *recipient, uint8_t hash[IOTEX_HASH_SIZE])
Sends a token transfer action to the blockchain.
Definition: account.h:135
void getEthereumAddress(char buffer[ETH_ADDRESS_C_STRING_SIZE])
Get the Ethereum address as a null terminated string.
Definition: account.cpp:59
void getPrivateKey(uint8_t buffer[IOTEX_PRIVATE_KEY_SIZE])
Get the private key as a byte array.
Definition: account.cpp:79
ResultCode sendExecutionAction(Connection< TAPI > &conn, uint64_t nonce, uint64_t gasLimit, const char *gasPrice, const char *amount, const char contract[IOTEX_ADDRESS_C_STRING_SIZE], IotexString data, uint8_t hash[IOTEX_HASH_SIZE])
Sends an execution action to the blockchain.
Definition: account.h:171
void getPublicKey(uint8_t buffer[IOTEX_PUBLIC_KEY_SIZE])
Get the public key as a byte array.
Definition: account.cpp:69
void signExecutionAction(iotex::ResponseTypes::ActionCore_Execution &execution, uint8_t signature[IOTEX_SIGNATURE_SIZE], uint8_t hash[IOTEX_HASH_SIZE]=nullptr)
Signs an execution action.
Definition: account.cpp:111
void getPrivateKeyString(char buffer[IOTEX_PRIVATE_KEY_C_STRING_SIZE])
Get the private key as a null terminated string.
Definition: account.cpp:84
void signMessage(const uint8_t *message, size_t size, uint8_t signature[IOTEX_SIGNATURE_SIZE])
Singns a message.
Definition: account.cpp:93
void getEthereumAddressBytes(uint8_t buffer[ETH_ADDRESS_SIZE])
Get the Ethereum address as a byte array.
Definition: account.cpp:64
Definition: connection.h:13
Definition: encoder.h:15
Definition: random.h:10
Definition: signer.h:12
Definition: abi.h:12