Skip to content

Commit aad8a6b

Browse files
authored
fix: address conversion for type base58 and bech32 (#261)
Signed-off-by: Ales Verbic <[email protected]>
1 parent 5b00409 commit aad8a6b

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

input/chainsync/transactionOutput.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"log/slog"
2121

2222
"github.com/SundaeSwap-finance/kugo"
23+
"github.com/blinklabs-io/gouroboros/base58"
24+
"github.com/blinklabs-io/gouroboros/bech32"
2325
"github.com/blinklabs-io/gouroboros/cbor"
2426
"github.com/blinklabs-io/gouroboros/ledger"
2527
"github.com/blinklabs-io/gouroboros/ledger/common"
@@ -78,9 +80,21 @@ func ExtractAssetDetailsFromMatch(match kugo.Match) (common.MultiAsset[uint64],
7880
}
7981

8082
func NewResolvedTransactionOutput(match kugo.Match) (ledger.TransactionOutput, error) {
83+
// FIXME - This is a patch to fix the issue with the address
84+
// Attempt to create an address using Bech32
8185
addr, err := common.NewAddress(match.Address)
8286
if err != nil {
83-
return nil, fmt.Errorf("failed to create address from match.Address: %w", err)
87+
// If Bech32 fails, try to convert from Base58 to Bech32
88+
bech32addr, err := ConvertBase58ToBech32(match.Address, "addr")
89+
if err != nil {
90+
return nil, fmt.Errorf("failed to convert base58 to bech32: %w", err)
91+
}
92+
93+
// Try to create the address again with the converted Bech32 address
94+
addr, err = common.NewAddress(bech32addr)
95+
if err != nil {
96+
return nil, fmt.Errorf("failed to create address from base58-converted bech32 address: %w", err)
97+
}
8498
}
8599

86100
assets, amount, err := ExtractAssetDetailsFromMatch(match)
@@ -133,3 +147,19 @@ func (txOut ResolvedTransactionOutput) Utxorpc() *utxorpc.TxOutput {
133147
// Placeholder for UTXO RPC representation
134148
return &utxorpc.TxOutput{}
135149
}
150+
151+
// ConvertBase58ToBech32 converts a Base58 string to a Bech32 string
152+
// using the given human-readable part (hrp) required for Bech32 encoding
153+
func ConvertBase58ToBech32(base58Str, hrp string) (string, error) {
154+
data := base58.Decode(base58Str)
155+
converted, err := bech32.ConvertBits(data, 8, 5, true)
156+
if err != nil {
157+
return "", fmt.Errorf("failed to convert bits: %w", err)
158+
}
159+
bech32Str, err := bech32.Encode(hrp, converted)
160+
if err != nil {
161+
return "", fmt.Errorf("failed to encode Bech32: %w", err)
162+
}
163+
164+
return bech32Str, nil
165+
}

input/chainsync/transactionOutput_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,17 @@ func TestResolvedTransactionOutput_MarshalJSON(t *testing.T) {
6565

6666
assert.JSONEq(t, expectedJSON, string(jsonOutput))
6767
}
68+
69+
func TestConvertBase58ToBech32(t *testing.T) {
70+
base58Str := "Ae2tdPwUPEYwFx4dmJheyNPPYXtvHbJLeCaA96o6Y2iiUL18cAt7AizN2zG"
71+
hrp := "addr"
72+
expectedBech32Str := "addr1stvpskppsdvpcpyxtepdyde6mklt6hf2e7quwcxgfztszs5gnalwwccfrwsqqxhsrytd2f4f5v6"
73+
74+
bech32Str, err := ConvertBase58ToBech32(base58Str, hrp)
75+
assert.Nil(t, err, "Expected no error when converting Base58 to Bech32")
76+
assert.Equal(t, expectedBech32Str, bech32Str, "The Bech32 string did not match the expected value")
77+
78+
addr, err := common.NewAddress(bech32Str)
79+
assert.Nil(t, err, "Expected no error when converting to common.Address")
80+
t.Logf("addr: %v", addr)
81+
}

0 commit comments

Comments
 (0)