Skip to content

Commit

Permalink
fix: address conversion for type base58 and bech32
Browse files Browse the repository at this point in the history
Signed-off-by: Ales Verbic <verbotenj@blinklabs.io>
  • Loading branch information
verbotenj committed Oct 14, 2024
1 parent bd08ec2 commit 36dcba0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
32 changes: 31 additions & 1 deletion input/chainsync/transactionOutput.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
14 changes: 14 additions & 0 deletions input/chainsync/transactionOutput_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 36dcba0

Please sign in to comment.