Transaction

Struct Transaction 

Source
pub struct Transaction {
    pub blockchain_rid: Vec<u8>,
    pub operations: Option<Vec<Operation>>,
    pub signers: Option<Vec<Vec<u8>>>,
    pub signatures: Option<Vec<Vec<u8>>>,
    pub merkle_hash_version: u8,
}
Expand description

Represents a blockchain transaction with operations and signatures.

A transaction contains a list of operations to be executed, along with the necessary signatures to authorize these operations. It supports both single and multi-signature scenarios.

Fields§

§blockchain_rid: Vec<u8>

Unique identifier of the blockchain this transaction belongs to

§operations: Option<Vec<Operation>>

List of operations to be executed in this transaction

§signers: Option<Vec<Vec<u8>>>

List of public keys of the signers

§signatures: Option<Vec<Vec<u8>>>

List of signatures corresponding to the signers

§merkle_hash_version: u8

Implementations§

Source§

impl Transaction

Source

pub fn new( blockchain_rid: Vec<u8>, operations: Option<Vec<Operation>>, signers: Option<Vec<Vec<u8>>>, signatures: Option<Vec<Vec<u8>>>, ) -> Self

Creates a new transaction with the specified parameters.

§Arguments
  • blockchain_rid - Unique identifier of the blockchain
  • operations - Optional list of operations to be executed
  • signers - Optional list of public keys of the signers
  • signatures - Optional list of signatures
§Returns

A new Transaction instance

Source

pub fn gvt_hex_encoded(&self) -> String

Returns the hex-encoded GTV (Generic Tree Value) representation of the transaction.

This method encodes the transaction into GTV format and returns it as a hexadecimal string.

§Returns

Hex-encoded string of the GTV-encoded transaction

Source

pub fn tx_rid(&self) -> Result<[u8; 32], HashError>

Computes the unique identifier (RID) of this transaction.

The transaction RID is computed by hashing the GTV representation of the transaction using the GTX hash function.

§Returns

A fixed-size 32 bytes containing the transaction RID

Source

pub fn tx_rid_hex(&self) -> Result<String, HashError>

Returns the hex-encoded transaction RID.

This is a convenience method that returns the transaction RID as a hexadecimal string.

§Returns

Hex-encoded string of the transaction RID

Source

pub fn sign_from_raw_priv_key(&mut self, private_key: &str) -> Result<(), Error>

Signs the transaction using a raw private key string.

§Arguments
  • private_key - Private key as a string
§Returns

Result indicating success or a secp256k1 error

§Errors

Returns an error if the private key is invalid or signing fails

Source

pub fn multi_sign_from_raw_priv_keys( &mut self, private_keys: &[&str], ) -> Result<(), Error>

Signs the transaction with multiple raw private key strings.

This method iteratively signs the transaction with each provided private key string, enabling multi-signature transactions.

§Arguments
  • private_keys - Slice of raw private key strings
§Returns

Result indicating success or a secp256k1 error

§Errors

Returns an error if any private key is invalid or signing fails

Source

pub fn sign(&mut self, private_key: &[u8; 32]) -> Result<(), Error>

Signs the transaction using a private key.

This method:

  1. Derives the public key from the private key
  2. Adds the public key to the signers list
  3. Signs the transaction RID
  4. Adds the signature to the signatures list
§Arguments
  • private_key - 32-byte private key
§Returns

Result indicating success or a secp256k1 error

§Errors

Returns an error if the private key is invalid or signing fails

Source

pub fn multi_sign(&mut self, private_keys: &[&[u8; 32]]) -> Result<(), Error>

Signs the transaction with multiple private keys.

This method iteratively signs the transaction with each provided private key, enabling multi-signature transactions.

§Arguments
  • private_keys - Slice of 32-byte private keys
§Returns

Result indicating success or a secp256k1 error

§Errors

Returns an error if any private key is invalid or signing fails

Source

pub fn confirmation_proof( proof: &str, ) -> Result<TransactionConfirmationProofData, String>

Decodes a hexadecimal string representation of a transaction confirmation proof into a TransactionConfirmationProofData struct.

This function is used to parse the proof data received from the blockchain to verify the inclusion of a transaction in a block. The input proof is expected to be a hex-encoded GTV (Generic Tree Value) structure representing the confirmation proof.

§Arguments
  • proof - A string slice containing the hex-encoded confirmation proof data.
§Returns

A Result which is:

  • Ok(TransactionConfirmationProofData) if the proof is successfully decoded and parsed into the TransactionConfirmationProofData struct.
  • Err(String) if the input string is not valid hexadecimal, if the GTV decoding fails, or if the decoded GTV structure does not match the expected format for a TransactionConfirmationProofData.
§Errors

This function will return an error string if:

  • The proof string cannot be hex-decoded.
  • The decoded bytes cannot be successfully GTV-decoded.
  • The root of the GTV-decoded data is not a dictionary (Op_Params::Dict).
  • Any required field (blockHeader, hash, txIndex, witness, merkleProofTree) is missing or has an incorrect type within the decoded GTV dictionary.
§Examples
use crate::utils::transaction::{Transaction, TransactionConfirmationProofData};
use crate::utils::operation::Params as Op_Params;

let proof_hex_encoded_data = "A48203AA308203A6308201230C0B626C6F636B486561646572A18201120482010EA582010A30820106A12204207A37DD331AC8FED64EEFCCA231B0F975DE7F4371CE5CA44105A5B117DF6DE251A1220420BAB0B26A302920A56F7FFB9428FA52A264657594624F12C73B1510BEB76EBCE1A12204209423052CE47270FB5ADE54B30F662AAB476BF26314680CD716C0EC1484EF5C63A308020601979ADDCE2EA306020400A926E1A0020500A48181307F30310C0B636F6E6669675F68617368A1220420C9A490594951ACBB668F05FE83287DB48CDD628811F9F5D3083BF087686C3BD4301A0C136D65726B6C655F686173685F76657273696F6EA303020102302E0C077072696D617279A123042102DD859FE30F3C6102B364A5FDEB3C8C3DA2B22F4E541015C3BEFDA753EC672E8E302A0C0468617368A1220420796D019516EB32366BAA60F08E73A78C94BBDCF9ED3724017AED6E9FC729AF923081830C0F6D65726B6C6550726F6F6654726565A570306EA303020167A303020101A3030201F6A530302EA303020165A303020100A1220420796D019516EB32366BAA60F08E73A78C94BBDCF9ED3724017AED6E9FC729AF92A52B3029A303020164A12204200000000000000000000000000000000000000000000000000000000000000000300E0C077478496E646578A303020100308201B90C077769746E657373A18201AC048201A800000004000000210202F6F59D4F007C52FB84FAF3B3E02CF7B8F9C2A4B953618047DBA2C85A17854F00000040D056BADD7014B638DB4FF06E2D86D570FF1FE712B00833FCA9D175BC926502A7613A7CDD1DA50326F9AEA3BBF94CD4043191E02CE5A4F0D81071B14CF841FD770000002102EF6254CCADB304E39244858F3E506EF58816A2769E019AD11C35842862D981F80000004062E6FD188816B85538A76990E2EE943CBDC40C161CA98A87B5B070FEDF7946CF73A6BEFB5C3F0DC3F664F52D8A53C8B79C52ADC023276F9836739FE0301BABA70000002103C146E1860AACC77EBF3B5741D04CFFBC316B37921D4029CAF2479AF5F2D573EA00000040EAD69772A61F5FA1B5C71A977D98F88B57702A6CA005D39BD72CC5064FE1B48F3C49B1CECDE24F8F6620CF2CB679314477BD96644E717C4B2F657DC7F7EEB6FB0000002102DD859FE30F3C6102B364A5FDEB3C8C3DA2B22F4E541015C3BEFDA753EC672E8E00000040D994B3945F0AF229FBC7FB3A480CA10357E8F58076BB0F375CCE6044FE36996F4755F9B5C7AA11894DBDE9AFA734E05B4501614692480820A28D52DB04F577F7";
let result = Transaction::confirmation_proof(proof_hex_encoded_data);

assert!(result.is_ok());
let proof_data = result.unwrap();
assert_eq!(proof_data.tx_index, 0);
// Further assertions can be made on other fields
Source

pub fn from_raw_data(tx: &str) -> Result<Self, String>

Creates a new Transaction (or Self) instance from a raw hexadecimal string.

This function is responsible for decoding a hexadecimal string representing transaction data into a structured format, likely using a custom Generalized Transaction Value (GTV) encoding scheme. It extracts key components such as the blockchain’s Root ID (RID), operations, signers, and signatures from the decoded data.

§Arguments
  • tx - A string slice (&str) containing the raw transaction data encoded in hexadecimal format.
§Returns

A Result<Self, String> which is:

  • Ok(Self): If the hexadecimal string is successfully decoded and parsed into a valid Transaction (or Self) instance.
  • Err(String): If any error occurs during the process, such as:
    • The input tx string is not valid hexadecimal.
    • The decoded data fails to conform to the expected GTV structure.
    • Specific components (like block_chain_rid, operations, signers, or signatures) are missing or are of an unexpected type within the GTV structure.
§Errors

This function can return an error string in the following scenarios:

  • “Invalid hex”: If hex::decode fails to parse the input tx string.
  • “GTV decode failed”: If gtv_decode encounters an error during the GTV deserialization process.
  • Panics with “Unexpected signer type”: (This is a current panic, ideally this would be converted to a Result::Err for robust error handling in a production system). This occurs if an element within the expected signers array is not a ByteArray.
  • Other potential errors related to unexpected data structures within the GTV result (e.g., if val2[0] or val2[2] are not arrays as expected).
§Examples
// Assuming `Transaction` is the type `Self` refers to, and `gtv_decode` and `hex` are available.
// Also assuming a valid hex string for a transaction.

// Example of a successful decode (conceptual, as actual hex depends on your GTV structure)
let valid_hex_tx = "010203..."; // Replace with a real valid hex transaction string
match Transaction::from_raw_data(valid_hex_tx) {
    Ok(transaction) => {
        println!("Successfully decoded transaction: {:?}", transaction);
        // Further assertions or usage of the transaction object
    },
    Err(e) => {
        eprintln!("Failed to decode transaction: {}", e);
    }
}

// Example of an invalid hex string
let invalid_hex_tx = "not_a_hex_string";
if let Err(e) = Transaction::from_raw_data(invalid_hex_tx) {
    assert_eq!(e, "Invalid hex");
}

// Example of a hex string that decodes but has invalid GTV structure
let malformed_gtv_hex = "0a0b0c..."; // Replace with a hex string that causes GTV decode or structural errors
if let Err(e) = Transaction::from_raw_data(malformed_gtv_hex) {
    assert!(e.contains("GTV decode failed") || e.contains("Unexpected"));
}
§Panics

This function currently panics if an element within the expected signers array is not of the OpParams::ByteArray type. For production-grade code, this panic should ideally be converted into a Result::Err for more graceful error handling.

§Implementation Details
  1. Decodes the input hexadecimal string tx into a byte vector.
  2. Deserializes the byte vector into a Params object using gtv_decode.
  3. Extracts the block_chain_rid from the first element of the main GTV array.
  4. Iterates through the third element of the main GTV array to parse signers, expecting each signer to be a ByteArray.
  5. (Further logic for operations and signatures would be described here if visible).

This function relies on the hex crate for hexadecimal decoding and a custom gtv_decode function for Generalized Transaction Value (GTV) deserialization. The OpParams enum is crucial for interpreting the structure of the decoded GTV data.

Trait Implementations§

Source§

impl Debug for Transaction

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Transaction

Source§

fn default() -> Self

Creates a new Transaction with default values and performs automatic initialization.

§Example
let mut tx = Transaction {
    blockchain_rid: hex::decode(brid).unwrap(),
    operations: Some(ops),
    ..Default::default()  // This will trigger auto initialization
};

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,