diff --git a/go.mod b/go.mod index 25654e3..8eb5ca6 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/SundaeSwap-finance/kugo v1.0.5 github.com/SundaeSwap-finance/ogmigo/v6 v6.0.0-20231128043329-e8ced51013a1 github.com/blinklabs-io/gouroboros v0.99.0 + github.com/cosmos/btcutil v1.0.5 github.com/gen2brain/beeep v0.0.0-20230602101333-f384c29b62dd github.com/gin-gonic/gin v1.10.0 github.com/kelseyhightower/envconfig v1.4.0 diff --git a/go.sum b/go.sum index e8211d9..8b47faf 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,8 @@ github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/ github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +github.com/cosmos/btcutil v1.0.5 h1:t+ZFcX77LpKtDBhjucvnOH8C2l2ioGsBNEQ3jef8xFk= +github.com/cosmos/btcutil v1.0.5/go.mod h1:IyB7iuqZMJlthe2tkIFL33xPyzbFYP0XVdS8P5lUPis= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/input/chainsync/transactionOutput.go b/input/chainsync/transactionOutput.go index 29f7576..c1c2fe9 100644 --- a/input/chainsync/transactionOutput.go +++ b/input/chainsync/transactionOutput.go @@ -23,6 +23,8 @@ import ( "github.com/blinklabs-io/gouroboros/cbor" "github.com/blinklabs-io/gouroboros/ledger" "github.com/blinklabs-io/gouroboros/ledger/common" + "github.com/cosmos/btcutil/base58" + "github.com/cosmos/btcutil/bech32" utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" ) @@ -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) +}