2018-11-08 03:22:31 +00:00
|
|
|
package keystore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
2020-04-30 15:00:37 +00:00
|
|
|
"path"
|
2018-11-08 03:22:31 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pborman/uuid"
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/crypto/bls"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/testing/require"
|
2018-11-08 03:22:31 +00:00
|
|
|
)
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestStoreAndGetKey(t *testing.T) {
|
2020-11-10 22:45:17 +00:00
|
|
|
tempDir := path.Join(t.TempDir(), "keystore", "file")
|
2020-09-23 08:59:49 +00:00
|
|
|
ks := &Keystore{
|
2020-04-30 15:00:37 +00:00
|
|
|
keysDirPath: tempDir,
|
2018-11-08 03:22:31 +00:00
|
|
|
scryptN: LightScryptN,
|
|
|
|
scryptP: LightScryptP,
|
|
|
|
}
|
|
|
|
|
2020-01-07 23:45:29 +00:00
|
|
|
key, err := NewKey()
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, ks.StoreKey(tempDir, key, "password"))
|
2018-11-08 03:22:31 +00:00
|
|
|
|
2020-04-30 15:00:37 +00:00
|
|
|
decryptedKey, err := ks.GetKey(tempDir, "password")
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, true, bytes.Equal(decryptedKey.SecretKey.Marshal(), key.SecretKey.Marshal()))
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
2019-03-29 22:26:41 +00:00
|
|
|
|
|
|
|
func TestStoreAndGetKeys(t *testing.T) {
|
2020-11-10 22:45:17 +00:00
|
|
|
tempDir := path.Join(t.TempDir(), "keystore")
|
2020-09-23 08:59:49 +00:00
|
|
|
ks := &Keystore{
|
2020-04-30 15:00:37 +00:00
|
|
|
keysDirPath: tempDir,
|
2019-03-29 22:26:41 +00:00
|
|
|
scryptN: LightScryptN,
|
|
|
|
scryptP: LightScryptP,
|
|
|
|
}
|
|
|
|
|
2020-01-07 23:45:29 +00:00
|
|
|
key, err := NewKey()
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, ks.StoreKey(tempDir+"/test-1", key, "password"))
|
2020-01-07 23:45:29 +00:00
|
|
|
key2, err := NewKey()
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, ks.StoreKey(tempDir+"/test-2", key, "password"))
|
2020-04-30 15:00:37 +00:00
|
|
|
decryptedKeys, err := ks.GetKeys(tempDir, "test", "password", false)
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-30 15:00:37 +00:00
|
|
|
for _, s := range decryptedKeys {
|
2020-08-25 10:18:29 +00:00
|
|
|
require.Equal(t, true, bytes.Equal(s.SecretKey.Marshal(), key.SecretKey.Marshal()) && !bytes.Equal(s.SecretKey.Marshal(), key2.SecretKey.Marshal()))
|
2019-03-29 22:26:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 03:22:31 +00:00
|
|
|
func TestEncryptDecryptKey(t *testing.T) {
|
|
|
|
newID := uuid.NewRandom()
|
2019-02-19 15:09:50 +00:00
|
|
|
b := []byte("hi")
|
|
|
|
b32 := bytesutil.ToBytes32(b)
|
2018-11-08 03:22:31 +00:00
|
|
|
password := "test"
|
|
|
|
|
2019-02-19 15:09:50 +00:00
|
|
|
pk, err := bls.SecretKeyFromBytes(b32[:])
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
2018-11-08 03:22:31 +00:00
|
|
|
key := &Key{
|
2019-02-19 15:09:50 +00:00
|
|
|
ID: newID,
|
|
|
|
SecretKey: pk,
|
|
|
|
PublicKey: pk.PublicKey(),
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 15:00:37 +00:00
|
|
|
keyJSON, err := EncryptKey(key, password, LightScryptN, LightScryptP)
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
2018-11-08 03:22:31 +00:00
|
|
|
|
2020-04-30 15:00:37 +00:00
|
|
|
decryptedKey, err := DecryptKey(keyJSON, password)
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, true, bytes.Equal(decryptedKey.ID, newID))
|
2019-02-19 15:09:50 +00:00
|
|
|
expected := pk.Marshal()
|
2020-08-25 10:18:29 +00:00
|
|
|
require.Equal(t, true, bytes.Equal(decryptedKey.SecretKey.Marshal(), expected))
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
2020-04-29 18:31:22 +00:00
|
|
|
|
|
|
|
func TestGetSymlinkedKeys(t *testing.T) {
|
2020-11-10 22:45:17 +00:00
|
|
|
tempDir := path.Join(t.TempDir(), "keystore")
|
2020-09-23 08:59:49 +00:00
|
|
|
ks := &Keystore{
|
2020-04-29 18:31:22 +00:00
|
|
|
scryptN: LightScryptN,
|
|
|
|
scryptP: LightScryptP,
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := NewKey()
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, ks.StoreKey(tempDir+"/files/test-1", key, "password"))
|
|
|
|
require.NoError(t, os.Symlink(tempDir+"/files/test-1", tempDir+"/test-1"))
|
2020-04-30 15:00:37 +00:00
|
|
|
decryptedKeys, err := ks.GetKeys(tempDir, "test", "password", false)
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(decryptedKeys))
|
2020-04-30 15:00:37 +00:00
|
|
|
for _, s := range decryptedKeys {
|
2020-08-25 10:18:29 +00:00
|
|
|
require.Equal(t, true, bytes.Equal(s.SecretKey.Marshal(), key.SecretKey.Marshal()))
|
2020-04-29 18:31:22 +00:00
|
|
|
}
|
|
|
|
}
|