-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
export.go
62 lines (54 loc) · 1.63 KB
/
export.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
package p2pkey
import (
keystore "github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
const keyTypeIdentifier = "P2P"
func FromEncryptedJSON(keyJSON []byte, password string) (KeyV2, error) {
return keys.FromEncryptedJSON(
keyTypeIdentifier,
keyJSON,
password,
adulteratedPassword,
func(_ EncryptedP2PKeyExport, rawPrivKey []byte) (KeyV2, error) {
return Raw(rawPrivKey).Key(), nil
},
)
}
type EncryptedP2PKeyExport struct {
KeyType string `json:"keyType"`
PublicKey string `json:"publicKey"`
PeerID PeerID `json:"peerID"`
Crypto keystore.CryptoJSON `json:"crypto"`
}
func (x EncryptedP2PKeyExport) GetCrypto() keystore.CryptoJSON {
return x.Crypto
}
func (key KeyV2) ToEncryptedJSON(password string, scryptParams utils.ScryptParams) (export []byte, err error) {
return keys.ToEncryptedJSON(
keyTypeIdentifier,
key.Raw(),
key,
password,
scryptParams,
adulteratedPassword,
func(id string, key KeyV2, cryptoJSON keystore.CryptoJSON) (EncryptedP2PKeyExport, error) {
rawPubKey, err := key.GetPublic().Bytes()
if err != nil {
return EncryptedP2PKeyExport{}, errors.Wrapf(err, "could not get raw public key")
}
return EncryptedP2PKeyExport{
KeyType: id,
PublicKey: hexutil.Encode(rawPubKey),
PeerID: key.PeerID(),
Crypto: cryptoJSON,
}, nil
},
)
}
func adulteratedPassword(password string) string {
return "p2pkey" + password
}