fix: create acc from mnemonic endpoint extended
`CreateAccountFromMnemonic` function renamed to `createAccountFromMnemonicAndDeriveAccountsForPaths` and extended in a way that it is able to derive accounts from passed mnemonic for the given paths, if paths is empty only an account from the given mnemonic will be generated. This endpoint doesn't store anything anywhere.
This commit is contained in:
parent
9565342101
commit
c1c7d2dbf6
|
@ -100,11 +100,11 @@ func (g *Generator) ImportJSONKey(json string, password string) (IdentifiedAccou
|
|||
return acc.ToIdentifiedAccountInfo(id), nil
|
||||
}
|
||||
|
||||
func (g *Generator) CreateAccountFromMnemonic(mnemonicPhrase string, bip39Passphrase string) (GeneratedAccountInfo, error) {
|
||||
func (g *Generator) CreateAccountFromMnemonicAndDeriveAccountsForPaths(mnemonicPhrase string, bip39Passphrase string, paths []string) (GeneratedAndDerivedAccountInfo, 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)
|
||||
return GeneratedAndDerivedAccountInfo{}, fmt.Errorf("can not create master extended key: %v", err)
|
||||
}
|
||||
|
||||
acc := &Account{
|
||||
|
@ -112,9 +112,22 @@ func (g *Generator) CreateAccountFromMnemonic(mnemonicPhrase string, bip39Passph
|
|||
extendedKey: masterExtendedKey,
|
||||
}
|
||||
|
||||
id := uuid.NewRandom().String()
|
||||
derivedAccountsInfo := make(map[string]AccountInfo)
|
||||
if len(paths) > 0 {
|
||||
derivedAccounts, err := g.deriveChildAccounts(acc, paths)
|
||||
if err != nil {
|
||||
return GeneratedAndDerivedAccountInfo{}, err
|
||||
}
|
||||
|
||||
return acc.ToGeneratedAccountInfo(id, mnemonicPhrase), nil
|
||||
for pathString, childAccount := range derivedAccounts {
|
||||
derivedAccountsInfo[pathString] = childAccount.ToAccountInfo()
|
||||
}
|
||||
}
|
||||
|
||||
id := uuid.NewRandom().String()
|
||||
accInfo := acc.ToGeneratedAccountInfo(id, mnemonicPhrase)
|
||||
|
||||
return accInfo.toGeneratedAndDerived(derivedAccountsInfo), nil
|
||||
}
|
||||
|
||||
func (g *Generator) ImportMnemonic(mnemonicPhrase string, bip39Passphrase string) (GeneratedAccountInfo, error) {
|
||||
|
|
|
@ -70,11 +70,11 @@ func TestGenerator_ImportPrivateKey(t *testing.T) {
|
|||
assert.Equal(t, testAccount.bip44Address0, info.Address)
|
||||
}
|
||||
|
||||
func TestGenerator_CreateAccountFromMnemonic(t *testing.T) {
|
||||
func TestGenerator_CreateAccountFromMnemonicAndDeriveAccountsForPaths(t *testing.T) {
|
||||
g := New(nil)
|
||||
assert.Equal(t, 0, len(g.accounts))
|
||||
|
||||
info, err := g.CreateAccountFromMnemonic(testAccount.mnemonic, testAccount.bip39Passphrase)
|
||||
info, err := g.CreateAccountFromMnemonicAndDeriveAccountsForPaths(testAccount.mnemonic, testAccount.bip39Passphrase, []string{})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, len(g.accounts))
|
||||
|
|
|
@ -49,8 +49,9 @@ type MultiAccountLoadAccountParams struct {
|
|||
|
||||
// MultiAccountImportMnemonicParams are the params sent to MultiAccountImportMnemonic.
|
||||
type MultiAccountImportMnemonicParams struct {
|
||||
MnemonicPhrase string `json:"mnemonicPhrase"`
|
||||
Bip39Passphrase string `json:"Bip39Passphrase"`
|
||||
MnemonicPhrase string `json:"mnemonicPhrase"`
|
||||
Bip39Passphrase string `json:"Bip39Passphrase"`
|
||||
Paths []string `json:"paths"`
|
||||
}
|
||||
|
||||
// MultiAccountGenerate generates account in memory without storing them.
|
||||
|
@ -158,8 +159,9 @@ 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 {
|
||||
// CreateAccountFromMnemonicAndDeriveAccountsForPaths returns an account derived from the mnemonic phrase and the Bip39Passphrase
|
||||
// and generate derived accounts for the list of paths without storing it
|
||||
func CreateAccountFromMnemonicAndDeriveAccountsForPaths(paramsJSON string) string {
|
||||
var p MultiAccountImportMnemonicParams
|
||||
|
||||
if err := json.Unmarshal([]byte(paramsJSON), &p); err != nil {
|
||||
|
@ -169,7 +171,7 @@ func CreateAccountFromMnemonic(paramsJSON string) string {
|
|||
// remove any duplicate whitespaces
|
||||
mnemonicPhraseNoExtraSpaces := strings.Join(strings.Fields(p.MnemonicPhrase), " ")
|
||||
|
||||
resp, err := statusBackend.AccountManager().AccountsGenerator().CreateAccountFromMnemonic(mnemonicPhraseNoExtraSpaces, p.Bip39Passphrase)
|
||||
resp, err := statusBackend.AccountManager().AccountsGenerator().CreateAccountFromMnemonicAndDeriveAccountsForPaths(mnemonicPhraseNoExtraSpaces, p.Bip39Passphrase, p.Paths)
|
||||
if err != nil {
|
||||
return makeJSONResponse(err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue