Skip to content

Commit c125c9a

Browse files
committed
fix: update address conversion with updated NewAddress
add custom KugoLogger to suppress debug logs Signed-off-by: Ales Verbic <[email protected]>
1 parent aad8a6b commit c125c9a

File tree

6 files changed

+87
-43
lines changed

6 files changed

+87
-43
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ toolchain go1.21.6
77
require (
88
github.com/SundaeSwap-finance/kugo v1.0.5
99
github.com/SundaeSwap-finance/ogmigo/v6 v6.0.0-20231128043329-e8ced51013a1
10-
github.com/blinklabs-io/gouroboros v0.99.0
10+
github.com/blinklabs-io/gouroboros v0.100.0
1111
github.com/gen2brain/beeep v0.0.0-20230602101333-f384c29b62dd
1212
github.com/gin-gonic/gin v1.10.0
1313
github.com/kelseyhightower/envconfig v1.4.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ github.com/SundaeSwap-finance/ogmigo/v6 v6.0.0-20231128043329-e8ced51013a1 h1:Lf
1010
github.com/SundaeSwap-finance/ogmigo/v6 v6.0.0-20231128043329-e8ced51013a1/go.mod h1:CsDGcgbkKoz6S4h0RJ30go7oXG+KhGE2KLhBpRFnEqA=
1111
github.com/aws/aws-sdk-go v1.48.7 h1:gDcOhmkohlNk20j0uWpko5cLBbwSkB+xpkshQO45F7Y=
1212
github.com/aws/aws-sdk-go v1.48.7/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
13-
github.com/blinklabs-io/gouroboros v0.99.0 h1:tGiti2mPjQM/rVOYi1ZrCSlKh8Qt3iQUkDdsOeWKuvw=
14-
github.com/blinklabs-io/gouroboros v0.99.0/go.mod h1:gU9pBcL1h584sVqYF8H7JJcB2x0n1HdWcmWP11VYxPE=
13+
github.com/blinklabs-io/gouroboros v0.100.0 h1:f1M0AA+Zi4YC4sbR0gJBOaMyKTClRTNY894r4Lm7TDk=
14+
github.com/blinklabs-io/gouroboros v0.100.0/go.mod h1:otpDPTJTU/EVt3J4axaJAA1gF/0UtalSdVxVqXK8Zuk=
1515
github.com/blinklabs-io/ouroboros-mock v0.3.4 h1:codPfiI5vLeD6YdhKL5VwYSzy2N3Dsgx6xjcLsqFaJQ=
1616
github.com/blinklabs-io/ouroboros-mock v0.3.4/go.mod h1:e/wgG1ZYVenroN2XEMXy7DgEfdmP7KXVRHIQKuh8E/0=
1717
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=

input/chainsync/chainsync.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/SundaeSwap-finance/ogmigo/v6/ouroboros/chainsync"
2929
"github.com/blinklabs-io/adder/event"
30+
"github.com/blinklabs-io/adder/internal/logging"
3031
"github.com/blinklabs-io/adder/plugin"
3132

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

430-
k := kugo.New(kugo.WithEndpoint(c.kupoUrl))
431+
KugoCustomLogger := logging.NewKugoCustomLogger(logging.LevelInfo)
432+
433+
k := kugo.New(
434+
kugo.WithEndpoint(c.kupoUrl),
435+
kugo.WithLogger(KugoCustomLogger),
436+
)
431437

432438
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
433439
defer cancel()

input/chainsync/transactionOutput.go

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ 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"
2523
"github.com/blinklabs-io/gouroboros/cbor"
2624
"github.com/blinklabs-io/gouroboros/ledger"
2725
"github.com/blinklabs-io/gouroboros/ledger/common"
@@ -80,21 +78,10 @@ func ExtractAssetDetailsFromMatch(match kugo.Match) (common.MultiAsset[uint64],
8078
}
8179

8280
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
81+
// Get common.Address from base58 or bech32 string
8582
addr, err := common.NewAddress(match.Address)
8683
if err != nil {
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-
}
84+
return nil, fmt.Errorf("failed to convert base58 to bech32: %w", err)
9885
}
9986

10087
assets, amount, err := ExtractAssetDetailsFromMatch(match)
@@ -147,19 +134,3 @@ func (txOut ResolvedTransactionOutput) Utxorpc() *utxorpc.TxOutput {
147134
// Placeholder for UTXO RPC representation
148135
return &utxorpc.TxOutput{}
149136
}
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: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,7 @@ func TestResolvedTransactionOutput_MarshalJSON(t *testing.T) {
6868

6969
func TestConvertBase58ToBech32(t *testing.T) {
7070
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)
71+
addr, err := common.NewAddress(base58Str)
7972
assert.Nil(t, err, "Expected no error when converting to common.Address")
8073
t.Logf("addr: %v", addr)
8174
}

internal/logging/kugoCustomLogger.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2024 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package logging
16+
17+
import (
18+
"log/slog"
19+
"os"
20+
21+
"github.com/SundaeSwap-finance/ogmigo/v6"
22+
)
23+
24+
// LogLevel represents the logging level (either INFO or DEBUG)
25+
type LogLevel int
26+
27+
const (
28+
LevelInfo LogLevel = iota
29+
LevelDebug
30+
)
31+
32+
// KugoCustomLogger is a custom logger that uses slog and filters based on the log level
33+
type KugoCustomLogger struct {
34+
logger *slog.Logger
35+
logLevel LogLevel
36+
}
37+
38+
// Info logs info-level messages
39+
func (l *KugoCustomLogger) Info(message string, kvs ...ogmigo.KeyValue) {
40+
l.logger.Info(message, convertKVs(kvs)...)
41+
}
42+
43+
// Debug logs debug-level messages only if log level is set to DEBUG
44+
func (l *KugoCustomLogger) Debug(message string, kvs ...ogmigo.KeyValue) {
45+
if l.logLevel >= LevelDebug {
46+
l.logger.Debug(message, convertKVs(kvs)...)
47+
}
48+
}
49+
50+
// With returns a new logger with additional context (key-value pairs)
51+
func (l *KugoCustomLogger) With(kvs ...ogmigo.KeyValue) ogmigo.Logger {
52+
return l // Here we just return the same logger, but you can add more context if needed
53+
}
54+
55+
// Helper function to convert ogmigo.KeyValue to slog key-value format
56+
// Flattens the key-value pairs into a single slice
57+
func convertKVs(kvs []ogmigo.KeyValue) []any {
58+
result := make([]any, 0, len(kvs)*2)
59+
for _, kv := range kvs {
60+
result = append(result, kv.Key, kv.Value)
61+
}
62+
return result
63+
}
64+
65+
func NewKugoCustomLogger(level LogLevel) *KugoCustomLogger {
66+
// Create a new slog logger that logs to stdout using JSON format
67+
handler := slog.NewJSONHandler(os.Stdout, nil)
68+
logger := slog.New(handler)
69+
70+
return &KugoCustomLogger{
71+
logger: logger,
72+
logLevel: level,
73+
}
74+
}

0 commit comments

Comments
 (0)