From add6d5d2d5548a5ddd04f8411c8cd05d18fb9a05 Mon Sep 17 00:00:00 2001 From: frank Date: Tue, 10 May 2022 17:02:06 +0800 Subject: [PATCH] add new ledger address with index cmd --- cmd/lotus-shed/ledger.go | 71 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/cmd/lotus-shed/ledger.go b/cmd/lotus-shed/ledger.go index 0e9c11742..1ddae231a 100644 --- a/cmd/lotus-shed/ledger.go +++ b/cmd/lotus-shed/ledger.go @@ -17,6 +17,7 @@ import ( "github.com/filecoin-project/lotus/chain/types" ledgerwallet "github.com/filecoin-project/lotus/chain/wallet/ledger" lcli "github.com/filecoin-project/lotus/cli" + cliutil "github.com/filecoin-project/lotus/cli/util" ) var ledgerCmd = &cli.Command{ @@ -28,6 +29,7 @@ var ledgerCmd = &cli.Command{ ledgerKeyInfoCmd, ledgerSignTestCmd, ledgerShowCmd, + ledgerNewAddressesCmd, }, } @@ -291,3 +293,72 @@ var ledgerShowCmd = &cli.Command{ return nil }, } + +var ledgerNewAddressesCmd = &cli.Command{ + Name: "new", + Flags: []cli.Flag{}, + Action: func(cctx *cli.Context) error { + ctx := lcli.ReqContext(cctx) + + if cctx.NArg() != 1 { + return fmt.Errorf("must pass account index") + } + + index, err := strconv.Atoi(cctx.Args().First()) + if err != nil { + return err + } + + if index < 0 { + return fmt.Errorf("account index must greater than 0") + } + + api, closer, err := cliutil.GetFullNodeAPI(cctx) + if err != nil { + return err + } + defer closer() + + fl, err := ledgerfil.FindLedgerFilecoinApp() + if err != nil { + return err + } + defer fl.Close() // nolint + + if err := ctx.Err(); err != nil { + return err + } + + p := []uint32{hdHard | 44, hdHard | 461, hdHard, 0, uint32(index)} + pubk, err := fl.GetPublicKeySECP256K1(p) + if err != nil { + return err + } + + addr, err := address.NewSecp256k1Address(pubk) + if err != nil { + return err + } + + var pd ledgerwallet.LedgerKeyInfo + pd.Address = addr + pd.Path = p + + b, err := json.Marshal(pd) + if err != nil { + return err + } + + var ki types.KeyInfo + ki.Type = types.KTSecp256k1Ledger + ki.PrivateKey = b + + _, err = api.WalletImport(ctx, &ki) + if err != nil { + return err + } + + fmt.Printf("%s %s\n", addr, printHDPath(p)) + return nil + }, +}