From 6127ff86d9111a2b6b196d4d68eb60f590afa713 Mon Sep 17 00:00:00 2001 From: amit-momin Date: Wed, 15 Jan 2025 18:00:42 -0600 Subject: [PATCH] Removed duplicated public key encoder --- pkg/solana/codec/public_key.go | 56 ---------------------- pkg/solana/codec/public_key_test.go | 72 ----------------------------- 2 files changed, 128 deletions(-) delete mode 100644 pkg/solana/codec/public_key.go delete mode 100644 pkg/solana/codec/public_key_test.go diff --git a/pkg/solana/codec/public_key.go b/pkg/solana/codec/public_key.go deleted file mode 100644 index 10540504d..000000000 --- a/pkg/solana/codec/public_key.go +++ /dev/null @@ -1,56 +0,0 @@ -package codec - -import ( - "fmt" - "reflect" - - "github.com/gagliardetto/solana-go" -) - -func NewPublicKey() *PublicKey { - return &PublicKey{} -} - -type PublicKey struct{} - -func (p PublicKey) Encode(value any, into []byte) ([]byte, error) { - if value == nil { - return []byte{}, nil - } - - switch v := value.(type) { - case string: - key, err := solana.PublicKeyFromBase58(v) - return append(into, key.Bytes()...), err - case []byte: - out := solana.PublicKeyFromBytes(v) - return out.Bytes(), nil - case *[]byte: - if v == nil { - return nil, fmt.Errorf("nil bytes provided for encoding public key") - } - out := solana.PublicKeyFromBytes(*v) - return append(into, out.Bytes()...), nil - case solana.PublicKey: - return append(into, v.Bytes()...), nil - default: - return nil, fmt.Errorf("unknown type %T provided for encoding solana public key", value) - } -} - -func (p PublicKey) Decode(encoded []byte) (any, []byte, error) { - return solana.PublicKeyFromBytes(encoded), nil, nil -} - -func (p PublicKey) GetType() reflect.Type { - // Pointer type so that nil can inject values and so that the NamedCodec won't wrap with no-nil pointer. - return reflect.TypeOf(&[]byte{}) -} - -func (p PublicKey) Size(_ int) (int, error) { - return solana.PublicKeyLength, nil -} - -func (p PublicKey) FixedSize() (int, error) { - return solana.PublicKeyLength, nil -} diff --git a/pkg/solana/codec/public_key_test.go b/pkg/solana/codec/public_key_test.go deleted file mode 100644 index d499f1ce2..000000000 --- a/pkg/solana/codec/public_key_test.go +++ /dev/null @@ -1,72 +0,0 @@ -package codec_test - -import ( - "testing" - - "github.com/gagliardetto/solana-go" - "github.com/stretchr/testify/require" - - "github.com/smartcontractkit/chainlink-solana/pkg/solana/codec" -) - -func TestPublicKey(t *testing.T) { - t.Run("encode and decode solana public key from []byte", func(t *testing.T) { - codec := codec.NewPublicKey() - into := []byte{} - encoded, err := codec.Encode([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, into) - require.NoError(t, err) - require.Len(t, encoded, solana.PublicKeyLength) - decoded, remaining, err := codec.Decode(encoded) - require.NoError(t, err) - require.Len(t, remaining, 0) - require.Len(t, decoded, solana.PublicKeyLength) - require.IsType(t, solana.PublicKey{}, decoded) - }) - - t.Run("encode and decode solana public key from *[]byte", func(t *testing.T) { - codec := codec.NewPublicKey() - pubKeyBytes := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} - into := []byte{} - encoded, err := codec.Encode(&pubKeyBytes, into) - require.NoError(t, err) - require.Len(t, encoded, solana.PublicKeyLength) - decoded, remaining, err := codec.Decode(encoded) - require.NoError(t, err) - require.Len(t, remaining, 0) - require.Len(t, decoded, solana.PublicKeyLength) - require.IsType(t, solana.PublicKey{}, decoded) - }) - - t.Run("encode and decode solana public key from solana public key type", func(t *testing.T) { - codec := codec.NewPublicKey() - into := []byte{} - encoded, err := codec.Encode(solana.PublicKey{}, into) - require.NoError(t, err) - require.Len(t, encoded, solana.PublicKeyLength) - decoded, remaining, err := codec.Decode(encoded) - require.NoError(t, err) - require.Len(t, remaining, 0) - require.Len(t, decoded, solana.PublicKeyLength) - require.IsType(t, solana.PublicKey{}, decoded) - }) - - t.Run("encode and decode solana public key from string", func(t *testing.T) { - codec := codec.NewPublicKey() - into := []byte{} - encoded, err := codec.Encode("11111111111111111111111111111111", into) - require.NoError(t, err) - require.Len(t, encoded, solana.PublicKeyLength) - decoded, remaining, err := codec.Decode(encoded) - require.NoError(t, err) - require.Len(t, remaining, 0) - require.Len(t, decoded, solana.PublicKeyLength) - require.IsType(t, solana.PublicKey{}, decoded) - }) - - t.Run("error encoding if invalid solana public key provided", func(t *testing.T) { - codec := codec.NewPublicKey() - into := []byte{} - _, err := codec.Encode("1", into) - require.Error(t, err) - }) -}