-
Notifications
You must be signed in to change notification settings - Fork 47
/
cmd_key.go
88 lines (76 loc) · 2.48 KB
/
cmd_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
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/iov-one/weave/crypto"
"golang.org/x/crypto/ed25519"
)
func cmdKeygen(input io.Reader, output io.Writer, args []string) error {
fl := flag.NewFlagSet("", flag.ExitOnError)
fl.Usage = func() {
fmt.Fprint(flag.CommandLine.Output(), `
Generate a new private key.
When successful a new file with binary content containing private key is
created. This command fails if the private key file already exists.
`)
fl.PrintDefaults()
}
var (
keyPathFl = fl.String("key", env("BNSCLI_PRIV_KEY", os.Getenv("HOME")+"/.bnsd.priv.key"),
"Path to the private key file that transaction should be signed with. You can use BNSCLI_PRIV_KEY environment variable to set it.")
)
fl.Parse(args)
if _, err := os.Stat(*keyPathFl); !os.IsNotExist(err) {
// Do not allow to overwrite already existing private key. User
// must manually delete it first to ensure we do not delete
// such crucial data by an accident (bad command usage).
return fmt.Errorf("private key file %q already exists, delete this file and try again", *keyPathFl)
}
_, priv, err := ed25519.GenerateKey(nil)
if err != nil {
return fmt.Errorf("cannot generate ed25519 key: %s", err)
}
fd, err := os.OpenFile(*keyPathFl, os.O_CREATE|os.O_WRONLY, 0400)
if err != nil {
return fmt.Errorf("cannot create public key file: %s", err)
}
defer fd.Close()
if _, err := fd.Write(priv); err != nil {
return fmt.Errorf("cannot write private key: %s", err)
}
if err := fd.Close(); err != nil {
return fmt.Errorf("cannot close private key file: %s", err)
}
return nil
}
func cmdKeyaddr(input io.Reader, output io.Writer, args []string) error {
fl := flag.NewFlagSet("", flag.ExitOnError)
fl.Usage = func() {
fmt.Fprint(flag.CommandLine.Output(), `
Print out a hex-address associated with your private key.
`)
fl.PrintDefaults()
}
var (
keyPathFl = fl.String("key", env("BNSCLI_PRIV_KEY", os.Getenv("HOME")+"/.bnsd.priv.key"),
"Path to the private key file that transaction should be signed with. You can use BNSCLI_PRIV_KEY environment variable to set it.")
)
fl.Parse(args)
raw, err := ioutil.ReadFile(*keyPathFl)
if err != nil {
return fmt.Errorf("cannot read private key file: %s", err)
}
if len(raw) != ed25519.PrivateKeySize {
return fmt.Errorf("invalid private key length: %d", len(raw))
}
key := &crypto.PrivateKey{
Priv: &crypto.PrivateKey_Ed25519{
Ed25519: raw,
},
}
_, err = fmt.Fprintln(output, key.PublicKey().Address())
return err
}