Add RestoreAccountAndLogin endpoint
This commit is contained in:
parent
ccc8c9122c
commit
31ae4c958b
|
@ -20,6 +20,7 @@ import (
|
||||||
signercore "github.com/ethereum/go-ethereum/signer/core/apitypes"
|
signercore "github.com/ethereum/go-ethereum/signer/core/apitypes"
|
||||||
|
|
||||||
"github.com/status-im/status-go/account"
|
"github.com/status-im/status-go/account"
|
||||||
|
"github.com/status-im/status-go/account/generator"
|
||||||
"github.com/status-im/status-go/appdatabase"
|
"github.com/status-im/status-go/appdatabase"
|
||||||
"github.com/status-im/status-go/connection"
|
"github.com/status-im/status-go/connection"
|
||||||
"github.com/status-im/status-go/eth-node/crypto"
|
"github.com/status-im/status-go/eth-node/crypto"
|
||||||
|
@ -711,12 +712,16 @@ func (b *GethStatusBackend) ConvertToKeycardAccount(account multiaccounts.Accoun
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *GethStatusBackend) CreateAccountAndLogin(request *requests.CreateAccount) error {
|
func (b *GethStatusBackend) RestoreAccountAndLogin(request *requests.RestoreAccount) error {
|
||||||
|
|
||||||
if err := request.Validate(); err != nil {
|
if err := request.Validate(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return b.generateOrImportAccount(request.Mnemonic, &request.CreateAccount)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *GethStatusBackend) generateOrImportAccount(mnemonic string, request *requests.CreateAccount) error {
|
||||||
if err := b.accountManager.InitKeystore(filepath.Join(request.BackupDisabledDataDir, keystoreRelativePath)); err != nil {
|
if err := b.accountManager.InitKeystore(filepath.Join(request.BackupDisabledDataDir, keystoreRelativePath)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -728,23 +733,31 @@ func (b *GethStatusBackend) CreateAccountAndLogin(request *requests.CreateAccoun
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
generator := b.accountManager.AccountsGenerator()
|
accountGenerator := b.accountManager.AccountsGenerator()
|
||||||
|
|
||||||
// generate 1(n) account with default mnemonic length and no passphrase
|
var info generator.GeneratedAccountInfo
|
||||||
generatedAccountInfos, err := generator.Generate(defaultMnemonicLength, 1, "")
|
if mnemonic == "" {
|
||||||
|
// generate 1(n) account with default mnemonic length and no passphrase
|
||||||
|
generatedAccountInfos, err := accountGenerator.Generate(defaultMnemonicLength, 1, "")
|
||||||
|
info = generatedAccountInfos[0]
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
|
info, err = accountGenerator.ImportMnemonic(mnemonic, "")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
derivedAddresses, err := accountGenerator.DeriveAddresses(info.ID, paths)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
info := generatedAccountInfos[0]
|
_, err = accountGenerator.StoreDerivedAccounts(info.ID, request.Password, paths)
|
||||||
|
|
||||||
derivedAddresses, err := generator.DeriveAddresses(info.ID, paths)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = generator.StoreDerivedAccounts(info.ID, request.Password, paths)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -760,7 +773,11 @@ func (b *GethStatusBackend) CreateAccountAndLogin(request *requests.CreateAccoun
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
settings.DisplayName = request.DisplayName
|
settings.DisplayName = request.DisplayName
|
||||||
settings.Mnemonic = &info.Mnemonic
|
|
||||||
|
// If restoring an account, we don't set the mnemonic
|
||||||
|
if mnemonic == "" {
|
||||||
|
settings.Mnemonic = &info.Mnemonic
|
||||||
|
}
|
||||||
|
|
||||||
nodeConfig, err := defaultNodeConfig(settings.InstallationID, request)
|
nodeConfig, err := defaultNodeConfig(settings.InstallationID, request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -808,6 +825,16 @@ func (b *GethStatusBackend) CreateAccountAndLogin(request *requests.CreateAccoun
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *GethStatusBackend) CreateAccountAndLogin(request *requests.CreateAccount) error {
|
||||||
|
|
||||||
|
if err := request.Validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.generateOrImportAccount("", request)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *GethStatusBackend) ConvertToRegularAccount(mnemonic string, currPassword string, newPassword string) error {
|
func (b *GethStatusBackend) ConvertToRegularAccount(mnemonic string, currPassword string, newPassword string) error {
|
||||||
|
|
|
@ -287,6 +287,31 @@ func CreateAccountAndLogin(requestJSON string) string {
|
||||||
return makeJSONResponse(nil)
|
return makeJSONResponse(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RestoreAccountAndLogin(requestJSON string) string {
|
||||||
|
var request requests.RestoreAccount
|
||||||
|
err := json.Unmarshal([]byte(requestJSON), &request)
|
||||||
|
if err != nil {
|
||||||
|
return makeJSONResponse(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = request.Validate()
|
||||||
|
if err != nil {
|
||||||
|
return makeJSONResponse(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
api.RunAsync(func() error {
|
||||||
|
log.Debug("starting a node and restoring account")
|
||||||
|
err := statusBackend.RestoreAccountAndLogin(&request)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("failed to restore account", "error", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Debug("started a node, and restored account")
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return makeJSONResponse(nil)
|
||||||
|
}
|
||||||
|
|
||||||
// SaveAccountAndLogin saves account in status-go database..
|
// SaveAccountAndLogin saves account in status-go database..
|
||||||
func SaveAccountAndLogin(accountData, password, settingsJSON, configJSON, subaccountData string) string {
|
func SaveAccountAndLogin(accountData, password, settingsJSON, configJSON, subaccountData string) string {
|
||||||
var account multiaccounts.Account
|
var account multiaccounts.Account
|
||||||
|
|
|
@ -32,6 +32,10 @@ type CreateAccount struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CreateAccount) Validate() error {
|
func (c *CreateAccount) Validate() error {
|
||||||
|
return ValidateAccountCreationRequest(*c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ValidateAccountCreationRequest(c CreateAccount) error {
|
||||||
// TODO(cammellos): Add proper validation for password/displayname/etc
|
// TODO(cammellos): Add proper validation for password/displayname/etc
|
||||||
if len(c.DisplayName) == 0 {
|
if len(c.DisplayName) == 0 {
|
||||||
return ErrCreateAccountInvalidDisplayName
|
return ErrCreateAccountInvalidDisplayName
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
package requests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ErrRestoreAccountInvalidMnemonic = errors.New("restore-account: invalid mnemonic")
|
||||||
|
|
||||||
|
type RestoreAccount struct {
|
||||||
|
Mnemonic string `json:"mnemonic"`
|
||||||
|
CreateAccount
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *RestoreAccount) Validate() error {
|
||||||
|
if len(c.Mnemonic) == 0 {
|
||||||
|
return ErrRestoreAccountInvalidMnemonic
|
||||||
|
}
|
||||||
|
|
||||||
|
return ValidateAccountCreationRequest(c.CreateAccount)
|
||||||
|
}
|
Loading…
Reference in New Issue