2018-11-08 03:22:31 +00:00
|
|
|
package keystore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pborman/uuid"
|
2019-02-12 19:50:39 +00:00
|
|
|
bls "github.com/prysmaticlabs/go-bls"
|
2019-02-13 23:49:06 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2018-11-08 03:22:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStoreandGetKey(t *testing.T) {
|
2019-02-13 23:49:06 +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-11 18:45:06 +00:00
|
|
|
if !newkey.SecretKey.IsEqual(key.SecretKey) {
|
|
|
|
t.Fatalf("retrieved secret keys are not equal %v , %v", newkey.SecretKey.LittleEndian(), key.SecretKey.LittleEndian())
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.RemoveAll(filedir); err != nil {
|
|
|
|
t.Errorf("unable to remove temporary files %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestEncryptDecryptKey(t *testing.T) {
|
|
|
|
newID := uuid.NewRandom()
|
2019-02-11 18:45:06 +00:00
|
|
|
blsKey := &bls.SecretKey{}
|
|
|
|
blsKey.SetByCSPRNG()
|
2018-11-08 03:22:31 +00:00
|
|
|
password := "test"
|
|
|
|
|
|
|
|
key := &Key{
|
2019-02-11 18:45:06 +00:00
|
|
|
ID: newID,
|
|
|
|
SecretKey: blsKey,
|
|
|
|
PublicKey: blsKey.GetPublicKey(),
|
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-11 18:45:06 +00:00
|
|
|
if !newkey.SecretKey.IsEqual(blsKey) {
|
|
|
|
t.Fatalf("decrypted key's value is not equal %v", newkey.SecretKey.LittleEndian())
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|