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