2019-12-09 15:09:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-07-23 10:21:13 +00:00
|
|
|
"bufio"
|
2020-06-29 19:41:43 +00:00
|
|
|
"encoding/base64"
|
2019-12-09 15:09:23 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2020-06-29 19:41:43 +00:00
|
|
|
"fmt"
|
2019-12-09 15:09:23 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-09-17 23:09:12 +00:00
|
|
|
"path"
|
2019-12-09 15:09:23 +00:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
2022-08-25 18:20:41 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/multiformats/go-base32"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"golang.org/x/xerrors"
|
2020-02-11 01:04:20 +00:00
|
|
|
|
2019-12-09 15:09:23 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/wallet"
|
2022-06-14 17:21:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/wallet/key"
|
2022-06-14 15:00:51 +00:00
|
|
|
_ "github.com/filecoin-project/lotus/lib/sigs/bls"
|
|
|
|
_ "github.com/filecoin-project/lotus/lib/sigs/secp"
|
2020-09-17 23:09:12 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules"
|
2020-06-29 19:41:43 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/lp2p"
|
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
2019-12-09 15:09:23 +00:00
|
|
|
)
|
|
|
|
|
2020-10-11 18:12:01 +00:00
|
|
|
var validTypes = []types.KeyType{types.KTBLS, types.KTSecp256k1, lp2p.KTLibp2pHost}
|
2020-06-29 19:41:43 +00:00
|
|
|
|
|
|
|
type keyInfoOutput struct {
|
2020-10-11 18:12:01 +00:00
|
|
|
Type types.KeyType
|
2019-12-09 15:09:23 +00:00
|
|
|
Address string
|
|
|
|
PublicKey string
|
|
|
|
}
|
|
|
|
|
2020-06-29 19:41:43 +00:00
|
|
|
var keyinfoCmd = &cli.Command{
|
2022-05-03 18:35:06 +00:00
|
|
|
Name: "key-info",
|
|
|
|
Aliases: []string{"keyinfo"},
|
|
|
|
Usage: "work with lotus keyinfo files (wallets and libp2p host keys)",
|
2020-06-29 19:41:43 +00:00
|
|
|
Description: `The subcommands of keyinfo provide helpful tools for working with keyinfo files without
|
|
|
|
having to run the lotus daemon.`,
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
keyinfoNewCmd,
|
|
|
|
keyinfoInfoCmd,
|
|
|
|
keyinfoImportCmd,
|
2020-09-17 23:09:12 +00:00
|
|
|
keyinfoVerifyCmd,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var keyinfoVerifyCmd = &cli.Command{
|
|
|
|
Name: "verify",
|
|
|
|
Usage: "verify the filename of a keystore object on disk with it's contents",
|
|
|
|
Description: `Keystore objects are base32 enocded strings, with wallets being dynamically named via
|
|
|
|
the wallet address. This command can ensure that the naming of these keystore objects are correct`,
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
filePath := cctx.Args().First()
|
|
|
|
fileName := path.Base(filePath)
|
|
|
|
|
|
|
|
inputFile, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer inputFile.Close() //nolint:errcheck
|
|
|
|
input := bufio.NewReader(inputFile)
|
|
|
|
|
|
|
|
keyContent, err := ioutil.ReadAll(input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var keyInfo types.KeyInfo
|
|
|
|
if err := json.Unmarshal(keyContent, &keyInfo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch keyInfo.Type {
|
|
|
|
case lp2p.KTLibp2pHost:
|
|
|
|
name, err := base32.RawStdEncoding.DecodeString(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("decoding key: '%s': %w", fileName, err)
|
|
|
|
}
|
|
|
|
|
2020-10-11 18:12:01 +00:00
|
|
|
if types.KeyType(name) != keyInfo.Type {
|
2020-09-17 23:09:12 +00:00
|
|
|
return fmt.Errorf("%s of type %s is incorrect", fileName, keyInfo.Type)
|
|
|
|
}
|
|
|
|
case modules.KTJwtHmacSecret:
|
|
|
|
name, err := base32.RawStdEncoding.DecodeString(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("decoding key: '%s': %w", fileName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(name) != modules.JWTSecretName {
|
|
|
|
return fmt.Errorf("%s of type %s is incorrect", fileName, keyInfo.Type)
|
|
|
|
}
|
2020-10-11 18:12:01 +00:00
|
|
|
case types.KTSecp256k1, types.KTBLS:
|
2020-09-17 23:09:12 +00:00
|
|
|
keystore := wallet.NewMemKeyStore()
|
|
|
|
w, err := wallet.NewWallet(keystore)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-08 23:33:50 +00:00
|
|
|
if _, err := w.WalletImport(cctx.Context, &keyInfo); err != nil {
|
2020-09-17 23:09:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := keystore.List()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(list) != 1 {
|
|
|
|
return fmt.Errorf("Unexpected number of keys, expected 1, found %d", len(list))
|
|
|
|
}
|
|
|
|
|
|
|
|
name, err := base32.RawStdEncoding.DecodeString(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("decoding key: '%s': %w", fileName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(name) != list[0] {
|
|
|
|
return fmt.Errorf("%s of type %s; file is named for %s, but key is actually %s", fileName, keyInfo.Type, string(name), list[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown keytype %s", keyInfo.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-06-29 19:41:43 +00:00
|
|
|
},
|
2019-12-09 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 19:41:43 +00:00
|
|
|
var keyinfoImportCmd = &cli.Command{
|
|
|
|
Name: "import",
|
|
|
|
Usage: "import a keyinfo file into a lotus repository",
|
|
|
|
Description: `The import command provides a way to import keyfiles into a lotus repository
|
|
|
|
without running the daemon.
|
|
|
|
|
|
|
|
Note: The LOTUS_PATH directory must be created. This command will not create this directory for you.
|
|
|
|
|
|
|
|
Examples
|
|
|
|
|
|
|
|
env LOTUS_PATH=/var/lib/lotus lotus-shed keyinfo import libp2p-host.keyinfo`,
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
flagRepo := cctx.String("repo")
|
|
|
|
|
|
|
|
var input io.Reader
|
|
|
|
if cctx.Args().Len() == 0 {
|
|
|
|
input = os.Stdin
|
|
|
|
} else {
|
|
|
|
var err error
|
2020-07-23 10:21:13 +00:00
|
|
|
inputFile, err := os.Open(cctx.Args().First())
|
2020-06-29 19:41:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-20 04:49:10 +00:00
|
|
|
defer inputFile.Close() //nolint:errcheck
|
2020-07-23 10:21:13 +00:00
|
|
|
input = bufio.NewReader(inputFile)
|
2020-06-29 19:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
encoded, err := ioutil.ReadAll(input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded, err := hex.DecodeString(strings.TrimSpace(string(encoded)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var keyInfo types.KeyInfo
|
|
|
|
if err := json.Unmarshal(decoded, &keyInfo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fsrepo, err := repo.NewFS(flagRepo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lkrepo, err := fsrepo.Lock(repo.FullNode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-20 04:49:10 +00:00
|
|
|
defer lkrepo.Close() //nolint:errcheck
|
2020-06-29 19:41:43 +00:00
|
|
|
|
|
|
|
keystore, err := lkrepo.KeyStore()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch keyInfo.Type {
|
|
|
|
case lp2p.KTLibp2pHost:
|
|
|
|
if err := keystore.Put(lp2p.KLibp2pHost, keyInfo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sk, err := crypto.UnmarshalPrivateKey(keyInfo.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
peerid, err := peer.IDFromPrivateKey(sk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%s\n", peerid.String())
|
|
|
|
|
|
|
|
break
|
2020-10-11 18:12:01 +00:00
|
|
|
case types.KTSecp256k1, types.KTBLS:
|
2020-06-29 19:41:43 +00:00
|
|
|
w, err := wallet.NewWallet(keystore)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-04 20:17:28 +00:00
|
|
|
addr, err := w.WalletImport(cctx.Context, &keyInfo)
|
2020-06-29 19:41:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%s\n", addr.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var keyinfoInfoCmd = &cli.Command{
|
|
|
|
Name: "info",
|
|
|
|
Usage: "print information about a keyinfo file",
|
|
|
|
Description: `The info command prints additional information about a key which can't easily
|
|
|
|
be retrieved by inspecting the file itself.
|
|
|
|
|
|
|
|
The 'format' flag takes a golang text/template template as its value.
|
|
|
|
|
2020-08-20 04:49:10 +00:00
|
|
|
The following fields can be retrieved through this command
|
2020-06-29 19:41:43 +00:00
|
|
|
Type
|
|
|
|
Address
|
|
|
|
PublicKey
|
|
|
|
|
|
|
|
The PublicKey value will be printed base64 encoded using golangs StdEncoding
|
|
|
|
|
|
|
|
Examples
|
|
|
|
|
2020-08-20 04:49:10 +00:00
|
|
|
Retrieve the address of a lotus wallet
|
2020-06-29 19:41:43 +00:00
|
|
|
lotus-shed keyinfo info --format '{{ .Address }}' wallet.keyinfo
|
|
|
|
`,
|
2019-12-09 15:09:23 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "format",
|
2020-06-29 19:41:43 +00:00
|
|
|
Value: "{{ .Type }} {{ .Address }}",
|
|
|
|
Usage: "specify which output columns to print",
|
2019-12-09 15:09:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
format := cctx.String("format")
|
|
|
|
|
|
|
|
var input io.Reader
|
|
|
|
if cctx.Args().Len() == 0 {
|
|
|
|
input = os.Stdin
|
|
|
|
} else {
|
2020-06-29 19:41:43 +00:00
|
|
|
var err error
|
2020-07-23 10:21:13 +00:00
|
|
|
inputFile, err := os.Open(cctx.Args().First())
|
2020-06-29 19:41:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-20 04:49:10 +00:00
|
|
|
defer inputFile.Close() //nolint:errcheck
|
2020-07-23 10:21:13 +00:00
|
|
|
input = bufio.NewReader(inputFile)
|
2019-12-09 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 19:41:43 +00:00
|
|
|
encoded, err := ioutil.ReadAll(input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-09 15:09:23 +00:00
|
|
|
|
2020-06-29 19:41:43 +00:00
|
|
|
decoded, err := hex.DecodeString(strings.TrimSpace(string(encoded)))
|
2019-12-09 15:09:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-29 19:41:43 +00:00
|
|
|
var keyInfo types.KeyInfo
|
|
|
|
if err := json.Unmarshal(decoded, &keyInfo); err != nil {
|
2019-12-09 15:09:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-29 19:41:43 +00:00
|
|
|
var kio keyInfoOutput
|
|
|
|
|
|
|
|
switch keyInfo.Type {
|
|
|
|
case lp2p.KTLibp2pHost:
|
|
|
|
kio.Type = keyInfo.Type
|
|
|
|
|
|
|
|
sk, err := crypto.UnmarshalPrivateKey(keyInfo.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pk := sk.GetPublic()
|
|
|
|
|
|
|
|
peerid, err := peer.IDFromPrivateKey(sk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pkBytes, err := pk.Raw()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
kio.Address = peerid.String()
|
|
|
|
kio.PublicKey = base64.StdEncoding.EncodeToString(pkBytes)
|
|
|
|
|
|
|
|
break
|
2020-10-11 18:12:01 +00:00
|
|
|
case types.KTSecp256k1, types.KTBLS:
|
2020-06-29 19:41:43 +00:00
|
|
|
kio.Type = keyInfo.Type
|
|
|
|
|
2022-06-14 17:21:34 +00:00
|
|
|
key, err := key.NewKey(keyInfo)
|
2020-06-29 19:41:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
kio.Address = key.Address.String()
|
|
|
|
kio.PublicKey = base64.StdEncoding.EncodeToString(key.PublicKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := template.New("output").Parse(format)
|
2019-12-09 15:09:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-29 19:41:43 +00:00
|
|
|
return tmpl.Execute(os.Stdout, kio)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var keyinfoNewCmd = &cli.Command{
|
|
|
|
Name: "new",
|
|
|
|
Usage: "create a new keyinfo file of the provided type",
|
|
|
|
ArgsUsage: "[bls|secp256k1|libp2p-host]",
|
|
|
|
Description: `Keyinfo files are base16 encoded json structures containing a type
|
|
|
|
string value, and a base64 encoded private key.
|
|
|
|
|
|
|
|
Both the bls and secp256k1 keyfiles can be imported into a running lotus daemon using
|
|
|
|
the 'lotus wallet import' command. Or imported to a non-running / unitialized repo using
|
|
|
|
the 'lotus-shed keyinfo import' command. Libp2p host keys can only be imported using lotus-shed
|
|
|
|
as lotus itself does not provide this functionality at the moment.`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "output",
|
|
|
|
Value: "<type>-<addr>.keyinfo",
|
|
|
|
Usage: "output file formt",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "silent",
|
|
|
|
Value: false,
|
|
|
|
Usage: "do not print the address to stdout",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if !cctx.Args().Present() {
|
|
|
|
return fmt.Errorf("please specify a type to generate")
|
|
|
|
}
|
|
|
|
|
2020-10-11 18:12:01 +00:00
|
|
|
keyType := types.KeyType(cctx.Args().First())
|
2020-06-29 19:41:43 +00:00
|
|
|
flagOutput := cctx.String("output")
|
|
|
|
|
|
|
|
if i := SliceIndex(len(validTypes), func(i int) bool {
|
|
|
|
if keyType == validTypes[i] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}); i == -1 {
|
|
|
|
return fmt.Errorf("invalid key type argument provided '%s'", keyType)
|
|
|
|
}
|
|
|
|
|
|
|
|
keystore := wallet.NewMemKeyStore()
|
|
|
|
|
|
|
|
var keyAddr string
|
|
|
|
var keyInfo types.KeyInfo
|
|
|
|
|
|
|
|
switch keyType {
|
|
|
|
case lp2p.KTLibp2pHost:
|
|
|
|
sk, err := lp2p.PrivKey(keystore)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ki, err := keystore.Get(lp2p.KLibp2pHost)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
peerid, err := peer.IDFromPrivateKey(sk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
keyAddr = peerid.String()
|
|
|
|
keyInfo = ki
|
|
|
|
|
|
|
|
break
|
2020-10-11 18:12:01 +00:00
|
|
|
case types.KTSecp256k1, types.KTBLS:
|
2022-06-14 17:21:34 +00:00
|
|
|
key, err := key.GenerateKey(keyType)
|
2020-06-29 19:41:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
keyAddr = key.Address.String()
|
|
|
|
keyInfo = key.KeyInfo
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := flagOutput
|
|
|
|
filename = strings.ReplaceAll(filename, "<addr>", keyAddr)
|
2020-10-11 18:12:01 +00:00
|
|
|
filename = strings.ReplaceAll(filename, "<type>", string(keyType))
|
2020-06-29 19:41:43 +00:00
|
|
|
|
2020-08-19 19:18:36 +00:00
|
|
|
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
2019-12-09 15:09:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-29 19:41:43 +00:00
|
|
|
defer func() {
|
|
|
|
if err := file.Close(); err != nil {
|
2021-02-11 11:00:26 +00:00
|
|
|
log.Warnf("failed to close output file: %v", err)
|
2020-06-29 19:41:43 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
bytes, err := json.Marshal(keyInfo)
|
|
|
|
if err != nil {
|
2019-12-09 15:09:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-29 19:41:43 +00:00
|
|
|
encoded := hex.EncodeToString(bytes)
|
|
|
|
if _, err := file.Write([]byte(encoded)); err != nil {
|
2019-12-09 15:09:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-29 19:41:43 +00:00
|
|
|
if !cctx.Bool("silent") {
|
|
|
|
fmt.Println(keyAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-12-09 15:09:23 +00:00
|
|
|
},
|
|
|
|
}
|
2020-06-29 19:41:43 +00:00
|
|
|
|
|
|
|
func SliceIndex(length int, fn func(i int) bool) int {
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
if fn(i) {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1
|
|
|
|
}
|