mirror of
https://github.com/status-im/status-go.git
synced 2025-02-12 23:06:51 +00:00
* Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
208 lines
5.0 KiB
Go
208 lines
5.0 KiB
Go
package accounts
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
"github.com/status-im/status-go/account"
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
|
"github.com/status-im/status-go/multiaccounts/settings"
|
|
"github.com/status-im/status-go/params"
|
|
)
|
|
|
|
const pathWalletRoot = "m/44'/60'/0'/0/0"
|
|
const pathEIP1581 = "m/43'/60'/1581'"
|
|
const pathDefaultWallet = pathWalletRoot + "/0"
|
|
const pathWhisper = pathEIP1581 + "/0'/0"
|
|
|
|
func NewAccountsAPI(manager *account.GethManager, config *params.NodeConfig, db *accounts.Database, feed *event.Feed) *API {
|
|
return &API{manager, config, db, feed}
|
|
}
|
|
|
|
// API is class with methods available over RPC.
|
|
type API struct {
|
|
manager *account.GethManager
|
|
config *params.NodeConfig
|
|
db *accounts.Database
|
|
feed *event.Feed
|
|
}
|
|
|
|
func (api *API) SaveAccounts(ctx context.Context, accounts []accounts.Account) error {
|
|
log.Info("[AccountsAPI::SaveAccounts]")
|
|
err := api.db.SaveAccounts(accounts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
api.feed.Send(accounts)
|
|
return nil
|
|
}
|
|
|
|
func (api *API) GetAccounts(ctx context.Context) ([]accounts.Account, error) {
|
|
return api.db.GetAccounts()
|
|
}
|
|
|
|
func (api *API) DeleteAccount(ctx context.Context, address types.Address) error {
|
|
return api.db.DeleteAccount(address)
|
|
}
|
|
|
|
func (api *API) AddAccountWatch(ctx context.Context, address string, name string, color string, emoji string) error {
|
|
account := accounts.Account{
|
|
Address: types.Address(common.HexToAddress(address)),
|
|
Type: "watch",
|
|
Name: name,
|
|
Emoji: emoji,
|
|
Color: color,
|
|
}
|
|
return api.SaveAccounts(ctx, []accounts.Account{account})
|
|
}
|
|
|
|
func (api *API) AddAccountWithMnemonic(
|
|
ctx context.Context,
|
|
mnemonic string,
|
|
password string,
|
|
name string,
|
|
color string,
|
|
emoji string,
|
|
) error {
|
|
mnemonicNoExtraSpaces := strings.Join(strings.Fields(mnemonic), " ")
|
|
|
|
err := api.verifyPassword(password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
generatedAccountInfo, err := api.manager.AccountsGenerator().ImportMnemonic(mnemonicNoExtraSpaces, "")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
paths := []string{pathWalletRoot, pathEIP1581, pathWhisper, pathDefaultWallet}
|
|
infos, err := api.manager.AccountsGenerator().DeriveAddresses(generatedAccountInfo.ID, paths)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = api.manager.AccountsGenerator().StoreDerivedAccounts(generatedAccountInfo.ID, password, []string{pathDefaultWallet})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
account := accounts.Account{
|
|
Address: types.Address(common.HexToAddress(infos[pathWalletRoot].Address)),
|
|
PublicKey: types.HexBytes(infos[pathWalletRoot].PublicKey),
|
|
Type: "seed",
|
|
Name: name,
|
|
Emoji: emoji,
|
|
Color: color,
|
|
Path: pathDefaultWallet,
|
|
}
|
|
return api.SaveAccounts(ctx, []accounts.Account{account})
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
info, err := api.manager.AccountsGenerator().ImportPrivateKey(privateKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = api.manager.AccountsGenerator().StoreAccount(info.ID, password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
account := accounts.Account{
|
|
Address: types.Address(common.HexToAddress(info.Address)),
|
|
PublicKey: types.HexBytes(info.PublicKey),
|
|
Type: "key",
|
|
Name: name,
|
|
Emoji: emoji,
|
|
Color: color,
|
|
Path: pathDefaultWallet,
|
|
}
|
|
|
|
return api.SaveAccounts(ctx, []accounts.Account{account})
|
|
}
|
|
|
|
func (api *API) GenerateAccount(
|
|
ctx context.Context,
|
|
password string,
|
|
name string,
|
|
color string,
|
|
emoji string,
|
|
) error {
|
|
err := api.verifyPassword(password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
address, err := api.db.GetWalletAddress()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
info, err := api.manager.AccountsGenerator().LoadAccount(address.Hex(), password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
latestDerivedPath, err := api.db.GetLatestDerivedPath()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
newDerivedPath := latestDerivedPath + 1
|
|
path := fmt.Sprint("m/", newDerivedPath)
|
|
infos, err := api.manager.AccountsGenerator().DeriveAddresses(info.ID, []string{path})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = api.manager.AccountsGenerator().StoreDerivedAccounts(info.ID, password, []string{path})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
acc := accounts.Account{
|
|
Address: types.Address(common.HexToAddress(infos[path].Address)),
|
|
PublicKey: types.HexBytes(infos[path].PublicKey),
|
|
Type: "generated",
|
|
Name: name,
|
|
Emoji: emoji,
|
|
Color: color,
|
|
Path: path,
|
|
}
|
|
|
|
err = api.db.SaveSettingField(settings.LatestDerivedPath, newDerivedPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return api.SaveAccounts(ctx, []accounts.Account{acc})
|
|
}
|
|
|
|
func (api *API) verifyPassword(password string) error {
|
|
address, err := api.db.GetChatAddress()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = api.manager.VerifyAccountPassword(api.config.KeyStoreDir, address.Hex(), password)
|
|
return err
|
|
}
|