mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 15:37:56 -05:00
* Ran gopls modernize to fix everything go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... * Override rules_go provided dependency for golang.org/x/tools to v0.38.0. To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches. * Fix buildtag violations and ignore buildtag violations in external * Introduce modernize analyzer package. * Add modernize "any" analyzer. * Fix violations of any analyzer * Add modernize "appendclipped" analyzer. * Fix violations of appendclipped * Add modernize "bloop" analyzer. * Add modernize "fmtappendf" analyzer. * Add modernize "forvar" analyzer. * Add modernize "mapsloop" analyzer. * Add modernize "minmax" analyzer. * Fix violations of minmax analyzer * Add modernize "omitzero" analyzer. * Add modernize "rangeint" analyzer. * Fix violations of rangeint. * Add modernize "reflecttypefor" analyzer. * Fix violations of reflecttypefor analyzer. * Add modernize "slicescontains" analyzer. * Add modernize "slicessort" analyzer. * Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686. * Add modernize "stringscutprefix" analyzer. * Add modernize "stringsbuilder" analyzer. * Fix violations of stringsbuilder analyzer. * Add modernize "stringsseq" analyzer. * Add modernize "testingcontext" analyzer. * Add modernize "waitgroup" analyzer. * Changelog fragment * gofmt * gazelle * Add modernize "newexpr" analyzer. * Disable newexpr until go1.26 * Add more details in WORKSPACE on how to update the override * @nalepae feedback on min() * gofmt * Fix violations of forvar
170 lines
5.9 KiB
Go
170 lines
5.9 KiB
Go
package local
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
fieldparams "github.com/OffchainLabs/prysm/v7/config/fieldparams"
|
|
"github.com/OffchainLabs/prysm/v7/crypto/bls"
|
|
"github.com/OffchainLabs/prysm/v7/encoding/bytesutil"
|
|
validatorpb "github.com/OffchainLabs/prysm/v7/proto/prysm/v1alpha1/validator-client"
|
|
"github.com/OffchainLabs/prysm/v7/testing/assert"
|
|
"github.com/OffchainLabs/prysm/v7/testing/require"
|
|
mock "github.com/OffchainLabs/prysm/v7/validator/accounts/testing"
|
|
"github.com/OffchainLabs/prysm/v7/validator/keymanager"
|
|
keystorev4 "github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4"
|
|
)
|
|
|
|
func TestLocalKeymanager_FetchValidatingPublicKeys(t *testing.T) {
|
|
wallet := &mock.Wallet{
|
|
Files: make(map[string]map[string][]byte),
|
|
WalletPassword: password,
|
|
}
|
|
dr := &Keymanager{
|
|
wallet: wallet,
|
|
accountsStore: &accountStore{},
|
|
}
|
|
// First, generate accounts and their keystore.json files.
|
|
ctx := t.Context()
|
|
numAccounts := 10
|
|
wantedPubKeys := make([][fieldparams.BLSPubkeyLength]byte, 0)
|
|
for range numAccounts {
|
|
privKey, err := bls.RandKey()
|
|
require.NoError(t, err)
|
|
pubKey := bytesutil.ToBytes48(privKey.PublicKey().Marshal())
|
|
wantedPubKeys = append(wantedPubKeys, pubKey)
|
|
dr.accountsStore.PublicKeys = append(dr.accountsStore.PublicKeys, pubKey[:])
|
|
dr.accountsStore.PrivateKeys = append(dr.accountsStore.PrivateKeys, privKey.Marshal())
|
|
}
|
|
require.NoError(t, dr.initializeKeysCachesFromKeystore())
|
|
publicKeys, err := dr.FetchValidatingPublicKeys(ctx)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, numAccounts, len(publicKeys))
|
|
// FetchValidatingPublicKeys is also used in generating the output of account list
|
|
// therefore the results must be in the same order as the order in which the accounts were derived
|
|
for i, key := range wantedPubKeys {
|
|
assert.Equal(t, key, publicKeys[i])
|
|
}
|
|
}
|
|
|
|
func TestLocalKeymanager_FetchValidatingPrivateKeys(t *testing.T) {
|
|
wallet := &mock.Wallet{
|
|
Files: make(map[string]map[string][]byte),
|
|
WalletPassword: password,
|
|
}
|
|
dr := &Keymanager{
|
|
wallet: wallet,
|
|
accountsStore: &accountStore{},
|
|
}
|
|
// First, generate accounts and their keystore.json files.
|
|
ctx := t.Context()
|
|
numAccounts := 10
|
|
wantedPrivateKeys := make([][32]byte, numAccounts)
|
|
for i := range numAccounts {
|
|
privKey, err := bls.RandKey()
|
|
require.NoError(t, err)
|
|
privKeyData := privKey.Marshal()
|
|
pubKey := bytesutil.ToBytes48(privKey.PublicKey().Marshal())
|
|
wantedPrivateKeys[i] = bytesutil.ToBytes32(privKeyData)
|
|
dr.accountsStore.PublicKeys = append(dr.accountsStore.PublicKeys, pubKey[:])
|
|
dr.accountsStore.PrivateKeys = append(dr.accountsStore.PrivateKeys, privKeyData)
|
|
}
|
|
require.NoError(t, dr.initializeKeysCachesFromKeystore())
|
|
privateKeys, err := dr.FetchValidatingPrivateKeys(ctx)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, numAccounts, len(privateKeys))
|
|
// FetchValidatingPrivateKeys is also used in generating the output of account list
|
|
// therefore the results must be in the same order as the order in which the accounts were created
|
|
for i, key := range wantedPrivateKeys {
|
|
assert.Equal(t, key, privateKeys[i])
|
|
}
|
|
}
|
|
|
|
func TestLocalKeymanager_Sign(t *testing.T) {
|
|
wallet := &mock.Wallet{
|
|
Files: make(map[string]map[string][]byte),
|
|
AccountPasswords: make(map[string]string),
|
|
WalletPassword: password,
|
|
}
|
|
dr := &Keymanager{
|
|
wallet: wallet,
|
|
accountsStore: &accountStore{},
|
|
}
|
|
|
|
// First, generate accounts and their keystore.json files.
|
|
ctx := t.Context()
|
|
numAccounts := 10
|
|
keystores := make([]*keymanager.Keystore, numAccounts)
|
|
passwords := make([]string, numAccounts)
|
|
for i := range numAccounts {
|
|
keystores[i] = createRandomKeystore(t, password)
|
|
passwords[i] = password
|
|
}
|
|
_, err := dr.ImportKeystores(ctx, keystores, passwords)
|
|
require.NoError(t, err)
|
|
|
|
var encodedKeystore []byte
|
|
for k, v := range wallet.Files[AccountsPath] {
|
|
if strings.Contains(k, "keystore") {
|
|
encodedKeystore = v
|
|
}
|
|
}
|
|
keystoreFile := &keymanager.Keystore{}
|
|
require.NoError(t, json.Unmarshal(encodedKeystore, keystoreFile))
|
|
|
|
// We extract the validator signing private key from the keystore
|
|
// by utilizing the password and initialize a new BLS secret key from
|
|
// its raw bytes.
|
|
decryptor := keystorev4.New()
|
|
enc, err := decryptor.Decrypt(keystoreFile.Crypto, dr.wallet.Password())
|
|
require.NoError(t, err)
|
|
store := &accountStore{}
|
|
require.NoError(t, json.Unmarshal(enc, store))
|
|
require.Equal(t, len(store.PublicKeys), len(store.PrivateKeys))
|
|
require.NotEqual(t, 0, len(store.PublicKeys))
|
|
dr.accountsStore = store
|
|
require.NoError(t, dr.initializeKeysCachesFromKeystore())
|
|
publicKeys, err := dr.FetchValidatingPublicKeys(ctx)
|
|
require.NoError(t, err)
|
|
require.Equal(t, len(publicKeys), len(store.PublicKeys))
|
|
|
|
// We prepare naive data to sign.
|
|
data := []byte("hello world")
|
|
signRequest := &validatorpb.SignRequest{
|
|
PublicKey: publicKeys[0][:],
|
|
SigningRoot: data,
|
|
}
|
|
sig, err := dr.Sign(ctx, signRequest)
|
|
require.NoError(t, err)
|
|
pubKey, err := bls.PublicKeyFromBytes(publicKeys[0][:])
|
|
require.NoError(t, err)
|
|
wrongPubKey, err := bls.PublicKeyFromBytes(publicKeys[1][:])
|
|
require.NoError(t, err)
|
|
if !sig.Verify(pubKey, data) {
|
|
t.Fatalf("Expected sig to verify for pubkey %#x and data %v", pubKey.Marshal(), data)
|
|
}
|
|
if sig.Verify(wrongPubKey, data) {
|
|
t.Fatalf("Expected sig not to verify for pubkey %#x and data %v", wrongPubKey.Marshal(), data)
|
|
}
|
|
}
|
|
|
|
func TestLocalKeymanager_Sign_NoPublicKeySpecified(t *testing.T) {
|
|
req := &validatorpb.SignRequest{
|
|
PublicKey: nil,
|
|
}
|
|
dr := &Keymanager{}
|
|
_, err := dr.Sign(t.Context(), req)
|
|
assert.ErrorContains(t, "nil public key", err)
|
|
}
|
|
|
|
func TestLocalKeymanager_Sign_NoPublicKeyInCache(t *testing.T) {
|
|
req := &validatorpb.SignRequest{
|
|
PublicKey: []byte("hello world"),
|
|
}
|
|
secretKeysCache = make(map[[fieldparams.BLSPubkeyLength]byte]bls.SecretKey)
|
|
dr := &Keymanager{}
|
|
_, err := dr.Sign(t.Context(), req)
|
|
assert.ErrorContains(t, "no signing key found in keys cache", err)
|
|
}
|