sendTransaction()

Send a new transaction on the blockchain.

Requesting the content of a wallet should not be considered a transaction. This distinction arises because such a request is handled by a single node and does not affect the global state of the network. Transactions, in contrast, are actions that have an impact on the overall network state.

When we talk about "affecting the global state of the network," we mean that the requested action results in a change of state and requires the execution of an operation by all nodes in the network or a specific subset of nodes.

Transactions are typically used for various purposes, including modifying the contents of a wallet, executing smart contracts, or creating/modifying/destroying assets on the blockchains. Transactions are the primary mechanism for carrying out these diverse operations and involve several parameters to achieve the desired outcomes.

Method Signature

public function sendTransaction($id, $from, $to, $timestamp, $type, $payload, $nonce, $signature, $blockchain) 

ID:

This represents the transaction hash, which is usually calculated as:

$ID = "0x" + sha256($blockchain + $from + $to + $hexPayload + $newNonce + $timestamp)

Where "From," "To," and "Payload" are in hexadecimal representation without the "0x" identifier, while the "ID" will contain the "0x" hexadecimal identifier.

From:

This field represents the wallet address of the entity initiating the transaction, without the "0x" hexadecimal identifier. Please note that the wallet address must be pre-registered on the chosen blockchain. Furthermore, the wallet must maintain an adequate balance of CIRX coins to cover the transaction fees associated with the particular transaction. These fees are essential for the seamless processing and execution of the requested transaction.

To:

This field represents the wallet address of the transaction recipient, without the "0x" hexadecimal identifier. If the particular transaction doesn't necessitate a recipient address, this field can be the same as the sender's address.

Timestamp:

Transaction timestamp in format YYYY:MM:DD-hh:mm:ss in UDT timezone.

Payload:

String in hexadecimal format without the "0x" hexadecimal identifier (base64 is also accepted).

Nonce:

Nonce value for the current transaction. It's crucial to obtain the current Nonce value from the sending wallet and incorporate it into the new transaction. For more information, please refer to the "getWalletNonce()" section.

Signature:

Signature of the transaction ID in hexadecimal format, without the "0x" identifier. The signature is obtained using Elliptic curve algorithm Secp256k1 (more cryptos will be available soon).

$signature = $circular->signMessage( $ID, $From_PrivateKey );

Public Key:

The public key of the "From" Wallet is included in the transaction to expedite the initial transaction pre-validation process. This eliminates the need to access wallet files for public key retrieval during this phase, thus enhancing the efficiency of the transaction.

Blockchain:

Blockchain Address where the transaction is sent, without the hexadecimal identifier.

Type:

Types of transactions:

  • C_TYPE_COIN: Transaction involving native coins (CIRX) without the use of Hyper Code (Smart Contracts).

  • C_TYPE_TOKEN: Transaction involving specified tokens without the use of Hyper Code.

  • C_TYPE_ASSET: Transaction involving a generic digital asset that is not represented by a token.

  • C_TYPE_CERTIFICATE: Transaction where the payload doesn't require processing and is a generic string.

  • C_TYPE_REGISTERWALLET: Transaction used to register a new wallet on a specified blockchain.

  • C_TYPE_HC_DEPLOYMENT: Transaction to deploy a new Hyper Code Program.

  • C_TYPE_HC_REQUEST: Transaction to call a specified endpoint of a Hyper Program.

  • C_TYPE_USERDEF: Unspecified type, defined by the user (not available yet).

Request

Parameters

Example:

<?php

require 'vendor/autoload.php';
use CircularProtocol\Api\CircularProtocolAPI;

$circular = new CircularProtocolAPI();

$blockchain  = "";
$address     = "";
$receiver    = "";
$senderPK    = "";

$from       = $circular->hexFix($address);
$to         = $circular->hexFix($receiver);
$senderPK   = $circular->hexFix($senderPK);
$payload    = [
        "Asset" => "CIRX",
        "Amount" => "1",
        "Action" => "CP_SEND",
        "Memo" => "PHP"
    ];

$payload    = $circular->stringToHex(json_encode($payload));
$blockchain = $circular->hexFix($blockchain);
$nonce      = intval($circular->getWalletNonce($blockchain, $from)->Response->Nonce) + 1;
$timestamp  = $circular->getFormattedTimestamp();
$id         = hash("sha256", $blockchain . $from . $to . $payload . strval($nonce) . $timestamp, false);
$signature  = $circular->signMessage($id, $senderPK);
$type       = "C_TYPE_COIN";
$data = [
    "ID"         => $id,
    "From"       => $from,
    "To"         => $to,
    "Timestamp"  => $timestamp,
    "Payload"    => strval($payload),
    "Nonce"      => strval($nonce),
    "Signature"  => $signature,
    "Blockchain" => $blockchain,
    "Type"       => $type,
    "Version"    => $circular->getVersion()
];

$result = $circular->fetch($circular->getNAGURL() . 'Circular_AddTransaction_', $data);

print_r($result);

Result

The result will be of type Array, but for easier readability, here is the equivalent in JSON.

{
  "Result": 200,
  "Response": {
    "TxID": "138dd7a19685c71f281d8f5b9d0de15dc7ce7e9433e1715ec6e45e0fade74c82",
    "Timestamp": "2024:09:26-21:57:12"
  },
  "Node": "8a93c6b6f8d166097ddfeb3e5e3a2998b35e0b6f0bd2e31a8a130a11b6749279"
}

Last updated