From c405029e37cb39ffc783817076454ba26e5b0f40 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Mon, 30 Nov 2020 17:28:59 -0400 Subject: [PATCH] fix: wallet derivation paths for generated accounts , imported seed phrases and private keys --- src/status/libstatus/accounts.nim | 28 ++++++++++++++----- .../components/AddAccountWithPrivateKey.qml | 2 +- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/status/libstatus/accounts.nim b/src/status/libstatus/accounts.nim index 667e62b0e2..5f52537058 100644 --- a/src/status/libstatus/accounts.nim +++ b/src/status/libstatus/accounts.nim @@ -229,15 +229,25 @@ proc MultiAccountImportPrivateKey*(privateKey: string): GeneratedAccount = error "Error getting account from private key", msg=e.msg -proc storeDerivedWallet*(account: GeneratedAccount, password: string, walletIndex: int) = +proc storeDerivedWallet*(account: GeneratedAccount, password: string, walletIndex: int, accountType: string): string = let hashedPassword = hashPassword(password) + let derivationPath = (if accountType == constants.GENERATED: "m/" else: "m/44'/60'/0'/0/") & $walletIndex let multiAccount = %* { "accountID": account.id, - "paths": ["m/44'/60'/0'/0/" & $walletIndex], + "paths": [derivationPath], "password": hashedPassword } let response = parseJson($nim_status.multiAccountStoreDerivedAccounts($multiAccount)); let error = response{"error"}.getStr + if error == "": + debug "Wallet stored succesfully" + return "m/44'/60'/0'/0/" & $walletIndex + raise newException(StatusGoException, "Error storing wallet: " & error) + +proc storePrivateKeyAccount*(account: GeneratedAccount, password: string) = + let hashedPassword = hashPassword(password) + let response = parseJson($nim_status.multiAccountStoreAccount($(%*{"accountID": account.id, "password": hashedPassword}))); + let error = response{"error"}.getStr if error == "": debug "Wallet stored succesfully" return @@ -245,9 +255,12 @@ proc storeDerivedWallet*(account: GeneratedAccount, password: string, walletInde proc saveAccount*(account: GeneratedAccount, password: string, color: string, accountType: string, isADerivedAccount = true, walletIndex: int = 0 ): DerivedAccount = try: - # Only store derived accounts. Private key accounts are not multiaccounts + var derivationPath = "m/44'/60'/0'/0/0" if (isADerivedAccount): - storeDerivedWallet(account, password, walletIndex) + # Only store derived accounts. Private key accounts are not multiaccounts + derivationPath = storeDerivedWallet(account, password, walletIndex, accountType) + elif accountType == constants.KEY: + storePrivateKeyAccount(account, password) var address = account.derived.defaultWallet.address var publicKey = account.derived.defaultWallet.publicKey @@ -256,14 +269,15 @@ proc saveAccount*(account: GeneratedAccount, password: string, color: string, ac address = account.address publicKey = account.publicKey - discard callPrivateRPC("accounts_saveAccounts", %* [ + echo "SAVING ACCOUNT" + echo callPrivateRPC("accounts_saveAccounts", %* [ [{ "color": color, "name": account.name, "address": address, "public-key": publicKey, "type": accountType, - "path": "m/44'/60'/0'/0/" & $walletIndex + "path": derivationPath }] ]) @@ -280,7 +294,7 @@ proc changeAccount*(account: WalletAccount): string = "address": account.address, "public-key": account.publicKey, "type": account.walletType, - "path": "m/44'/60'/0'/0/1" + "path": "m/44'/60'/0'/0/1" # <--- TODO: fix this. Derivation path is not supposed to change }] ]) diff --git a/ui/app/AppLayouts/Wallet/components/AddAccountWithPrivateKey.qml b/ui/app/AppLayouts/Wallet/components/AddAccountWithPrivateKey.qml index 2316a92924..a3bffcfc54 100644 --- a/ui/app/AppLayouts/Wallet/components/AddAccountWithPrivateKey.qml +++ b/ui/app/AppLayouts/Wallet/components/AddAccountWithPrivateKey.qml @@ -122,7 +122,7 @@ ModalPopup { return loading = false } - const error = walletModel.addAccountsFromPrivateKey(accountPKeyInput.text, passwordInput.text, accountNameInput.text, selectedColor) + const error = walletModel.addAccountsFromPrivateKey(accountPKeyInput.text, passwordInput.text, accountNameInput.text, accountColorInput.selectedColor) loading = false if (error) { errorSound.play()