diff --git a/account/generator/generator.go b/account/generator/generator.go index 8df0abba3..aabc0cdf9 100644 --- a/account/generator/generator.go +++ b/account/generator/generator.go @@ -100,6 +100,23 @@ func (g *Generator) ImportJSONKey(json string, password string) (IdentifiedAccou return acc.ToIdentifiedAccountInfo(id), nil } +func (g *Generator) CreateAccountFromMnemonic(mnemonicPhrase string, bip39Passphrase string) (GeneratedAccountInfo, error) { + mnemonic := extkeys.NewMnemonic() + masterExtendedKey, err := extkeys.NewMaster(mnemonic.MnemonicSeed(mnemonicPhrase, bip39Passphrase)) + if err != nil { + return GeneratedAccountInfo{}, fmt.Errorf("can not create master extended key: %v", err) + } + + acc := &Account{ + privateKey: masterExtendedKey.ToECDSA(), + extendedKey: masterExtendedKey, + } + + id := uuid.NewRandom().String() + + return acc.ToGeneratedAccountInfo(id, mnemonicPhrase), nil +} + func (g *Generator) ImportMnemonic(mnemonicPhrase string, bip39Passphrase string) (GeneratedAccountInfo, error) { mnemonic := extkeys.NewMnemonic() masterExtendedKey, err := extkeys.NewMaster(mnemonic.MnemonicSeed(mnemonicPhrase, bip39Passphrase)) diff --git a/account/generator/generator_test.go b/account/generator/generator_test.go index 130614385..d52f92864 100644 --- a/account/generator/generator_test.go +++ b/account/generator/generator_test.go @@ -70,6 +70,18 @@ func TestGenerator_ImportPrivateKey(t *testing.T) { assert.Equal(t, testAccount.bip44Address0, info.Address) } +func TestGenerator_CreateAccountFromMnemonic(t *testing.T) { + g := New(nil) + assert.Equal(t, 0, len(g.accounts)) + + info, err := g.CreateAccountFromMnemonic(testAccount.mnemonic, testAccount.bip39Passphrase) + + assert.NoError(t, err) + assert.Equal(t, 0, len(g.accounts)) + assert.Equal(t, 36, len(info.ID)) + assert.Equal(t, 66, len(info.KeyUID)) +} + func TestGenerator_ImportMnemonic(t *testing.T) { g := New(nil) assert.Equal(t, 0, len(g.accounts)) diff --git a/mobile/multiaccount.go b/mobile/multiaccount.go index aae1bd508..26da01c52 100644 --- a/mobile/multiaccount.go +++ b/mobile/multiaccount.go @@ -158,6 +158,30 @@ func MultiAccountImportPrivateKey(paramsJSON string) string { return string(out) } +// CreateAccountFromMnemonic returns an account derived from the mnemonic phrase and the Bip39Passphrase without storing it. +func CreateAccountFromMnemonic(paramsJSON string) string { + var p MultiAccountImportMnemonicParams + + if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil { + return makeJSONResponse(err) + } + + // remove any duplicate whitespaces + mnemonicPhraseNoExtraSpaces := strings.Join(strings.Fields(p.MnemonicPhrase), " ") + + resp, err := statusBackend.AccountManager().AccountsGenerator().CreateAccountFromMnemonic(mnemonicPhraseNoExtraSpaces, p.Bip39Passphrase) + if err != nil { + return makeJSONResponse(err) + } + + out, err := json.Marshal(resp) + if err != nil { + return makeJSONResponse(err) + } + + return string(out) +} + // MultiAccountImportMnemonic imports an account derived from the mnemonic phrase and the Bip39Passphrase storing it. func MultiAccountImportMnemonic(paramsJSON string) string { var p MultiAccountImportMnemonicParams