-
Notifications
You must be signed in to change notification settings - Fork 217
/
key.go
85 lines (70 loc) · 1.87 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
// (c) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package key
import (
"context"
"crypto/ecdsa"
"fmt"
"os"
"path/filepath"
"github.com/ethereum/go-ethereum/common"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
)
type Key struct {
PrivKey *ecdsa.PrivateKey
Address common.Address
}
func createKey(pk *ecdsa.PrivateKey) *Key {
return &Key{pk, ethcrypto.PubkeyToAddress(pk.PublicKey)}
}
// Load attempts to open a [Key] stored at [file].
func Load(file string) (*Key, error) {
pk, err := ethcrypto.LoadECDSA(file)
if err != nil {
return nil, fmt.Errorf("problem loading private key from %s: %w", file, err)
}
return createKey(pk), nil
}
// LoadAll loads all keys in [dir].
func LoadAll(ctx context.Context, dir string) ([]*Key, error) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, fmt.Errorf("unable to create %s: %w", dir, err)
}
return nil, nil
}
var files []string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if path == dir {
return nil
}
files = append(files, path)
return nil
})
if err != nil {
return nil, fmt.Errorf("could not walk %s: %w", dir, err)
}
ks := make([]*Key, len(files))
for i, file := range files {
k, err := Load(file)
if err != nil {
return nil, fmt.Errorf("could not load key at %s: %w", file, err)
}
ks[i] = k
}
return ks, nil
}
// Save persists a [Key] to [dir] (where the filename is the hex-encoded
// address).
func (k *Key) Save(dir string) error {
fp := filepath.Join(dir, k.Address.Hex())
return ethcrypto.SaveECDSA(fp, k.PrivKey)
}
// Generate creates a new [Key] and returns it.
func Generate() (*Key, error) {
pk, err := ethcrypto.GenerateKey()
if err != nil {
return nil, fmt.Errorf("%w: cannot generate key", err)
}
return createKey(pk), nil
}