Skip to content

Commit 4da5894

Browse files
Merge pull request #198 from kpitapeersyst/pkg/xrpl/refactor/encoding-utils
refactor(pkg): add EncodeToUpperHex utility and use across packages
2 parents 79e7349 + 77703e2 commit 4da5894

File tree

21 files changed

+107
-45
lines changed

21 files changed

+107
-45
lines changed

address-codec/compat_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/Peersyst/xrpl-go/pkg/crypto"
11+
"github.com/Peersyst/xrpl-go/pkg/hexutil"
1112
"github.com/stretchr/testify/require"
1213
)
1314

@@ -82,7 +83,7 @@ func TestCompat_EncodeDecodeAccountID(t *testing.T) {
8283
// Test decoding
8384
_, decoded, err := DecodeClassicAddressToAccountID(tc.Base58)
8485
require.NoError(t, err)
85-
require.Equal(t, strings.ToUpper(tc.Hex), strings.ToUpper(hex.EncodeToString(decoded)), "Decoding mismatch for base58: %s", tc.Base58)
86+
require.Equal(t, strings.ToUpper(tc.Hex), hexutil.EncodeToUpperHex(decoded), "Decoding mismatch for base58: %s", tc.Base58)
8687
})
8788
}
8889
}
@@ -104,7 +105,7 @@ func TestCompat_EncodeDecodeNodePublic(t *testing.T) {
104105
// Test decoding
105106
decoded, err := DecodeNodePublicKey(tc.Base58)
106107
require.NoError(t, err)
107-
require.Equal(t, strings.ToUpper(tc.Hex), strings.ToUpper(hex.EncodeToString(decoded)), "Decoding mismatch for base58: %s", tc.Base58)
108+
require.Equal(t, strings.ToUpper(tc.Hex), hexutil.EncodeToUpperHex(decoded), "Decoding mismatch for base58: %s", tc.Base58)
108109
})
109110
}
110111
}
@@ -126,7 +127,7 @@ func TestCompat_EncodeDecodeAccountPublic(t *testing.T) {
126127
// Test decoding
127128
decoded, err := DecodeAccountPublicKey(tc.Base58)
128129
require.NoError(t, err)
129-
require.Equal(t, strings.ToUpper(tc.Hex), strings.ToUpper(hex.EncodeToString(decoded)), "Decoding mismatch for base58: %s", tc.Base58)
130+
require.Equal(t, strings.ToUpper(tc.Hex), hexutil.EncodeToUpperHex(decoded), "Decoding mismatch for base58: %s", tc.Base58)
130131
})
131132
}
132133
}
@@ -160,7 +161,7 @@ func TestCompat_DecodeSeed(t *testing.T) {
160161
t.Run(tc.Base58, func(t *testing.T) {
161162
decoded, cryptoType, err := DecodeSeed(tc.Base58)
162163
require.NoError(t, err)
163-
require.Equal(t, strings.ToUpper(tc.Hex), strings.ToUpper(hex.EncodeToString(decoded)), "Seed decoding mismatch for base58: %s", tc.Base58)
164+
require.Equal(t, strings.ToUpper(tc.Hex), hexutil.EncodeToUpperHex(decoded), "Seed decoding mismatch for base58: %s", tc.Base58)
164165

165166
// Check type by comparing with known implementations
166167
if tc.Type == "ed25519" {

binary-codec/codec.go

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

1313
"github.com/Peersyst/xrpl-go/binary-codec/serdes"
1414
"github.com/Peersyst/xrpl-go/binary-codec/types"
15+
"github.com/Peersyst/xrpl-go/pkg/hexutil"
1516
)
1617

1718
var (
@@ -64,7 +65,7 @@ func Encode(json map[string]any) (string, error) {
6465
return "", err
6566
}
6667

67-
return strings.ToUpper(hex.EncodeToString(b)), nil
68+
return hexutil.EncodeToUpperHex(b), nil
6869
}
6970

7071
// EncodeForMultisigning encodes a transaction into binary format in preparation for providing one

binary-codec/ledger_data.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"encoding/binary"
55
"encoding/hex"
66
"strconv"
7-
"strings"
87

98
"github.com/Peersyst/xrpl-go/binary-codec/definitions"
109
"github.com/Peersyst/xrpl-go/binary-codec/serdes"
10+
"github.com/Peersyst/xrpl-go/pkg/hexutil"
1111
)
1212

1313
// LedgerData represents the data of a ledger.
@@ -53,21 +53,21 @@ func DecodeLedgerData(data string) (LedgerData, error) {
5353
return LedgerData{}, err
5454
}
5555

56-
ledgerData.ParentHash = strings.ToUpper(hex.EncodeToString(parentHash))
56+
ledgerData.ParentHash = hexutil.EncodeToUpperHex(parentHash)
5757

5858
transactionHash, err := parser.ReadBytes(32)
5959
if err != nil {
6060
return LedgerData{}, err
6161
}
6262

63-
ledgerData.TransactionHash = strings.ToUpper(hex.EncodeToString(transactionHash))
63+
ledgerData.TransactionHash = hexutil.EncodeToUpperHex(transactionHash)
6464

6565
accountHash, err := parser.ReadBytes(32)
6666
if err != nil {
6767
return LedgerData{}, err
6868
}
6969

70-
ledgerData.AccountHash = strings.ToUpper(hex.EncodeToString(accountHash))
70+
ledgerData.AccountHash = hexutil.EncodeToUpperHex(accountHash)
7171

7272
parentCloseTime, err := parser.ReadBytes(4)
7373
if err != nil {

binary-codec/quality.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
bigdecimal "github.com/Peersyst/xrpl-go/pkg/big-decimal"
11+
"github.com/Peersyst/xrpl-go/pkg/hexutil"
1112
)
1213

1314
const (
@@ -68,7 +69,7 @@ func EncodeQuality(quality string) (string, error) {
6869
serialized := make([]byte, 8)
6970
binary.BigEndian.PutUint64(serialized, mantissa)
7071
serialized[0] += byte(exp) + 100
71-
return strings.ToUpper(hex.EncodeToString(serialized)), nil
72+
return hexutil.EncodeToUpperHex(serialized), nil
7273
}
7374

7475
// DecodeQuality decodes a quality amount from a hex string to a string.

binary-codec/types/amount.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
addresscodec "github.com/Peersyst/xrpl-go/address-codec"
1717
"github.com/Peersyst/xrpl-go/binary-codec/types/interfaces"
1818
bigdecimal "github.com/Peersyst/xrpl-go/pkg/big-decimal"
19+
"github.com/Peersyst/xrpl-go/pkg/hexutil"
1920
)
2021

2122
const (
@@ -278,7 +279,7 @@ func deserializeCurrencyCode(data []byte) (string, error) {
278279
ok, _ := regexp.MatchString(IOUCodeRegex, iso)
279280

280281
if !ok {
281-
return strings.ToUpper(hex.EncodeToString(data)), nil
282+
return hexutil.EncodeToUpperHex(data), nil
282283
}
283284
return iso, nil
284285
}

binary-codec/types/blob.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ package types
44
import (
55
"encoding/hex"
66
"errors"
7-
"strings"
87

98
"github.com/Peersyst/xrpl-go/binary-codec/types/interfaces"
9+
"github.com/Peersyst/xrpl-go/pkg/hexutil"
1010
)
1111

1212
// ErrNoLengthPrefix error is raised when no length prefix size is given.
@@ -41,5 +41,5 @@ func (b *Blob) ToJSON(p interfaces.BinaryParser, opts ...int) (any, error) {
4141
return nil, err
4242
}
4343
// Convert the bytes to a hexadecimal string and return it.
44-
return strings.ToUpper(hex.EncodeToString(val)), nil
44+
return hexutil.EncodeToUpperHex(val), nil
4545
}

binary-codec/types/hash.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ package types
44
import (
55
"encoding/hex"
66
"fmt"
7-
"strings"
87

98
"github.com/Peersyst/xrpl-go/binary-codec/types/interfaces"
9+
"github.com/Peersyst/xrpl-go/pkg/hexutil"
1010
)
1111

1212
// ErrInvalidHashLength struct is used when the hash length does not meet the expected value.
@@ -84,5 +84,5 @@ func (h hash) ToJSON(p interfaces.BinaryParser, _ ...int) (any, error) {
8484
if err != nil {
8585
return nil, err
8686
}
87-
return strings.ToUpper(hex.EncodeToString(b)), nil
87+
return hexutil.EncodeToUpperHex(b), nil
8888
}

binary-codec/types/issue.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"encoding/binary"
66
"encoding/hex"
77
"errors"
8-
"strings"
98

109
addresscodec "github.com/Peersyst/xrpl-go/address-codec"
1110
"github.com/Peersyst/xrpl-go/binary-codec/types/interfaces"
11+
"github.com/Peersyst/xrpl-go/pkg/hexutil"
1212
)
1313

1414
const (
@@ -137,7 +137,7 @@ func (i *Issue) ToJSON(p interfaces.BinaryParser, _ ...int) (any, error) {
137137
// mpt_issuance_id = sequence (BE) + issuer account
138138
seqBE = append(seqBE, currencyOrAccount...)
139139
return map[string]any{
140-
"mpt_issuance_id": strings.ToUpper(hex.EncodeToString(seqBE)),
140+
"mpt_issuance_id": hexutil.EncodeToUpperHex(seqBE),
141141
}, nil
142142
}
143143

@@ -183,7 +183,7 @@ func decodeCurrencyBytes(currencyBytes []byte) string {
183183
}
184184

185185
// Return hex-encoded currency for non-standard codes
186-
return strings.ToUpper(hex.EncodeToString(currencyBytes))
186+
return hexutil.EncodeToUpperHex(currencyBytes)
187187
}
188188

189189
func (i *Issue) isIssueObject(obj any) bool {

binary-codec/types/uint64.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"github.com/Peersyst/xrpl-go/binary-codec/types/interfaces"
13+
"github.com/Peersyst/xrpl-go/pkg/hexutil"
1314
)
1415

1516
// UInt64 represents a 64-bit unsigned integer.
@@ -86,7 +87,7 @@ func (u *UInt64) ToJSON(p interfaces.BinaryParser, _ ...int) (any, error) {
8687
if err != nil {
8788
return nil, err
8889
}
89-
return strings.ToUpper(hex.EncodeToString(b)), nil
90+
return hexutil.EncodeToUpperHex(b), nil
9091
}
9192

9293
// isNumeric checks if a string only contains numerical values.

binary-codec/types/vector256.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
package types
33

44
import (
5-
"encoding/hex"
65
"fmt"
7-
"strings"
86

97
"github.com/Peersyst/xrpl-go/binary-codec/types/interfaces"
8+
"github.com/Peersyst/xrpl-go/pkg/hexutil"
109
)
1110

1211
// HashLengthBytes is the byte length of a hash in Vector256.
@@ -86,7 +85,7 @@ func (v *Vector256) ToJSON(p interfaces.BinaryParser, opts ...int) (any, error)
8685
var value []string
8786

8887
for i := 0; i < len(b); i += HashLengthBytes {
89-
value = append(value, strings.ToUpper(hex.EncodeToString(b[i:i+HashLengthBytes])))
88+
value = append(value, hexutil.EncodeToUpperHex(b[i:i+HashLengthBytes]))
9089
}
9190

9291
return value, nil

0 commit comments

Comments
 (0)