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: u8Implementations§
Source§impl Transaction
impl Transaction
Sourcepub fn new(
blockchain_rid: Vec<u8>,
operations: Option<Vec<Operation>>,
signers: Option<Vec<Vec<u8>>>,
signatures: Option<Vec<Vec<u8>>>,
) -> Self
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 blockchainoperations- Optional list of operations to be executedsigners- Optional list of public keys of the signerssignatures- Optional list of signatures
§Returns
A new Transaction instance
Sourcepub fn gvt_hex_encoded(&self) -> String
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
Sourcepub fn tx_rid(&self) -> Result<[u8; 32], HashError>
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
Sourcepub fn tx_rid_hex(&self) -> Result<String, HashError>
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
Sourcepub fn multi_sign_from_raw_priv_keys(
&mut self,
private_keys: &[&str],
) -> Result<(), Error>
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
Sourcepub fn sign(&mut self, private_key: &[u8; 32]) -> Result<(), Error>
pub fn sign(&mut self, private_key: &[u8; 32]) -> Result<(), Error>
Signs the transaction using a private key.
This method:
- Derives the public key from the private key
- Adds the public key to the signers list
- Signs the transaction RID
- 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
Sourcepub fn multi_sign(&mut self, private_keys: &[&[u8; 32]]) -> Result<(), Error>
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
Sourcepub fn confirmation_proof(
proof: &str,
) -> Result<TransactionConfirmationProofData, String>
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 theTransactionConfirmationProofDatastruct.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 aTransactionConfirmationProofData.
§Errors
This function will return an error string if:
- The
proofstring 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 fieldsSourcepub fn from_raw_data(tx: &str) -> Result<Self, String>
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 validTransaction(orSelf) instance.Err(String): If any error occurs during the process, such as:- The input
txstring is not valid hexadecimal. - The decoded data fails to conform to the expected GTV structure.
- Specific components (like
block_chain_rid,operations,signers, orsignatures) are missing or are of an unexpected type within the GTV structure.
- The input
§Errors
This function can return an error string in the following scenarios:
- “Invalid hex”: If
hex::decodefails to parse the inputtxstring. - “GTV decode failed”: If
gtv_decodeencounters 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::Errfor robust error handling in a production system). This occurs if an element within the expected signers array is not aByteArray. - Other potential errors related to unexpected data structures within the GTV
result(e.g., ifval2[0]orval2[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
- Decodes the input hexadecimal string
txinto a byte vector. - Deserializes the byte vector into a
Paramsobject usinggtv_decode. - Extracts the
block_chain_ridfrom the first element of the main GTV array. - Iterates through the third element of the main GTV array to parse
signers, expecting each signer to be aByteArray. - (Further logic for
operationsandsignatureswould 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.