-
Notifications
You must be signed in to change notification settings - Fork 179
/
keys.go
91 lines (76 loc) · 1.92 KB
/
keys.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
package unittest
import (
"crypto/rand"
"encoding/hex"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/model/dkg"
"github.com/onflow/flow-go/model/encodable"
)
func NetworkingKey() (crypto.PrivateKey, error) {
seed := make([]byte, crypto.KeyGenSeedMinLenECDSAP256)
n, err := rand.Read(seed)
if err != nil || n != crypto.KeyGenSeedMinLenECDSAP256 {
return nil, err
}
sk, err := crypto.GeneratePrivateKey(crypto.ECDSAP256, seed)
return sk, err
}
func NetworkingKeys(n int) ([]crypto.PrivateKey, error) {
keys := make([]crypto.PrivateKey, 0, n)
for i := 0; i < n; i++ {
key, err := NetworkingKey()
if err != nil {
return nil, err
}
keys = append(keys, key)
}
return keys, nil
}
func StakingKey() (crypto.PrivateKey, error) {
seed := make([]byte, crypto.KeyGenSeedMinLenBLSBLS12381)
n, err := rand.Read(seed)
if err != nil || n != crypto.KeyGenSeedMinLenBLSBLS12381 {
return nil, err
}
sk, err := crypto.GeneratePrivateKey(crypto.BLSBLS12381, seed)
return sk, err
}
func StakingKeys(n int) ([]crypto.PrivateKey, error) {
keys := make([]crypto.PrivateKey, 0, n)
for i := 0; i < n; i++ {
key, err := StakingKey()
if err != nil {
return nil, err
}
keys = append(keys, key)
}
return keys, nil
}
func DKGParticipantPriv() *dkg.DKGParticipantPriv {
privKey, _ := StakingKey()
randBeaconKey := encodable.RandomBeaconPrivKey{
PrivateKey: privKey,
}
return &dkg.DKGParticipantPriv{
NodeID: IdentifierFixture(),
RandomBeaconPrivKey: randBeaconKey,
}
}
func MustDecodePublicKeyHex(algo crypto.SigningAlgorithm, keyHex string) crypto.PublicKey {
keyBytes, err := hex.DecodeString(keyHex)
if err != nil {
panic(err)
}
key, err := crypto.DecodePublicKey(algo, keyBytes)
if err != nil {
panic(err)
}
return key
}
func MustDecodeSignatureHex(sigHex string) crypto.Signature {
sigBytes, err := hex.DecodeString(sigHex)
if err != nil {
panic(err)
}
return sigBytes
}