Skip to content

Commit

Permalink
chore: specify optional flag to connect rpc node to store keyname map…
Browse files Browse the repository at this point in the history
…ping
  • Loading branch information
RafilxTenfen committed Dec 23, 2024
1 parent 909aaf8 commit 0cbe284
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions eotsmanager/cmd/eotsd/daemon/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package daemon
const (
forceFlag = "force"
rpcListenerFlag = "rpc-listener"
rpcClientFlag = "rpc-client"
flagInteractive = "interactive"
flagNoBackup = "no-backup"
flagMultisig = "multisig"
Expand Down
20 changes: 16 additions & 4 deletions eotsmanager/cmd/eotsd/daemon/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func NewKeysCmd() *cobra.Command {
panic("failed to find keys add command")
}

addCmd.Flags().String(rpcClientFlag, "", "The RPC address of a running eotsd to connect and save new key")

// Override the original RunE function to run almost the same as
// the sdk, but it allows empty hd path and allow to save the key
// in the name mapping
Expand Down Expand Up @@ -75,6 +77,8 @@ func saveKeyOnPostRun(cmd *cobra.Command, commandName string) {
panic(fmt.Sprintf("failed to find keys %s command", commandName))
}

subCmd.Flags().String(rpcClientFlag, "", "The RPC address of a running eotsd to connect and save new key")

subCmd.PostRunE = func(cmd *cobra.Command, args []string) error {
keyName := args[0]
_, err := saveKeyNameMapping(cmd, keyName)
Expand All @@ -94,10 +98,17 @@ func saveKeyNameMapping(cmd *cobra.Command, keyName string) (*types.BIP340PubKey
return nil, fmt.Errorf("failed to load config: %w", err)
}

client, err := eotsclient.NewEOTSManagerGRpcClient(cfg.RPCListener)
if err == nil {
// if it can connect to the server, it means the server is running
// so the db is in use and we should send to the client to save the new key mapping
rpcListener, err := cmd.Flags().GetString(rpcClientFlag)
if err != nil {
return nil, err
}

if len(rpcListener) > 0 {
client, err := eotsclient.NewEOTSManagerGRpcClient(rpcListener)
if err != nil {
return nil, err
}

kr, err := eotsmanager.InitKeyring(clientCtx.HomeDir, clientCtx.Keyring.Backend(), strings.NewReader(""))
if err != nil {
return nil, fmt.Errorf("failed to init keyring: %w", err)
Expand All @@ -111,6 +122,7 @@ func saveKeyNameMapping(cmd *cobra.Command, keyName string) (*types.BIP340PubKey
if err := client.SaveEOTSKeyName(eotsPk.MustToBTCPK(), keyName); err != nil {
return nil, fmt.Errorf("failed to save key name mapping: %w", err)
}

return eotsPk, nil
}

Expand Down

0 comments on commit 0cbe284

Please sign in to comment.