github.com/status-im/status-go@v1.1.0/account/utils.go (about) 1 package account 2 3 import ( 4 "errors" 5 "fmt" 6 7 "github.com/status-im/status-go/eth-node/types" 8 "github.com/status-im/status-go/extkeys" 9 "github.com/status-im/status-go/multiaccounts" 10 ) 11 12 // errors 13 var ( 14 ErrInvalidAccountAddressOrKey = errors.New("cannot parse address or key to valid account address") 15 ErrInvalidMnemonicPhraseLength = errors.New("invalid mnemonic phrase length; valid lengths are 12, 15, 18, 21, and 24") 16 ) 17 18 type LoginParams struct { 19 ChatAddress types.Address `json:"chatAddress"` 20 Password string `json:"password"` 21 MainAccount types.Address `json:"mainAccount"` 22 WatchAddresses []types.Address `json:"watchAddresses"` 23 MultiAccount *multiaccounts.Account `json:"multiAccount"` 24 } 25 26 type ErrZeroAddress struct { 27 field string 28 } 29 30 func (e *ErrZeroAddress) Error() string { 31 return fmt.Sprintf("%s contains an empty address", e.field) 32 } 33 34 // Info contains wallet and chat addresses and public keys of an account. 35 type Info struct { 36 WalletAddress string 37 WalletPubKey string 38 ChatAddress string 39 ChatPubKey string 40 } 41 42 // SelectedExtKey is a container for the selected (logged in) external account. 43 type SelectedExtKey struct { 44 Address types.Address 45 AccountKey *types.Key 46 SubAccounts []types.Account 47 } 48 49 // Hex dumps address of a given extended key as hex string. 50 func (k *SelectedExtKey) Hex() string { 51 if k == nil { 52 return "0x0" 53 } 54 55 return k.Address.Hex() 56 } 57 58 // ParseAccountString parses hex encoded string and returns is as types.Account. 59 func ParseAccountString(account string) (types.Account, error) { 60 // valid address, convert to account 61 if types.IsHexAddress(account) { 62 return types.Account{Address: types.HexToAddress(account)}, nil 63 } 64 65 return types.Account{}, ErrInvalidAccountAddressOrKey 66 } 67 68 // FromAddress converts account address from string to types.Address. 69 // The function is useful to format "From" field of send transaction struct. 70 func FromAddress(accountAddress string) types.Address { 71 from, err := ParseAccountString(accountAddress) 72 if err != nil { 73 return types.Address{} 74 } 75 76 return from.Address 77 } 78 79 // ToAddress converts account address from string to *common.Address. 80 // The function is useful to format "To" field of send transaction struct. 81 func ToAddress(accountAddress string) *types.Address { 82 to, err := ParseAccountString(accountAddress) 83 if err != nil { 84 return nil 85 } 86 87 return &to.Address 88 } 89 90 func GetRandomMnemonic() (string, error) { 91 // generate mnemonic phrase 92 mn := extkeys.NewMnemonic() 93 mnemonic, err := mn.MnemonicPhrase(extkeys.EntropyStrength128, extkeys.EnglishLanguage) 94 if err != nil { 95 return "", fmt.Errorf("can not create mnemonic seed: %v", err) 96 } 97 return mnemonic, nil 98 }