/
commands.go
80 lines (68 loc) · 1.51 KB
/
commands.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
package signingKey
import (
"crypto/rand"
"errors"
"fmt"
"io/ioutil"
"time"
pui "github.com/manifoldco/promptui"
cli "github.com/urfave/cli"
ed25519 "gx/ipfs/QmaPHkZLbQQbvcyavn8q1GFHg6o6yeceyHFSJ3Pjf3p3TQ/go-crypto/ed25519"
)
var KeyNew = cli.Command{
Name: "sk:new",
Action: func(c *cli.Context) error {
// ask for name of signing key
p := pui.Prompt{
Label: "Enter name of new signing key",
}
name, err := p.Run()
if err != nil {
panic(err)
}
if name == "" {
return errors.New("please enter a name for this signing key")
}
// ask for password
p = pui.Prompt{
Label: "Enter password for signing key encryption",
Mask: '*',
}
pw, err := p.Run()
if err != nil {
panic(err)
}
// ask for password confirmation
p = pui.Prompt{
Label: "Confirm password",
Mask: '*',
}
pwConfirm, err := p.Run()
if err != nil {
panic(err)
}
if pw != pwConfirm {
return errors.New("failed to confirm password")
}
// create key store
pub, priv, err := ed25519.GenerateKey(rand.Reader)
sk := SingingKey{
Name: name,
PublicKey: pub[:],
privateKey: priv[:],
CreateAt: time.Now(),
Version: uint8(1),
}
// export signing key
data, err := sk.Export([]byte(pw))
if err != nil {
return err
}
fileName := fmt.Sprintf("%s_%d.signing_key.json", name, time.Now().Unix())
if err := ioutil.WriteFile(fileName, data, 0775); err != nil {
return err
}
fmt.Println(fmt.Sprintf("wrote signing key to: %s", fileName))
return nil
},
}