2018-03-22 12:31:12 +00:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
2019-07-26 14:45:10 +00:00
|
|
|
"encoding/json"
|
2018-03-22 12:31:12 +00:00
|
|
|
"errors"
|
2019-07-26 14:45:10 +00:00
|
|
|
"fmt"
|
2018-03-22 12:31:12 +00:00
|
|
|
|
2019-12-11 13:59:37 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2024-02-19 12:53:01 +00:00
|
|
|
"github.com/status-im/status-go/extkeys"
|
2020-12-10 13:38:58 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts"
|
2018-03-22 12:31:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// errors
|
|
|
|
var (
|
2019-07-24 18:59:15 +00:00
|
|
|
ErrInvalidAccountAddressOrKey = errors.New("cannot parse address or key to valid account address")
|
|
|
|
ErrInvalidMnemonicPhraseLength = errors.New("invalid mnemonic phrase length; valid lengths are 12, 15, 18, 21, and 24")
|
2018-03-22 12:31:12 +00:00
|
|
|
)
|
|
|
|
|
2019-07-26 14:45:10 +00:00
|
|
|
type LoginParams struct {
|
2020-12-15 18:00:31 +00:00
|
|
|
ChatAddress types.Address `json:"chatAddress"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
MainAccount types.Address `json:"mainAccount"`
|
|
|
|
WatchAddresses []types.Address `json:"watchAddresses"`
|
2020-12-10 13:38:58 +00:00
|
|
|
MultiAccount *multiaccounts.Account `json:"multiAccount"`
|
2019-07-26 14:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ErrZeroAddress struct {
|
|
|
|
field string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrZeroAddress) Error() string {
|
|
|
|
return fmt.Sprintf("%s contains an empty address", e.field)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newErrZeroAddress(field string) *ErrZeroAddress {
|
|
|
|
return &ErrZeroAddress{
|
|
|
|
field: field,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseLoginParams(paramsJSON string) (LoginParams, error) {
|
|
|
|
var (
|
|
|
|
params LoginParams
|
2019-12-11 13:59:37 +00:00
|
|
|
zeroAddress types.Address
|
2019-07-26 14:45:10 +00:00
|
|
|
)
|
|
|
|
if err := json.Unmarshal([]byte(paramsJSON), ¶ms); err != nil {
|
|
|
|
return params, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.ChatAddress == zeroAddress {
|
|
|
|
return params, newErrZeroAddress("ChatAddress")
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.MainAccount == zeroAddress {
|
|
|
|
return params, newErrZeroAddress("MainAccount")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, address := range params.WatchAddresses {
|
|
|
|
if address == zeroAddress {
|
|
|
|
return params, newErrZeroAddress("WatchAddresses")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return params, nil
|
|
|
|
}
|
|
|
|
|
2019-01-18 09:01:14 +00:00
|
|
|
// Info contains wallet and chat addresses and public keys of an account.
|
|
|
|
type Info struct {
|
|
|
|
WalletAddress string
|
|
|
|
WalletPubKey string
|
|
|
|
ChatAddress string
|
|
|
|
ChatPubKey string
|
|
|
|
}
|
|
|
|
|
2018-03-22 12:31:12 +00:00
|
|
|
// SelectedExtKey is a container for the selected (logged in) external account.
|
|
|
|
type SelectedExtKey struct {
|
2019-12-11 13:59:37 +00:00
|
|
|
Address types.Address
|
2019-12-19 18:27:27 +00:00
|
|
|
AccountKey *types.Key
|
|
|
|
SubAccounts []types.Account
|
2018-03-22 12:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hex dumps address of a given extended key as hex string.
|
|
|
|
func (k *SelectedExtKey) Hex() string {
|
|
|
|
if k == nil {
|
|
|
|
return "0x0"
|
|
|
|
}
|
|
|
|
|
|
|
|
return k.Address.Hex()
|
|
|
|
}
|
|
|
|
|
2019-12-19 18:27:27 +00:00
|
|
|
// ParseAccountString parses hex encoded string and returns is as types.Account.
|
|
|
|
func ParseAccountString(account string) (types.Account, error) {
|
2018-03-22 12:31:12 +00:00
|
|
|
// valid address, convert to account
|
2019-12-11 13:59:37 +00:00
|
|
|
if types.IsHexAddress(account) {
|
2019-12-19 18:27:27 +00:00
|
|
|
return types.Account{Address: types.HexToAddress(account)}, nil
|
2018-03-22 12:31:12 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 18:27:27 +00:00
|
|
|
return types.Account{}, ErrInvalidAccountAddressOrKey
|
2018-03-22 12:31:12 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:59:37 +00:00
|
|
|
// FromAddress converts account address from string to types.Address.
|
|
|
|
// The function is useful to format "From" field of send transaction struct.
|
|
|
|
func FromAddress(accountAddress string) types.Address {
|
|
|
|
from, err := ParseAccountString(accountAddress)
|
|
|
|
if err != nil {
|
|
|
|
return types.Address{}
|
|
|
|
}
|
|
|
|
|
2019-12-19 18:27:27 +00:00
|
|
|
return from.Address
|
2019-12-11 13:59:37 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 18:27:27 +00:00
|
|
|
// ToAddress converts account address from string to *common.Address.
|
2018-03-22 12:31:12 +00:00
|
|
|
// The function is useful to format "To" field of send transaction struct.
|
2019-12-19 18:27:27 +00:00
|
|
|
func ToAddress(accountAddress string) *types.Address {
|
2018-03-22 12:31:12 +00:00
|
|
|
to, err := ParseAccountString(accountAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &to.Address
|
|
|
|
}
|
2024-02-19 12:53:01 +00:00
|
|
|
|
|
|
|
func GetRandomMnemonic() (string, error) {
|
|
|
|
// generate mnemonic phrase
|
|
|
|
mn := extkeys.NewMnemonic()
|
|
|
|
mnemonic, err := mn.MnemonicPhrase(extkeys.EntropyStrength128, extkeys.EnglishLanguage)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("can not create mnemonic seed: %v", err)
|
|
|
|
}
|
|
|
|
return mnemonic, nil
|
|
|
|
}
|