Published 27 June 2019

Examples: C++: Send transaction

Project:

https://github.com/CREDITSCOM/examples

Branch:

https://github.com/CREDITSCOM/examples/tree/master/API/CreditsCPlusPlusDemoCMake

 

In order to transfer the tokens to the specified address, wallet’s public key, private key and public address of transfer destination have to be shared to the class Client

 

Client.h

void set_keys(const std::string& publicKey, const std::string& privateKey, const std::string& targetKey);

 

Client.cpp

void client::set_keys(const std::string& publicKey, const std::string& privateKey, const std::string& targetKey)

{

  m_keys = make_unique<keys>(publicKey.c_str(), privateKey.c_str(), targetKey.c_str());

}

 

method of the class Client for token transfer

 

Client.h

void transfer_coins(int32_t integral, int32_t fraction, double fee_value);

 

Client.cpp

// gets integer and fraction parts of the tokens amount for transfer and reward amount (the transfer this amount equals 0.9 by default)

 

void client::transfer_coins(int32_t integral, int32_t fraction, double fee_value)

{

  try

  {

    auto tr = make_transaction(integral, fraction, fee_value);

    TransactionFlowResult tfr;

    m_api->TransactionFlow(tfr, *tr);

    tfr.printTo(std::cout);

  }

  catch (const std::exception ex)

  {

    throw std::exception(ex.what());

  }

}

 

// method to create a transaction, form its signature and to sign the  transaction

std::unique_ptr<api::Transaction> client::make_transaction(int32_t integral, int32_t fraction, double fee_value)

{

  auto tr = std::make_unique<api::Transaction>();

 

  WalletTransactionsCountGetResult wtcgr;

  auto& pka = m_keys->PublicKeyAddress();

  m_api->WalletTransactionsCountGet(wtcgr, pka);

 

  tr->id = wtcgr.lastTransactionInnerId + 1;

  tr->source = std::string{ m_keys->PublicKeyAddress() };

  tr->target = std::string{ m_keys->TargetPublicKeyAddress() };

  tr->amount.integral = integral;

  tr->amount.fraction = fraction;

  tr->fee.commission = fee(fee_value);

  tr->currency = 1;

 

  std::vector<byte> msg;

 

  cp(msg, tr->id, 6);

  copy(&((unsigned char*)tr->source.c_str())[0], &((unsigned char*)tr->source.c_str())[tr->source.size()], back_inserter(msg));

  copy(&((unsigned char*)tr->target.c_str())[0], &((unsigned char*)tr->target.c_str())[tr->target.size()], back_inserter(msg));

  cp(msg, tr->amount.integral, 4);

  cp(msg, tr->amount.fraction, 8);

  cp(msg, tr->fee.commission, 2); 

  msg.push_back(1); 

  msg.push_back(0);

 

  unsigned char signature[SIGNATURE_LEN];

  unsigned long long signatureLen;

  crypto_sign_detached(signature, &signatureLen, msg.data(), msg.size(), (unsigned char*)m_keys->PrivateKeyAddress().c_str());

 

  if (crypto_sign_verify_detached(signature, msg.data(), msg.size(), (unsigned char*)tr->source.c_str()) != 0) {

    throw std::exception("Incorrect signature!");

  }

 

  tr->signature = std::string{ reinterpret_cast<char*>(signature), SIGNATURE_LEN };

 

  return std::move(tr);

}

 

Call example

int main(int argc, char* argv[])

{

  auto c = make_unique<client>(“127.0.0.1”, 9090);

  c->set_keys(“your public key”, “your private key”, “your target key”);

  // method transfers 1 CS and displays the result on the screen

  c->transfer_coins(1, 0, 0.9);

}

Votes 0, average rating 0

Contents