A place for all the lotus tools

This commit is contained in:
Travis Person 2019-12-09 16:09:23 +01:00
parent dec458718f
commit 8d42ca4933
5 changed files with 330 additions and 0 deletions

52
cmd/lotus-shed/base16.go Normal file
View File

@ -0,0 +1,52 @@
package main
import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"gopkg.in/urfave/cli.v2"
)
var base16Cmd = &cli.Command{
Name: "base16",
Description: "standard hex",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "decode",
Value: false,
Usage: "Decode the value",
},
},
Action: func(cctx *cli.Context) error {
var input io.Reader
if cctx.Args().Len() == 0 {
input = os.Stdin
} else {
input = strings.NewReader(cctx.Args().First())
}
bytes, err := ioutil.ReadAll(input)
if err != nil {
return nil
}
if cctx.Bool("decode") {
decoded, err := hex.DecodeString(strings.TrimSpace(string(bytes)))
if err != nil {
return err
}
fmt.Println(string(decoded))
} else {
encoded := hex.EncodeToString(bytes)
fmt.Println(encoded)
}
return nil
},
}

53
cmd/lotus-shed/base32.go Normal file
View File

@ -0,0 +1,53 @@
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"gopkg.in/urfave/cli.v2"
"github.com/multiformats/go-base32"
)
var base32Cmd = &cli.Command{
Name: "base32",
Description: "multiformats base32",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "decode",
Value: false,
Usage: "Decode the multiformats base32",
},
},
Action: func(cctx *cli.Context) error {
var input io.Reader
if cctx.Args().Len() == 0 {
input = os.Stdin
} else {
input = strings.NewReader(cctx.Args().First())
}
bytes, err := ioutil.ReadAll(input)
if err != nil {
return nil
}
if cctx.Bool("decode") {
decoded, err := base32.RawStdEncoding.DecodeString(strings.TrimSpace(string(bytes)))
if err != nil {
return err
}
fmt.Println(string(decoded))
} else {
encoded := base32.RawStdEncoding.EncodeToString(bytes)
fmt.Println(encoded)
}
return nil
},
}

84
cmd/lotus-shed/keyinfo.go Normal file
View File

@ -0,0 +1,84 @@
package main
import (
"encoding/hex"
"encoding/json"
"io"
"io/ioutil"
"os"
"strings"
"text/template"
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
)
type walletInfo struct {
Type string
Address string
PublicKey string
}
func (wi walletInfo) String() string {
bs, _ := json.Marshal(wi)
return string(bs)
}
var keyinfoCmd = &cli.Command{
Name: "keyinfo",
Description: "decode a keyinfo",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Value: "{{.Address}}",
Usage: "Format to output",
},
},
Action: func(cctx *cli.Context) error {
format := cctx.String("format")
var input io.Reader
if cctx.Args().Len() == 0 {
input = os.Stdin
} else {
input = strings.NewReader(cctx.Args().First())
}
bytes, err := ioutil.ReadAll(input)
data, err := hex.DecodeString(strings.TrimSpace(string(bytes)))
if err != nil {
return err
}
var ki types.KeyInfo
if err := json.Unmarshal(data, &ki); err != nil {
return err
}
key, err := wallet.NewKey(ki)
if err != nil {
return err
}
bs, err := json.Marshal(key)
if err != nil {
return err
}
var wi walletInfo
if err := json.Unmarshal(bs, &wi); err != nil {
return err
}
tmpl, err := template.New("").Parse(format)
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, wi)
},
}

36
cmd/lotus-shed/main.go Normal file
View File

@ -0,0 +1,36 @@
package main
import (
"os"
logging "github.com/ipfs/go-log"
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/lotus/build"
)
var log = logging.Logger("lotus-shed")
func main() {
logging.SetLogLevel("*", "INFO")
local := []*cli.Command{
base32Cmd,
base16Cmd,
keyinfoCmd,
peerkeyCmd,
}
app := &cli.App{
Name: "lotus-shed",
Usage: "A place for all the lotus tools",
Version: build.Version,
Commands: local,
}
if err := app.Run(os.Args); err != nil {
log.Warnf("%+v", err)
os.Exit(1)
return
}
}

105
cmd/lotus-shed/peerkey.go Normal file
View File

@ -0,0 +1,105 @@
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/node/modules/lp2p"
"github.com/libp2p/go-libp2p-core/peer"
)
type keystore struct {
set bool
info types.KeyInfo
}
func (ks *keystore) Put(name string, info types.KeyInfo) error {
ks.info = info
ks.set = true
return nil
}
func (ks *keystore) Get(name string) (types.KeyInfo, error) {
if !ks.set {
return types.KeyInfo{}, types.ErrKeyInfoNotFound
}
return ks.info, nil
}
func (ks *keystore) Delete(name string) error {
panic("Implement me")
return nil
}
func (ks *keystore) List() ([]string, error) {
panic("Implement me")
return []string{}, nil
}
var peerkeyCmd = &cli.Command{
Name: "peerkey",
Description: "create libp2p host key",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output",
Value: "<peerid>.peerkey",
Usage: "Output file format",
},
&cli.BoolFlag{
Name: "silent",
Value: false,
Usage: "Do not print peerid at end",
},
},
Action: func(cctx *cli.Context) error {
output := cctx.String("output")
ks := keystore{}
sk, err := lp2p.PrivKey(&ks)
if err != nil {
return err
}
bs, err := json.Marshal(ks.info)
if err != nil {
return err
}
peerid, err := peer.IDFromPrivateKey(sk)
if err != nil {
return err
}
output = strings.ReplaceAll(output, "<peerid>", peerid.String())
f, err := os.Create(output)
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
log.Warnf("failed to close output file: %w", err)
}
}()
if _, err := f.Write(bs); err != nil {
return err
}
if !cctx.Bool("silent") {
fmt.Println(peerid.String())
}
return nil
},
}