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