-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
main.go
90 lines (78 loc) · 1.72 KB
/
main.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
89
90
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
_ "github.com/filecoin-project/lotus/lib/sigs/bls"
_ "github.com/filecoin-project/lotus/lib/sigs/secp"
"github.com/urfave/cli/v2"
)
func main() {
app := cli.NewApp()
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "type",
Aliases: []string{"t"},
Value: "bls",
Usage: "specify key type to generate (bls or secp256k1)",
},
&cli.StringFlag{
Name: "out",
Aliases: []string{"o"},
Usage: "specify key file name to generate",
},
}
app.Action = func(cctx *cli.Context) error {
memks := wallet.NewMemKeyStore()
w, err := wallet.NewWallet(memks)
if err != nil {
return err
}
var kt types.KeyType
switch cctx.String("type") {
case "bls":
kt = types.KTBLS
case "secp256k1":
kt = types.KTSecp256k1
default:
return fmt.Errorf("unrecognized key type: %q", cctx.String("type"))
}
kaddr, err := w.WalletNew(cctx.Context, kt)
if err != nil {
return err
}
ki, err := w.WalletExport(cctx.Context, kaddr)
if err != nil {
return err
}
outFile := fmt.Sprintf("%s.key", kaddr)
if cctx.IsSet("out") {
outFile = fmt.Sprintf("%s.key", cctx.String("out"))
}
fi, err := os.Create(outFile)
if err != nil {
return err
}
defer func() {
err2 := fi.Close()
if err == nil {
err = err2
}
}()
b, err := json.Marshal(ki)
if err != nil {
return err
}
if _, err := fi.Write(b); err != nil {
return fmt.Errorf("failed to write key info to file: %w", err)
}
fmt.Println("Generated new key: ", kaddr)
return nil
}
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
os.Exit(1)
}
}