From 0961422c5e6a136c1e3dcbb53ebb510541c138f8 Mon Sep 17 00:00:00 2001 From: Chris Gianelloni Date: Tue, 7 Oct 2025 09:24:24 -0400 Subject: [PATCH] feat(ledger): add leios specific hash function Signed-off-by: Chris Gianelloni --- ledger/allegra/allegra.go | 9 +++++++++ ledger/alonzo/alonzo.go | 9 +++++++++ ledger/babbage/babbage.go | 9 +++++++++ ledger/byron/byron.go | 8 ++++++++ ledger/common/tx.go | 1 + ledger/conway/conway.go | 9 +++++++++ ledger/leios/tx.go | 17 ++++++++++++++++- ledger/mary/mary.go | 9 +++++++++ ledger/shelley/shelley.go | 9 +++++++++ ledger/tx.go | 7 +++++++ 10 files changed, 86 insertions(+), 1 deletion(-) diff --git a/ledger/allegra/allegra.go b/ledger/allegra/allegra.go index ff89c892..b22b2233 100644 --- a/ledger/allegra/allegra.go +++ b/ledger/allegra/allegra.go @@ -236,6 +236,7 @@ func (b *AllegraTransactionBody) Utxorpc() (*utxorpc.Tx, error) { type AllegraTransaction struct { cbor.StructAsArray cbor.DecodeStoreCbor + hash *common.Blake2b256 Body AllegraTransactionBody WitnessSet shelley.ShelleyTransactionWitnessSet TxMetadata *cbor.LazyValue @@ -264,6 +265,14 @@ func (t AllegraTransaction) Id() common.Blake2b256 { return t.Body.Id() } +func (t AllegraTransaction) LeiosHash() common.Blake2b256 { + if t.hash == nil { + tmpHash := common.Blake2b256Hash(t.Cbor()) + t.hash = &tmpHash + } + return *t.hash +} + func (t AllegraTransaction) Inputs() []common.TransactionInput { return t.Body.Inputs() } diff --git a/ledger/alonzo/alonzo.go b/ledger/alonzo/alonzo.go index 197ef095..73e21fac 100644 --- a/ledger/alonzo/alonzo.go +++ b/ledger/alonzo/alonzo.go @@ -587,6 +587,7 @@ func (w AlonzoTransactionWitnessSet) Redeemers() common.TransactionWitnessRedeem type AlonzoTransaction struct { cbor.StructAsArray cbor.DecodeStoreCbor + hash *common.Blake2b256 Body AlonzoTransactionBody WitnessSet AlonzoTransactionWitnessSet TxIsValid bool @@ -616,6 +617,14 @@ func (t AlonzoTransaction) Id() common.Blake2b256 { return t.Body.Id() } +func (t AlonzoTransaction) LeiosHash() common.Blake2b256 { + if t.hash == nil { + tmpHash := common.Blake2b256Hash(t.Cbor()) + t.hash = &tmpHash + } + return *t.hash +} + func (t AlonzoTransaction) Inputs() []common.TransactionInput { return t.Body.Inputs() } diff --git a/ledger/babbage/babbage.go b/ledger/babbage/babbage.go index c2469027..fcb23ef3 100644 --- a/ledger/babbage/babbage.go +++ b/ledger/babbage/babbage.go @@ -729,6 +729,7 @@ func (w BabbageTransactionWitnessSet) Redeemers() common.TransactionWitnessRedee type BabbageTransaction struct { cbor.StructAsArray cbor.DecodeStoreCbor + hash *common.Blake2b256 Body BabbageTransactionBody WitnessSet BabbageTransactionWitnessSet TxIsValid bool @@ -758,6 +759,14 @@ func (t BabbageTransaction) Id() common.Blake2b256 { return t.Body.Id() } +func (t BabbageTransaction) LeiosHash() common.Blake2b256 { + if t.hash == nil { + tmpHash := common.Blake2b256Hash(t.Cbor()) + t.hash = &tmpHash + } + return *t.hash +} + func (t BabbageTransaction) Inputs() []common.TransactionInput { return t.Body.Inputs() } diff --git a/ledger/byron/byron.go b/ledger/byron/byron.go index 28658004..a8345bf1 100644 --- a/ledger/byron/byron.go +++ b/ledger/byron/byron.go @@ -279,6 +279,14 @@ func (t *ByronTransaction) Metadata() *cbor.LazyValue { return t.Attributes } +func (t *ByronTransaction) LeiosHash() common.Blake2b256 { + if t.hash == nil { + tmpHash := common.Blake2b256Hash(t.Cbor()) + t.hash = &tmpHash + } + return *t.hash +} + func (t *ByronTransaction) IsValid() bool { return true } diff --git a/ledger/common/tx.go b/ledger/common/tx.go index 4bda1511..f53bd504 100644 --- a/ledger/common/tx.go +++ b/ledger/common/tx.go @@ -27,6 +27,7 @@ type Transaction interface { Type() int Cbor() []byte Hash() Blake2b256 + LeiosHash() Blake2b256 Metadata() *cbor.LazyValue IsValid() bool Consumed() []TransactionInput diff --git a/ledger/conway/conway.go b/ledger/conway/conway.go index 3e014ebf..55662be2 100644 --- a/ledger/conway/conway.go +++ b/ledger/conway/conway.go @@ -514,6 +514,7 @@ func (b *ConwayTransactionBody) Utxorpc() (*utxorpc.Tx, error) { type ConwayTransaction struct { cbor.StructAsArray cbor.DecodeStoreCbor + hash *common.Blake2b256 Body ConwayTransactionBody WitnessSet ConwayTransactionWitnessSet TxIsValid bool @@ -543,6 +544,14 @@ func (t ConwayTransaction) Id() common.Blake2b256 { return t.Body.Id() } +func (t ConwayTransaction) LeiosHash() common.Blake2b256 { + if t.hash == nil { + tmpHash := common.Blake2b256Hash(t.Cbor()) + t.hash = &tmpHash + } + return *t.hash +} + func (t ConwayTransaction) Inputs() []common.TransactionInput { return t.Body.Inputs() } diff --git a/ledger/leios/tx.go b/ledger/leios/tx.go index cfdb0e34..7a51a430 100644 --- a/ledger/leios/tx.go +++ b/ledger/leios/tx.go @@ -15,11 +15,26 @@ package leios import ( + "github.com/blinklabs-io/gouroboros/ledger/common" "github.com/blinklabs-io/gouroboros/ledger/conway" ) type ( - LeiosTransaction = conway.ConwayTransaction LeiosTransactionBody = conway.ConwayTransactionBody LeiosTransactionWitnessSet = conway.ConwayTransactionWitnessSet ) + +type LeiosTransaction struct { + conway.ConwayTransaction + hash *common.Blake2b256 + Body LeiosTransactionBody + WitnessSet LeiosTransactionWitnessSet +} + +func (t *LeiosTransaction) LeiosHash() common.Blake2b256 { + if t.hash == nil { + tmpHash := common.Blake2b256Hash(t.Cbor()) + t.hash = &tmpHash + } + return *t.hash +} diff --git a/ledger/mary/mary.go b/ledger/mary/mary.go index f71137a3..898661fe 100644 --- a/ledger/mary/mary.go +++ b/ledger/mary/mary.go @@ -244,6 +244,7 @@ func (b *MaryTransactionBody) Utxorpc() (*utxorpc.Tx, error) { type MaryTransaction struct { cbor.StructAsArray cbor.DecodeStoreCbor + hash *common.Blake2b256 Body MaryTransactionBody WitnessSet shelley.ShelleyTransactionWitnessSet TxMetadata *cbor.LazyValue @@ -272,6 +273,14 @@ func (t MaryTransaction) Id() common.Blake2b256 { return t.Body.Id() } +func (t MaryTransaction) LeiosHash() common.Blake2b256 { + if t.hash == nil { + tmpHash := common.Blake2b256Hash(t.Cbor()) + t.hash = &tmpHash + } + return *t.hash +} + func (t MaryTransaction) Inputs() []common.TransactionInput { return t.Body.Inputs() } diff --git a/ledger/shelley/shelley.go b/ledger/shelley/shelley.go index 043804d6..929df113 100644 --- a/ledger/shelley/shelley.go +++ b/ledger/shelley/shelley.go @@ -537,6 +537,7 @@ func (w ShelleyTransactionWitnessSet) Redeemers() common.TransactionWitnessRedee type ShelleyTransaction struct { cbor.StructAsArray cbor.DecodeStoreCbor + hash *common.Blake2b256 Body ShelleyTransactionBody WitnessSet ShelleyTransactionWitnessSet TxMetadata *cbor.LazyValue @@ -565,6 +566,14 @@ func (t ShelleyTransaction) Id() common.Blake2b256 { return t.Body.Id() } +func (t ShelleyTransaction) LeiosHash() common.Blake2b256 { + if t.hash == nil { + tmpHash := common.Blake2b256Hash(t.Cbor()) + t.hash = &tmpHash + } + return *t.hash +} + func (t ShelleyTransaction) Inputs() []common.TransactionInput { return t.Body.Inputs() } diff --git a/ledger/tx.go b/ledger/tx.go index 253e42c1..bfba6c54 100644 --- a/ledger/tx.go +++ b/ledger/tx.go @@ -46,6 +46,8 @@ func NewTransactionFromCbor(txType uint, data []byte) (Transaction, error) { return NewBabbageTransactionFromCbor(data) case TxTypeConway: return NewConwayTransactionFromCbor(data) + case TxTypeLeios: + return NewLeiosTransactionFromCbor(data) } return nil, fmt.Errorf("unknown transaction type: %d", txType) } @@ -69,6 +71,8 @@ func NewTransactionBodyFromCbor( return NewBabbageTransactionBodyFromCbor(data) case TxTypeConway: return NewConwayTransactionBodyFromCbor(data) + case TxTypeLeios: + return NewLeiosTransactionBodyFromCbor(data) } return nil, fmt.Errorf("unknown transaction type: %d", txType) } @@ -116,5 +120,8 @@ func DetermineTransactionType(data []byte) (uint, error) { if _, err := NewConwayTransactionFromCbor(data); err == nil { return TxTypeConway, nil } + if _, err := NewLeiosTransactionFromCbor(data); err == nil { + return TxTypeLeios, nil + } return 0, errors.New("unknown transaction type") }