2022-02-01 19:54:19 +00:00
|
|
|
package local
|
2020-08-11 23:15:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"testing"
|
|
|
|
|
2022-08-16 12:20:13 +00:00
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/crypto/bls"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/require"
|
2020-08-11 23:15:06 +00:00
|
|
|
)
|
|
|
|
|
2022-02-01 19:54:19 +00:00
|
|
|
func TestLocalKeymanager_ExtractKeystores(t *testing.T) {
|
2022-01-06 17:33:08 +00:00
|
|
|
secretKeysCache = make(map[[fieldparams.BLSPubkeyLength]byte]bls.SecretKey)
|
2020-10-10 02:07:28 +00:00
|
|
|
dr := &Keymanager{}
|
2020-08-11 23:15:06 +00:00
|
|
|
validatingKeys := make([]bls.SecretKey, 10)
|
|
|
|
for i := 0; i < len(validatingKeys); i++ {
|
2020-10-30 19:06:33 +00:00
|
|
|
secretKey, err := bls.RandKey()
|
|
|
|
require.NoError(t, err)
|
2020-08-11 23:15:06 +00:00
|
|
|
validatingKeys[i] = secretKey
|
2020-10-10 02:07:28 +00:00
|
|
|
secretKeysCache[bytesutil.ToBytes48(secretKey.PublicKey().Marshal())] = secretKey
|
2020-08-11 23:15:06 +00:00
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
password := "password"
|
|
|
|
|
|
|
|
// Extracting 0 public keys should return 0 keystores.
|
|
|
|
keystores, err := dr.ExtractKeystores(ctx, nil, password)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, len(keystores))
|
|
|
|
|
|
|
|
// We attempt to extract a few indices.
|
|
|
|
keystores, err = dr.ExtractKeystores(
|
|
|
|
ctx,
|
|
|
|
[]bls.PublicKey{
|
|
|
|
validatingKeys[3].PublicKey(),
|
|
|
|
validatingKeys[5].PublicKey(),
|
|
|
|
validatingKeys[7].PublicKey(),
|
|
|
|
},
|
|
|
|
password,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
receivedPubKeys := make([][]byte, len(keystores))
|
|
|
|
for i, k := range keystores {
|
|
|
|
pubKeyBytes, err := hex.DecodeString(k.Pubkey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
receivedPubKeys[i] = pubKeyBytes
|
|
|
|
}
|
|
|
|
assert.DeepEqual(t, receivedPubKeys, [][]byte{
|
|
|
|
validatingKeys[3].PublicKey().Marshal(),
|
|
|
|
validatingKeys[5].PublicKey().Marshal(),
|
|
|
|
validatingKeys[7].PublicKey().Marshal(),
|
|
|
|
})
|
|
|
|
}
|