-
Notifications
You must be signed in to change notification settings - Fork 461
/
key_info.go
114 lines (99 loc) · 2.08 KB
/
key_info.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
package types
import (
"encoding/json"
"fmt"
"math"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/crypto"
)
var (
ErrKeyInfoNotFound = fmt.Errorf("key info not found")
ErrKeyExists = fmt.Errorf("key already exists")
)
// KeyType defines a type of a key
type KeyType string
func (kt *KeyType) UnmarshalJSON(bb []byte) error {
{
// first option, try unmarshaling as string
var s string
err := json.Unmarshal(bb, &s)
if err == nil {
*kt = KeyType(s)
return nil
}
}
{
var b byte
err := json.Unmarshal(bb, &b)
if err != nil {
return fmt.Errorf("could not unmarshal KeyType either as string nor integer: %w", err)
}
bst := crypto.SigType(b)
switch bst {
case crypto.SigTypeBLS:
*kt = KTBLS
case crypto.SigTypeSecp256k1:
*kt = KTSecp256k1
case crypto.SigTypeDelegated:
*kt = KTDelegated
default:
return fmt.Errorf("unknown sigtype: %d", bst)
}
return nil
}
}
type SigType = crypto.SigType
const (
SigTypeUnknown = SigType(math.MaxUint8)
SigTypeSecp256k1 = SigType(iota)
SigTypeBLS
SigTypeDelegated
)
const (
KTUnknown KeyType = "unknown"
KTBLS KeyType = "bls"
KTSecp256k1 KeyType = "secp256k1"
KTSecp256k1Ledger KeyType = "secp256k1-ledger"
KTDelegated KeyType = "delegated"
)
func KeyType2Sign(kt KeyType) SigType {
switch kt {
case KTSecp256k1:
return SigTypeSecp256k1
case KTBLS:
return SigTypeBLS
case KTDelegated:
return SigTypeDelegated
default:
return SigTypeUnknown
}
}
func SignType2Key(kt SigType) KeyType {
switch kt {
case SigTypeSecp256k1:
return KTSecp256k1
case SigTypeBLS:
return KTBLS
case SigTypeDelegated:
return KTDelegated
default:
return KTUnknown
}
}
func AddressProtocol2SignType(p address.Protocol) SigType {
switch p {
case address.BLS:
return SigTypeBLS
case address.SECP256K1:
return SigTypeSecp256k1
case address.Delegated:
return SigTypeDelegated
default:
return SigTypeUnknown
}
}
// KeyInfo is used for storing keys in KeyStore
type KeyInfo struct {
Type KeyType
PrivateKey []byte
}