Skip to content

Commit

Permalink
chore: add show key command that prints the pub key (#49)
Browse files Browse the repository at this point in the history
* chore: add show key command that prints the pub key

* chore: address pr comments

* chore: add #49 to changelog
  • Loading branch information
RafilxTenfen authored Nov 29, 2024
1 parent 7bf736a commit 8d23c16
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ with encrypted file keyring usage
through API
* [#48](https://github.com/babylonlabs-io/covenant-emulator/pull/48) Add covenant
signer version that requires unlocking
* [#49](https://github.com/babylonlabs-io/covenant-emulator/pull/49) Add show-key command

## v0.9.0

Expand Down
72 changes: 71 additions & 1 deletion cmd/covd/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"

"github.com/babylonlabs-io/babylon/types"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/jessevdk/go-flags"

"github.com/urfave/cli"

covcfg "github.com/babylonlabs-io/covenant-emulator/config"
Expand All @@ -29,7 +31,7 @@ var createKeyCommand = cli.Command{
},
cli.StringFlag{
Name: keyNameFlag,
Usage: "The unique name of the Covenant key",
Usage: "The unique name of the covenant key",
Required: true,
},
cli.StringFlag{
Expand Down Expand Up @@ -99,6 +101,74 @@ func createKey(ctx *cli.Context) error {
return flags.NewIniParser(fileParser).WriteFile(covcfg.ConfigFile(homePath), flags.IniIncludeComments|flags.IniIncludeDefaults)
}

var showKeyCommand = cli.Command{
Name: "show-key",
ShortName: "sk",
Usage: "Show a Covenant account in the keyring.",
Flags: []cli.Flag{
cli.StringFlag{
Name: chainIdFlag,
Usage: "The chainID of the consumer chain",
Value: defaultChainID,
},
cli.StringFlag{
Name: keyNameFlag,
Usage: "The name of the covenant key",
Required: true,
},
cli.StringFlag{
Name: passphraseFlag,
Usage: "The pass phrase used to decrypt the key",
Value: defaultPassphrase,
},
cli.StringFlag{
Name: keyringBackendFlag,
Usage: "Select keyring's backend",
Value: defaultKeyringBackend,
},
cli.StringFlag{
Name: homeFlag,
Usage: "The home directory for the covenant",
Value: covcfg.DefaultCovenantDir,
},
},
Action: showKey,
}

func showKey(ctx *cli.Context) error {
homePath := ctx.String(homeFlag)
chainID := ctx.String(chainIdFlag)
keyName := ctx.String(keyNameFlag)
backend := ctx.String(keyringBackendFlag)
passphrase := ctx.String(passphraseFlag)

sdkCtx, err := keyring.CreateClientCtx(homePath, chainID)
if err != nil {
return err
}

krController, err := keyring.NewChainKeyringController(sdkCtx, keyName, backend)
if err != nil {
return err
}

privKey, err := krController.GetChainPrivKey(passphrase)
if err != nil {
return err
}

_, pk := btcec.PrivKeyFromBytes(privKey.Key)
bip340Key := types.NewBIP340PubKeyFromBTCPK(pk)
printRespJSON(
&covenantKey{
Name: ctx.String(keyNameFlag),
PublicKey: bip340Key.MarshalHex(),
},
)

return nil
}

func printRespJSON(resp interface{}) {
jsonBytes, err := json.MarshalIndent(resp, "", " ")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/covd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {
app := cli.NewApp()
app.Name = "covd"
app.Usage = "Covenant Emulator Daemon (covd)."
app.Commands = append(app.Commands, startCommand, initCommand, createKeyCommand)
app.Commands = append(app.Commands, startCommand, initCommand, createKeyCommand, showKeyCommand)

if err := app.Run(os.Args); err != nil {
fatal(err)
Expand Down

0 comments on commit 8d23c16

Please sign in to comment.