github.com/status-im/status-go@v1.1.0/protocol/requests/login.go (about)

     1  package requests
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	"errors"
     6  	"strings"
     7  
     8  	"github.com/status-im/status-go/eth-node/crypto"
     9  )
    10  
    11  var (
    12  	ErrLoginInvalidKeyUID                   = errors.New("login: invalid key-uid")
    13  	ErrLoginInvalidKeycardWhisperPrivateKey = errors.New("login: invalid keycard whisper private key")
    14  )
    15  
    16  type Login struct {
    17  	Password string `json:"password"`
    18  	KeyUID   string `json:"keyUid"`
    19  
    20  	KdfIterations         int    `json:"kdfIterations"` // FIXME: KdfIterations should be loaded from multiaccounts db.
    21  	RuntimeLogLevel       string `json:"runtimeLogLevel"`
    22  	WakuV2Nameserver      string `json:"wakuV2Nameserver"`
    23  	BandwidthStatsEnabled bool   `json:"bandwidthStatsEnabled"`
    24  
    25  	KeycardWhisperPrivateKey string `json:"keycardWhisperPrivateKey"`
    26  
    27  	// Mnemonic allows to log in to an account when password is lost.
    28  	// This is needed for the "Lost keycard -> Start using without keycard" flow, when a keycard account database
    29  	// exists locally, but now the keycard is lost. In this case client is responsible for calling
    30  	// `convertToRegularAccount` after a successful login. This could be improved in the future.
    31  	// When non-empty, mnemonic is used to generate required keypairs and:
    32  	// - Password is ignored and replaced with encryption public key
    33  	// - KeycardWhisperPrivateKey is ignored and replaced with chat private key
    34  	Mnemonic string `json:"mnemonic"`
    35  
    36  	WalletSecretsConfig
    37  
    38  	APIConfig          *APIConfig `json:"apiConfig"`
    39  	StatusProxyEnabled bool       `json:"statusProxyEnabled"`
    40  }
    41  
    42  func (c *Login) Validate() error {
    43  	if c.KeyUID == "" {
    44  		return ErrLoginInvalidKeyUID
    45  	}
    46  
    47  	if c.KeycardWhisperPrivateKey != "" {
    48  		_, err := parsePrivateKey(c.KeycardWhisperPrivateKey)
    49  		if err != nil {
    50  			return ErrLoginInvalidKeycardWhisperPrivateKey
    51  		}
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  func (c *Login) ChatPrivateKey() *ecdsa.PrivateKey {
    58  	// Skip error check, as it's already validated in Validate
    59  	privateKey, _ := parsePrivateKey(c.KeycardWhisperPrivateKey)
    60  	return privateKey
    61  }
    62  
    63  func parsePrivateKey(privateKeyHex string) (*ecdsa.PrivateKey, error) {
    64  	privateKeyHex = strings.TrimPrefix(privateKeyHex, "0x")
    65  	return crypto.HexToECDSA(privateKeyHex)
    66  }