2019-08-20 15:38:40 +00:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-11 15:18:28 +00:00
|
|
|
"errors"
|
2022-01-24 09:29:18 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2019-08-20 15:38:40 +00:00
|
|
|
|
2022-01-24 09:29:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-08-28 07:49:03 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
status-im/status-react#9203 Faster tx fetching with less request
*** How it worked before this PR on multiaccount creation:
- On multiacc creation we scanned chain for eth and erc20 transfers. For
each address of a new empty multiaccount this scan required
1. two `eth_getBalance` requests to find out that there is no any
balance change between zero and the last block, for eth transfers
2. and `chain-size/100000` (currently ~100) `eth_getLogs` requests,
for erc20 transfers
- For some reason we scanned an address of the chat account as well, and
also accounts were not deduplicated. So even for an empty multiacc we
scanned chain twice for each chat and main wallet addresses, in result
app had to execute about 400 requests.
- As mentioned above, `eth_getBalance` requests were used to check if
there were any eth transfers, and that caused empty history in case
if user already used all available eth (so that both zero and latest
blocks show 0 eth for an address). There might have been transactions
but we wouldn't fetch/show them.
- There was no upper limit for the number of rpc requests during the
scan, so it could require indefinite number of requests; the scanning
algorithm was written so that we persisted the whole history of
transactions or tried to scan form the beginning again in case of
failure, giving up only after 10 minutes of failures. In result
addresses with sufficient number of transactions would never be fully
scanned and during these 10 minutes app could use gigabytes of
internet data.
- Failures were caused by `eth_getBlockByNumber`/`eth_getBlockByHash`
requests. These requests return significantly bigger responses than
`eth_getBalance`/`eth_transactionsCount` and it is likely that
execution of thousands of them in parallel caused failures for
accounts with hundreds of transactions. Even for an account with 12k
we could successfully determine blocks with transaction in a few
minutes using `eth_getBalance` requests, but `eth_getBlock...`
couldn't be processed for this acc.
- There was no caching for for `eth_getBalance` requests, and this
caused in average 3-4 times more such requests than is needed.
*** How it works now on multiaccount creation:
- On multiacc creation we scan chain for last ~30 eth transactions and
then check erc20 in the range where these eth transactions were found.
For an empty address in multiacc this means:
1. two `eth_getBalance` transactions to determine that there was no
balance change between zero and the last block; two
`eth_transactionsCount` requests to determine there are no outgoing
transactions for this address; total 4 requests for eth transfers
2. 20 `eth_getLogs` for erc20 transfers. This number can be lowered,
but that's not a big deal
- Deduplication of addresses is added and also we don't scan chat
account, so a new multiacc requires ~25 (we also request latest block
number and probably execute a few other calls) request to determine
that multiacc is empty (comparing to ~400 before)
- In case if address contains transactions we:
1. determine the range which contains 20-25 outgoing eth/erc20
transactions. This usually requires up to 10 `eth_transactionCount`
requests
2. then we scan chain for eth transfers using `eth_getBalance` and
`eth_transactionCount` (for double checking zero balances)
3. we make sure that we do not scan db for more than 30 blocks with
transfers. That's important for accounts with mostly incoming
transactions, because the range found on the first step might
contain any number of incoming transfers, but only 20-25 outgoing
transactions
4. when we found ~30 blocks in a given range, we update initial
range `from` block using the oldest found block
5. and now we scan db for erc20transfers using `eth_getLogs`
`oldest-found-eth-block`-`latest-block`, we make not more than 20 calls
6. when all blocks which contain incoming/outgoing transfers for a
given address are found, we save these blocks to db and mark that
transfers from these blocks are still to be fetched
7. Then we select latest ~30 (the number can be adjusted) blocks from
these which were found and fetch transfers, this requires 3-4
requests per transfer.
8. we persist scanned range so that we know were to start next time
9. we dispatch an event which tells client that transactions are found
10. client fetches latest 20 transfers
- when user presses "fetch more" button we check if app's db contains next
20 transfers, if not we scan chain again and return transfers after
small fixes
2019-12-18 11:01:46 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2022-01-24 09:29:18 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
2019-12-11 13:59:37 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2019-08-20 15:38:40 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2022-09-06 07:10:40 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/keypairs"
|
2022-03-23 18:47:00 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/settings"
|
2022-01-24 09:29:18 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2022-07-06 16:12:49 +00:00
|
|
|
"github.com/status-im/status-go/protocol"
|
2019-08-20 15:38:40 +00:00
|
|
|
)
|
|
|
|
|
2022-04-11 16:35:18 +00:00
|
|
|
const pathWalletRoot = "m/44'/60'/0'/0"
|
2022-01-24 09:29:18 +00:00
|
|
|
const pathDefaultWallet = pathWalletRoot + "/0"
|
|
|
|
|
2022-07-06 16:12:49 +00:00
|
|
|
func NewAccountsAPI(manager *account.GethManager, config *params.NodeConfig, db *accounts.Database, feed *event.Feed, messenger **protocol.Messenger) *API {
|
|
|
|
return &API{manager, config, db, feed, messenger}
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// API is class with methods available over RPC.
|
|
|
|
type API struct {
|
2022-07-06 16:12:49 +00:00
|
|
|
manager *account.GethManager
|
|
|
|
config *params.NodeConfig
|
|
|
|
db *accounts.Database
|
|
|
|
feed *event.Feed
|
|
|
|
messenger **protocol.Messenger
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 10:42:51 +00:00
|
|
|
type DerivedAddress struct {
|
|
|
|
Address common.Address `json:"address"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
HasActivity bool `json:"hasActivity"`
|
|
|
|
AlreadyCreated bool `json:"alreadyCreated"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) SaveAccounts(ctx context.Context, accounts []*accounts.Account) error {
|
status-im/status-react#9203 Faster tx fetching with less request
*** How it worked before this PR on multiaccount creation:
- On multiacc creation we scanned chain for eth and erc20 transfers. For
each address of a new empty multiaccount this scan required
1. two `eth_getBalance` requests to find out that there is no any
balance change between zero and the last block, for eth transfers
2. and `chain-size/100000` (currently ~100) `eth_getLogs` requests,
for erc20 transfers
- For some reason we scanned an address of the chat account as well, and
also accounts were not deduplicated. So even for an empty multiacc we
scanned chain twice for each chat and main wallet addresses, in result
app had to execute about 400 requests.
- As mentioned above, `eth_getBalance` requests were used to check if
there were any eth transfers, and that caused empty history in case
if user already used all available eth (so that both zero and latest
blocks show 0 eth for an address). There might have been transactions
but we wouldn't fetch/show them.
- There was no upper limit for the number of rpc requests during the
scan, so it could require indefinite number of requests; the scanning
algorithm was written so that we persisted the whole history of
transactions or tried to scan form the beginning again in case of
failure, giving up only after 10 minutes of failures. In result
addresses with sufficient number of transactions would never be fully
scanned and during these 10 minutes app could use gigabytes of
internet data.
- Failures were caused by `eth_getBlockByNumber`/`eth_getBlockByHash`
requests. These requests return significantly bigger responses than
`eth_getBalance`/`eth_transactionsCount` and it is likely that
execution of thousands of them in parallel caused failures for
accounts with hundreds of transactions. Even for an account with 12k
we could successfully determine blocks with transaction in a few
minutes using `eth_getBalance` requests, but `eth_getBlock...`
couldn't be processed for this acc.
- There was no caching for for `eth_getBalance` requests, and this
caused in average 3-4 times more such requests than is needed.
*** How it works now on multiaccount creation:
- On multiacc creation we scan chain for last ~30 eth transactions and
then check erc20 in the range where these eth transactions were found.
For an empty address in multiacc this means:
1. two `eth_getBalance` transactions to determine that there was no
balance change between zero and the last block; two
`eth_transactionsCount` requests to determine there are no outgoing
transactions for this address; total 4 requests for eth transfers
2. 20 `eth_getLogs` for erc20 transfers. This number can be lowered,
but that's not a big deal
- Deduplication of addresses is added and also we don't scan chat
account, so a new multiacc requires ~25 (we also request latest block
number and probably execute a few other calls) request to determine
that multiacc is empty (comparing to ~400 before)
- In case if address contains transactions we:
1. determine the range which contains 20-25 outgoing eth/erc20
transactions. This usually requires up to 10 `eth_transactionCount`
requests
2. then we scan chain for eth transfers using `eth_getBalance` and
`eth_transactionCount` (for double checking zero balances)
3. we make sure that we do not scan db for more than 30 blocks with
transfers. That's important for accounts with mostly incoming
transactions, because the range found on the first step might
contain any number of incoming transfers, but only 20-25 outgoing
transactions
4. when we found ~30 blocks in a given range, we update initial
range `from` block using the oldest found block
5. and now we scan db for erc20transfers using `eth_getLogs`
`oldest-found-eth-block`-`latest-block`, we make not more than 20 calls
6. when all blocks which contain incoming/outgoing transfers for a
given address are found, we save these blocks to db and mark that
transfers from these blocks are still to be fetched
7. Then we select latest ~30 (the number can be adjusted) blocks from
these which were found and fetch transfers, this requires 3-4
requests per transfer.
8. we persist scanned range so that we know were to start next time
9. we dispatch an event which tells client that transactions are found
10. client fetches latest 20 transfers
- when user presses "fetch more" button we check if app's db contains next
20 transfers, if not we scan chain again and return transfers after
small fixes
2019-12-18 11:01:46 +00:00
|
|
|
log.Info("[AccountsAPI::SaveAccounts]")
|
2022-07-06 16:12:49 +00:00
|
|
|
err := (*api.messenger).SaveAccounts(accounts)
|
2019-08-28 07:49:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
api.feed.Send(accounts)
|
|
|
|
return nil
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 10:42:51 +00:00
|
|
|
func (api *API) GetAccounts(ctx context.Context) ([]*accounts.Account, error) {
|
2022-05-09 08:00:48 +00:00
|
|
|
accounts, err := api.db.GetAccounts()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range accounts {
|
2022-05-18 10:42:51 +00:00
|
|
|
account := accounts[i]
|
2022-05-09 08:00:48 +00:00
|
|
|
if account.Wallet && account.DerivedFrom == "" {
|
|
|
|
address, err := api.db.GetWalletRootAddress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
account.DerivedFrom = address.Hex()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return accounts, nil
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
2019-12-16 15:23:36 +00:00
|
|
|
|
2023-02-01 08:46:15 +00:00
|
|
|
func (api *API) DeleteAccount(ctx context.Context, address types.Address, password string) error {
|
|
|
|
if len(password) > 0 {
|
|
|
|
acc, err := api.db.GetAccountByAddress(address)
|
|
|
|
if err != nil {
|
2022-05-18 10:42:51 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-02-01 08:46:15 +00:00
|
|
|
if acc.Type != accounts.AccountTypeWatch {
|
|
|
|
err = api.manager.DeleteAccount(address, password)
|
|
|
|
var e *account.ErrCannotLocateKeyFile
|
|
|
|
if err != nil && !errors.As(err, &e) {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-12 11:06:58 +00:00
|
|
|
|
2023-02-01 08:46:15 +00:00
|
|
|
allAccountsOfKeypairWithKeyUID, err := api.db.GetAccountsByKeyUID(acc.KeyUID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lastAcccountOfKeypairWithTheSameKey := len(allAccountsOfKeypairWithKeyUID) == 1
|
|
|
|
if lastAcccountOfKeypairWithTheSameKey {
|
|
|
|
err = api.manager.DeleteAccount(types.Address(common.HexToAddress(acc.DerivedFrom)), password)
|
|
|
|
var e *account.ErrCannotLocateKeyFile
|
|
|
|
if err != nil && !errors.As(err, &e) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-24 09:29:18 +00:00
|
|
|
|
2023-01-10 09:38:34 +00:00
|
|
|
return (*api.messenger).DeleteAccount(address)
|
|
|
|
}
|
|
|
|
|
2022-03-11 10:59:15 +00:00
|
|
|
func (api *API) AddAccountWatch(ctx context.Context, address string, name string, color string, emoji string) error {
|
2022-05-18 10:42:51 +00:00
|
|
|
account := &accounts.Account{
|
2022-01-24 09:29:18 +00:00
|
|
|
Address: types.Address(common.HexToAddress(address)),
|
2022-08-25 15:09:08 +00:00
|
|
|
Type: accounts.AccountTypeWatch,
|
2022-01-24 09:29:18 +00:00
|
|
|
Name: name,
|
2022-03-11 10:59:15 +00:00
|
|
|
Emoji: emoji,
|
2022-01-24 09:29:18 +00:00
|
|
|
Color: color,
|
|
|
|
}
|
2022-05-18 10:42:51 +00:00
|
|
|
return api.SaveAccounts(ctx, []*accounts.Account{account})
|
2022-01-24 09:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) AddAccountWithMnemonic(
|
|
|
|
ctx context.Context,
|
|
|
|
mnemonic string,
|
|
|
|
password string,
|
|
|
|
name string,
|
|
|
|
color string,
|
2022-03-11 10:59:15 +00:00
|
|
|
emoji string,
|
2022-01-24 09:29:18 +00:00
|
|
|
) error {
|
2022-04-13 09:15:26 +00:00
|
|
|
return api.addAccountWithMnemonic(ctx, mnemonic, password, name, color, emoji, pathWalletRoot)
|
|
|
|
}
|
|
|
|
|
2022-10-26 20:05:37 +00:00
|
|
|
func (api *API) AddAccountWithMnemonicPasswordVerified(
|
|
|
|
ctx context.Context,
|
|
|
|
mnemonic string,
|
|
|
|
password string,
|
|
|
|
name string,
|
|
|
|
color string,
|
|
|
|
emoji string,
|
|
|
|
) error {
|
|
|
|
return api.addAccountWithMnemonicPasswordVerified(ctx, mnemonic, password, name, color, emoji, pathWalletRoot)
|
|
|
|
}
|
|
|
|
|
2022-04-13 09:15:26 +00:00
|
|
|
func (api *API) AddAccountWithMnemonicAndPath(
|
|
|
|
ctx context.Context,
|
|
|
|
mnemonic string,
|
|
|
|
password string,
|
|
|
|
name string,
|
|
|
|
color string,
|
|
|
|
emoji string,
|
|
|
|
path string,
|
|
|
|
) error {
|
|
|
|
return api.addAccountWithMnemonic(ctx, mnemonic, password, name, color, emoji, path)
|
|
|
|
}
|
2022-01-24 09:29:18 +00:00
|
|
|
|
2022-10-26 20:05:37 +00:00
|
|
|
func (api *API) AddAccountWithMnemonicAndPathPasswordVerified(
|
|
|
|
ctx context.Context,
|
|
|
|
mnemonic string,
|
|
|
|
password string,
|
|
|
|
name string,
|
|
|
|
color string,
|
|
|
|
emoji string,
|
|
|
|
path string,
|
|
|
|
) error {
|
|
|
|
return api.addAccountWithMnemonicPasswordVerified(ctx, mnemonic, password, name, color, emoji, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddAccountWithPrivateKeyPasswordVerified adds an accounts.Account created from the given private key
|
|
|
|
// assuming that client has already authenticated logged in use, this function doesn't verify a password.
|
|
|
|
func (api *API) AddAccountWithPrivateKeyPasswordVerified(
|
2022-04-13 09:15:26 +00:00
|
|
|
ctx context.Context,
|
|
|
|
privateKey string,
|
|
|
|
password string,
|
|
|
|
name string,
|
|
|
|
color string,
|
|
|
|
emoji string,
|
|
|
|
) error {
|
2022-01-24 09:29:18 +00:00
|
|
|
|
2022-04-13 09:15:26 +00:00
|
|
|
info, err := api.manager.AccountsGenerator().ImportPrivateKey(privateKey)
|
2022-01-24 09:29:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-13 09:15:26 +00:00
|
|
|
addressExists, err := api.db.AddressExists(types.Address(common.HexToAddress(info.Address)))
|
2022-01-24 09:29:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-13 09:15:26 +00:00
|
|
|
if addressExists {
|
|
|
|
return errors.New("account already exists")
|
|
|
|
}
|
2022-01-24 09:29:18 +00:00
|
|
|
|
2022-04-13 09:15:26 +00:00
|
|
|
_, err = api.manager.AccountsGenerator().StoreAccount(info.ID, password)
|
2022-04-11 15:18:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-18 10:42:51 +00:00
|
|
|
account := &accounts.Account{
|
2022-04-13 09:15:26 +00:00
|
|
|
Address: types.Address(common.HexToAddress(info.Address)),
|
2022-09-05 11:52:53 +00:00
|
|
|
KeyUID: info.KeyUID,
|
2022-04-13 09:15:26 +00:00
|
|
|
PublicKey: types.HexBytes(info.PublicKey),
|
2022-07-06 16:12:49 +00:00
|
|
|
Type: accounts.AccountTypeKey,
|
2022-01-24 09:29:18 +00:00
|
|
|
Name: name,
|
2022-03-11 10:59:15 +00:00
|
|
|
Emoji: emoji,
|
2022-01-24 09:29:18 +00:00
|
|
|
Color: color,
|
|
|
|
Path: pathDefaultWallet,
|
|
|
|
}
|
2022-04-13 09:15:26 +00:00
|
|
|
|
2022-05-18 10:42:51 +00:00
|
|
|
return api.SaveAccounts(ctx, []*accounts.Account{account})
|
2022-01-24 09:29:18 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 20:05:37 +00:00
|
|
|
func (api *API) AddAccountWithPrivateKey(
|
|
|
|
ctx context.Context,
|
|
|
|
privateKey string,
|
|
|
|
password string,
|
|
|
|
name string,
|
|
|
|
color string,
|
|
|
|
emoji string,
|
|
|
|
) error {
|
|
|
|
err := api.verifyPassword(password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.AddAccountWithPrivateKeyPasswordVerified(ctx, privateKey, password, name, color, emoji)
|
|
|
|
}
|
|
|
|
|
2022-04-13 09:15:26 +00:00
|
|
|
func (api *API) GenerateAccount(
|
2022-01-24 09:29:18 +00:00
|
|
|
ctx context.Context,
|
|
|
|
password string,
|
|
|
|
name string,
|
|
|
|
color string,
|
2022-03-11 10:59:15 +00:00
|
|
|
emoji string,
|
2022-01-24 09:29:18 +00:00
|
|
|
) error {
|
2022-05-09 08:00:48 +00:00
|
|
|
address, err := api.db.GetWalletRootAddress()
|
2022-01-24 09:29:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-13 09:15:26 +00:00
|
|
|
latestDerivedPath, err := api.db.GetLatestDerivedPath()
|
2022-01-24 09:29:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-13 09:15:26 +00:00
|
|
|
newDerivedPath := latestDerivedPath + 1
|
|
|
|
path := fmt.Sprint(pathWalletRoot, "/", newDerivedPath)
|
|
|
|
|
|
|
|
err = api.generateAccount(ctx, password, name, color, emoji, path, address.Hex())
|
2022-04-11 15:18:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-13 09:15:26 +00:00
|
|
|
|
|
|
|
err = api.db.SaveSettingField(settings.LatestDerivedPath, newDerivedPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-04-11 15:18:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 09:15:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-26 20:05:37 +00:00
|
|
|
func (api *API) GenerateAccountPasswordVerified(
|
|
|
|
ctx context.Context,
|
|
|
|
password string,
|
|
|
|
name string,
|
|
|
|
color string,
|
|
|
|
emoji string,
|
|
|
|
) error {
|
|
|
|
address, err := api.db.GetWalletRootAddress()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
latestDerivedPath, err := api.db.GetLatestDerivedPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newDerivedPath := latestDerivedPath + 1
|
|
|
|
path := fmt.Sprint(pathWalletRoot, "/", newDerivedPath)
|
|
|
|
|
|
|
|
err = api.generateAccountPasswordVerified(ctx, password, name, color, emoji, path, address.Hex())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = api.db.SaveSettingField(settings.LatestDerivedPath, newDerivedPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-13 09:15:26 +00:00
|
|
|
func (api *API) GenerateAccountWithDerivedPath(
|
|
|
|
ctx context.Context,
|
|
|
|
password string,
|
|
|
|
name string,
|
|
|
|
color string,
|
|
|
|
emoji string,
|
|
|
|
path string,
|
|
|
|
derivedFrom string,
|
|
|
|
) error {
|
|
|
|
return api.generateAccount(ctx, password, name, color, emoji, path, derivedFrom)
|
|
|
|
}
|
|
|
|
|
2022-10-26 20:05:37 +00:00
|
|
|
func (api *API) GenerateAccountWithDerivedPathPasswordVerified(
|
|
|
|
ctx context.Context,
|
|
|
|
password string,
|
|
|
|
name string,
|
|
|
|
color string,
|
|
|
|
emoji string,
|
|
|
|
path string,
|
|
|
|
derivedFrom string,
|
|
|
|
) error {
|
|
|
|
return api.generateAccountPasswordVerified(ctx, password, name, color, emoji, path, derivedFrom)
|
|
|
|
}
|
|
|
|
|
2022-04-13 09:15:26 +00:00
|
|
|
func (api *API) verifyPassword(password string) error {
|
|
|
|
address, err := api.db.GetChatAddress()
|
2022-01-24 09:29:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-13 09:15:26 +00:00
|
|
|
_, err = api.manager.VerifyAccountPassword(api.config.KeyStoreDir, address.Hex(), password)
|
|
|
|
return err
|
|
|
|
}
|
2022-01-24 09:29:18 +00:00
|
|
|
|
2022-10-26 20:05:37 +00:00
|
|
|
// addAccountWithMnemonicPasswordVerified adds an accounts.Account derived from the given Mnemonic
|
|
|
|
// assuming that client has already authenticated logged in use, this function doesn't verify a password.
|
|
|
|
func (api *API) addAccountWithMnemonicPasswordVerified(
|
2022-01-24 09:29:18 +00:00
|
|
|
ctx context.Context,
|
2022-04-13 09:15:26 +00:00
|
|
|
mnemonic string,
|
2022-01-24 09:29:18 +00:00
|
|
|
password string,
|
|
|
|
name string,
|
|
|
|
color string,
|
2022-03-11 10:59:15 +00:00
|
|
|
emoji string,
|
2022-04-13 09:15:26 +00:00
|
|
|
path string,
|
2022-01-24 09:29:18 +00:00
|
|
|
) error {
|
2022-04-13 09:15:26 +00:00
|
|
|
mnemonicNoExtraSpaces := strings.Join(strings.Fields(mnemonic), " ")
|
|
|
|
|
|
|
|
generatedAccountInfo, err := api.manager.AccountsGenerator().ImportMnemonic(mnemonicNoExtraSpaces, "")
|
2022-01-24 09:29:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-13 09:15:26 +00:00
|
|
|
_, err = api.manager.AccountsGenerator().StoreAccount(generatedAccountInfo.ID, password)
|
2022-01-24 09:29:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-13 09:15:26 +00:00
|
|
|
accountinfos, err := api.manager.AccountsGenerator().StoreDerivedAccounts(generatedAccountInfo.ID, password, []string{path})
|
2022-01-24 09:29:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-13 09:15:26 +00:00
|
|
|
|
2022-05-18 10:42:51 +00:00
|
|
|
account := &accounts.Account{
|
2022-04-13 09:15:26 +00:00
|
|
|
Address: types.Address(common.HexToAddress(accountinfos[path].Address)),
|
2022-09-05 11:52:53 +00:00
|
|
|
KeyUID: generatedAccountInfo.KeyUID,
|
2022-04-13 09:15:26 +00:00
|
|
|
PublicKey: types.HexBytes(accountinfos[path].PublicKey),
|
2022-07-06 16:12:49 +00:00
|
|
|
Type: accounts.AccountTypeSeed,
|
2022-04-13 09:15:26 +00:00
|
|
|
Name: name,
|
|
|
|
Emoji: emoji,
|
|
|
|
Color: color,
|
|
|
|
Path: path,
|
|
|
|
DerivedFrom: generatedAccountInfo.Address,
|
2022-01-24 09:29:18 +00:00
|
|
|
}
|
2022-05-18 10:42:51 +00:00
|
|
|
return api.SaveAccounts(ctx, []*accounts.Account{account})
|
2022-04-13 09:15:26 +00:00
|
|
|
}
|
2022-01-24 09:29:18 +00:00
|
|
|
|
2022-10-26 20:05:37 +00:00
|
|
|
func (api *API) addAccountWithMnemonic(
|
2022-04-13 09:15:26 +00:00
|
|
|
ctx context.Context,
|
2022-10-26 20:05:37 +00:00
|
|
|
mnemonic string,
|
2022-04-13 09:15:26 +00:00
|
|
|
password string,
|
|
|
|
name string,
|
|
|
|
color string,
|
|
|
|
emoji string,
|
|
|
|
path string,
|
|
|
|
) error {
|
|
|
|
err := api.verifyPassword(password)
|
2022-01-24 09:29:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-26 20:05:37 +00:00
|
|
|
return api.addAccountWithMnemonicPasswordVerified(ctx, mnemonic, password, name, color, emoji, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateAccountPasswordVerified adds an accounts.Account generated from the given path
|
|
|
|
// assuming that client has already authenticated logged in use, this function doesn't verify a password.
|
|
|
|
func (api *API) generateAccountPasswordVerified(
|
|
|
|
ctx context.Context,
|
|
|
|
password string,
|
|
|
|
name string,
|
|
|
|
color string,
|
|
|
|
emoji string,
|
|
|
|
path string,
|
|
|
|
address string,
|
|
|
|
) error {
|
2022-04-13 09:15:26 +00:00
|
|
|
info, err := api.manager.AccountsGenerator().LoadAccount(address, password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-01-24 09:29:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 09:15:26 +00:00
|
|
|
infos, err := api.manager.AccountsGenerator().DeriveAddresses(info.ID, []string{path})
|
2022-01-24 09:29:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-13 09:15:26 +00:00
|
|
|
_, err = api.manager.AccountsGenerator().StoreDerivedAccounts(info.ID, password, []string{path})
|
2022-01-24 09:29:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-13 09:15:26 +00:00
|
|
|
|
2022-05-18 10:42:51 +00:00
|
|
|
acc := &accounts.Account{
|
2022-04-13 09:15:26 +00:00
|
|
|
Address: types.Address(common.HexToAddress(infos[path].Address)),
|
2022-09-05 11:52:53 +00:00
|
|
|
KeyUID: info.KeyUID,
|
2022-04-13 09:15:26 +00:00
|
|
|
PublicKey: types.HexBytes(infos[path].PublicKey),
|
2022-07-06 16:12:49 +00:00
|
|
|
Type: accounts.AccountTypeGenerated,
|
2022-04-13 09:15:26 +00:00
|
|
|
Name: name,
|
|
|
|
Emoji: emoji,
|
|
|
|
Color: color,
|
|
|
|
Path: path,
|
|
|
|
DerivedFrom: address,
|
|
|
|
}
|
|
|
|
|
2022-05-18 10:42:51 +00:00
|
|
|
return api.SaveAccounts(ctx, []*accounts.Account{acc})
|
2022-01-24 09:29:18 +00:00
|
|
|
}
|
2022-08-24 16:01:23 +00:00
|
|
|
|
2022-10-26 20:05:37 +00:00
|
|
|
func (api *API) generateAccount(
|
|
|
|
ctx context.Context,
|
|
|
|
password string,
|
|
|
|
name string,
|
|
|
|
color string,
|
|
|
|
emoji string,
|
|
|
|
path string,
|
|
|
|
address string,
|
|
|
|
) error {
|
|
|
|
err := api.verifyPassword(password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.generateAccountPasswordVerified(ctx, password, name, color, emoji, path, address)
|
|
|
|
}
|
|
|
|
|
2022-08-24 16:01:23 +00:00
|
|
|
func (api *API) VerifyPassword(password string) bool {
|
|
|
|
err := api.verifyPassword(password)
|
2022-09-27 22:59:02 +00:00
|
|
|
return err == nil
|
2022-08-24 16:01:23 +00:00
|
|
|
}
|
2022-09-06 07:10:40 +00:00
|
|
|
|
2023-02-01 08:46:15 +00:00
|
|
|
func (api *API) AddMigratedKeyPair(ctx context.Context, kcUID string, kpName string, keyUID string, accountAddresses []string, password string) error {
|
2022-09-06 07:10:40 +00:00
|
|
|
var addresses []types.Address
|
|
|
|
for _, addr := range accountAddresses {
|
|
|
|
addresses = append(addresses, types.Address(common.HexToAddress(addr)))
|
|
|
|
}
|
|
|
|
|
2022-11-08 13:30:33 +00:00
|
|
|
err := api.db.AddMigratedKeyPair(kcUID, kpName, keyUID, addresses)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once we migrate a keypair, corresponding keystore files need to be deleted.
|
2023-02-01 08:46:15 +00:00
|
|
|
if len(password) > 0 {
|
|
|
|
for _, addr := range addresses {
|
|
|
|
err = api.manager.DeleteAccount(addr, password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-11-08 13:30:33 +00:00
|
|
|
}
|
|
|
|
return nil
|
2022-09-06 07:10:40 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 09:57:22 +00:00
|
|
|
func (api *API) RemoveMigratedAccountsForKeycard(ctx context.Context, kcUID string, accountAddresses []string) error {
|
|
|
|
var addresses []types.Address
|
|
|
|
for _, addr := range accountAddresses {
|
|
|
|
addresses = append(addresses, types.Address(common.HexToAddress(addr)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.db.RemoveMigratedAccountsForKeycard(kcUID, addresses)
|
|
|
|
}
|
|
|
|
|
2022-12-07 17:14:19 +00:00
|
|
|
func (api *API) GetAllKnownKeycards(ctx context.Context) ([]*keypairs.KeyPair, error) {
|
|
|
|
return api.db.GetAllKnownKeycards()
|
|
|
|
}
|
|
|
|
|
2022-09-06 07:10:40 +00:00
|
|
|
func (api *API) GetAllMigratedKeyPairs(ctx context.Context) ([]*keypairs.KeyPair, error) {
|
|
|
|
return api.db.GetAllMigratedKeyPairs()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) GetMigratedKeyPairByKeyUID(ctx context.Context, keyUID string) ([]*keypairs.KeyPair, error) {
|
|
|
|
return api.db.GetMigratedKeyPairByKeyUID(keyUID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) SetKeycardName(ctx context.Context, kcUID string, kpName string) error {
|
|
|
|
return api.db.SetKeycardName(kcUID, kpName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) KeycardLocked(ctx context.Context, kcUID string) error {
|
|
|
|
return api.db.KeycardLocked(kcUID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) KeycardUnlocked(ctx context.Context, kcUID string) error {
|
|
|
|
return api.db.KeycardUnlocked(kcUID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) DeleteKeycard(ctx context.Context, kcUID string) error {
|
|
|
|
return api.db.DeleteKeycard(kcUID)
|
|
|
|
}
|
2022-09-20 10:45:50 +00:00
|
|
|
|
2023-01-25 10:18:28 +00:00
|
|
|
func (api *API) DeleteKeypair(ctx context.Context, keyUID string) error {
|
|
|
|
return api.db.DeleteKeypair(keyUID)
|
|
|
|
}
|
|
|
|
|
2022-09-20 10:45:50 +00:00
|
|
|
func (api *API) UpdateKeycardUID(ctx context.Context, oldKcUID string, newKcUID string) error {
|
|
|
|
return api.db.UpdateKeycardUID(oldKcUID, newKcUID)
|
|
|
|
}
|