github.com/onflow/flow-go@v0.33.17/cmd/bootstrap/utils/key_generation_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	sdkcrypto "github.com/onflow/flow-go-sdk/crypto"
    11  	"github.com/onflow/flow-go/crypto"
    12  
    13  	"github.com/onflow/flow-go/model/bootstrap"
    14  	"github.com/onflow/flow-go/model/flow"
    15  	"github.com/onflow/flow-go/utils/unittest"
    16  )
    17  
    18  func TestGenerateUnstakedNetworkingKey(t *testing.T) {
    19  
    20  	key, err := GeneratePublicNetworkingKey(unittest.SeedFixture(crypto.KeyGenSeedMinLen))
    21  	require.NoError(t, err)
    22  	assert.Equal(t, crypto.ECDSASecp256k1, key.Algorithm())
    23  	assert.Equal(t, X962_NO_INVERSION, key.PublicKey().EncodeCompressed()[0])
    24  
    25  	keys, err := GenerateUnstakedNetworkingKeys(20, unittest.SeedFixtures(20, crypto.KeyGenSeedMinLen))
    26  	require.NoError(t, err)
    27  	for _, key := range keys {
    28  		assert.Equal(t, crypto.ECDSASecp256k1, key.Algorithm())
    29  		assert.Equal(t, X962_NO_INVERSION, key.PublicKey().EncodeCompressed()[0])
    30  	}
    31  
    32  }
    33  
    34  func TestGenerateKeys(t *testing.T) {
    35  	_, err := GenerateKeys(crypto.BLSBLS12381, 0, unittest.SeedFixtures(2, crypto.KeyGenSeedMinLen))
    36  	require.EqualError(t, err, "n needs to match the number of seeds (0 != 2)")
    37  
    38  	_, err = GenerateKeys(crypto.BLSBLS12381, 3, unittest.SeedFixtures(2, crypto.KeyGenSeedMinLen))
    39  	require.EqualError(t, err, "n needs to match the number of seeds (3 != 2)")
    40  
    41  	keys, err := GenerateKeys(crypto.BLSBLS12381, 2, unittest.SeedFixtures(2, crypto.KeyGenSeedMinLen))
    42  	require.NoError(t, err)
    43  	require.Len(t, keys, 2)
    44  }
    45  
    46  func TestGenerateStakingKeys(t *testing.T) {
    47  	keys, err := GenerateStakingKeys(2, unittest.SeedFixtures(2, crypto.KeyGenSeedMinLen))
    48  	require.NoError(t, err)
    49  	require.Len(t, keys, 2)
    50  }
    51  
    52  func TestWriteMachineAccountFiles(t *testing.T) {
    53  	nodes := append(
    54  		unittest.PrivateNodeInfosFixture(5, unittest.WithRole(flow.RoleConsensus)),
    55  		unittest.PrivateNodeInfosFixture(5, unittest.WithRole(flow.RoleCollection))...,
    56  	)
    57  
    58  	chainID := flow.Localnet
    59  	chain := chainID.Chain()
    60  
    61  	nodeIDLookup := make(map[string]flow.Identifier)
    62  	expected := make(map[string]bootstrap.NodeMachineAccountInfo)
    63  	for i, node := range nodes {
    64  		// See comments in WriteMachineAccountFiles for why addresses take this form
    65  		addr, err := chain.AddressAtIndex(uint64(6 + i*2))
    66  		require.NoError(t, err)
    67  		private, err := node.Private()
    68  		require.NoError(t, err)
    69  
    70  		expected[addr.HexWithPrefix()] = bootstrap.NodeMachineAccountInfo{
    71  			Address:           addr.HexWithPrefix(),
    72  			EncodedPrivateKey: private.NetworkPrivKey.Encode(),
    73  			KeyIndex:          0,
    74  			SigningAlgorithm:  private.NetworkPrivKey.Algorithm(),
    75  			HashAlgorithm:     sdkcrypto.SHA3_256,
    76  		}
    77  		nodeIDLookup[addr.HexWithPrefix()] = node.NodeID
    78  	}
    79  
    80  	write := func(path string, value interface{}) error {
    81  		actual, ok := value.(bootstrap.NodeMachineAccountInfo)
    82  		require.True(t, ok)
    83  
    84  		expectedInfo, ok := expected[actual.Address]
    85  		require.True(t, ok)
    86  		nodeID, ok := nodeIDLookup[actual.Address]
    87  		require.True(t, ok)
    88  		expectedPath := fmt.Sprintf(bootstrap.PathNodeMachineAccountInfoPriv, nodeID)
    89  
    90  		assert.Equal(t, expectedPath, path)
    91  		assert.Equal(t, expectedInfo, actual)
    92  		// remove the value from the mapping, this ensures each one is passed
    93  		// to the write function exactly once
    94  		delete(expected, actual.Address)
    95  
    96  		return nil
    97  	}
    98  
    99  	err := WriteMachineAccountFiles(chainID, nodes, write)
   100  	require.NoError(t, err)
   101  	assert.Len(t, expected, 0)
   102  }
   103  
   104  func TestWriteStakingNetworkingKeyFiles(t *testing.T) {
   105  	nodes := unittest.PrivateNodeInfosFixture(20, unittest.WithAllRoles())
   106  
   107  	// track expected calls to the write func
   108  	expected := make(map[flow.Identifier]bootstrap.NodeInfoPriv)
   109  	for _, node := range nodes {
   110  		private, err := node.Private()
   111  		require.NoError(t, err)
   112  		expected[node.NodeID] = private
   113  	}
   114  
   115  	// check that the correct path and value are passed to the write function
   116  	write := func(path string, value interface{}) error {
   117  		actual, ok := value.(bootstrap.NodeInfoPriv)
   118  		require.True(t, ok)
   119  
   120  		expectedInfo, ok := expected[actual.NodeID]
   121  		require.True(t, ok)
   122  		expectedPath := fmt.Sprintf(bootstrap.PathNodeInfoPriv, expectedInfo.NodeID)
   123  
   124  		assert.Equal(t, expectedPath, path)
   125  		assert.Equal(t, expectedInfo, actual)
   126  		// remove the value from the mapping, this ensures each one is passed
   127  		// to the write function exactly once
   128  		delete(expected, expectedInfo.NodeID)
   129  
   130  		return nil
   131  	}
   132  
   133  	err := WriteStakingNetworkingKeyFiles(nodes, write)
   134  	require.NoError(t, err)
   135  	assert.Len(t, expected, 0)
   136  }