// Generate a random private keyuint8_tpkBytes[IOTEX_PRIVATE_KEY_SIZE];randomGenerator.fillRandom(pkBytes,sizeof(pkBytes));// Create the account Accountaccount(pkBytes);
Getting account details
Now that the account is created, you can get the address in Ethereum or IoTeX format, and the public/private keys:
// Create buffers for the account detailscharpublicKeyBuf[IOTEX_PUBLIC_KEY_C_STRING_SIZE] = {0};charprivateKeyBuf[IOTEX_PRIVATE_KEY_C_STRING_SIZE] = {0};charethAddressBuf[ETH_ADDRESS_C_STRING_SIZE] = {0};charioAddressBuf[IOTEX_ADDRESS_C_STRING_SIZE] = {0};// Get the account details (ad hexadecimal strings)account.getPublicKeyString(publicKeyBuf);account.getPrivateKeyString(privateKeyBuf);account.getEthereumAddress(ethAddressBuf);account.getIotexAddress(ioAddressBuf);
Sending a transfer of IOTX
Sends a transfer of IOTX tokens
First create the account object:
// Private key of the origin addressconstchar pkString[] =<private_key>;// Destination IoTeX addresschardestinationAddr[IOTEX_ADDRESS_C_STRING_SIZE] =<destination_address>;// Convert the private key and address hex strings to byte arraysuint8_tpkBytes[IOTEX_PRIVATE_KEY_SIZE];signer.str2hex(pkString, pkBytes, IOTEX_SIGNATURE_SIZE);// Create the account objectAccountoriginAccount(pkBytes);// Get the IoTeX addresscharoriginIotexAddr[IOTEX_ADDRESS_C_STRING_SIZE] ="";originAccount.getIotexAddress(originIotexAddr);
Then query the blockchain to get the account nonce:
AccountMeta accMeta;ResultCode result =connection.api.wallets.getAccount(originIotexAddr, accMeta);IotexString nonceString =accMeta.pendingNonce;uint64_t nonce =atoi(nonceString.c_str());
Now broadcast the transfer with the retrieved nonce:
// Amount of RAU to transfer. Eg: 1 RAUcharamount[IOTEX_ADDRESS_C_STRING_SIZE] ="1";// Send the transfer uint8_thash[IOTEX_HASH_SIZE] = {0};result = originAccount.sendTokenTransferAction(connection, nonce, 20000000, "1000000000000", amount, destinationAddr, hash);
First initialize the storage module (not needed for Nano33 IoT or Linux):
// The number of bytes we want to use for the EEPROM// This is passed to EEPROM.begin()// Should be at least IOTEX_PRIVATE_KEY_SIZEuint32_t eepromSize = IOTEX_PRIVATE_KEY_SIZE;storage.Initialize(eepromSize);
Now store the private key:
// Specify any address number between 0 and (eepromSize - IOTEX_PRIVATE_KEY_SIZE). // This is the EEPROM address where the private key is stored. // There needs to be suficcient space between eepromAddress and eepromSize to store the whole private key (at least IOTEX_PRIVATE_KEY_SIZE bytes)
constuint32_t eepromAddress =0;ResultCode result =storage.savePrivateKey(eepromAddress, pkBytes);
You can also read the private key that is stored in the device:
result =storage.readPrivateKey(eepromAddress, pkBytes);
// The token address. Eg: VITA tokenconstchar tokenAddress[] ="io1hp6y4eqr90j7tmul4w2wa8pm7wx462hq0mg4tw";uint8_thash[IOTEX_HASH_SIZE] = {0};ResultCode result = originAccount.sendExecutionAction(connection, atoi(accMeta.pendingNonce.c_str()), 20000000, "1000000000000", "0", tokenAddress, callData, hash);
Calling a contract function
This example shows how to call a contract function using the addData function of the contract with address io1n49gavyahsukdvvxxandkxephdx93n3atcrqur as an example. You can find the contract ABI here.
This documentation portal is currently undergoing updates to align with the IoTeX 2.0 Whitepaper release. Information provided here may be incomplete, or out-of-date. Please use this portal for preliminary reference only, and check out the official IoTeX 2.0 Whitepaper for updated information.