/
multibls.go
74 lines (63 loc) · 1.91 KB
/
multibls.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
package multibls
import (
"strings"
"github.com/harmony-one/harmony/internal/utils"
bls_core "github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/crypto/bls"
)
// PrivateKeys stores the bls secret keys that belongs to the node
type PrivateKeys []bls.PrivateKeyWrapper
// PublicKeys stores the bls public keys that belongs to the node
type PublicKeys []bls.PublicKeyWrapper
// SerializeToHexStr wrapper
func (multiKey PublicKeys) SerializeToHexStr() string {
if multiKey == nil {
return ""
}
var builder strings.Builder
for _, pubKey := range multiKey {
builder.WriteString(pubKey.Bytes.Hex() + ";")
}
return builder.String()
}
// Contains wrapper
func (multiKey PublicKeys) Contains(pubKey *bls_core.PublicKey) bool {
for _, key := range multiKey {
if key.Object.IsEqual(pubKey) {
return true
}
}
return false
}
// GetPublicKeys wrapper
func (multiKey PrivateKeys) GetPublicKeys() PublicKeys {
pubKeys := make([]bls.PublicKeyWrapper, len(multiKey))
for i, key := range multiKey {
pubKeys[i] = *key.Pub
}
return pubKeys
}
// Dedup will return a new list of dedupped private keys.
// This func won't modify the original slice.
func (multiKey PrivateKeys) Dedup() PrivateKeys {
uniqueKeys := make(map[bls.SerializedPublicKey]struct{})
deduped := make(PrivateKeys, 0, len(multiKey))
for _, priKey := range multiKey {
if _, ok := uniqueKeys[priKey.Pub.Bytes]; ok {
utils.Logger().Warn().Str("PubKey", priKey.Pub.Bytes.Hex()).Msg("Duplicate private key ignored!")
continue
}
uniqueKeys[priKey.Pub.Bytes] = struct{}{}
deduped = append(deduped, priKey)
}
return deduped
}
// GetPrivateKeys creates a multibls PrivateKeys using bls.SecretKey
func GetPrivateKeys(secretKeys ...*bls_core.SecretKey) PrivateKeys {
keys := make(PrivateKeys, 0, len(secretKeys))
for _, secretKey := range secretKeys {
key := bls.WrapperFromPrivateKey(secretKey)
keys = append(keys, key)
}
return keys
}