-
Notifications
You must be signed in to change notification settings - Fork 2
/
key_new.go
88 lines (74 loc) · 1.79 KB
/
key_new.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 cmds
import (
"context"
"fmt"
"github.com/ProtoconNet/mitum-currency/v3/types"
"os"
"strings"
"github.com/ProtoconNet/mitum2/base"
"github.com/ProtoconNet/mitum2/util"
"github.com/ProtoconNet/mitum2/util/hint"
)
type KeyNewCommand struct {
BaseCommand
Seed string `arg:"" name:"seed" optional:"" help:"seed for generating key"`
KeyType string `help:"select btc or ether" default:"btc"`
}
func (cmd *KeyNewCommand) Run(pctx context.Context) error {
if _, err := cmd.prepare(pctx); err != nil {
return err
}
cmd.Log.Debug().
Str("seed", cmd.Seed).
Msg("flags")
if _, err := cmd.prepare(pctx); err != nil {
return err
}
var key base.Privatekey
switch {
case len(cmd.Seed) > 0:
if len(strings.TrimSpace(cmd.Seed)) < 1 {
cmd.Log.Warn().Msg("seed consists with empty spaces")
}
if len(cmd.KeyType) > 0 && cmd.KeyType == "ether" {
i, err := types.NewMEPrivatekeyFromSeed(cmd.Seed)
if err != nil {
return err
}
key = i
} else {
i, err := base.NewMPrivatekeyFromSeed(cmd.Seed)
if err != nil {
return err
}
key = i
}
default:
if len(cmd.KeyType) > 0 && cmd.KeyType == "ether" {
key = types.NewMEPrivatekey()
} else {
key = base.NewMPrivatekey()
}
}
o := struct {
PrivateKey base.PKKey `json:"privatekey"` //nolint:tagliatelle //...
Publickey base.PKKey `json:"publickey"`
Hint interface{} `json:"hint,omitempty"`
Seed string `json:"seed"`
Type string `json:"type"`
}{
Seed: cmd.Seed,
PrivateKey: key,
Publickey: key.Publickey(),
Type: "privatekey",
}
if hinter, ok := (interface{})(key).(hint.Hinter); ok {
o.Hint = hinter.Hint()
}
b, err := util.MarshalJSONIndent(o)
if err != nil {
return err
}
_, _ = fmt.Fprintln(os.Stdout, string(b))
return nil
}