2023-05-24 15:02:53 +00:00
|
|
|
package requests
|
|
|
|
|
2024-06-26 11:14:27 +00:00
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
2023-05-24 15:02:53 +00:00
|
|
|
|
2024-06-26 11:14:27 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrLoginInvalidKeyUID = errors.New("login: invalid key-uid")
|
|
|
|
ErrLoginInvalidKeycardWhisperPrivateKey = errors.New("login: invalid keycard whisper private key")
|
|
|
|
)
|
2023-05-24 15:02:53 +00:00
|
|
|
|
|
|
|
type Login struct {
|
2024-03-28 15:01:44 +00:00
|
|
|
Password string `json:"password"`
|
|
|
|
KeyUID string `json:"keyUid"`
|
2023-05-24 15:02:53 +00:00
|
|
|
|
2024-06-26 11:14:27 +00:00
|
|
|
KdfIterations int `json:"kdfIterations"` // FIXME: KdfIterations should be loaded from multiaccounts db.
|
2024-06-18 13:36:53 +00:00
|
|
|
RuntimeLogLevel string `json:"runtimeLogLevel"`
|
|
|
|
WakuV2Nameserver string `json:"wakuV2Nameserver"`
|
|
|
|
BandwidthStatsEnabled bool `json:"bandwidthStatsEnabled"`
|
2023-06-28 12:07:33 +00:00
|
|
|
|
2024-06-26 11:14:27 +00:00
|
|
|
KeycardWhisperPrivateKey string `json:"keycardWhisperPrivateKey"`
|
|
|
|
|
|
|
|
// Mnemonic allows to log in to an account when password is lost.
|
|
|
|
// This is needed for the "Lost keycard -> Start using without keycard" flow, when a keycard account database
|
|
|
|
// exists locally, but now the keycard is lost. In this case client is responsible for calling
|
|
|
|
// `convertToRegularAccount` after a successful login. This could be improved in the future.
|
|
|
|
// When non-empty, mnemonic is used to generate required keypairs and:
|
|
|
|
// - Password is ignored and replaced with encryption public key
|
|
|
|
// - KeycardWhisperPrivateKey is ignored and replaced with chat private key
|
|
|
|
Mnemonic string `json:"mnemonic"`
|
|
|
|
|
2023-05-24 15:02:53 +00:00
|
|
|
WalletSecretsConfig
|
2024-06-26 14:33:22 +00:00
|
|
|
|
2024-07-31 06:21:11 +00:00
|
|
|
APIConfig *APIConfig `json:"apiConfig"`
|
|
|
|
StatusProxyEnabled bool `json:"statusProxyEnabled"`
|
2023-05-24 15:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Login) Validate() error {
|
|
|
|
if c.KeyUID == "" {
|
|
|
|
return ErrLoginInvalidKeyUID
|
|
|
|
}
|
2024-06-26 11:14:27 +00:00
|
|
|
|
|
|
|
if c.KeycardWhisperPrivateKey != "" {
|
|
|
|
_, err := parsePrivateKey(c.KeycardWhisperPrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return ErrLoginInvalidKeycardWhisperPrivateKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:02:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-06-26 11:14:27 +00:00
|
|
|
|
|
|
|
func (c *Login) ChatPrivateKey() *ecdsa.PrivateKey {
|
|
|
|
// Skip error check, as it's already validated in Validate
|
|
|
|
privateKey, _ := parsePrivateKey(c.KeycardWhisperPrivateKey)
|
|
|
|
return privateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePrivateKey(privateKeyHex string) (*ecdsa.PrivateKey, error) {
|
|
|
|
privateKeyHex = strings.TrimPrefix(privateKeyHex, "0x")
|
|
|
|
return crypto.HexToECDSA(privateKeyHex)
|
|
|
|
}
|