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 2569097
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
32 changes: 31 additions & 1 deletion input/chainsync/transactionOutput.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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 2569097

Please sign in to comment.