feat: move create accounts to status go (#2492)

This commit is contained in:
Anthony Laibe 2022-01-24 10:29:18 +01:00 committed by GitHub
parent 52b2c5db7b
commit 2b8b2be1f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 188 additions and 16 deletions

View File

@ -780,6 +780,12 @@ func (db *Database) GetChatAddress() (rst types.Address, err error) {
return return
} }
func (db *Database) GetLatestDerivedPath() (uint, error) {
var result uint
err := db.db.QueryRow("SELECT latest_derived_path FROM settings WHERE synthetic_id = 'id'").Scan(&result)
return result, err
}
func (db *Database) GetAddresses() (rst []types.Address, err error) { func (db *Database) GetAddresses() (rst []types.Address, err error) {
rows, err := db.db.Query("SELECT address FROM accounts ORDER BY created_at") rows, err := db.db.Query("SELECT address FROM accounts ORDER BY created_at")
if err != nil { if err != nil {

View File

@ -342,7 +342,13 @@ func (b *StatusNode) rpcStatsService() *rpcstats.Service {
func (b *StatusNode) accountsService(accountsFeed *event.Feed) *accountssvc.Service { func (b *StatusNode) accountsService(accountsFeed *event.Feed) *accountssvc.Service {
if b.accountsSrvc == nil { if b.accountsSrvc == nil {
b.accountsSrvc = accountssvc.NewService(accounts.NewDB(b.appDB), b.multiaccountsDB, b.gethAccountManager.Manager, accountsFeed) b.accountsSrvc = accountssvc.NewService(
accounts.NewDB(b.appDB),
b.multiaccountsDB,
b.gethAccountManager,
b.config,
accountsFeed,
)
} }
return b.accountsSrvc return b.accountsSrvc

View File

@ -2,20 +2,32 @@ package accounts
import ( import (
"context" "context"
"fmt"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log" "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/eth-node/types"
"github.com/status-im/status-go/multiaccounts/accounts" "github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/params"
) )
func NewAccountsAPI(db *accounts.Database, feed *event.Feed) *API { const pathWalletRoot = "m/44'/60'/0'/0/0"
return &API{db, feed} 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. // API is class with methods available over RPC.
type API struct { type API struct {
manager *account.GethManager
config *params.NodeConfig
db *accounts.Database db *accounts.Database
feed *event.Feed feed *event.Feed
} }
@ -37,3 +49,151 @@ func (api *API) GetAccounts(ctx context.Context) ([]accounts.Account, error) {
func (api *API) DeleteAccount(ctx context.Context, address types.Address) error { func (api *API) DeleteAccount(ctx context.Context, address types.Address) error {
return api.db.DeleteAccount(address) return api.db.DeleteAccount(address)
} }
func (api *API) AddAccountWatch(ctx context.Context, address string, name string, color string) error {
account := accounts.Account{
Address: types.Address(common.HexToAddress(address)),
Type: "watch",
Name: name,
Color: color,
}
return api.SaveAccounts(ctx, []accounts.Account{account})
}
func (api *API) AddAccountWithMnemonic(
ctx context.Context,
mnemonic string,
password string,
name string,
color 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,
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,
) 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,
Color: color,
Path: pathDefaultWallet,
}
return api.SaveAccounts(ctx, []accounts.Account{account})
}
func (api *API) GenerateAccount(
ctx context.Context,
password string,
name string,
color 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
}
account := accounts.Account{
Address: types.Address(common.HexToAddress(infos[path].Address)),
PublicKey: types.HexBytes(infos[path].PublicKey),
Type: "generated",
Name: name,
Color: color,
Path: path,
}
err = api.db.SaveSetting("latest-derived-path", newDerivedPath)
if err != nil {
return err
}
return api.SaveAccounts(ctx, []accounts.Account{account})
}
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
}

View File

@ -3,7 +3,6 @@ package accounts
import ( import (
"errors" "errors"
"github.com/status-im/status-go/account"
"github.com/status-im/status-go/images" "github.com/status-im/status-go/images"
"github.com/status-im/status-go/multiaccounts" "github.com/status-im/status-go/multiaccounts"
) )
@ -13,14 +12,13 @@ var (
ErrUpdatingWrongAccount = errors.New("failed to update wrong account. Please login with that account first") ErrUpdatingWrongAccount = errors.New("failed to update wrong account. Please login with that account first")
) )
func NewMultiAccountsAPI(db *multiaccounts.Database, manager *account.Manager) *MultiAccountsAPI { func NewMultiAccountsAPI(db *multiaccounts.Database) *MultiAccountsAPI {
return &MultiAccountsAPI{db: db, manager: manager} return &MultiAccountsAPI{db: db}
} }
// MultiAccountsAPI is class with methods available over RPC. // MultiAccountsAPI is class with methods available over RPC.
type MultiAccountsAPI struct { type MultiAccountsAPI struct {
db *multiaccounts.Database db *multiaccounts.Database
manager *account.Manager
} }
func (api *MultiAccountsAPI) UpdateAccount(account multiaccounts.Account) error { func (api *MultiAccountsAPI) UpdateAccount(account multiaccounts.Account) error {

View File

@ -8,24 +8,26 @@ import (
"github.com/status-im/status-go/account" "github.com/status-im/status-go/account"
"github.com/status-im/status-go/multiaccounts" "github.com/status-im/status-go/multiaccounts"
"github.com/status-im/status-go/multiaccounts/accounts" "github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/params"
) )
// NewService initializes service instance. // NewService initializes service instance.
func NewService(db *accounts.Database, mdb *multiaccounts.Database, manager *account.Manager, feed *event.Feed) *Service { func NewService(db *accounts.Database, mdb *multiaccounts.Database, manager *account.GethManager, config *params.NodeConfig, feed *event.Feed) *Service {
return &Service{db, mdb, manager, feed} return &Service{db, mdb, manager, config, feed}
} }
// Service is a browsers service. // Service is a browsers service.
type Service struct { type Service struct {
db *accounts.Database db *accounts.Database
mdb *multiaccounts.Database mdb *multiaccounts.Database
manager *account.Manager manager *account.GethManager
config *params.NodeConfig
feed *event.Feed feed *event.Feed
} }
// Start a service. // Start a service.
func (s *Service) Start() error { func (s *Service) Start() error {
return nil return s.manager.InitKeystore(s.config.KeyStoreDir)
} }
// Stop a service. // Stop a service.
@ -44,12 +46,12 @@ func (s *Service) APIs() []rpc.API {
{ {
Namespace: "accounts", Namespace: "accounts",
Version: "0.1.0", Version: "0.1.0",
Service: NewAccountsAPI(s.db, s.feed), Service: NewAccountsAPI(s.manager, s.config, s.db, s.feed),
}, },
{ {
Namespace: "multiaccounts", Namespace: "multiaccounts",
Version: "0.1.0", Version: "0.1.0",
Service: NewMultiAccountsAPI(s.mdb, s.manager), Service: NewMultiAccountsAPI(s.mdb),
}, },
} }
} }