fix: prevent to import same account twice (#2633)

This commit is contained in:
Anthony Laibe 2022-04-11 17:18:28 +02:00 committed by GitHub
parent 631907839a
commit f1cd9120c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package accounts
import (
"context"
"errors"
"fmt"
"strings"
@ -85,6 +86,14 @@ func (api *API) AddAccountWithMnemonic(
return err
}
addressExists, err := api.db.AddressExists(types.Address(common.HexToAddress(accountInfos[pathWalletRoot].Address)))
if err != nil {
return err
}
if addressExists {
return errors.New("account already exists")
}
account := accounts.Account{
Address: types.Address(common.HexToAddress(accountInfos[pathWalletRoot].Address)),
PublicKey: types.HexBytes(accountInfos[pathWalletRoot].PublicKey),
@ -115,6 +124,14 @@ func (api *API) AddAccountWithPrivateKey(
return err
}
addressExists, err := api.db.AddressExists(types.Address(common.HexToAddress(info.Address)))
if err != nil {
return err
}
if addressExists {
return errors.New("account already exists")
}
_, err = api.manager.AccountsGenerator().StoreAccount(info.ID, password)
if err != nil {
return err