-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
key.go
65 lines (55 loc) · 1.41 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
package csakey
import (
"crypto/ed25519"
"errors"
"time"
"github.com/smartcontractkit/chainlink/v2/core/utils"
"github.com/smartcontractkit/chainlink/v2/core/utils/crypto"
)
type Key struct {
ID uint
PublicKey crypto.PublicKey
privateKey []byte
EncryptedPrivateKey crypto.EncryptedPrivateKey
CreatedAt time.Time
UpdatedAt time.Time
}
// New creates a new CSA key consisting of an ed25519 key. It encrypts the
// Key with the passphrase.
func New(passphrase string, scryptParams utils.ScryptParams) (*Key, error) {
pubkey, privkey, err := ed25519.GenerateKey(nil)
if err != nil {
return nil, err
}
encPrivkey, err := crypto.NewEncryptedPrivateKey(privkey, passphrase, scryptParams)
if err != nil {
return nil, err
}
return &Key{
PublicKey: crypto.PublicKey(pubkey),
privateKey: privkey,
EncryptedPrivateKey: *encPrivkey,
}, nil
}
func (k *Key) Unlock(password string) error {
pk, err := k.EncryptedPrivateKey.Decrypt(password)
if err != nil {
return err
}
k.privateKey = pk
return nil
}
func (k *Key) Unsafe_GetPrivateKey() ([]byte, error) {
if k.privateKey == nil {
return nil, errors.New("key has not been unlocked")
}
return k.privateKey, nil
}
func (k Key) ToV2() KeyV2 {
pk := ed25519.PrivateKey(k.privateKey)
return KeyV2{
privateKey: &pk,
PublicKey: ed25519.PublicKey(k.PublicKey),
Version: 1,
}
}