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

func SendTransaction(
    id string, 
    sender string, 
    to string, 
    timestamp string, 
    transactionType string, 
    payload string, 
    nonce string, 
    signature string, 
    blockchain string
) map[string]interface{}

ID:

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

id = "0x" + utils.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. Remember that you have to add to the imports:

"github.com/circular-protocol/circular-go/utils"

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 = utils.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

NameTypeDescription

id

String

Calculated TxID in hex

from

String

Wallet address in hex

to

String

Receiver's wallet address in hex

timestamp

String

Date string in "YYYY:MM:DD-HH:mm:ss" format

payload

String

String in hexadecimal format

nonce

String

Nonce value for the current transaction.

signature

String

Signature of the transaction ID in hexadecimal

blockchain

String

Blockchain address in hex

type

String

One of the types listed before

Example:

package main

import (
	"encoding/json"
	"fmt"
	"strconv"

	"github.com/circular-protocol/circular-go/circular_protocol_api"
	"github.com/circular-protocol/circular-go/utils"
)

func main() {

	from := ""
	to := ""
	blockchain := ""
	privateKey := ""

	payload := map[string]interface{}{
		"Asset":  "CIRX",
		"Amount": "1",
		"Action": "CP_SEND",
		"Memo":   "This is a test",
	}

	from = utils.HexFix(from)
	privateKey = utils.HexFix(privateKey)
	to = utils.HexFix(to)
	blockchain = utils.HexFix(blockchain)

	jsonPayload, _ := json.Marshal(payload)

	hexPayload := utils.StringToHex(string(jsonPayload))

	response := circular_protocol_api.GetWalletNonce(blockchain, from)
	responseMap, err := response["Response"].(map[string]interface{})

	nonceFloat := responseMap["Nonce"].(float64)

	nonce := int(nonceFloat)
	newNonce := strconv.Itoa(nonce + 1)

	timestamp := utils.GetFormattedTimestamp()
	dataToHash := blockchain + from + to + hexPayload + newNonce + timestamp
	id := utils.Sha256(dataToHash)

	signature := utils.SignMessage(id, privateKey)["Signature"].(string)
	transactionType := "C_TYPE_COIN"

	response = circular_protocol_api.SendTransaction(id, from, to, timestamp, transactionType, hexPayload, newNonce, signature, blockchain)

	// Print the response
	fmt.Println(response)
}

Result

The result will be of type Map, 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