diff --git a/.changeset/purple-seas-help.md b/.changeset/purple-seas-help.md new file mode 100644 index 00000000000..01f8b0049b6 --- /dev/null +++ b/.changeset/purple-seas-help.md @@ -0,0 +1,5 @@ +--- +"chainlink": minor +--- + +#removed unused key files and move public_key test file. diff --git a/core/services/keystore/keys/vrfkey/private_key.go b/core/services/keystore/keys/vrfkey/private_key.go deleted file mode 100644 index dd2545fdd28..00000000000 --- a/core/services/keystore/keys/vrfkey/private_key.go +++ /dev/null @@ -1,83 +0,0 @@ -package vrfkey - -import ( - "encoding/json" - "fmt" - - "github.com/ethereum/go-ethereum/accounts/keystore" - "github.com/google/uuid" - "github.com/pkg/errors" - "go.dedis.ch/kyber/v3" - - "github.com/smartcontractkit/chainlink/v2/core/services/signatures/secp256k1" -) - -// PrivateKey represents the secret used to construct a VRF proof. -// -// Don't serialize directly, use Encrypt method, with user-supplied passphrase. -// The unencrypted PrivateKey struct should only live in-memory. -// -// Only use it if you absolutely need it (i.e., for a novel crypto protocol.) -// Implement whatever cryptography you need on this struct, so your callers -// don't need to know the secret key explicitly. (See, e.g., MarshaledProof.) -type PrivateKey struct { - k kyber.Scalar - PublicKey secp256k1.PublicKey -} - -func (k PrivateKey) ToV2() KeyV2 { - return KeyV2{ - k: &k.k, - PublicKey: k.PublicKey, - } -} - -// fromGethKey returns the vrfkey representation of gethKey. Do not abuse this -// to convert an ethereum key into a VRF key! -func fromGethKey(gethKey *keystore.Key) *PrivateKey { - secretKey := secp256k1.IntToScalar(gethKey.PrivateKey.D) - rawPublicKey, err := secp256k1.ScalarToPublicPoint(secretKey).MarshalBinary() - if err != nil { - panic(err) // Only way this can happen is out-of-memory failure - } - var publicKey secp256k1.PublicKey - copy(publicKey[:], rawPublicKey) - return &PrivateKey{secretKey, publicKey} -} - -func (k *PrivateKey) String() string { - return fmt.Sprintf("PrivateKey{k: , PublicKey: %s}", k.PublicKey) -} - -// GoString reduces the risk of accidentally logging the private key -func (k *PrivateKey) GoString() string { - return k.String() -} - -// Decrypt returns the PrivateKey in e, decrypted via auth, or an error -func Decrypt(e EncryptedVRFKey, auth string) (*PrivateKey, error) { - // NOTE: We do this shuffle to an anonymous struct - // solely to add a throwaway UUID, so we can leverage - // the keystore.DecryptKey from the geth which requires it - // as of 1.10.0. - keyJSON, err := json.Marshal(struct { - Address string `json:"address"` - Crypto keystore.CryptoJSON `json:"crypto"` - Version int `json:"version"` - Id string `json:"id"` - }{ - Address: e.VRFKey.Address, - Crypto: e.VRFKey.Crypto, - Version: e.VRFKey.Version, - Id: uuid.New().String(), - }) - if err != nil { - return nil, errors.Wrapf(err, "while marshaling key for decryption") - } - gethKey, err := keystore.DecryptKey(keyJSON, adulteratedPassword(auth)) - if err != nil { - return nil, errors.Wrapf(err, "could not decrypt VRF key %s", - e.PublicKey.String()) - } - return fromGethKey(gethKey), nil -} diff --git a/core/services/keystore/keys/vrfkey/private_key_test.go b/core/services/keystore/keys/vrfkey/private_key_test.go deleted file mode 100644 index a4010e780df..00000000000 --- a/core/services/keystore/keys/vrfkey/private_key_test.go +++ /dev/null @@ -1,40 +0,0 @@ -package vrfkey - -import ( - "encoding/json" - "fmt" - "testing" - - "github.com/ethereum/go-ethereum/accounts/keystore" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" - "github.com/smartcontractkit/chainlink/v2/core/utils" -) - -func TestVRFKeys_PrivateKey(t *testing.T) { - jsonKey := `{"PublicKey":"0xd2377bc6be8a2c5ce163e1867ee42ef111e320686f940a98e52e9c019ca0606800","vrf_key":{"address":"b94276ad4e5452732ec0cccf30ef7919b67844b6","crypto":{"cipher":"aes-128-ctr","ciphertext":"ff66d61d02dba54a61bab1ceb8414643f9e76b7351785d2959e2c8b50ee69a92","cipherparams":{"iv":"75705da271b11e330a27b8d593a3930c"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"efe5b372e4fe79d0af576a79d65a1ee35d0792d9c92b70107b5ada1817ea7c7b"},"mac":"e4d0bb08ffd004ab03aeaa42367acbd9bb814c6cfd981f5157503f54c30816e7"},"version":3}}` - k, err := FromEncryptedJSON([]byte(jsonKey), "p4SsW0rD1!@#_") - require.NoError(t, err) - cryptoJSON, err := keystore.EncryptKey(k.toGethKey(), adulteratedPassword(testutils.Password), utils.FastScryptParams.N, utils.FastScryptParams.P) - require.NoError(t, err) - var gethKey gethKeyStruct - err = json.Unmarshal(cryptoJSON, &gethKey) - require.NoError(t, err) - - ek := EncryptedVRFKey{ - PublicKey: k.PublicKey, - VRFKey: gethKey, - } - - pk, err := Decrypt(ek, testutils.Password) - require.NoError(t, err) - _, err = Decrypt(ek, "wrong-password") - assert.Error(t, err) - - kv2 := pk.ToV2() - - assert.Equal(t, fmt.Sprintf("VRFKeyV2{PublicKey: %s}", kv2.PublicKey), kv2.String()) - assert.Equal(t, fmt.Sprintf("PrivateKey{k: , PublicKey: %s}", pk.PublicKey), pk.String()) -} diff --git a/core/services/keystore/keys/vrfkey/public_key_test.go b/core/services/signatures/secp256k1/public_key_test.go similarity index 77% rename from core/services/keystore/keys/vrfkey/public_key_test.go rename to core/services/signatures/secp256k1/public_key_test.go index 70c26ae27fe..eabce44968f 100644 --- a/core/services/keystore/keys/vrfkey/public_key_test.go +++ b/core/services/signatures/secp256k1/public_key_test.go @@ -1,10 +1,8 @@ -package vrfkey +package secp256k1 import ( "testing" - "github.com/smartcontractkit/chainlink/v2/core/services/signatures/secp256k1" - "github.com/smartcontractkit/chainlink/v2/core/services/signatures/cryptotest" "github.com/stretchr/testify/assert" @@ -14,12 +12,12 @@ import ( func TestValueScanIdentityPointSet(t *testing.T) { randomStream := cryptotest.NewStream(t, 0) for i := 0; i < 10; i++ { - p := suite.Point().Pick(randomStream) - var pk, nPk, nnPk secp256k1.PublicKey + p := NewBlakeKeccackSecp256k1().Point().Pick(randomStream) + var pk, nPk, nnPk PublicKey marshaledKey, err := p.MarshalBinary() require.NoError(t, err, "failed to marshal public key") require.Equal(t, copy(pk[:], marshaledKey), - secp256k1.CompressedPublicKeyLength, "failed to copy marshaled key to pk") + CompressedPublicKeyLength, "failed to copy marshaled key to pk") assert.NotEqual(t, pk, nPk, "equality test succeeds on different keys!") np, err := pk.Point() require.NoError(t, err, "failed to marshal public key") @@ -37,7 +35,7 @@ func TestValueScanIdentityPointSet(t *testing.T) { // Tests that PublicKey.Hash gives the same result as the VRFCoordinator's func TestHash(t *testing.T) { - pk, err := secp256k1.NewPublicKeyFromHex("0x9dc09a0f898f3b5e8047204e7ce7e44b587920932f08431e29c9bf6923b8450a01") + pk, err := NewPublicKeyFromHex("0x9dc09a0f898f3b5e8047204e7ce7e44b587920932f08431e29c9bf6923b8450a01") assert.NoError(t, err) assert.Equal(t, "0xc4406d555db624837188b91514a5f47e34d825d935ab887a35c06a3e7c41de69", pk.MustHash().String()) }