-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
key.go
123 lines (104 loc) · 2.76 KB
/
key.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
package p2pkey
import (
"database/sql/driver"
"encoding/hex"
"encoding/json"
"strconv"
"time"
"github.com/ethereum/go-ethereum/accounts/keystore"
cryptop2p "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/pkg/errors"
)
// Key represents a libp2p private key
type Key struct {
cryptop2p.PrivKey
}
func (k Key) ToV2() KeyV2 {
return KeyV2{
PrivKey: k.PrivKey,
peerID: k.PeerID(),
}
}
// PublicKeyBytes is generated using cryptop2p.PubKey.Raw()
type PublicKeyBytes []byte
func (pkb PublicKeyBytes) String() string {
return hex.EncodeToString(pkb)
}
func (pkb PublicKeyBytes) MarshalJSON() ([]byte, error) {
return json.Marshal(hex.EncodeToString(pkb))
}
func (pkb *PublicKeyBytes) UnmarshalJSON(input []byte) error {
var hexString string
if err := json.Unmarshal(input, &hexString); err != nil {
return err
}
result, err := hex.DecodeString(hexString)
if err != nil {
return err
}
*pkb = PublicKeyBytes(result)
return nil
}
func (pkb *PublicKeyBytes) Scan(value interface{}) error {
switch v := value.(type) {
case []byte:
*pkb = v
return nil
default:
return errors.Errorf("invalid public key bytes got %T wanted []byte", v)
}
}
func (pkb PublicKeyBytes) Value() (driver.Value, error) {
return []byte(pkb), nil
}
func (k Key) GetPeerID() (PeerID, error) {
peerID, err := peer.IDFromPrivateKey(k)
if err != nil {
return "", errors.WithStack(err)
}
return PeerID(peerID), err
}
func (k Key) PeerID() PeerID {
peerID, err := peer.IDFromPrivateKey(k)
if err != nil {
panic(err)
}
return PeerID(peerID)
}
type EncryptedP2PKey struct {
ID int32
PeerID PeerID
PubKey PublicKeyBytes
EncryptedPrivKey []byte
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}
func (ep2pk *EncryptedP2PKey) SetID(value string) error {
result, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return err
}
ep2pk.ID = int32(result)
return nil
}
// Decrypt returns the PrivateKey in e, decrypted via auth, or an error
func (ep2pk EncryptedP2PKey) Decrypt(auth string) (k Key, err error) {
var cryptoJSON keystore.CryptoJSON
err = json.Unmarshal(ep2pk.EncryptedPrivKey, &cryptoJSON)
if err != nil {
return k, errors.Wrapf(err, "invalid JSON for P2P key %s (0x%x)", ep2pk.PeerID.String(), ep2pk.PubKey)
}
marshalledPrivK, err := keystore.DecryptDataV3(cryptoJSON, adulteratedPassword(auth))
if err != nil {
return k, errors.Wrapf(err, "could not decrypt P2P key %s (0x%x)", ep2pk.PeerID.String(), ep2pk.PubKey)
}
privK, err := cryptop2p.UnmarshalPrivateKey(marshalledPrivK)
if err != nil {
return k, errors.Wrapf(err, "could not unmarshal P2P private key for %s (0x%x)", ep2pk.PeerID.String(), ep2pk.PubKey)
}
return Key{
privK,
}, nil
}