2018-11-08 03:22:31 +00:00
|
|
|
package keystore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pborman/uuid"
|
2019-02-15 18:31:07 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
2019-03-03 17:31:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2019-02-15 19:04:51 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2018-11-08 03:22:31 +00:00
|
|
|
)
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestStoreAndGetKey(t *testing.T) {
|
2019-02-15 19:04:51 +00:00
|
|
|
tmpdir := testutil.TempDir()
|
2018-11-08 03:22:31 +00:00
|
|
|
filedir := tmpdir + "/keystore"
|
2019-01-31 11:57:57 +00:00
|
|
|
ks := &Store{
|
2018-11-08 03:22:31 +00:00
|
|
|
keysDirPath: filedir,
|
|
|
|
scryptN: LightScryptN,
|
|
|
|
scryptP: LightScryptP,
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := NewKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("key generation failed %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ks.StoreKey(filedir, key, "password"); err != nil {
|
|
|
|
t.Fatalf("unable to store key %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newkey, err := ks.GetKey(filedir, "password")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get key %v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-19 15:09:50 +00:00
|
|
|
if !bytes.Equal(newkey.SecretKey.Marshal(), key.SecretKey.Marshal()) {
|
|
|
|
t.Fatalf("retrieved secret keys are not equal %v , %v", newkey.SecretKey.Marshal(), key.SecretKey.Marshal())
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.RemoveAll(filedir); err != nil {
|
|
|
|
t.Errorf("unable to remove temporary files %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-03-29 22:26:41 +00:00
|
|
|
|
|
|
|
func TestStoreAndGetKeys(t *testing.T) {
|
|
|
|
tmpdir := testutil.TempDir()
|
|
|
|
filePrefix := "/keystore"
|
|
|
|
ks := &Store{
|
|
|
|
keysDirPath: tmpdir,
|
|
|
|
scryptN: LightScryptN,
|
|
|
|
scryptP: LightScryptP,
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := NewKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("key generation failed %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ks.StoreKey(tmpdir+filePrefix+"/test-1", key, "password"); err != nil {
|
|
|
|
t.Fatalf("unable to store key %v", err)
|
|
|
|
}
|
|
|
|
key2, err := NewKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("key generation failed %v", err)
|
|
|
|
}
|
|
|
|
if err := ks.StoreKey(tmpdir+filePrefix+"/test-2", key2, "password"); err != nil {
|
|
|
|
t.Fatalf("unable to store key %v", err)
|
|
|
|
}
|
|
|
|
newkeys, err := ks.GetKeys(tmpdir+filePrefix, "test", "password")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get key %v", err)
|
|
|
|
}
|
|
|
|
for _, s := range newkeys {
|
|
|
|
if !bytes.Equal(s.SecretKey.Marshal(), key.SecretKey.Marshal()) && !bytes.Equal(s.SecretKey.Marshal(), key2.SecretKey.Marshal()) {
|
|
|
|
t.Fatalf("retrieved secret keys are not equal %v ", s.SecretKey.Marshal())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.RemoveAll(tmpdir + filePrefix + "-2"); err != nil {
|
|
|
|
t.Errorf("unable to remove temporary files %v", err)
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(tmpdir + filePrefix + "-1"); err != nil {
|
|
|
|
t.Errorf("unable to remove temporary files %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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[:])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(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
|
|
|
}
|
|
|
|
|
|
|
|
keyjson, err := EncryptKey(key, password, LightScryptN, LightScryptP)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to encrypt key %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
newkey, err := DecryptKey(keyjson, password)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to decrypt keystore %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(newkey.ID, newID) {
|
|
|
|
t.Fatalf("decrypted key's uuid doesn't match %v", newkey.ID)
|
|
|
|
}
|
|
|
|
|
2019-02-19 15:09:50 +00:00
|
|
|
expected := pk.Marshal()
|
|
|
|
if !bytes.Equal(newkey.SecretKey.Marshal(), expected) {
|
|
|
|
t.Fatalf("decrypted key's value is not equal %v", newkey.SecretKey.Marshal())
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|