Skip to content

Commit b4351b9

Browse files
jkawanJenita
andauthored
feat(ledger): Added error handling in ledger.Address Bytes() function (#1028)
* feat:added eerror handling to Bytes function Signed-off-by: Jenita <[email protected]> * feat:added eerror handling to Bytes function Signed-off-by: Jenita <[email protected]> --------- Signed-off-by: Jenita <[email protected]> Co-authored-by: Jenita <[email protected]>
1 parent f818b7a commit b4351b9

File tree

9 files changed

+91
-44
lines changed

9 files changed

+91
-44
lines changed

ledger/alonzo/alonzo.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ func (o AlonzoTransactionOutput) Datum() *cbor.LazyValue {
337337
return nil
338338
}
339339

340-
func (o AlonzoTransactionOutput) Utxorpc() *utxorpc.TxOutput {
340+
func (o AlonzoTransactionOutput) Utxorpc() (*utxorpc.TxOutput, error) {
341341
var assets []*utxorpc.Multiasset
342342
if o.Assets() != nil {
343343
tmpAssets := o.Assets()
@@ -357,14 +357,20 @@ func (o AlonzoTransactionOutput) Utxorpc() *utxorpc.TxOutput {
357357
}
358358
}
359359

360+
addressBytes, err := o.OutputAddress.Bytes()
361+
if err != nil {
362+
return nil, fmt.Errorf("failed to get address bytes: %w", err)
363+
}
364+
360365
return &utxorpc.TxOutput{
361-
Address: o.OutputAddress.Bytes(),
362-
Coin: o.Amount(),
363-
Assets: assets,
364-
Datum: &utxorpc.Datum{
365-
Hash: o.TxOutputDatumHash.Bytes(),
366+
Address: addressBytes,
367+
Coin: o.Amount(),
368+
Assets: assets,
369+
Datum: &utxorpc.Datum{
370+
Hash: o.TxOutputDatumHash.Bytes(),
371+
},
366372
},
367-
}
373+
nil
368374
}
369375

370376
type AlonzoRedeemer struct {

ledger/babbage/babbage.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -505,12 +505,17 @@ func (o BabbageTransactionOutput) Datum() *cbor.LazyValue {
505505
return nil
506506
}
507507

508-
func (o BabbageTransactionOutput) Utxorpc() *utxorpc.TxOutput {
508+
func (o BabbageTransactionOutput) Utxorpc() (*utxorpc.TxOutput, error) {
509+
// Handle address bytes
510+
addressBytes, err := o.OutputAddress.Bytes()
511+
if err != nil {
512+
return nil, fmt.Errorf("failed to get output address bytes: %w", err)
513+
}
509514
var address []byte
510-
if o.OutputAddress.Bytes() == nil {
515+
if addressBytes == nil {
511516
address = []byte{}
512517
} else {
513-
address = o.OutputAddress.Bytes()
518+
address = addressBytes
514519
}
515520

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

542547
return &utxorpc.TxOutput{
543-
Address: address,
544-
Coin: o.Amount(),
545-
Assets: assets,
546-
Datum: &utxorpc.Datum{
547-
Hash: datumHash,
548-
// OriginalCbor: o.Datum().Cbor(),
548+
Address: address,
549+
Coin: o.Amount(),
550+
Assets: assets,
551+
Datum: &utxorpc.Datum{
552+
Hash: datumHash,
553+
// OriginalCbor: o.Datum().Cbor(),
554+
},
555+
// Script: o.ScriptRef,
549556
},
550-
// Script: o.ScriptRef,
551-
}
557+
nil
552558
}
553559

554560
type BabbageTransactionWitnessSet struct {

ledger/babbage/babbage_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2775,7 +2775,10 @@ func TestBabbageTransactionOutput_Utxorpc_DatumOptionNil(t *testing.T) {
27752775
DatumOption: &BabbageTransactionOutputDatumOption{},
27762776
}
27772777

2778-
txOutput := output.Utxorpc()
2778+
txOutput, err := output.Utxorpc()
2779+
if err != nil {
2780+
t.Fatalf("Utxorpc() failed: %v", err) // Fail and stop the test
2781+
}
27792782

27802783
assert.NotNil(t, txOutput)
27812784
assert.Equal(t, []byte{}, txOutput.Datum.Hash)

ledger/byron/byron.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,16 @@ func (o ByronTransactionOutput) Datum() *cbor.LazyValue {
430430
return nil
431431
}
432432

433-
func (o ByronTransactionOutput) Utxorpc() *utxorpc.TxOutput {
434-
return &utxorpc.TxOutput{
435-
Address: o.OutputAddress.Bytes(),
436-
Coin: o.Amount(),
433+
func (o ByronTransactionOutput) Utxorpc() (*utxorpc.TxOutput, error) {
434+
addressBytes, err := o.OutputAddress.Bytes()
435+
if err != nil {
436+
return nil, fmt.Errorf("failed to get address bytes: %w", err)
437437
}
438+
return &utxorpc.TxOutput{
439+
Address: addressBytes,
440+
Coin: o.Amount(),
441+
},
442+
nil
438443
}
439444

440445
type ByronBlockVersion struct {

ledger/common/address.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,11 @@ func (a *Address) UnmarshalCBOR(data []byte) error {
269269
}
270270

271271
func (a *Address) MarshalCBOR() ([]byte, error) {
272-
addrBytes := a.Bytes()
272+
addrBytes, err := a.Bytes()
273+
if err != nil {
274+
return nil, fmt.Errorf("failed to get address bytes: %w", err)
275+
}
276+
273277
if a.addressType == AddressTypeByron {
274278
return addrBytes, nil
275279
}
@@ -376,7 +380,7 @@ func (a Address) generateHRP() string {
376380
}
377381

378382
// Bytes returns the underlying bytes for the address
379-
func (a Address) Bytes() []byte {
383+
func (a Address) Bytes() ([]byte, error) {
380384
if a.addressType == AddressTypeByron {
381385
tmpPayload := []any{
382386
a.paymentAddress,
@@ -385,8 +389,7 @@ func (a Address) Bytes() []byte {
385389
}
386390
rawPayload, err := cbor.Encode(tmpPayload)
387391
if err != nil {
388-
// TODO: handle error (#851)
389-
return nil
392+
return nil, fmt.Errorf("failed to encode Byron address payload: %w", err)
390393
}
391394
tmpData := []any{
392395
cbor.Tag{
@@ -397,10 +400,9 @@ func (a Address) Bytes() []byte {
397400
}
398401
ret, err := cbor.Encode(tmpData)
399402
if err != nil {
400-
// TODO: handle error (#851)
401-
return nil
403+
return nil, fmt.Errorf("failed to encode Byron address data: %w", err)
402404
}
403-
return ret
405+
return ret, nil
404406
}
405407
ret := []byte{}
406408
ret = append(
@@ -410,12 +412,15 @@ func (a Address) Bytes() []byte {
410412
ret = append(ret, a.paymentAddress...)
411413
ret = append(ret, a.stakingAddress...)
412414
ret = append(ret, a.extraData...)
413-
return ret
415+
return ret, nil
414416
}
415417

416418
// String returns the bech32-encoded version of the address
417419
func (a Address) String() string {
418-
data := a.Bytes()
420+
data, err := a.Bytes()
421+
if err != nil {
422+
panic(fmt.Sprintf("failed to get address bytes: %v", err))
423+
}
419424
if a.addressType == AddressTypeByron {
420425
// Encode data to base58
421426
encoded := base58.Encode(data)

ledger/common/tx.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type TransactionOutput interface {
7070
Datum() *cbor.LazyValue
7171
DatumHash() *Blake2b256
7272
Cbor() []byte
73-
Utxorpc() *utxorpc.TxOutput
73+
Utxorpc() (*utxorpc.TxOutput, error)
7474
}
7575

7676
type TransactionWitnessSet interface {
@@ -200,7 +200,10 @@ func TransactionBodyToUtxorpc(tx TransactionBody) *utxorpc.Tx {
200200
txi = append(txi, input)
201201
}
202202
for _, o := range tx.Outputs() {
203-
output := o.Utxorpc()
203+
output, err := o.Utxorpc()
204+
if err != nil {
205+
return nil
206+
}
204207
txo = append(txo, output)
205208
}
206209
for _, ri := range tx.ReferenceInputs() {

ledger/mary/mary.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,17 @@ func (o MaryTransactionOutput) Datum() *cbor.LazyValue {
455455
return nil
456456
}
457457

458-
func (o MaryTransactionOutput) Utxorpc() *utxorpc.TxOutput {
459-
return &utxorpc.TxOutput{
460-
Address: o.OutputAddress.Bytes(),
461-
Coin: o.Amount(),
462-
// Assets: o.Assets,
458+
func (o MaryTransactionOutput) Utxorpc() (*utxorpc.TxOutput, error) {
459+
addressBytes, err := o.OutputAddress.Bytes()
460+
if err != nil {
461+
return nil, fmt.Errorf("failed to get address bytes: %w", err)
463462
}
463+
return &utxorpc.TxOutput{
464+
Address: addressBytes,
465+
Coin: o.Amount(),
466+
// Assets: o.Assets,
467+
},
468+
err
464469
}
465470

466471
type MaryTransactionOutputValue struct {

ledger/shelley/pparams_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,20 @@ func TestShelleyTransactionOutput_Utxorpc(t *testing.T) {
195195
}
196196

197197
// Convert it to utxorpc format
198-
actual := output.Utxorpc()
198+
actual, err := output.Utxorpc()
199+
if err != nil {
200+
t.Fatalf("Utxorpc() failed: %v", err) // Fail immediately on error
201+
}
202+
203+
// Get expected address bytes (with error handling)
204+
expectedAddressBytes, err := address.Bytes()
205+
if err != nil {
206+
t.Fatalf("address.Bytes() failed: %v", err)
207+
}
199208

200209
// expected output in utxorpc format
201210
expected := &cardano.TxOutput{
202-
Address: address.Bytes(),
211+
Address: expectedAddressBytes,
203212
Coin: 1000,
204213
}
205214

ledger/shelley/shelley.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,16 @@ func (o ShelleyTransactionOutput) Datum() *cbor.LazyValue {
401401
return nil
402402
}
403403

404-
func (o ShelleyTransactionOutput) Utxorpc() *utxorpc.TxOutput {
404+
func (o ShelleyTransactionOutput) Utxorpc() (*utxorpc.TxOutput, error) {
405+
addressBytes, err := o.OutputAddress.Bytes()
406+
if err != nil {
407+
return nil, fmt.Errorf("failed to get address bytes: %w", err)
408+
}
409+
405410
return &utxorpc.TxOutput{
406-
Address: o.OutputAddress.Bytes(),
411+
Address: addressBytes,
407412
Coin: o.Amount(),
408-
}
413+
}, nil
409414
}
410415

411416
type ShelleyTransactionWitnessSet struct {

0 commit comments

Comments
 (0)