-
Notifications
You must be signed in to change notification settings - Fork 463
/
keyinfo.go
131 lines (113 loc) · 3.02 KB
/
keyinfo.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
package crypto
import (
"bytes"
"encoding/json"
"fmt"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-address"
"github.com/pkg/errors"
bls "github.com/filecoin-project/filecoin-ffi"
)
const (
stBLS = "bls"
stSecp256k1 = "secp256k1"
)
// KeyInfo is a key and its type used for signing.
type KeyInfo struct {
// Private key.
PrivateKey []byte `json:"privateKey"`
// Cryptographic system used to generate private key.
SigType SigType `json:"type"`
}
type keyInfo struct {
// Private key.
PrivateKey []byte `json:"privateKey"`
// Cryptographic system used to generate private key.
SigType interface{} `json:"type"`
}
func (ki *KeyInfo) UnmarshalJSON(data []byte) error {
k := keyInfo{}
err := json.Unmarshal(data, &k)
if err != nil {
return err
}
switch k.SigType.(type) {
case string:
st := k.SigType.(string)
if st == stBLS {
ki.SigType = crypto.SigTypeBLS
} else if st == stSecp256k1 {
ki.SigType = crypto.SigTypeSecp256k1
} else {
return fmt.Errorf("unknown sig type value: %s", st)
}
case byte:
ki.SigType = crypto.SigType(k.SigType.(byte))
case float64:
ki.SigType = crypto.SigType(k.SigType.(float64))
case int:
ki.SigType = crypto.SigType(k.SigType.(int))
case int64:
ki.SigType = crypto.SigType(k.SigType.(int64))
default:
return fmt.Errorf("unknown sig type: %T", k.SigType)
}
ki.PrivateKey = k.PrivateKey
return nil
}
func (ki *KeyInfo) MarshalJSON() ([]byte, error) {
k := keyInfo{}
k.PrivateKey = ki.PrivateKey
if ki.SigType == crypto.SigTypeBLS {
k.SigType = stBLS
} else if ki.SigType == crypto.SigTypeSecp256k1 {
k.SigType = stSecp256k1
} else {
return nil, fmt.Errorf("unsupport keystore types %T", k.SigType)
}
return json.Marshal(k)
}
// Key returns the private key of KeyInfo
func (ki *KeyInfo) Key() []byte {
return ki.PrivateKey
}
// Type returns the type of curve used to generate the private key
func (ki *KeyInfo) Type() SigType {
return ki.SigType
}
// Equals returns true if the KeyInfo is equal to other.
func (ki *KeyInfo) Equals(other *KeyInfo) bool {
if ki == nil && other == nil {
return true
}
if ki == nil || other == nil {
return false
}
if ki.SigType != other.SigType {
return false
}
return bytes.Equal(ki.PrivateKey, other.PrivateKey)
}
// Address returns the address for this keyinfo
func (ki *KeyInfo) Address() (address.Address, error) {
if ki.SigType == SigTypeBLS {
return address.NewBLSAddress(ki.PublicKey())
}
if ki.SigType == SigTypeSecp256k1 {
return address.NewSecp256k1Address(ki.PublicKey())
}
return address.Undef, errors.Errorf("can not generate address for unknown crypto system: %d", ki.SigType)
}
// Returns the public key part as uncompressed bytes.
func (ki *KeyInfo) PublicKey() []byte {
if ki.SigType == SigTypeBLS {
var blsPrivateKey bls.PrivateKey
copy(blsPrivateKey[:], ki.PrivateKey)
publicKey := bls.PrivateKeyPublicKey(blsPrivateKey)
return publicKey[:]
}
if ki.SigType == SigTypeSecp256k1 {
return PublicKeyForSecpSecretKey(ki.PrivateKey)
}
return []byte{}
}