commit edb65855078641af75e37c06d31b9c5d8e14f064
parent 246065dedd31508482eff4e70b244fc19b1b36ce
Author: Shivakumar <[email protected]>
Date: Wed, 8 Jul 2026 01:34:43 +0530
chore: Clone now returns a defensive copy of Tx
Signed-off-by: Shivakumar <[email protected]>
Diffstat:
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/internal/transaction/transaction.go b/internal/transaction/transaction.go
@@ -135,8 +135,8 @@ func (tx *Transaction) Serialize() ([]byte, error) {
}
// Deserialize reconstructs a Transaction from a JSON byte slice.
-// Note: This function only performs structural decoding. Callers MUST call Verify()
-// to ensure the cryptographic integrity and authenticity of the data, especially
+// Note: This function only performs structural decoding. Callers MUST call Verify()
+// to ensure the cryptographic integrity and authenticity of the data, especially
// when deserializing transactions from untrusted sources (e.g., network, disk).
func Deserialize(data []byte) (*Transaction, error) {
var tx Transaction
@@ -146,6 +146,21 @@ func Deserialize(data []byte) (*Transaction, error) {
return &tx, nil
}
+// Clone returns a defensive copy of the Transaction.
+func (tx *Transaction) Clone() *Transaction {
+ if tx == nil {
+ return nil
+ }
+ return &Transaction{
+ TxID: bytes.Clone(tx.TxID),
+ Nonce: bytes.Clone(tx.Nonce),
+ IssuerDID: tx.IssuerDID,
+ MerkleRoot: bytes.Clone(tx.MerkleRoot),
+ Timestamp: tx.Timestamp,
+ Signature: bytes.Clone(tx.Signature),
+ }
+}
+
// calculateTxID computes the SHA-256 hash of the transaction's canonical fields.
func (tx *Transaction) calculateTxID() ([]byte, error) {
sb, err := tx.signingBytes()