Skip to content
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
9 changes: 5 additions & 4 deletions address-codec/compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/Peersyst/xrpl-go/pkg/crypto"
"github.com/Peersyst/xrpl-go/pkg/hexutil"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -82,7 +83,7 @@ func TestCompat_EncodeDecodeAccountID(t *testing.T) {
// Test decoding
_, decoded, err := DecodeClassicAddressToAccountID(tc.Base58)
require.NoError(t, err)
require.Equal(t, strings.ToUpper(tc.Hex), strings.ToUpper(hex.EncodeToString(decoded)), "Decoding mismatch for base58: %s", tc.Base58)
require.Equal(t, strings.ToUpper(tc.Hex), hexutil.EncodeToUpperHex(decoded), "Decoding mismatch for base58: %s", tc.Base58)
})
}
}
Expand All @@ -104,7 +105,7 @@ func TestCompat_EncodeDecodeNodePublic(t *testing.T) {
// Test decoding
decoded, err := DecodeNodePublicKey(tc.Base58)
require.NoError(t, err)
require.Equal(t, strings.ToUpper(tc.Hex), strings.ToUpper(hex.EncodeToString(decoded)), "Decoding mismatch for base58: %s", tc.Base58)
require.Equal(t, strings.ToUpper(tc.Hex), hexutil.EncodeToUpperHex(decoded), "Decoding mismatch for base58: %s", tc.Base58)
})
}
}
Expand All @@ -126,7 +127,7 @@ func TestCompat_EncodeDecodeAccountPublic(t *testing.T) {
// Test decoding
decoded, err := DecodeAccountPublicKey(tc.Base58)
require.NoError(t, err)
require.Equal(t, strings.ToUpper(tc.Hex), strings.ToUpper(hex.EncodeToString(decoded)), "Decoding mismatch for base58: %s", tc.Base58)
require.Equal(t, strings.ToUpper(tc.Hex), hexutil.EncodeToUpperHex(decoded), "Decoding mismatch for base58: %s", tc.Base58)
})
}
}
Expand Down Expand Up @@ -160,7 +161,7 @@ func TestCompat_DecodeSeed(t *testing.T) {
t.Run(tc.Base58, func(t *testing.T) {
decoded, cryptoType, err := DecodeSeed(tc.Base58)
require.NoError(t, err)
require.Equal(t, strings.ToUpper(tc.Hex), strings.ToUpper(hex.EncodeToString(decoded)), "Seed decoding mismatch for base58: %s", tc.Base58)
require.Equal(t, strings.ToUpper(tc.Hex), hexutil.EncodeToUpperHex(decoded), "Seed decoding mismatch for base58: %s", tc.Base58)

// Check type by comparing with known implementations
if tc.Type == "ed25519" {
Expand Down
3 changes: 2 additions & 1 deletion binary-codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/Peersyst/xrpl-go/binary-codec/serdes"
"github.com/Peersyst/xrpl-go/binary-codec/types"
"github.com/Peersyst/xrpl-go/pkg/hexutil"
)

var (
Expand Down Expand Up @@ -64,7 +65,7 @@ func Encode(json map[string]any) (string, error) {
return "", err
}

return strings.ToUpper(hex.EncodeToString(b)), nil
return hexutil.EncodeToUpperHex(b), nil
}

// EncodeForMultisigning encodes a transaction into binary format in preparation for providing one
Expand Down
8 changes: 4 additions & 4 deletions binary-codec/ledger_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/binary"
"encoding/hex"
"strconv"
"strings"

"github.com/Peersyst/xrpl-go/binary-codec/definitions"
"github.com/Peersyst/xrpl-go/binary-codec/serdes"
"github.com/Peersyst/xrpl-go/pkg/hexutil"
)

// LedgerData represents the data of a ledger.
Expand Down Expand Up @@ -53,21 +53,21 @@ func DecodeLedgerData(data string) (LedgerData, error) {
return LedgerData{}, err
}

ledgerData.ParentHash = strings.ToUpper(hex.EncodeToString(parentHash))
ledgerData.ParentHash = hexutil.EncodeToUpperHex(parentHash)

transactionHash, err := parser.ReadBytes(32)
if err != nil {
return LedgerData{}, err
}

ledgerData.TransactionHash = strings.ToUpper(hex.EncodeToString(transactionHash))
ledgerData.TransactionHash = hexutil.EncodeToUpperHex(transactionHash)

accountHash, err := parser.ReadBytes(32)
if err != nil {
return LedgerData{}, err
}

ledgerData.AccountHash = strings.ToUpper(hex.EncodeToString(accountHash))
ledgerData.AccountHash = hexutil.EncodeToUpperHex(accountHash)

parentCloseTime, err := parser.ReadBytes(4)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion binary-codec/quality.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

bigdecimal "github.com/Peersyst/xrpl-go/pkg/big-decimal"
"github.com/Peersyst/xrpl-go/pkg/hexutil"
)

const (
Expand Down Expand Up @@ -68,7 +69,7 @@ func EncodeQuality(quality string) (string, error) {
serialized := make([]byte, 8)
binary.BigEndian.PutUint64(serialized, mantissa)
serialized[0] += byte(exp) + 100
return strings.ToUpper(hex.EncodeToString(serialized)), nil
return hexutil.EncodeToUpperHex(serialized), nil
}

// DecodeQuality decodes a quality amount from a hex string to a string.
Expand Down
3 changes: 2 additions & 1 deletion binary-codec/types/amount.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
addresscodec "github.com/Peersyst/xrpl-go/address-codec"
"github.com/Peersyst/xrpl-go/binary-codec/types/interfaces"
bigdecimal "github.com/Peersyst/xrpl-go/pkg/big-decimal"
"github.com/Peersyst/xrpl-go/pkg/hexutil"
)

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

if !ok {
return strings.ToUpper(hex.EncodeToString(data)), nil
return hexutil.EncodeToUpperHex(data), nil
}
return iso, nil
}
Expand Down
4 changes: 2 additions & 2 deletions binary-codec/types/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package types
import (
"encoding/hex"
"errors"
"strings"

"github.com/Peersyst/xrpl-go/binary-codec/types/interfaces"
"github.com/Peersyst/xrpl-go/pkg/hexutil"
)

// ErrNoLengthPrefix error is raised when no length prefix size is given.
Expand Down Expand Up @@ -41,5 +41,5 @@ func (b *Blob) ToJSON(p interfaces.BinaryParser, opts ...int) (any, error) {
return nil, err
}
// Convert the bytes to a hexadecimal string and return it.
return strings.ToUpper(hex.EncodeToString(val)), nil
return hexutil.EncodeToUpperHex(val), nil
}
4 changes: 2 additions & 2 deletions binary-codec/types/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package types
import (
"encoding/hex"
"fmt"
"strings"

"github.com/Peersyst/xrpl-go/binary-codec/types/interfaces"
"github.com/Peersyst/xrpl-go/pkg/hexutil"
)

// ErrInvalidHashLength struct is used when the hash length does not meet the expected value.
Expand Down Expand Up @@ -84,5 +84,5 @@ func (h hash) ToJSON(p interfaces.BinaryParser, _ ...int) (any, error) {
if err != nil {
return nil, err
}
return strings.ToUpper(hex.EncodeToString(b)), nil
return hexutil.EncodeToUpperHex(b), nil
}
6 changes: 3 additions & 3 deletions binary-codec/types/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"encoding/binary"
"encoding/hex"
"errors"
"strings"

addresscodec "github.com/Peersyst/xrpl-go/address-codec"
"github.com/Peersyst/xrpl-go/binary-codec/types/interfaces"
"github.com/Peersyst/xrpl-go/pkg/hexutil"
)

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

Expand Down Expand Up @@ -183,7 +183,7 @@ func decodeCurrencyBytes(currencyBytes []byte) string {
}

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

func (i *Issue) isIssueObject(obj any) bool {
Expand Down
3 changes: 2 additions & 1 deletion binary-codec/types/uint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/Peersyst/xrpl-go/binary-codec/types/interfaces"
"github.com/Peersyst/xrpl-go/pkg/hexutil"
)

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

// isNumeric checks if a string only contains numerical values.
Expand Down
5 changes: 2 additions & 3 deletions binary-codec/types/vector256.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
package types

import (
"encoding/hex"
"fmt"
"strings"

"github.com/Peersyst/xrpl-go/binary-codec/types/interfaces"
"github.com/Peersyst/xrpl-go/pkg/hexutil"
)

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

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

return value, nil
Expand Down
5 changes: 2 additions & 3 deletions examples/multisigning/rpc/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package main

import (
"encoding/hex"
"fmt"
"maps"
"strings"

"github.com/Peersyst/xrpl-go/pkg/hexutil"
"github.com/Peersyst/xrpl-go/xrpl"
"github.com/Peersyst/xrpl-go/xrpl/faucet"
"github.com/Peersyst/xrpl-go/xrpl/ledger-entry-types"
Expand Down Expand Up @@ -123,7 +122,7 @@ func main() {
BaseTx: transaction.BaseTx{
Account: master.GetAddress(),
},
Domain: types.Domain(strings.ToUpper(hex.EncodeToString([]byte("example.com")))),
Domain: types.Domain(hexutil.EncodeToUpperHex([]byte("example.com"))),
}

flatAs := as.Flatten()
Expand Down
5 changes: 2 additions & 3 deletions examples/multisigning/ws/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package main

import (
"encoding/hex"
"fmt"
"maps"
"strings"

"github.com/Peersyst/xrpl-go/pkg/hexutil"
"github.com/Peersyst/xrpl-go/xrpl"
"github.com/Peersyst/xrpl-go/xrpl/faucet"
"github.com/Peersyst/xrpl-go/xrpl/ledger-entry-types"
Expand Down Expand Up @@ -143,7 +142,7 @@ func main() {
BaseTx: transaction.BaseTx{
Account: master.GetAddress(),
},
Domain: types.Domain(strings.ToUpper(hex.EncodeToString([]byte("example.com")))),
Domain: types.Domain(hexutil.EncodeToUpperHex([]byte("example.com"))),
}

flatAs := as.Flatten()
Expand Down
9 changes: 5 additions & 4 deletions pkg/crypto/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"bytes"
"crypto/ed25519"
"encoding/hex"
"strings"

"github.com/Peersyst/xrpl-go/pkg/hexutil"
)

const (
Expand Down Expand Up @@ -50,9 +51,9 @@ func (c ED25519CryptoAlgorithm) DeriveKeypair(decodedSeed []byte, validator bool
return "", "", err
}
pubKey = append([]byte{c.prefix}, pubKey...)
public := strings.ToUpper(hex.EncodeToString(pubKey))
public := hexutil.EncodeToUpperHex(pubKey)
privKey = append([]byte{c.prefix}, privKey...)
private := strings.ToUpper(hex.EncodeToString(privKey[:32+len([]byte{c.prefix})]))
private := hexutil.EncodeToUpperHex(privKey[:32+len([]byte{c.prefix})])
return private, public, nil
}

Expand All @@ -64,7 +65,7 @@ func (c ED25519CryptoAlgorithm) Sign(msg, privKey string) (string, error) {
}
rawPriv := ed25519.NewKeyFromSeed(b[1:])
signedMsg := ed25519.Sign(rawPriv, []byte(msg))
return strings.ToUpper(hex.EncodeToString(signedMsg)), nil
return hexutil.EncodeToUpperHex(signedMsg), nil
}

// Validate validates a signature for a message with a public key.
Expand Down
5 changes: 3 additions & 2 deletions pkg/crypto/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/big"
"strings"

"github.com/Peersyst/xrpl-go/pkg/hexutil"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
ecdsa "github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa"
Expand Down Expand Up @@ -100,13 +101,13 @@ func (c SECP256K1CryptoAlgorithm) DeriveKeypair(seed []byte, validator bool) (st
privateKey := scalarWithPrivateGen.Mod(scalarWithPrivateGen, order)

privKeyBytes := privateKey.Bytes()
private := strings.ToUpper(hex.EncodeToString(privKeyBytes))
private := hexutil.EncodeToUpperHex(privKeyBytes)

_, pubKey := btcec.PrivKeyFromBytes(privKeyBytes)

pubKeyBytes := pubKey.SerializeCompressed()

return "00" + private, strings.ToUpper(hex.EncodeToString(pubKeyBytes)), nil
return "00" + private, hexutil.EncodeToUpperHex(pubKeyBytes), nil
}

// Sign signs a message with a private key.
Expand Down
12 changes: 12 additions & 0 deletions pkg/hexutil/hexutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Package hexutil provides utility functions for hexadecimal encoding.
package hexutil

import (
"encoding/hex"
"strings"
)

// EncodeToUpperHex encodes bytes to an uppercase hexadecimal string.
func EncodeToUpperHex(b []byte) string {
return strings.ToUpper(hex.EncodeToString(b))
}
48 changes: 48 additions & 0 deletions pkg/hexutil/hexutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package hexutil

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestEncodeToUpperHex(t *testing.T) {
tt := []struct {
name string
input []byte
expected string
}{
{
name: "pass - nil input returns empty string",
input: nil,
expected: "",
},
{
name: "pass - empty input returns empty string",
input: []byte{},
expected: "",
},
{
name: "pass - single byte encodes correctly",
input: []byte{0xFF},
expected: "FF",
},
{
name: "pass - multiple bytes encode correctly",
input: []byte{0xDE, 0xAD, 0xBE, 0xEF},
expected: "DEADBEEF",
},
{
name: "pass - output is uppercase",
input: []byte{0xab, 0xcd, 0xef},
expected: "ABCDEF",
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
got := EncodeToUpperHex(tc.input)
require.Equal(t, tc.expected, got)
})
}
}
Loading