Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update address conversion with updated NewAddress #262

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.21.6
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/blinklabs-io/gouroboros v0.100.0
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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/SundaeSwap-finance/ogmigo/v6 v6.0.0-20231128043329-e8ced51013a1 h1:Lf
github.com/SundaeSwap-finance/ogmigo/v6 v6.0.0-20231128043329-e8ced51013a1/go.mod h1:CsDGcgbkKoz6S4h0RJ30go7oXG+KhGE2KLhBpRFnEqA=
github.com/aws/aws-sdk-go v1.48.7 h1:gDcOhmkohlNk20j0uWpko5cLBbwSkB+xpkshQO45F7Y=
github.com/aws/aws-sdk-go v1.48.7/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/blinklabs-io/gouroboros v0.99.0 h1:tGiti2mPjQM/rVOYi1ZrCSlKh8Qt3iQUkDdsOeWKuvw=
github.com/blinklabs-io/gouroboros v0.99.0/go.mod h1:gU9pBcL1h584sVqYF8H7JJcB2x0n1HdWcmWP11VYxPE=
github.com/blinklabs-io/gouroboros v0.100.0 h1:f1M0AA+Zi4YC4sbR0gJBOaMyKTClRTNY894r4Lm7TDk=
github.com/blinklabs-io/gouroboros v0.100.0/go.mod h1:otpDPTJTU/EVt3J4axaJAA1gF/0UtalSdVxVqXK8Zuk=
github.com/blinklabs-io/ouroboros-mock v0.3.4 h1:codPfiI5vLeD6YdhKL5VwYSzy2N3Dsgx6xjcLsqFaJQ=
github.com/blinklabs-io/ouroboros-mock v0.3.4/go.mod h1:e/wgG1ZYVenroN2XEMXy7DgEfdmP7KXVRHIQKuh8E/0=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
Expand Down
8 changes: 7 additions & 1 deletion input/chainsync/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/SundaeSwap-finance/ogmigo/v6/ouroboros/chainsync"
"github.com/blinklabs-io/adder/event"
"github.com/blinklabs-io/adder/internal/logging"
"github.com/blinklabs-io/adder/plugin"

ouroboros "github.com/blinklabs-io/gouroboros"
Expand Down Expand Up @@ -427,7 +428,12 @@ func getKupoClient(c *ChainSync) (*kugo.Client, error) {
return c.kupoClient, nil
}

k := kugo.New(kugo.WithEndpoint(c.kupoUrl))
KugoCustomLogger := logging.NewKugoCustomLogger(logging.LevelInfo)

k := kugo.New(
kugo.WithEndpoint(c.kupoUrl),
kugo.WithLogger(KugoCustomLogger),
)

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
Expand Down
33 changes: 2 additions & 31 deletions input/chainsync/transactionOutput.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ 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 @@ -80,21 +78,10 @@ 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
// Get common.Address from base58 or bech32 string
addr, err := common.NewAddress(match.Address)
if err != nil {
// 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)
}
return nil, fmt.Errorf("failed to convert base58 to bech32: %w", err)
}

assets, amount, err := ExtractAssetDetailsFromMatch(match)
Expand Down Expand Up @@ -147,19 +134,3 @@ 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
}
9 changes: 1 addition & 8 deletions input/chainsync/transactionOutput_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,7 @@ func TestResolvedTransactionOutput_MarshalJSON(t *testing.T) {

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)
addr, err := common.NewAddress(base58Str)
assert.Nil(t, err, "Expected no error when converting to common.Address")
t.Logf("addr: %v", addr)
}
74 changes: 74 additions & 0 deletions internal/logging/kugoCustomLogger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package logging

import (
"log/slog"
"os"

"github.com/SundaeSwap-finance/ogmigo/v6"
)

// LogLevel represents the logging level (either INFO or DEBUG)
type LogLevel int

const (
LevelInfo LogLevel = iota
LevelDebug
)

// KugoCustomLogger is a custom logger that uses slog and filters based on the log level
type KugoCustomLogger struct {
logger *slog.Logger
logLevel LogLevel
}

// Info logs info-level messages
func (l *KugoCustomLogger) Info(message string, kvs ...ogmigo.KeyValue) {
l.logger.Info(message, convertKVs(kvs)...)
}

// Debug logs debug-level messages only if log level is set to DEBUG
func (l *KugoCustomLogger) Debug(message string, kvs ...ogmigo.KeyValue) {
if l.logLevel >= LevelDebug {
l.logger.Debug(message, convertKVs(kvs)...)
}
}

// With returns a new logger with additional context (key-value pairs)
func (l *KugoCustomLogger) With(kvs ...ogmigo.KeyValue) ogmigo.Logger {
return l // Here we just return the same logger, but you can add more context if needed
}

// Helper function to convert ogmigo.KeyValue to slog key-value format
// Flattens the key-value pairs into a single slice
func convertKVs(kvs []ogmigo.KeyValue) []any {
result := make([]any, 0, len(kvs)*2)
for _, kv := range kvs {
result = append(result, kv.Key, kv.Value)
}
return result
}

func NewKugoCustomLogger(level LogLevel) *KugoCustomLogger {
// Create a new slog logger that logs to stdout using JSON format
handler := slog.NewJSONHandler(os.Stdout, nil)
logger := slog.New(handler)

return &KugoCustomLogger{
logger: logger,
logLevel: level,
}
}