-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
export.go
83 lines (75 loc) · 2.6 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package ocr2key
import (
"encoding/hex"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/chaintype"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/starkkey"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
const keyTypeIdentifier = "OCR2"
// EncryptedOCRKeyExport represents encrypted OCR key export
type EncryptedOCRKeyExport struct {
KeyType string `json:"keyType"`
ChainType chaintype.ChainType `json:"chainType"`
ID string `json:"id"`
OnchainPublicKey string `json:"onchainPublicKey"`
OffChainPublicKey string `json:"offchainPublicKey"`
ConfigPublicKey string `json:"configPublicKey"`
Crypto keystore.CryptoJSON `json:"crypto"`
}
func (x EncryptedOCRKeyExport) GetCrypto() keystore.CryptoJSON {
return x.Crypto
}
// FromEncryptedJSON returns key from encrypted json
func FromEncryptedJSON(keyJSON []byte, password string) (KeyBundle, error) {
return keys.FromEncryptedJSON(
keyTypeIdentifier,
keyJSON,
password,
adulteratedPassword,
func(export EncryptedOCRKeyExport, rawPrivKey []byte) (KeyBundle, error) {
var kb KeyBundle
switch export.ChainType {
case chaintype.EVM:
kb = newKeyBundle(new(evmKeyring))
case chaintype.Cosmos:
kb = newKeyBundle(new(cosmosKeyring))
case chaintype.Solana:
kb = newKeyBundle(new(solanaKeyring))
case chaintype.StarkNet:
kb = newKeyBundle(new(starkkey.OCR2Key))
default:
return nil, chaintype.NewErrInvalidChainType(export.ChainType)
}
if err := kb.Unmarshal(rawPrivKey); err != nil {
return nil, err
}
return kb, nil
},
)
}
// ToEncryptedJSON returns encrypted JSON representing key
func ToEncryptedJSON(key KeyBundle, password string, scryptParams utils.ScryptParams) (export []byte, err error) {
return keys.ToEncryptedJSON(
keyTypeIdentifier,
key.Raw(),
key,
password,
scryptParams,
adulteratedPassword,
func(id string, key KeyBundle, cryptoJSON keystore.CryptoJSON) (EncryptedOCRKeyExport, error) {
pubKeyConfig := key.ConfigEncryptionPublicKey()
pubKey := key.OffchainPublicKey()
return EncryptedOCRKeyExport{
KeyType: id,
ChainType: key.ChainType(),
ID: key.ID(),
OnchainPublicKey: key.OnChainPublicKey(),
OffChainPublicKey: hex.EncodeToString(pubKey[:]),
ConfigPublicKey: hex.EncodeToString(pubKeyConfig[:]),
Crypto: cryptoJSON,
}, nil
},
)
}