mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-11-24 14:35:47 +00:00
feat: add check for initialize-trust-wallet if provided mnemonic conforms to bip39
Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
parent
ecb5cf553c
commit
35abc9518f
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/cosmos/go-bip39"
|
||||||
"github.com/planetmint/planetmint-go/lib/trustwallet"
|
"github.com/planetmint/planetmint-go/lib/trustwallet"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@ -27,7 +28,7 @@ func initializeTrustWalletCmdFunc(cmd *cobra.Command, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// if no mnemonic is given create one otherwise try to recover from given mnemonic
|
// if no mnemonic is given create one
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
fmt.Println("initializing Trust Wallet")
|
fmt.Println("initializing Trust Wallet")
|
||||||
mnemonic, err := connector.CreateMnemonic()
|
mnemonic, err := connector.CreateMnemonic()
|
||||||
@ -35,21 +36,24 @@ func initializeTrustWalletCmdFunc(cmd *cobra.Command, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println(mnemonic)
|
fmt.Println(mnemonic)
|
||||||
} else {
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println("recovering Trust Wallet from mnemonic...")
|
fmt.Println("recovering Trust Wallet from mnemonic...")
|
||||||
words := strings.Split(args[0], ",")
|
words := strings.Split(args[0], ",")
|
||||||
if len(words) != 12 && len(words) != 24 {
|
if len(words) != 12 && len(words) != 24 {
|
||||||
return fmt.Errorf("expected length of mnemonic is 12 or 24, got: %d", len(words))
|
return fmt.Errorf("expected length of mnemonic is 12 or 24, got: %d", len(words))
|
||||||
}
|
}
|
||||||
// TODO: check if provided words are valid
|
|
||||||
mnemonic := strings.Join(words, " ")
|
mnemonic := strings.Join(words, " ")
|
||||||
fmt.Println("Mnemonic: ", mnemonic) // TODO: remove before merging
|
if !bip39.IsMnemonicValid(mnemonic) {
|
||||||
|
return fmt.Errorf("invalid mnemonic, please check provided words")
|
||||||
|
}
|
||||||
response, err := connector.RecoverFromMnemonic(mnemonic)
|
response, err := connector.RecoverFromMnemonic(mnemonic)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println(response)
|
fmt.Println(response)
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user