Skip to content

Download Payment

Tan Xiu Haw edited this page Nov 10, 2023 · 4 revisions

Download Payment

  • Following parameters required:
    • Download Channel ID
    • Download Size
    • Feedback Fee
    • Maximum Fee
    • Deadline
    • Network ID
#include <xpxchaincpp/sdk.h>
#include <iostream>
#include <vector>

using namespace xpx_chain_sdk;

int main() 
{
  xpx_chain_sdk::Config config = xpx_chain_sdk::GetConfig();
  config.nodeAddress = "127.0.0.1";
  config.port = "3000";

  uint64_t driveSize = 1024;
  uint64_t replicatorsCount = 5;
  xpx_chain_sdk::Amount verificationFeeAmount = 100;
  xpx_chain_sdk::NetworkIdentifier networkIdentifier = xpx_chain_sdk::NetworkIdentifier::Mijin_Test;
  auto client = xpx_chain_sdk::getClient(config);

  xpx_chain_sdk::Hash256 downloadChannelID;
  // GET/download_channels
  xpx_chain_sdk::ParseHexStringIntoContainer("8D9F310639BF743017AE2FD2190D84767869A185DD7CA94967B2DF73176D12A6", 64, downloadChannelID);
  uint64_t downloadSize = 1000;
  xpx_chain_sdk::Amount feedbackFeeAmount = 10;
  std::vector<xpx_chain_sdk::Key> listOfPublicKeys;

  for (std::array<uint8_t, 32> key : listOfPublicKeys)
  {
    xpx_chain_sdk:: Key newKey;
    xpx_chain_sdk::ParseHexStringIntoContainer("0455F5326D5C380115E4BAE4B6EB2CEF4F83C3E5C4517494BFE11F5554135CF7", 64, newKey);
    listOfPublicKeys.emplace_back(newKey);
  }

  auto downloadPaymentTransaction = xpx_chain_sdk::CreateDownloadPaymentTransaction
  (
    downloadChannelID,
    downloadSize,
    feedbackFeeAmount,
    std::nullopt,
    std::nullopt,
    networkIdentifier
  );

  std::string privateKey = "819F72066B17FFD71B8B4142C5AEAE4B997B0882ABDF2C263B02869382BD93A0";
  auto account = std::make_shared<xpx_chain_sdk::Account>([privateKey](xpx_chain_sdk::PrivateKeySupplierReason reason, xpx_chain_sdk::PrivateKeySupplierParam param)
  {
    xpx_chain_sdk::Key key;
    ParseHexStringIntoContainer(privateKey.c_str(), privateKey.size(), key);
    return xpx_chain_sdk::PrivateKey(key.data(), key.size());
  }, networkIdentifier);

  account->signTransaction(downloadPaymentTransaction.get());
  xpx_chain_sdk::Hash256 hash = downloadPaymentTransaction->hash();
  auto bin = downloadPaymentTransaction->binary();
  client->transactions()->announceNewTransaction(bin);
 
  for (int i = 0; i < sizeof(hash); i++)
  {
    std::cout << ToHex(hash[i]);
  }
  std::cout << std::endl;
  return 0;
}