From 7ff9cf9916ae1648ccc3475c86e45c9df6ab8d1f Mon Sep 17 00:00:00 2001 From: pavel-raykov <165708424+pavel-raykov@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:42:20 +0100 Subject: [PATCH] Remove unused deprecated key interfaces. (#15210) * Remove unused deprecated key interfaces. * Minor * Minor * Minor * Minor * Minor * Minor --- .changeset/healthy-shirts-remain.md | 5 + core/services/keystore/keys/csakey/key.go | 65 --------- .../services/keystore/keys/csakey/key_test.go | 58 -------- core/services/keystore/keys/p2pkey/key.go | 125 ------------------ .../services/keystore/keys/p2pkey/key_test.go | 109 --------------- .../keystore/keys/p2pkey/key_v2_test.go | 8 +- core/web/presenters/csa_key_test.go | 12 +- 7 files changed, 13 insertions(+), 369 deletions(-) create mode 100644 .changeset/healthy-shirts-remain.md delete mode 100644 core/services/keystore/keys/csakey/key.go delete mode 100644 core/services/keystore/keys/csakey/key_test.go delete mode 100644 core/services/keystore/keys/p2pkey/key.go delete mode 100644 core/services/keystore/keys/p2pkey/key_test.go diff --git a/.changeset/healthy-shirts-remain.md b/.changeset/healthy-shirts-remain.md new file mode 100644 index 00000000000..0ce310e1ce3 --- /dev/null +++ b/.changeset/healthy-shirts-remain.md @@ -0,0 +1,5 @@ +--- +"chainlink": minor +--- + +#removed Remove unused deprecated key interfaces. diff --git a/core/services/keystore/keys/csakey/key.go b/core/services/keystore/keys/csakey/key.go deleted file mode 100644 index 054994f93ea..00000000000 --- a/core/services/keystore/keys/csakey/key.go +++ /dev/null @@ -1,65 +0,0 @@ -package csakey - -import ( - "crypto/ed25519" - "errors" - "time" - - "github.com/smartcontractkit/chainlink/v2/core/utils" - "github.com/smartcontractkit/chainlink/v2/core/utils/crypto" -) - -type Key struct { - ID uint - PublicKey crypto.PublicKey - privateKey []byte - EncryptedPrivateKey crypto.EncryptedPrivateKey - CreatedAt time.Time - UpdatedAt time.Time -} - -// New creates a new CSA key consisting of an ed25519 key. It encrypts the -// Key with the passphrase. -func New(passphrase string, scryptParams utils.ScryptParams) (*Key, error) { - pubkey, privkey, err := ed25519.GenerateKey(nil) - if err != nil { - return nil, err - } - - encPrivkey, err := crypto.NewEncryptedPrivateKey(privkey, passphrase, scryptParams) - if err != nil { - return nil, err - } - - return &Key{ - PublicKey: crypto.PublicKey(pubkey), - privateKey: privkey, - EncryptedPrivateKey: *encPrivkey, - }, nil -} - -func (k *Key) Unlock(password string) error { - pk, err := k.EncryptedPrivateKey.Decrypt(password) - if err != nil { - return err - } - k.privateKey = pk - return nil -} - -func (k *Key) Unsafe_GetPrivateKey() ([]byte, error) { - if k.privateKey == nil { - return nil, errors.New("key has not been unlocked") - } - - return k.privateKey, nil -} - -func (k Key) ToV2() KeyV2 { - pk := ed25519.PrivateKey(k.privateKey) - return KeyV2{ - privateKey: &pk, - PublicKey: ed25519.PublicKey(k.PublicKey), - Version: 1, - } -} diff --git a/core/services/keystore/keys/csakey/key_test.go b/core/services/keystore/keys/csakey/key_test.go deleted file mode 100644 index 8ac05f74cf5..00000000000 --- a/core/services/keystore/keys/csakey/key_test.go +++ /dev/null @@ -1,58 +0,0 @@ -package csakey - -import ( - "crypto/ed25519" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/smartcontractkit/chainlink/v2/core/utils" -) - -func Test_New(t *testing.T) { - passphrase := "passphrase" - key, err := New(passphrase, utils.FastScryptParams) - require.NoError(t, err) - - rawprivkey, err := key.EncryptedPrivateKey.Decrypt("passphrase") - require.NoError(t, err) - - privkey := ed25519.PrivateKey(rawprivkey) - assert.Equal(t, ed25519.PublicKey(key.PublicKey), privkey.Public()) -} - -func Test_Unlock(t *testing.T) { - passphrase := "passphrase" - key, err := New(passphrase, utils.FastScryptParams) - require.NoError(t, err) - - err = key.Unlock(passphrase) - require.NoError(t, err) - - expected, err := key.EncryptedPrivateKey.Decrypt(passphrase) - require.NoError(t, err) - - assert.Equal(t, expected, key.privateKey) -} - -func Test_GetPrivateKey(t *testing.T) { - passphrase := "passphrase" - key, err := New(passphrase, utils.FastScryptParams) - require.NoError(t, err) - - privkey, err := key.Unsafe_GetPrivateKey() - require.NoError(t, err) - assert.Equal(t, key.privateKey, privkey) -} - -func TestKey_ToV2(t *testing.T) { - passphrase := "passphrase" - key, err := New(passphrase, utils.FastScryptParams) - require.NoError(t, err) - - v2Key := key.ToV2() - - assert.Equal(t, key.PublicKey.String(), v2Key.PublicKeyString()) - assert.Equal(t, ed25519.PrivateKey(key.privateKey), *v2Key.privateKey) -} diff --git a/core/services/keystore/keys/p2pkey/key.go b/core/services/keystore/keys/p2pkey/key.go deleted file mode 100644 index abf4f70294c..00000000000 --- a/core/services/keystore/keys/p2pkey/key.go +++ /dev/null @@ -1,125 +0,0 @@ -package p2pkey - -import ( - "crypto/ed25519" - "database/sql/driver" - "encoding/hex" - "encoding/json" - "strconv" - "time" - - "github.com/ethereum/go-ethereum/accounts/keystore" - "github.com/pkg/errors" - - ragep2ptypes "github.com/smartcontractkit/libocr/ragep2p/types" -) - -// Key represents a p2p private key -type Key struct { - PrivKey ed25519.PrivateKey -} - -func (k Key) ToV2() KeyV2 { - return KeyV2{ - PrivKey: k.PrivKey, - peerID: k.PeerID(), - } -} - -// PublicKeyBytes is a [ed25519.PublicKey] -type PublicKeyBytes []byte - -func (pkb PublicKeyBytes) String() string { - return hex.EncodeToString(pkb) -} - -func (pkb PublicKeyBytes) MarshalJSON() ([]byte, error) { - return json.Marshal(hex.EncodeToString(pkb)) -} - -func (pkb *PublicKeyBytes) UnmarshalJSON(input []byte) error { - var hexString string - if err := json.Unmarshal(input, &hexString); err != nil { - return err - } - - result, err := hex.DecodeString(hexString) - if err != nil { - return err - } - - *pkb = result - return nil -} - -func (pkb *PublicKeyBytes) Scan(value interface{}) error { - switch v := value.(type) { - case []byte: - *pkb = v - return nil - default: - return errors.Errorf("invalid public key bytes got %T wanted []byte", v) - } -} - -func (pkb PublicKeyBytes) Value() (driver.Value, error) { - return []byte(pkb), nil -} - -func (k Key) GetPeerID() (PeerID, error) { - peerID, err := ragep2ptypes.PeerIDFromPrivateKey(k.PrivKey) - if err != nil { - return PeerID{}, errors.WithStack(err) - } - return PeerID(peerID), err -} - -func (k Key) PeerID() PeerID { - peerID, err := k.GetPeerID() - if err != nil { - panic(err) - } - return peerID -} - -type EncryptedP2PKey struct { - ID int32 - PeerID PeerID - PubKey PublicKeyBytes - EncryptedPrivKey []byte - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt *time.Time -} - -func (ep2pk *EncryptedP2PKey) SetID(value string) error { - result, err := strconv.ParseInt(value, 10, 32) - - if err != nil { - return err - } - - ep2pk.ID = int32(result) - return nil -} - -// Decrypt returns the PrivateKey in e, decrypted via auth, or an error -func (ep2pk EncryptedP2PKey) Decrypt(auth string) (k Key, err error) { - var cryptoJSON keystore.CryptoJSON - err = json.Unmarshal(ep2pk.EncryptedPrivKey, &cryptoJSON) - if err != nil { - return k, errors.Wrapf(err, "invalid JSON for P2P key %s (0x%x)", ep2pk.PeerID.String(), ep2pk.PubKey) - } - marshalledPrivK, err := keystore.DecryptDataV3(cryptoJSON, adulteratedPassword(auth)) - if err != nil { - return k, errors.Wrapf(err, "could not decrypt P2P key %s (0x%x)", ep2pk.PeerID.String(), ep2pk.PubKey) - } - - privK, err := UnmarshalPrivateKey(marshalledPrivK) - if err != nil { - return k, errors.Wrapf(err, "could not unmarshal P2P private key for %s (0x%x)", ep2pk.PeerID.String(), ep2pk.PubKey) - } - return Key{ - privK, - }, nil -} diff --git a/core/services/keystore/keys/p2pkey/key_test.go b/core/services/keystore/keys/p2pkey/key_test.go deleted file mode 100644 index 57490483e86..00000000000 --- a/core/services/keystore/keys/p2pkey/key_test.go +++ /dev/null @@ -1,109 +0,0 @@ -package p2pkey - -import ( - "crypto/ed25519" - "crypto/rand" - "encoding/hex" - "encoding/json" - "testing" - - "github.com/ethereum/go-ethereum/accounts/keystore" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/smartcontractkit/chainlink/v2/core/utils" -) - -func TestP2PKeys_KeyStruct(t *testing.T) { - _, pk, err := ed25519.GenerateKey(rand.Reader) - require.NoError(t, err) - - k := Key{PrivKey: pk} - - t.Run("converts into V2 key", func(t *testing.T) { - k2 := k.ToV2() - - assert.Equal(t, k.PrivKey, k2.PrivKey) - assert.Equal(t, k.PeerID(), k2.peerID) - }) - - t.Run("returns PeerID", func(t *testing.T) { - pid, err := k.GetPeerID() - require.NoError(t, err) - pid2 := k.PeerID() - - assert.Equal(t, pid, pid2) - }) -} - -func TestP2PKeys_PublicKeyBytes(t *testing.T) { - pk, _, err := ed25519.GenerateKey(rand.Reader) - require.NoError(t, err) - - pkb := PublicKeyBytes(pk) - assert.Equal(t, hex.EncodeToString(pkb), pkb.String()) - - b, err := pkb.MarshalJSON() - require.NoError(t, err) - assert.NotEmpty(t, b) - - err = pkb.UnmarshalJSON(b) - assert.NoError(t, err) - - err = pkb.UnmarshalJSON([]byte("")) - assert.Error(t, err) - - err = pkb.Scan([]byte(pk)) - assert.NoError(t, err) - - err = pkb.Scan("invalid-type") - assert.Error(t, err) - - sv, err := pkb.Value() - assert.NoError(t, err) - assert.NotEmpty(t, sv) -} - -func TestP2PKeys_EncryptedP2PKey(t *testing.T) { - _, privk, err := ed25519.GenerateKey(rand.Reader) - require.NoError(t, err) - - k := Key{PrivKey: privk} - - pubkr := k.PrivKey.Public().(ed25519.PublicKey) - - var marshalledPrivK []byte - marshalledPrivK, err = MarshalPrivateKey(k.PrivKey) - require.NoError(t, err) - cryptoJSON, err := keystore.EncryptDataV3(marshalledPrivK, []byte(adulteratedPassword("password")), utils.FastScryptParams.N, utils.FastScryptParams.P) - require.NoError(t, err) - encryptedPrivKey, err := json.Marshal(&cryptoJSON) - require.NoError(t, err) - - p2pk := EncryptedP2PKey{ - ID: 1, - PeerID: k.PeerID(), - PubKey: []byte(pubkr), - EncryptedPrivKey: encryptedPrivKey, - } - - t.Run("sets a different ID", func(t *testing.T) { - err := p2pk.SetID("12") - require.NoError(t, err) - - assert.Equal(t, int32(12), p2pk.ID) - - err = p2pk.SetID("invalid") - assert.Error(t, err) - }) - - t.Run("decrypts key", func(t *testing.T) { - k, err := p2pk.Decrypt("invalid-pass") - assert.Empty(t, k) - assert.Error(t, err) - - k, err = p2pk.Decrypt("password") - require.NoError(t, err) - assert.NotEmpty(t, k) - }) -} diff --git a/core/services/keystore/keys/p2pkey/key_v2_test.go b/core/services/keystore/keys/p2pkey/key_v2_test.go index d93678b8f2d..56a93e4db1a 100644 --- a/core/services/keystore/keys/p2pkey/key_v2_test.go +++ b/core/services/keystore/keys/p2pkey/key_v2_test.go @@ -7,6 +7,7 @@ import ( "testing" ragep2ptypes "github.com/smartcontractkit/libocr/ragep2p/types" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -22,15 +23,12 @@ func TestP2PKeys_Raw(t *testing.T) { } func TestP2PKeys_KeyV2(t *testing.T) { - _, pk, err := ed25519.GenerateKey(rand.Reader) + kv2, err := NewV2() require.NoError(t, err) - k := Key{PrivKey: pk} - kv2 := k.ToV2() - pkv2 := kv2.PrivKey.Public().(ed25519.PublicKey) assert.Equal(t, kv2.String(), kv2.GoString()) - assert.Equal(t, ragep2ptypes.PeerID(k.PeerID()).String(), kv2.ID()) + assert.Equal(t, ragep2ptypes.PeerID(kv2.PeerID()).String(), kv2.ID()) assert.Equal(t, hex.EncodeToString(pkv2), kv2.PublicKeyHex()) } diff --git a/core/web/presenters/csa_key_test.go b/core/web/presenters/csa_key_test.go index 06f84db7dd5..d514519fafd 100644 --- a/core/web/presenters/csa_key_test.go +++ b/core/web/presenters/csa_key_test.go @@ -9,15 +9,13 @@ import ( "github.com/stretchr/testify/require" "github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/csakey" - "github.com/smartcontractkit/chainlink/v2/core/utils" ) func TestCSAKeyResource(t *testing.T) { - key, err := csakey.New("passphrase", utils.FastScryptParams) + keyV2, err := csakey.NewV2() require.NoError(t, err) - key.ID = 1 - r := NewCSAKeyResource(key.ToV2()) + r := NewCSAKeyResource(keyV2) b, err := jsonapi.Marshal(r) require.NoError(t, err) @@ -25,13 +23,13 @@ func TestCSAKeyResource(t *testing.T) { { "data":{ "type":"csaKeys", - "id":"%s", + "id":"%[1]s", "attributes":{ - "publicKey": "csa_%s", + "publicKey": "csa_%[1]s", "version": 1 } } - }`, key.PublicKey.String(), key.PublicKey.String()) + }`, keyV2.PublicKeyString()) assert.JSONEq(t, expected, string(b)) }