refactor: update keypair handling according to the latest account management updates

- Modified `addNewPrivateKeyKeypair` and `addNewSeedPhraseKeypair` to accept `AccountCreationDetails` instead of individual parameters.
- Updated related calls in the controller and service layers.
- Removed unused calls/unnecessary parameters.
This commit is contained in:
Sale Djenic
2025-08-13 16:39:08 +02:00
committed by saledjenic
parent 705bebfc15
commit 7e0c1909d0
10 changed files with 83 additions and 100 deletions
@@ -179,29 +179,29 @@ proc addWalletAccount*(self: Controller, createKeystoreFile, doPasswordHashing:
return false
return true
proc addNewPrivateKeyKeypair*(self: Controller, privateKey: string, doPasswordHashing: bool, keyUid, keypairName,
rootWalletMasterKey: string, account: WalletAccountDto): bool =
proc addNewPrivateKeyKeypair*(self: Controller, privateKey: string, doPasswordHashing: bool,
keypairName: string, accountCreationDetails: AccountCreationDetails): bool =
let password = self.getPassword() # password must not be empty in this context
if password.len == 0:
info "cannot create keystore file if provided password is empty", keypairName=keypairName, keyUid=keyUid
info "cannot create keystore file if provided password is empty", keypairName=keypairName
return false
let err = self.walletAccountService.addNewPrivateKeyKeypair(privateKey, password, doPasswordHashing, keyUid,
keyPairName, rootWalletMasterKey, account)
let err = self.walletAccountService.addNewPrivateKeyKeypair(privateKey, password, doPasswordHashing, keypairName,
accountCreationDetails)
if err.len > 0:
info "adding new keypair from private key failed", keypairName=keypairName, keyUid=keyUid
info "adding new keypair from private key failed", keypairName=keypairName
return false
return true
proc addNewSeedPhraseKeypair*(self: Controller, seedPhrase: string, doPasswordHashing: bool, keyUid, keypairName,
rootWalletMasterKey: string, accounts: seq[WalletAccountDto]): bool =
proc addNewSeedPhraseKeypair*(self: Controller, seedPhrase: string, doPasswordHashing: bool, keypairName: string,
accountCreationDetails: AccountCreationDetails): bool =
let password = self.getPassword() # password must not be empty in this context
if password.len == 0:
info "cannot create keystore file if provided password is empty", keypairName=keypairName, keyUid=keyUid
info "cannot create keystore file if provided password is empty", keypairName=keypairName
return false
let err = self.walletAccountService.addNewSeedPhraseKeypair(seedPhrase, password, doPasswordHashing, keyUid,
keypairName, rootWalletMasterKey, accounts)
let err = self.walletAccountService.addNewSeedPhraseKeypair(seedPhrase, password, doPasswordHashing, keypairName,
accountCreationDetails)
if err.len > 0:
info "adding new keypair from seed phrase failed", keypairName=keypairName, keyUid=keyUid
info "adding new keypair from seed phrase failed", keypairName=keypairName
return false
return true
@@ -624,14 +624,8 @@ proc doAddAccount[T](self: Module[T]) =
success = self.controller.addNewPrivateKeyKeypair(
privateKey = self.controller.getGeneratedAccount().privateKey,
doPasswordHashing = not singletonInstance.userProfile.getIsKeycardUser(),
keyUid = keyUid,
keypairName = keypairName,
rootWalletMasterKey = rootWalletMasterKey,
account = WalletAccountDto(
address: address,
keyUid: keyUid,
publicKey: publicKey,
walletType: accountType,
accountCreationDetails = AccountCreationDetails(
path: path,
name: self.view.getAccountName(),
colorId: self.view.getSelectedColorId(),
@@ -644,19 +638,13 @@ proc doAddAccount[T](self: Module[T]) =
success = self.controller.addNewSeedPhraseKeypair(
seedPhrase = self.controller.getSeedPhrase(),
doPasswordHashing = not singletonInstance.userProfile.getIsKeycardUser(),
keyUid = keyUid,
keypairName = keypairName,
rootWalletMasterKey = rootWalletMasterKey,
accounts = @[WalletAccountDto(
address: address,
keyUid: keyUid,
publicKey: publicKey,
walletType: accountType,
accountCreationDetails = AccountCreationDetails(
path: path,
name: self.view.getAccountName(),
colorId: self.view.getSelectedColorId(),
emoji: self.view.getSelectedEmoji()
)]
)
)
if not success:
error "failed to store new seed phrase account", address=selectedAddrItem.getAddress()
@@ -738,12 +738,11 @@ proc updateKeycardUid*(self: Controller, keyUid: string, keycardUid: string) =
self.tmpKeycardUid = keycardUid
info "update keycard uid failed", oldKeycardUid=self.tmpKeycardUid, newKeycardUid=keycardUid
proc addNewSeedPhraseKeypair*(self: Controller, seedPhrase, keyUid, keypairName, rootWalletMasterKey: string,
proc addNewKeycardStoredKeypair*(self: Controller, keyUid, keypairName, rootWalletMasterKey: string,
accounts: seq[WalletAccountDto]): bool =
let err = self.walletAccountService.addNewSeedPhraseKeypair(seedPhrase, password = "", doPasswordHashing = false, keyUid,
keypairName, rootWalletMasterKey, accounts)
let err = self.walletAccountService.addNewKeycardStoredKeypair(keyUid, keypairName, rootWalletMasterKey, accounts)
if err.len > 0:
info "adding new keypair from seed phrase failed", keypairName=keypairName, keyUid=keyUid
info "adding new keypair from keycard failed", keypairName=keypairName, keyUid=keyUid
return false
return true
@@ -55,8 +55,7 @@ proc addAccountsToWallet(self: CreatingAccountNewSeedPhraseState, controller: Co
colorId: account.getColorId(),
emoji: account.getEmoji()
))
return controller.addNewSeedPhraseKeypair(
seedPhrase = "",
return controller.addNewKeycardStoredKeypair(
keyUid = kpForProcessing.getKeyUid(),
keypairName = kpForProcessing.getName(),
rootWalletMasterKey = kpForProcessing.getDerivedFrom(),
@@ -55,8 +55,7 @@ proc addAccountsToWallet(self: CreatingAccountOldSeedPhraseState, controller: Co
colorId: account.getColorId(),
emoji: account.getEmoji()
))
return controller.addNewSeedPhraseKeypair(
seedPhrase = "",
return controller.addNewKeycardStoredKeypair(
keyUid = kpForProcessing.getKeyUid(),
keypairName = kpForProcessing.getName(),
rootWalletMasterKey = kpForProcessing.getDerivedFrom(),
@@ -25,8 +25,7 @@ proc addAccountsToWallet(self: ImportingFromKeycardState, controller: Controller
colorId: account.getColorId(),
emoji: account.getEmoji()
))
return controller.addNewSeedPhraseKeypair(
seedPhrase = "",
return controller.addNewKeycardStoredKeypair(
keyUid = kpForProcessing.getKeyUid(),
keypairName = kpForProcessing.getName(),
rootWalletMasterKey = kpForProcessing.getDerivedFrom(),
@@ -36,6 +36,12 @@ type
position*: int
hideFromTotalBalance*: bool
type AccountCreationDetails* = object
name*: string
path*: string
emoji*: string
colorId*: string
proc toWalletAccountDto*(jsonObj: JsonNode): WalletAccountDto =
result = WalletAccountDto()
discard jsonObj.getProp("name", result.name)
@@ -55,7 +61,13 @@ proc toWalletAccountDto*(jsonObj: JsonNode): WalletAccountDto =
discard jsonObj.getProp("createdAt", result.createdAt)
discard jsonObj.getProp("position", result.position)
discard jsonObj.getProp("hidden", result.hideFromTotalBalance)
result.assetsLoading = true
proc toAccountCreationDetails*(jsonObj: JsonNode): AccountCreationDetails =
result = AccountCreationDetails()
discard jsonObj.getProp("name", result.name)
discard jsonObj.getProp("path", result.path)
discard jsonObj.getProp("emoji", result.emoji)
discard jsonObj.getProp("colorId", result.colorId)
proc `$`*(self: WalletAccountDto): string =
result = fmt"""WalletAccountDto[
@@ -342,9 +342,9 @@ proc addWalletAccount*(self: Service, password: string, doPasswordHashing: bool,
error "error: ", procName="addWalletAccount", errName=e.name, errDesription=e.msg
return e.msg
## Mandatory fields for account: `address`, `keyUid`, `walletType`, `path`, `publicKey`, `name`, `emoji`, `colorId`
## Mandatory fields for account: `path`, `name`, `emoji`, `colorId`
proc addNewPrivateKeyKeypair*(self: Service, privateKey, password: string, doPasswordHashing: bool,
keyUid, keypairName, rootWalletMasterKey: string, account: WalletAccountDto): string =
keypairName: string, accountCreationDetails: AccountCreationDetails): string =
if password.len == 0:
let err = "for adding new private key account, password must be provided"
error "error", err
@@ -353,11 +353,7 @@ proc addNewPrivateKeyKeypair*(self: Service, privateKey, password: string, doPas
if doPasswordHashing:
finalPassword = utils.hashPassword(password)
try:
var response = status_go_accounts.importPrivateKey(privateKey, finalPassword)
if not response.error.isNil:
error "status-go error importing private key", procName="addNewPrivateKeyKeypair", errCode=response.error.code, errDesription=response.error.message
return response.error.message
response = status_go_accounts.addKeypair(finalPassword, keyUid, keypairName, KeypairTypeKey, rootWalletMasterKey, @[account])
var response = status_go_accounts.addKeypairViaPrivateKey(privateKey, finalPassword, keypairName, accountCreationDetails)
if not response.error.isNil:
error "status-go error adding keypair", procName="addNewPrivateKeyKeypair", errCode=response.error.code, errDesription=response.error.message
return response.error.message
@@ -386,27 +382,36 @@ proc makePrivateKeyKeypairFullyOperable*(self: Service, keyUid, privateKey, pass
error "error: ", procName="makePrivateKeyKeypairFullyOperable", errName=e.name, errDesription=e.msg
return e.msg
## Mandatory fields for all accounts: `address`, `keyUid`, `walletType`, `path`, `publicKey`, `name`, `emoji`, `colorId`
## Mandatory fields for all accounts are `path`, `name`, `emoji`, `colorId`
proc addNewSeedPhraseKeypair*(self: Service, seedPhrase, password: string, doPasswordHashing: bool,
keyUid, keypairName, rootWalletMasterKey: string, accounts: seq[WalletAccountDto]): string =
keypairName: string, accountCreationDetails: AccountCreationDetails): string =
var finalPassword = password
if password.len > 0 and doPasswordHashing:
finalPassword = utils.hashPassword(password)
try:
if seedPhrase.len > 0 and password.len > 0:
let response = status_go_accounts.importMnemonic(seedPhrase, finalPassword)
if not response.error.isNil:
error "status-go error importing private key", procName="addNewSeedPhraseKeypair", errCode=response.error.code, errDesription=response.error.message
return response.error.message
let response = status_go_accounts.addKeypair(finalPassword, keyUid, keypairName, KeypairTypeSeed, rootWalletMasterKey, accounts)
var response = status_go_accounts.addKeypairViaSeedPhrase(seedPhrase, finalPassword, keypairName, accountCreationDetails)
if not response.error.isNil:
error "status-go error adding keypair", procName="addNewSeedPhraseKeypair", errCode=response.error.code, errDesription=response.error.message
return response.error.message
self.addNewKeypairsAccountsToLocalStoreAndNotify()
return ""
except Exception as e:
error "error: ", procName="addNewSeedPhraseKeypair", errName=e.name, errDesription=e.msg
return e.msg
proc addNewKeycardStoredKeypair*(self: Service, keyUid, keypairName, rootWalletMasterKey: string, accounts: seq[WalletAccountDto]): string =
try:
var response = status_go_accounts.addKeypairStoredToKeycard(keyUid, rootWalletMasterKey, keypairName, accounts)
if not response.error.isNil:
error "status-go error adding keypair", procName="addNewKeycardStoredKeypair", errCode=response.error.code, errDesription=response.error.message
return response.error.message
for i in 0 ..< accounts.len:
self.addNewKeypairsAccountsToLocalStoreAndNotify()
return ""
except Exception as e:
error "error: ", procName="addNewSeedPhraseKeypair", errName=e.name, errDesription=e.msg
error "error: ", procName="addNewKeycardStoredKeypair", errName=e.name, errDesription=e.msg
return e.msg
proc makeSeedPhraseKeypairFullyOperable*(self: Service, keyUid, mnemonic, password: string, doPasswordHashing: bool): string =
+27 -45
View File
@@ -65,41 +65,35 @@ proc addAccount*(password, name, address, path, publicKey, keyUid, accountType,
]
return core.callPrivateRPC("accounts_addAccount", payload)
## Adds a new keypair and creates a Keystore file if password is provided, otherwise it only creates a new keypair. Notifies paired devices.
proc addKeypair*(password, keyUid, keypairName, keypairType, rootWalletMasterKey: string, accounts: seq[WalletAccountDto]):
RpcResponse[JsonNode] =
var kpJson = %* {
"key-uid": keyUid,
"name": keypairName,
"type": keypairType,
"derived-from": rootWalletMasterKey,
"last-used-derivation-index": 0, #when adding new keypair it's always 0
#"synced-from": "", present on the status-go side, used for synchronization, no need to set it here
#"clock": 0, we leave this empty, set on the status-go side
"accounts": []
}
proc addKeypairViaPrivateKey*(privateKey, password, name: string, accountCreationDetails: AccountCreationDetails): RpcResponse[JsonNode] =
let payload = %* [privateKey, password, name, accountCreationDetails]
return core.callPrivateRPC("accounts_addKeypairViaPrivateKey", payload)
for acc in accounts:
kpJson["accounts"].add(
%*{
"address": acc.address,
"key-uid": keyUid,
"wallet": false, #this refers to the default wallet account and it's set at the moment of Status chat account creation, cannot be changed later
"chat": false, #this refers to Status chat account, set when the Status account is created, cannot be changed later
"type": acc.walletType,
"path": acc.path,
"public-key": acc.publicKey,
"name": acc.name,
"emoji": acc.emoji,
"colorId": acc.colorId,
"hidden": acc.hideFromTotalBalance
#"clock" we leave this empty, set on the status-go side
#"removed" present on the status-go side, used for synchronization, no need to set it here
}
proc addKeypairViaSeedPhrase*(seedPhrase, password, name: string, accountCreationDetails: AccountCreationDetails): RpcResponse[JsonNode] =
let payload = %* [seedPhrase, password, name, accountCreationDetails]
return core.callPrivateRPC("accounts_addKeypairViaSeedPhrase", payload)
proc addKeypairStoredToKeycard*(keyUID, masterAddress, name: string, walletAccounts: seq[WalletAccountDto]): RpcResponse[JsonNode] =
var accountsJson: JsonNode = %* []
for acc in walletAccounts:
accountsJson.add(%*{
"address": acc.address,
"key-uid": acc.keyUID,
"wallet": acc.isWallet,
"chat": acc.isChat,
"type": acc.walletType,
"path": acc.path,
"public-key": acc.publicKey,
"name": acc.name,
"emoji": acc.emoji,
"colorId": acc.colorId,
"hidden": acc.hideFromTotalBalance
# other fields are set on the status-go side
}
)
let payload = %* [password, kpJson]
return core.callPrivateRPC("accounts_addKeypair", payload)
let payload = %* [keyUID, masterAddress, name, accountsJson]
return core.callPrivateRPC("accounts_addKeypairStoredToKeycard", payload)
## Adds a new account without creating a Keystore file and notifies paired devices
proc addAccountWithoutKeystoreFileCreation*(name, address, path, publicKey, keyUid, accountType, colorId, emoji: string, hideFromTotalBalance: bool):
@@ -127,7 +121,7 @@ proc updateAccount*(name, address, path: string, publicKey, keyUid, accountType,
#"removed" present on the status-go side, used for synchronization, no need to set it here
}
]
return core.callPrivateRPC("accounts_saveAccount", payload)
return core.callPrivateRPC("accounts_updateAccount", payload)
proc decompressPk*(publicKey: string): RpcResponse[string] =
discard
@@ -202,12 +196,6 @@ proc getRandomMnemonic*(): RpcResponse[JsonNode] =
let payload = %* []
return core.callPrivateRPC("accounts_getRandomMnemonic", payload)
## Imports a new mnemonic and creates local keystore file.
proc importMnemonic*(mnemonic, password: string):
RpcResponse[JsonNode] =
let payload = %* [mnemonic, password]
return core.callPrivateRPC("accounts_importMnemonic", payload)
proc makeSeedPhraseKeypairFullyOperable*(mnemonic, password: string):
RpcResponse[JsonNode] =
let payload = %* [mnemonic, password]
@@ -237,12 +225,6 @@ proc createAccountFromMnemonicAndDeriveAccountsForPaths*(mnemonic: string, paths
error "error doing rpc request", methodName = "createAccountFromMnemonicAndDeriveAccountsForPaths", exception=e.msg
raise newException(RpcException, e.msg)
## Imports a new private key and creates local keystore file.
proc importPrivateKey*(privateKey, password: string):
RpcResponse[JsonNode] =
let payload = %* [privateKey, password]
return core.callPrivateRPC("accounts_importPrivateKey", payload)
proc makePrivateKeyKeypairFullyOperable*(privateKey, password: string):
RpcResponse[JsonNode] =
let payload = %* [privateKey, password]