added function which creates an account from the provided mnemonic

This commit is contained in:
Sale Djenic 2022-08-19 17:13:28 +02:00 committed by saledjenic
parent f43f43cc30
commit 81f58dc869
3 changed files with 53 additions and 0 deletions

View File

@ -100,6 +100,23 @@ func (g *Generator) ImportJSONKey(json string, password string) (IdentifiedAccou
return acc.ToIdentifiedAccountInfo(id), nil 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) { func (g *Generator) ImportMnemonic(mnemonicPhrase string, bip39Passphrase string) (GeneratedAccountInfo, error) {
mnemonic := extkeys.NewMnemonic() mnemonic := extkeys.NewMnemonic()
masterExtendedKey, err := extkeys.NewMaster(mnemonic.MnemonicSeed(mnemonicPhrase, bip39Passphrase)) masterExtendedKey, err := extkeys.NewMaster(mnemonic.MnemonicSeed(mnemonicPhrase, bip39Passphrase))

View File

@ -70,6 +70,18 @@ func TestGenerator_ImportPrivateKey(t *testing.T) {
assert.Equal(t, testAccount.bip44Address0, info.Address) 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) { func TestGenerator_ImportMnemonic(t *testing.T) {
g := New(nil) g := New(nil)
assert.Equal(t, 0, len(g.accounts)) assert.Equal(t, 0, len(g.accounts))

View File

@ -158,6 +158,30 @@ func MultiAccountImportPrivateKey(paramsJSON string) string {
return string(out) 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. // MultiAccountImportMnemonic imports an account derived from the mnemonic phrase and the Bip39Passphrase storing it.
func MultiAccountImportMnemonic(paramsJSON string) string { func MultiAccountImportMnemonic(paramsJSON string) string {
var p MultiAccountImportMnemonicParams var p MultiAccountImportMnemonicParams