446 add command to initialize tw (#448)

* fix: use correct array index on OSCresponse.data
* feat: add trust-wallet initialize cmd
* feat: add trust-wallet keys subcommand
---------

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2024-09-24 15:10:26 +02:00 committed by GitHub
parent e404fef08b
commit dacd261df4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 151 additions and 10 deletions

View File

@ -129,6 +129,7 @@ func initRootCmd(
),
genutilcli.ValidateGenesisCmd(app.ModuleBasics),
AddGenesisAccountCmd(app.DefaultNodeHome),
TrustWalletCmd(),
tmcli.NewCompletionCmd(rootCmd, true),
debug.Cmd(),
config.Cmd(),

View File

@ -0,0 +1,140 @@
package cmd
import (
"errors"
"strconv"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/go-bip39"
"github.com/planetmint/planetmint-go/lib/trustwallet"
"github.com/spf13/cobra"
)
const (
flagSerialPort = "serial-port"
)
func TrustWalletCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "trust-wallet [command]",
Short: "Trust Wallet subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
cmd.AddCommand(
initializeCmd(),
keysCmd(),
)
return cmd
}
func initializeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "initialize \"[mnemonic]\"",
Short: "Initialize a Trust Wallet",
Long: `Initialize a Trust Wallet with a mnemonic phrase (optional). If no mnemonic is provided then one is created for you.
Provided mnemonics must be 12 or 24 words long and adhere to bip39.`,
RunE: initializeCmdFunc,
Args: cobra.RangeArgs(0, 1),
}
cmd.Flags().String(flagSerialPort, "/dev/ttyACM0", "The serial port your Trust Wallet is connected to")
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
func initializeCmdFunc(cmd *cobra.Command, args []string) error {
serialPort, err := cmd.Flags().GetString(flagSerialPort)
if err != nil {
return err
}
connector, err := trustwallet.NewTrustWalletConnector(serialPort)
if err != nil {
return err
}
// create mnemonic if non is given
if len(args) == 0 {
cmd.Println("Initializing Trust Wallet. This may take a few seconds...")
mnemonic, err := connector.CreateMnemonic()
if err != nil {
return err
}
cmd.Println("Created mnemonic:")
cmd.Println(mnemonic + "\n")
cmd.Println("IMPORTANT: Store your mnemonic securely in an offline location, such as a hardware wallet, encrypted USB, or paper stored in a safe, never online or on cloud storage!")
return nil
}
// recover from given mnemonic
cmd.Println("Recovering Trust Wallet from mnemonic...")
words := strings.Split(args[0], " ")
if len(words) != 12 && len(words) != 24 {
return errors.New("expected length of mnemonic is 12 or 24, got: " + strconv.Itoa(len(words)))
}
mnemonic := strings.Join(words, " ")
if !bip39.IsMnemonicValid(mnemonic) {
return errors.New("invalid mnemonic, please check provided words")
}
response, err := connector.RecoverFromMnemonic(mnemonic)
if err != nil {
return err
}
cmd.Println(response)
return nil
}
func keysCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "keys",
Short: "Retrieve keys from Trust Wallet",
Long: `Retrieve keys from Trust Wallet. Includes:
planetmint address,
extended planetmint public key,
extended liquid public key,
raw planetmint key (hex encoded)`,
RunE: keysCmdFunc,
Args: cobra.ExactArgs(0),
}
cmd.Flags().String(flagSerialPort, "/dev/ttyACM0", "The serial port your Trust Wallet is connected to")
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
func keysCmdFunc(cmd *cobra.Command, _ []string) error {
serialPort, err := cmd.Flags().GetString(flagSerialPort)
if err != nil {
return err
}
connector, err := trustwallet.NewTrustWalletConnector(serialPort)
if err != nil {
return err
}
cmd.Println("Retrieving keys from Trust Wallet. This may take a few seconds...")
cmd.Println()
keys, err := connector.GetPlanetmintKeys()
if err != nil {
return err
}
cmd.Println("- address: " + keys.PlanetmintAddress)
cmd.Printf(" pubkey: [{\"@type\":\"PlanetmintExtended\",\"key\":\"%s\"},{\"@type\":\"PlanetmintHex\",\"key\":\"%s\"},{\"@type\":\"LiquidExtended\",\"key\":\"%s\"}]\n", keys.ExtendedPlanetmintPubkey, keys.RawPlanetmintPubkey, keys.ExtendedLiquidPubkey)
cmd.Println(" type: hardware")
return nil
}

View File

@ -44,8 +44,8 @@ func (t *Connector) ValiseGet() (string, error) {
if err != nil {
return "", err
}
if len(response.Data) > 0 {
return response.Data[0], nil
if len(response.Data) > 1 {
return response.Data[1], nil
}
return "", errors.New("no data returned")
}
@ -55,8 +55,8 @@ func (t *Connector) CreateMnemonic() (string, error) {
if err != nil {
return "", err
}
if len(response.Data) > 0 {
return response.Data[0], nil
if len(response.Data) > 1 {
return response.Data[1], nil
}
return "", errors.New("no data returned")
}
@ -66,8 +66,8 @@ func (t *Connector) InjectPlanetminkeyToSE050(slot int) (bool, error) {
if err != nil {
return false, err
}
if len(response.Data) > 0 {
return response.Data[0] == "0", nil
if len(response.Data) > 1 {
return response.Data[1] == "0", nil
}
return false, errors.New("no data returned")
}
@ -77,8 +77,8 @@ func (t *Connector) RecoverFromMnemonic(mnemonic string) (string, error) {
if err != nil {
return "", err
}
if len(response.Data) > 0 {
return response.Data[0], nil
if len(response.Data) > 1 {
return response.Data[1], nil
}
return "", errors.New("no data returned")
}
@ -108,8 +108,8 @@ func (t *Connector) GetSeedSE050() (string, error) {
if err != nil {
return "", err
}
if len(response.Data) > 0 {
return response.Data[0], nil
if len(response.Data) > 1 {
return response.Data[1], nil
}
return "", errors.New("no data returned")
}