add account conversion (#2277)
This commit is contained in:
parent
2f6b32b1f5
commit
6c2e9652d0
|
@ -478,6 +478,39 @@ func TestLoginWithKey(t *testing.T) {
|
|||
require.Equal(t, crypto.PubkeyToAddress(chatKey.PublicKey), extkey.Address)
|
||||
}
|
||||
|
||||
func TestVerifyDatabasePassword(t *testing.T) {
|
||||
utils.Init()
|
||||
|
||||
b := NewGethStatusBackend()
|
||||
chatKey, err := gethcrypto.GenerateKey()
|
||||
require.NoError(t, err)
|
||||
walletKey, err := gethcrypto.GenerateKey()
|
||||
require.NoError(t, err)
|
||||
keyUIDHex := sha256.Sum256(gethcrypto.FromECDSAPub(&chatKey.PublicKey))
|
||||
keyUID := types.EncodeHex(keyUIDHex[:])
|
||||
main := multiaccounts.Account{
|
||||
KeyUID: keyUID,
|
||||
}
|
||||
tmpdir, err := ioutil.TempDir("", "verify-database-password-")
|
||||
require.NoError(t, err)
|
||||
defer os.Remove(tmpdir)
|
||||
conf, err := params.NewNodeConfig(tmpdir, 1777)
|
||||
require.NoError(t, err)
|
||||
keyhex := hex.EncodeToString(gethcrypto.FromECDSA(chatKey))
|
||||
|
||||
require.NoError(t, b.AccountManager().InitKeystore(conf.KeyStoreDir))
|
||||
b.UpdateRootDataDir(conf.DataDir)
|
||||
require.NoError(t, b.OpenAccounts())
|
||||
|
||||
address := crypto.PubkeyToAddress(walletKey.PublicKey)
|
||||
require.NoError(t, b.SaveAccountAndStartNodeWithKey(main, "test-pass", settings, conf, []accounts.Account{{Address: address, Wallet: true}}, keyhex))
|
||||
require.NoError(t, b.Logout())
|
||||
require.NoError(t, b.StopNode())
|
||||
|
||||
require.Error(t, b.VerifyDatabasePassword(main.KeyUID, "wrong-pass"))
|
||||
require.NoError(t, b.VerifyDatabasePassword(main.KeyUID, "test-pass"))
|
||||
}
|
||||
|
||||
func TestDeleteMulticcount(t *testing.T) {
|
||||
backend := NewGethStatusBackend()
|
||||
|
||||
|
@ -553,3 +586,92 @@ func TestDeleteMulticcount(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
require.Equal(t, 3, len(files))
|
||||
}
|
||||
|
||||
func TestConvertAccount(t *testing.T) {
|
||||
backend := NewGethStatusBackend()
|
||||
password := "123123"
|
||||
rootDataDir, err := ioutil.TempDir("", "test-keystore-dir")
|
||||
require.NoError(t, err)
|
||||
defer os.Remove(rootDataDir)
|
||||
|
||||
keyStoreDir := filepath.Join(rootDataDir, "keystore")
|
||||
|
||||
backend.rootDataDir = rootDataDir
|
||||
|
||||
err = backend.AccountManager().InitKeystore(keyStoreDir)
|
||||
require.NoError(t, err)
|
||||
|
||||
backend.AccountManager()
|
||||
accs, err := backend.AccountManager().
|
||||
AccountsGenerator().
|
||||
GenerateAndDeriveAddresses(12, 1, "", []string{"m/44'/60'/0'/0"})
|
||||
require.NoError(t, err)
|
||||
|
||||
generateAccount := accs[0]
|
||||
accountInfo, err := backend.AccountManager().
|
||||
AccountsGenerator().
|
||||
StoreAccount(generateAccount.ID, password)
|
||||
require.NoError(t, err)
|
||||
|
||||
account := multiaccounts.Account{
|
||||
Name: "foo",
|
||||
Timestamp: 1,
|
||||
KeyUID: generateAccount.KeyUID,
|
||||
}
|
||||
|
||||
err = backend.ensureAppDBOpened(account, password)
|
||||
require.NoError(t, err)
|
||||
|
||||
settings := accounts.Settings{
|
||||
Address: types.HexToAddress(accountInfo.Address),
|
||||
CurrentNetwork: "mainnet_rpc",
|
||||
DappsAddress: types.HexToAddress(accountInfo.Address),
|
||||
EIP1581Address: types.HexToAddress(accountInfo.Address),
|
||||
InstallationID: "d3efcff6-cffa-560e-a547-21d3858cbc51",
|
||||
KeyUID: account.KeyUID,
|
||||
LatestDerivedPath: 0,
|
||||
Name: "Jittery Cornflowerblue Kingbird",
|
||||
Networks: &networks,
|
||||
PhotoPath: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR/mAAAAjklEQVR4nOzXwQmFMBAAUZXUYh32ZB32ZB02sxYQQSZGsod55/91WFgSS0RM+SyjA56ZRZhFmEWYRRT6h+M6G16zrxv6fdJpmUWYRbxsYr13dKfanpN0WmYRZhGzXz6AWYRZRIfbaX26fT9Jk07LLMIsosPt9I/dTDotswizCG+nhFmEWYRZhFnEHQAA///z1CFkYamgfQAAAABJRU5ErkJggg==",
|
||||
PreviewPrivacy: false,
|
||||
PublicKey: accountInfo.PublicKey,
|
||||
SigningPhrase: "yurt joey vibe",
|
||||
WalletRootAddress: types.HexToAddress(accountInfo.Address)}
|
||||
|
||||
err = backend.saveAccountsAndSettings(
|
||||
settings,
|
||||
¶ms.NodeConfig{},
|
||||
nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = backend.OpenAccounts()
|
||||
require.NoError(t, err)
|
||||
|
||||
err = backend.SaveAccount(account)
|
||||
require.NoError(t, err)
|
||||
|
||||
files, err := ioutil.ReadDir(rootDataDir)
|
||||
require.NoError(t, err)
|
||||
require.NotEqual(t, 3, len(files))
|
||||
|
||||
keycardPassword := "0xcafecafe"
|
||||
|
||||
keycardAccount := account
|
||||
keycardAccount.KeycardPairing = "pairing"
|
||||
|
||||
keycardSettings := accounts.Settings{
|
||||
KeycardInstanceUID: "0xdeadbeef",
|
||||
KeycardPAiredOn: 1,
|
||||
KeycardPairing: "pairing",
|
||||
}
|
||||
|
||||
err = backend.ConvertToKeycardAccount(keyStoreDir, keycardAccount, keycardSettings, password, keycardPassword)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = os.Stat(keyStoreDir)
|
||||
require.Error(t, err)
|
||||
require.True(t, os.IsNotExist(err))
|
||||
|
||||
err = backend.ensureAppDBOpened(keycardAccount, keycardPassword)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
|
|
@ -463,6 +463,77 @@ func (b *GethStatusBackend) ChangeDatabasePassword(keyUID string, password strin
|
|||
return nil
|
||||
}
|
||||
|
||||
func (b *GethStatusBackend) ConvertToKeycardAccount(keyStoreDir string, account multiaccounts.Account, settings accounts.Settings, password string, newPassword string) error {
|
||||
err := b.multiaccountsDB.UpdateAccountKeycardPairing(account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = b.ensureAppDBOpened(account, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
accountDB := accounts.NewDB(b.appDB)
|
||||
err = accountDB.SaveSetting("keycard-instance_uid", settings.KeycardInstanceUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = accountDB.SaveSetting("keycard-paired_on", settings.KeycardPAiredOn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = accountDB.SaveSetting("keycard-pairing", settings.KeycardPairing)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = accountDB.SaveSetting("mnemonic", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = accountDB.DeleteSeedAndKeyAccounts()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dbPath := filepath.Join(b.rootDataDir, fmt.Sprintf("%s.db", account.KeyUID))
|
||||
err = appdatabase.ChangeDatabasePassword(dbPath, password, newPassword)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = b.closeAppDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.RemoveAll(keyStoreDir)
|
||||
}
|
||||
|
||||
func (b *GethStatusBackend) VerifyDatabasePassword(keyUID string, password string) error {
|
||||
err := b.ensureAppDBOpened(multiaccounts.Account{KeyUID: keyUID}, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
accountsDB := accounts.NewDB(b.appDB)
|
||||
_, err = accountsDB.GetWalletAddress()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = b.closeAppDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *GethStatusBackend) SaveAccountAndStartNodeWithKey(acc multiaccounts.Account, password string, settings accounts.Settings, nodecfg *params.NodeConfig, subaccs []accounts.Account, keyHex string) error {
|
||||
err := b.SaveAccount(acc)
|
||||
if err != nil {
|
||||
|
|
|
@ -175,6 +175,10 @@ func VerifyAccountPassword(keyStoreDir, address, password string) string {
|
|||
return makeJSONResponse(err)
|
||||
}
|
||||
|
||||
func VerifyDatabasePassword(keyUID, password string) string {
|
||||
return makeJSONResponse(statusBackend.VerifyDatabasePassword(keyUID, password))
|
||||
}
|
||||
|
||||
// MigrateKeyStoreDir migrates key files to a new directory
|
||||
func MigrateKeyStoreDir(accountData, password, oldDir, newDir string) string {
|
||||
var account multiaccounts.Account
|
||||
|
@ -663,3 +667,22 @@ func ChangeDatabasePassword(keyUID, password, newPassword string) string {
|
|||
}
|
||||
return makeJSONResponse(nil)
|
||||
}
|
||||
|
||||
func ConvertToKeycardAccount(keyStoreDir, accountData, settingsJSON, password, newPassword string) string {
|
||||
var account multiaccounts.Account
|
||||
err := json.Unmarshal([]byte(accountData), &account)
|
||||
if err != nil {
|
||||
return makeJSONResponse(err)
|
||||
}
|
||||
var settings accounts.Settings
|
||||
err = json.Unmarshal([]byte(settingsJSON), &settings)
|
||||
if err != nil {
|
||||
return makeJSONResponse(err)
|
||||
}
|
||||
|
||||
err = statusBackend.ConvertToKeycardAccount(keyStoreDir, account, settings, password, newPassword)
|
||||
if err != nil {
|
||||
return makeJSONResponse(err)
|
||||
}
|
||||
return makeJSONResponse(nil)
|
||||
}
|
||||
|
|
|
@ -551,6 +551,11 @@ func (db *Database) DeleteAccount(address types.Address) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (db *Database) DeleteSeedAndKeyAccounts() error {
|
||||
_, err := db.db.Exec("DELETE FROM accounts WHERE type = ? OR type = ?", accountTypeSeed, accountTypeKey)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *Database) GetNotificationsEnabled() (bool, error) {
|
||||
var result bool
|
||||
err := db.db.QueryRow("SELECT notifications_enabled FROM settings WHERE synthetic_id = 'id'").Scan(&result)
|
||||
|
|
|
@ -130,6 +130,11 @@ func (db *Database) UpdateAccount(account Account) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (db *Database) UpdateAccountKeycardPairing(account Account) error {
|
||||
_, err := db.db.Exec("UPDATE accounts SET keycardPairing = ? WHERE keyUid = ?", account.KeycardPairing, account.KeyUID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *Database) UpdateAccountTimestamp(keyUID string, loginTimestamp int64) error {
|
||||
_, err := db.db.Exec("UPDATE accounts SET loginTimestamp = ? WHERE keyUid = ?", loginTimestamp, keyUID)
|
||||
return err
|
||||
|
|
Loading…
Reference in New Issue