Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add create ledger wallet address by account index command #8657

Merged
merged 5 commits into from
May 27, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions cmd/lotus-shed/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -28,6 +29,7 @@ var ledgerCmd = &cli.Command{
ledgerKeyInfoCmd,
ledgerSignTestCmd,
ledgerShowCmd,
ledgerNewAddressesCmd,
},
}

Expand Down Expand Up @@ -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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CodeQL probably wants you to use

Suggested change
index, err := strconv.Atoi(cctx.Args().First())
index, err := strconv.ParseUint(cctx.Args().First(), 10, 32)

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
},
}