-
Notifications
You must be signed in to change notification settings - Fork 13
/
codec.go
91 lines (85 loc) · 2.68 KB
/
codec.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 encoding
import (
"fmt"
"github.com/dashpay/tenderdash/crypto"
"github.com/dashpay/tenderdash/crypto/bls12381"
"github.com/dashpay/tenderdash/crypto/ed25519"
"github.com/dashpay/tenderdash/crypto/secp256k1"
"github.com/dashpay/tenderdash/internal/jsontypes"
cryptoproto "github.com/dashpay/tenderdash/proto/tendermint/crypto"
)
func init() {
jsontypes.MustRegister((*cryptoproto.PublicKey)(nil))
jsontypes.MustRegister((*cryptoproto.PublicKey_Bls12381)(nil))
jsontypes.MustRegister((*cryptoproto.PublicKey_Ed25519)(nil))
jsontypes.MustRegister((*cryptoproto.PublicKey_Secp256K1)(nil))
}
// PubKeyToProto takes crypto.PubKey and transforms it to a protobuf Pubkey
func PubKeyToProto(k crypto.PubKey) (cryptoproto.PublicKey, error) {
var kp cryptoproto.PublicKey
switch k := k.(type) {
case ed25519.PubKey:
kp = cryptoproto.PublicKey{
Sum: &cryptoproto.PublicKey_Ed25519{
Ed25519: k,
},
}
case secp256k1.PubKey:
kp = cryptoproto.PublicKey{
Sum: &cryptoproto.PublicKey_Secp256K1{
Secp256K1: k,
},
}
case bls12381.PubKey:
kp = cryptoproto.PublicKey{
Sum: &cryptoproto.PublicKey_Bls12381{
Bls12381: k,
},
}
default:
return kp, fmt.Errorf("toproto: key type %v is not supported", k)
}
return kp, nil
}
// MustPubKeyToProto returns protobuf encoded public-key otherwise panics error
func MustPubKeyToProto(k crypto.PubKey) cryptoproto.PublicKey {
pubKey, err := PubKeyToProto(k)
if err != nil {
panic(err)
}
return pubKey
}
// PubKeyFromProto takes a protobuf Pubkey and transforms it to a crypto.Pubkey
func PubKeyFromProto(k cryptoproto.PublicKey) (crypto.PubKey, error) {
if k.Sum == nil {
return nil, fmt.Errorf("fromproto: key is nil")
}
switch k := k.Sum.(type) {
case *cryptoproto.PublicKey_Ed25519:
if len(k.Ed25519) != ed25519.PubKeySize {
return nil, fmt.Errorf("invalid size for PubKeyEd25519. Got %d, expected %d",
len(k.Ed25519), ed25519.PubKeySize)
}
pk := make(ed25519.PubKey, ed25519.PubKeySize)
copy(pk, k.Ed25519)
return pk, nil
case *cryptoproto.PublicKey_Secp256K1:
if len(k.Secp256K1) != secp256k1.PubKeySize {
return nil, fmt.Errorf("invalid size for PubKeySecp256k1. Got %d, expected %d",
len(k.Secp256K1), secp256k1.PubKeySize)
}
pk := make(secp256k1.PubKey, secp256k1.PubKeySize)
copy(pk, k.Secp256K1)
return pk, nil
case *cryptoproto.PublicKey_Bls12381:
if len(k.Bls12381) != bls12381.PubKeySize {
return nil, fmt.Errorf("invalid size for PubKeyBLS12381. Got %d, expected %d",
len(k.Bls12381), bls12381.PubKeySize)
}
pk := make(bls12381.PubKey, bls12381.PubKeySize)
copy(pk, k.Bls12381)
return pk, nil
default:
return nil, fmt.Errorf("fromproto: key type %v is not supported", k)
}
}