2018-11-08 03:22:31 +00:00
|
|
|
package keystore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-04-18 20:42:07 +00:00
|
|
|
"os"
|
2020-11-10 22:45:17 +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/require"
|
2018-11-08 03:22:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMarshalAndUnmarshal(t *testing.T) {
|
|
|
|
testID := uuid.NewRandom()
|
2020-10-30 19:06:33 +00:00
|
|
|
blsKey, err := bls.RandKey()
|
|
|
|
require.NoError(t, err)
|
2019-12-03 20:29:05 +00:00
|
|
|
|
2018-11-08 03:22:31 +00:00
|
|
|
key := &Key{
|
|
|
|
ID: testID,
|
|
|
|
SecretKey: blsKey,
|
2019-02-19 15:09:50 +00:00
|
|
|
PublicKey: blsKey.PublicKey(),
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
|
|
|
marshalledObject, err := key.MarshalJSON()
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
2018-11-08 03:22:31 +00:00
|
|
|
newKey := &Key{
|
2019-02-19 15:09:50 +00:00
|
|
|
ID: []byte{},
|
|
|
|
SecretKey: blsKey,
|
|
|
|
PublicKey: blsKey.PublicKey(),
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = newKey.UnmarshalJSON(marshalledObject)
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, true, bytes.Equal(newKey.ID, testID))
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStoreRandomKey(t *testing.T) {
|
2020-09-23 08:59:49 +00:00
|
|
|
ks := &Keystore{
|
2020-11-10 22:45:17 +00:00
|
|
|
keysDirPath: path.Join(t.TempDir(), "keystore"),
|
2018-11-08 03:22:31 +00:00
|
|
|
scryptN: LightScryptN,
|
|
|
|
scryptP: LightScryptP,
|
|
|
|
}
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, storeNewRandomKey(ks, "password"))
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
2019-02-22 15:11:26 +00:00
|
|
|
|
2018-11-08 03:22:31 +00:00
|
|
|
func TestNewKeyFromBLS(t *testing.T) {
|
2019-02-19 15:09:50 +00:00
|
|
|
b := []byte("hi")
|
|
|
|
b32 := bytesutil.ToBytes32(b)
|
|
|
|
blskey, err := bls.SecretKeyFromBytes(b32[:])
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
2019-08-26 21:07:09 +00:00
|
|
|
key, err := NewKeyFromBLS(blskey)
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
2018-11-08 03:22:31 +00:00
|
|
|
|
2019-02-19 15:09:50 +00:00
|
|
|
expected := blskey.Marshal()
|
2020-08-25 10:18:29 +00:00
|
|
|
require.Equal(t, true, bytes.Equal(expected, key.SecretKey.Marshal()))
|
2020-01-07 23:45:29 +00:00
|
|
|
_, err = NewKey()
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteFile(t *testing.T) {
|
2020-11-10 22:45:17 +00:00
|
|
|
tempDir := path.Join(t.TempDir(), "keystore", "file")
|
2018-11-08 03:22:31 +00:00
|
|
|
testKeystore := []byte{'t', 'e', 's', 't'}
|
|
|
|
|
2020-04-30 15:00:37 +00:00
|
|
|
err := writeKeyFile(tempDir, testKeystore)
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
2018-11-08 03:22:31 +00:00
|
|
|
|
2022-04-18 20:42:07 +00:00
|
|
|
keystore, err := os.ReadFile(tempDir)
|
2020-08-25 10:18:29 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, true, bytes.Equal(keystore, testKeystore))
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|