github.com/prysmaticlabs/prysm@v1.4.4/shared/interop/generate_keys_test.go (about)

     1  package interop_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  
     7  	"github.com/bazelbuild/rules_go/go/tools/bazel"
     8  	"github.com/ethereum/go-ethereum/common/hexutil"
     9  	"github.com/go-yaml/yaml"
    10  	"github.com/prysmaticlabs/prysm/shared/interop"
    11  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    12  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    13  )
    14  
    15  type TestCase struct {
    16  	Privkey string `yaml:"privkey"`
    17  }
    18  
    19  type KeyTest struct {
    20  	TestCases []*TestCase `yaml:"test_cases"`
    21  }
    22  
    23  func TestKeyGenerator(t *testing.T) {
    24  	path, err := bazel.Runfile("keygen_test_vector.yaml")
    25  	require.NoError(t, err)
    26  	file, err := ioutil.ReadFile(path)
    27  	require.NoError(t, err)
    28  	testCases := &KeyTest{}
    29  	require.NoError(t, yaml.Unmarshal(file, testCases))
    30  	priv, _, err := interop.DeterministicallyGenerateKeys(0, 1000)
    31  	require.NoError(t, err)
    32  	// cross-check with the first 1000 keys generated from the python spec
    33  	for i, key := range priv {
    34  		hexKey := testCases.TestCases[i].Privkey
    35  		nKey, err := hexutil.Decode("0x" + hexKey)
    36  		if err != nil {
    37  			t.Error(err)
    38  			continue
    39  		}
    40  		assert.DeepEqual(t, key.Marshal(), nKey)
    41  	}
    42  }