2020-08-18 00:29:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
2020-10-11 18:12:01 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-08-18 00:29:26 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/wallet"
|
|
|
|
_ "github.com/filecoin-project/lotus/lib/sigs/bls"
|
2023-01-13 19:11:13 +00:00
|
|
|
_ "github.com/filecoin-project/lotus/lib/sigs/delegated"
|
2020-08-18 00:29:26 +00:00
|
|
|
_ "github.com/filecoin-project/lotus/lib/sigs/secp"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)",
|
|
|
|
},
|
2020-12-04 10:39:35 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "out",
|
|
|
|
Aliases: []string{"o"},
|
|
|
|
Usage: "specify key file name to generate",
|
|
|
|
},
|
2020-08-18 00:29:26 +00:00
|
|
|
}
|
|
|
|
app.Action = func(cctx *cli.Context) error {
|
|
|
|
memks := wallet.NewMemKeyStore()
|
|
|
|
w, err := wallet.NewWallet(memks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-11 18:12:01 +00:00
|
|
|
var kt types.KeyType
|
2020-08-18 00:29:26 +00:00
|
|
|
switch cctx.String("type") {
|
|
|
|
case "bls":
|
2020-10-11 18:12:01 +00:00
|
|
|
kt = types.KTBLS
|
2020-08-18 00:29:26 +00:00
|
|
|
case "secp256k1":
|
2020-10-11 18:12:01 +00:00
|
|
|
kt = types.KTSecp256k1
|
2020-08-18 00:29:26 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unrecognized key type: %q", cctx.String("type"))
|
|
|
|
}
|
|
|
|
|
2020-09-04 20:17:28 +00:00
|
|
|
kaddr, err := w.WalletNew(cctx.Context, kt)
|
2020-08-18 00:29:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-04 20:17:28 +00:00
|
|
|
ki, err := w.WalletExport(cctx.Context, kaddr)
|
2020-08-18 00:29:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-04 10:39:35 +00:00
|
|
|
outFile := fmt.Sprintf("%s.key", kaddr)
|
|
|
|
if cctx.IsSet("out") {
|
|
|
|
outFile = fmt.Sprintf("%s.key", cctx.String("out"))
|
|
|
|
}
|
|
|
|
fi, err := os.Create(outFile)
|
2020-08-18 00:29:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-20 04:49:10 +00:00
|
|
|
defer func() {
|
|
|
|
err2 := fi.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = err2
|
|
|
|
}
|
|
|
|
}()
|
2020-08-18 00:29:26 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|