diff --git a/input/chainsync/transactionOutput.go b/input/chainsync/transactionOutput.go index 29f7576..c5f7d14 100644 --- a/input/chainsync/transactionOutput.go +++ b/input/chainsync/transactionOutput.go @@ -20,6 +20,8 @@ import ( "log/slog" "github.com/SundaeSwap-finance/kugo" + "github.com/blinklabs-io/gouroboros/base58" + "github.com/blinklabs-io/gouroboros/bech32" "github.com/blinklabs-io/gouroboros/cbor" "github.com/blinklabs-io/gouroboros/ledger" "github.com/blinklabs-io/gouroboros/ledger/common" @@ -78,9 +80,21 @@ func ExtractAssetDetailsFromMatch(match kugo.Match) (common.MultiAsset[uint64], } func NewResolvedTransactionOutput(match kugo.Match) (ledger.TransactionOutput, error) { + // FIXME - This is a patch to fix the issue with the address + // Attempt to create an address using Bech32 addr, err := common.NewAddress(match.Address) if err != nil { - return nil, fmt.Errorf("failed to create address from match.Address: %w", err) + // If Bech32 fails, try to convert from Base58 to Bech32 + bech32addr, err := ConvertBase58ToBech32(match.Address, "addr") + if err != nil { + return nil, fmt.Errorf("failed to convert base58 to bech32: %w", err) + } + + // Try to create the address again with the converted Bech32 address + addr, err = common.NewAddress(bech32addr) + if err != nil { + return nil, fmt.Errorf("failed to create address from base58-converted bech32 address: %w", err) + } } assets, amount, err := ExtractAssetDetailsFromMatch(match) @@ -133,3 +147,19 @@ func (txOut ResolvedTransactionOutput) Utxorpc() *utxorpc.TxOutput { // Placeholder for UTXO RPC representation return &utxorpc.TxOutput{} } + +// ConvertBase58ToBech32 converts a Base58 string to a Bech32 string +// using the given human-readable part (hrp) required for Bech32 encoding +func ConvertBase58ToBech32(base58Str, hrp string) (string, error) { + data := base58.Decode(base58Str) + converted, err := bech32.ConvertBits(data, 8, 5, true) + if err != nil { + return "", fmt.Errorf("failed to convert bits: %w", err) + } + bech32Str, err := bech32.Encode(hrp, converted) + if err != nil { + return "", fmt.Errorf("failed to encode Bech32: %w", err) + } + + return bech32Str, nil +} diff --git a/input/chainsync/transactionOutput_test.go b/input/chainsync/transactionOutput_test.go index a80273d..7be18c2 100644 --- a/input/chainsync/transactionOutput_test.go +++ b/input/chainsync/transactionOutput_test.go @@ -65,3 +65,17 @@ func TestResolvedTransactionOutput_MarshalJSON(t *testing.T) { assert.JSONEq(t, expectedJSON, string(jsonOutput)) } + +func TestConvertBase58ToBech32(t *testing.T) { + base58Str := "Ae2tdPwUPEYwFx4dmJheyNPPYXtvHbJLeCaA96o6Y2iiUL18cAt7AizN2zG" + hrp := "addr" + expectedBech32Str := "addr1stvpskppsdvpcpyxtepdyde6mklt6hf2e7quwcxgfztszs5gnalwwccfrwsqqxhsrytd2f4f5v6" + + bech32Str, err := ConvertBase58ToBech32(base58Str, hrp) + assert.Nil(t, err, "Expected no error when converting Base58 to Bech32") + assert.Equal(t, expectedBech32Str, bech32Str, "The Bech32 string did not match the expected value") + + addr, err := common.NewAddress(bech32Str) + assert.Nil(t, err, "Expected no error when converting to common.Address") + t.Logf("addr: %v", addr) +}