Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions ledger/alonzo/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func (o AlonzoTransactionOutput) Datum() *cbor.LazyValue {
return nil
}

func (o AlonzoTransactionOutput) Utxorpc() *utxorpc.TxOutput {
func (o AlonzoTransactionOutput) Utxorpc() (*utxorpc.TxOutput, error) {
var assets []*utxorpc.Multiasset
if o.Assets() != nil {
tmpAssets := o.Assets()
Expand All @@ -357,14 +357,20 @@ func (o AlonzoTransactionOutput) Utxorpc() *utxorpc.TxOutput {
}
}

addressBytes, err := o.OutputAddress.Bytes()
if err != nil {
return nil, fmt.Errorf("failed to get address bytes: %w", err)
}

return &utxorpc.TxOutput{
Address: o.OutputAddress.Bytes(),
Coin: o.Amount(),
Assets: assets,
Datum: &utxorpc.Datum{
Hash: o.TxOutputDatumHash.Bytes(),
Address: addressBytes,
Coin: o.Amount(),
Assets: assets,
Datum: &utxorpc.Datum{
Hash: o.TxOutputDatumHash.Bytes(),
},
},
}
nil
}

type AlonzoRedeemer struct {
Expand Down
28 changes: 17 additions & 11 deletions ledger/babbage/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,12 +505,17 @@ func (o BabbageTransactionOutput) Datum() *cbor.LazyValue {
return nil
}

func (o BabbageTransactionOutput) Utxorpc() *utxorpc.TxOutput {
func (o BabbageTransactionOutput) Utxorpc() (*utxorpc.TxOutput, error) {
// Handle address bytes
addressBytes, err := o.OutputAddress.Bytes()
if err != nil {
return nil, fmt.Errorf("failed to get output address bytes: %w", err)
}
var address []byte
if o.OutputAddress.Bytes() == nil {
if addressBytes == nil {
address = []byte{}
} else {
address = o.OutputAddress.Bytes()
address = addressBytes
}

var assets []*utxorpc.Multiasset
Expand Down Expand Up @@ -540,15 +545,16 @@ func (o BabbageTransactionOutput) Utxorpc() *utxorpc.TxOutput {
}

return &utxorpc.TxOutput{
Address: address,
Coin: o.Amount(),
Assets: assets,
Datum: &utxorpc.Datum{
Hash: datumHash,
// OriginalCbor: o.Datum().Cbor(),
Address: address,
Coin: o.Amount(),
Assets: assets,
Datum: &utxorpc.Datum{
Hash: datumHash,
// OriginalCbor: o.Datum().Cbor(),
},
// Script: o.ScriptRef,
},
// Script: o.ScriptRef,
}
nil
}

type BabbageTransactionWitnessSet struct {
Expand Down
5 changes: 4 additions & 1 deletion ledger/babbage/babbage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2775,7 +2775,10 @@ func TestBabbageTransactionOutput_Utxorpc_DatumOptionNil(t *testing.T) {
DatumOption: &BabbageTransactionOutputDatumOption{},
}

txOutput := output.Utxorpc()
txOutput, err := output.Utxorpc()
if err != nil {
t.Fatalf("Utxorpc() failed: %v", err) // Fail and stop the test
}

assert.NotNil(t, txOutput)
assert.Equal(t, []byte{}, txOutput.Datum.Hash)
Expand Down
13 changes: 9 additions & 4 deletions ledger/byron/byron.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,16 @@ func (o ByronTransactionOutput) Datum() *cbor.LazyValue {
return nil
}

func (o ByronTransactionOutput) Utxorpc() *utxorpc.TxOutput {
return &utxorpc.TxOutput{
Address: o.OutputAddress.Bytes(),
Coin: o.Amount(),
func (o ByronTransactionOutput) Utxorpc() (*utxorpc.TxOutput, error) {
addressBytes, err := o.OutputAddress.Bytes()
if err != nil {
return nil, fmt.Errorf("failed to get address bytes: %w", err)
}
return &utxorpc.TxOutput{
Address: addressBytes,
Coin: o.Amount(),
},
nil
}

type ByronBlockVersion struct {
Expand Down
23 changes: 14 additions & 9 deletions ledger/common/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ func (a *Address) UnmarshalCBOR(data []byte) error {
}

func (a *Address) MarshalCBOR() ([]byte, error) {
addrBytes := a.Bytes()
addrBytes, err := a.Bytes()
if err != nil {
return nil, fmt.Errorf("failed to get address bytes: %w", err)
}

if a.addressType == AddressTypeByron {
return addrBytes, nil
}
Expand Down Expand Up @@ -376,7 +380,7 @@ func (a Address) generateHRP() string {
}

// Bytes returns the underlying bytes for the address
func (a Address) Bytes() []byte {
func (a Address) Bytes() ([]byte, error) {
if a.addressType == AddressTypeByron {
tmpPayload := []any{
a.paymentAddress,
Expand All @@ -385,8 +389,7 @@ func (a Address) Bytes() []byte {
}
rawPayload, err := cbor.Encode(tmpPayload)
if err != nil {
// TODO: handle error (#851)
return nil
return nil, fmt.Errorf("failed to encode Byron address payload: %w", err)
}
tmpData := []any{
cbor.Tag{
Expand All @@ -397,10 +400,9 @@ func (a Address) Bytes() []byte {
}
ret, err := cbor.Encode(tmpData)
if err != nil {
// TODO: handle error (#851)
return nil
return nil, fmt.Errorf("failed to encode Byron address data: %w", err)
}
return ret
return ret, nil
}
ret := []byte{}
ret = append(
Expand All @@ -410,12 +412,15 @@ func (a Address) Bytes() []byte {
ret = append(ret, a.paymentAddress...)
ret = append(ret, a.stakingAddress...)
ret = append(ret, a.extraData...)
return ret
return ret, nil
}

// String returns the bech32-encoded version of the address
func (a Address) String() string {
data := a.Bytes()
data, err := a.Bytes()
if err != nil {
panic(fmt.Sprintf("failed to get address bytes: %v", err))
}
if a.addressType == AddressTypeByron {
// Encode data to base58
encoded := base58.Encode(data)
Expand Down
7 changes: 5 additions & 2 deletions ledger/common/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type TransactionOutput interface {
Datum() *cbor.LazyValue
DatumHash() *Blake2b256
Cbor() []byte
Utxorpc() *utxorpc.TxOutput
Utxorpc() (*utxorpc.TxOutput, error)
}

type TransactionWitnessSet interface {
Expand Down Expand Up @@ -200,7 +200,10 @@ func TransactionBodyToUtxorpc(tx TransactionBody) *utxorpc.Tx {
txi = append(txi, input)
}
for _, o := range tx.Outputs() {
output := o.Utxorpc()
output, err := o.Utxorpc()
if err != nil {
return nil
}
txo = append(txo, output)
}
for _, ri := range tx.ReferenceInputs() {
Expand Down
15 changes: 10 additions & 5 deletions ledger/mary/mary.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,12 +455,17 @@ func (o MaryTransactionOutput) Datum() *cbor.LazyValue {
return nil
}

func (o MaryTransactionOutput) Utxorpc() *utxorpc.TxOutput {
return &utxorpc.TxOutput{
Address: o.OutputAddress.Bytes(),
Coin: o.Amount(),
// Assets: o.Assets,
func (o MaryTransactionOutput) Utxorpc() (*utxorpc.TxOutput, error) {
addressBytes, err := o.OutputAddress.Bytes()
if err != nil {
return nil, fmt.Errorf("failed to get address bytes: %w", err)
}
return &utxorpc.TxOutput{
Address: addressBytes,
Coin: o.Amount(),
// Assets: o.Assets,
},
err
}

type MaryTransactionOutputValue struct {
Expand Down
13 changes: 11 additions & 2 deletions ledger/shelley/pparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,20 @@ func TestShelleyTransactionOutput_Utxorpc(t *testing.T) {
}

// Convert it to utxorpc format
actual := output.Utxorpc()
actual, err := output.Utxorpc()
if err != nil {
t.Fatalf("Utxorpc() failed: %v", err) // Fail immediately on error
}

// Get expected address bytes (with error handling)
expectedAddressBytes, err := address.Bytes()
if err != nil {
t.Fatalf("address.Bytes() failed: %v", err)
}

// expected output in utxorpc format
expected := &cardano.TxOutput{
Address: address.Bytes(),
Address: expectedAddressBytes,
Coin: 1000,
}

Expand Down
11 changes: 8 additions & 3 deletions ledger/shelley/shelley.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,16 @@ func (o ShelleyTransactionOutput) Datum() *cbor.LazyValue {
return nil
}

func (o ShelleyTransactionOutput) Utxorpc() *utxorpc.TxOutput {
func (o ShelleyTransactionOutput) Utxorpc() (*utxorpc.TxOutput, error) {
addressBytes, err := o.OutputAddress.Bytes()
if err != nil {
return nil, fmt.Errorf("failed to get address bytes: %w", err)
}

return &utxorpc.TxOutput{
Address: o.OutputAddress.Bytes(),
Address: addressBytes,
Coin: o.Amount(),
}
}, nil
}

type ShelleyTransactionWitnessSet struct {
Expand Down
Loading