-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(@desktop/keycard): import or restore a Keycard via a seed phrase
Fixes: #7029
- Loading branch information
1 parent
8600ef3
commit b2cb263
Showing
44 changed files
with
588 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
.../modules/shared_modules/keycard_popup/internal/creating_account_old_seed_phrase_state.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
type | ||
CreatingAccountOldSeedPhraseState* = ref object of State | ||
paths: seq[string] | ||
addresses: seq[string] | ||
|
||
proc newCreatingAccountOldSeedPhraseState*(flowType: FlowType, backState: State): CreatingAccountOldSeedPhraseState = | ||
result = CreatingAccountOldSeedPhraseState() | ||
result.setup(flowType, StateType.CreatingAccountOldSeedPhrase, backState) | ||
|
||
proc delete*(self: CreatingAccountOldSeedPhraseState) = | ||
self.State.delete | ||
|
||
proc resolvePaths(self: CreatingAccountOldSeedPhraseState, controller: Controller) = | ||
let kpForPRocessing = controller.getKeyPairForProcessing() | ||
var i = 0 | ||
for account in kpForPRocessing.getAccountsModel().getItems(): | ||
account.setPath(PATH_WALLET_ROOT & "/" & $i) | ||
self.paths.add(account.getPath()) | ||
i.inc | ||
|
||
proc findIndexForPath(self: CreatingAccountOldSeedPhraseState, path: string): int = | ||
var ind = -1 | ||
for p in self.paths: | ||
ind.inc | ||
if p == path: | ||
return ind | ||
return ind | ||
|
||
proc resolveAddresses(self: CreatingAccountOldSeedPhraseState, controller: Controller, keycardEvent: KeycardEvent): bool = | ||
if keycardEvent.generatedWalletAccounts.len != self.paths.len: | ||
return false | ||
let kpForPRocessing = controller.getKeyPairForProcessing() | ||
for account in kpForPRocessing.getAccountsModel().getItems(): | ||
let index = self.findIndexForPath(account.getPath()) | ||
if index == -1: | ||
## should never be here | ||
return false | ||
kpForPRocessing.setDerivedFrom(keycardEvent.masterKeyAddress) | ||
account.setAddress(keycardEvent.generatedWalletAccounts[index].address) | ||
account.setPubKey(keycardEvent.generatedWalletAccounts[index].publicKey) | ||
self.addresses.add(keycardEvent.generatedWalletAccounts[index].address) | ||
return true | ||
|
||
proc addAccountsToWallet(self: CreatingAccountOldSeedPhraseState, controller: Controller): bool = | ||
let kpForPRocessing = controller.getKeyPairForProcessing() | ||
for account in kpForPRocessing.getAccountsModel().getItems(): | ||
if not controller.addWalletAccount(name = account.getName(), | ||
address = account.getAddress(), | ||
path = account.getPath(), | ||
addressAccountIsDerivedFrom = kpForPRocessing.getDerivedFrom(), | ||
publicKey = account.getPubKey(), | ||
keyUid = kpForPRocessing.getKeyUid(), | ||
accountType = if account.getPath() == PATH_DEFAULT_WALLET: SEED else: GENERATED, | ||
color = account.getColor(), | ||
emoji = account.getEmoji()): | ||
return false | ||
return true | ||
|
||
proc doMigration(self: CreatingAccountOldSeedPhraseState, controller: Controller) = | ||
let kpForPRocessing = controller.getKeyPairForProcessing() | ||
var kpDto = KeyPairDto(keycardUid: controller.getKeycardUid(), | ||
keycardName: kpForPRocessing.getName(), | ||
keycardLocked: false, | ||
accountsAddresses: self.addresses, | ||
keyUid: kpForPRocessing.getKeyUid()) | ||
controller.addMigratedKeyPair(kpDto) | ||
|
||
proc runStoreMetadataFlow(self: CreatingAccountOldSeedPhraseState, controller: Controller) = | ||
let kpForPRocessing = controller.getKeyPairForProcessing() | ||
controller.runStoreMetadataFlow(kpForPRocessing.getName(), controller.getPin(), self.paths) | ||
|
||
method executePrePrimaryStateCommand*(self: CreatingAccountOldSeedPhraseState, controller: Controller) = | ||
if self.flowType == FlowType.SetupNewKeycardOldSeedPhrase: | ||
self.resolvePaths(controller) | ||
controller.runDeriveAccountFlow(bip44Paths = self.paths, controller.getPin()) | ||
|
||
method executePreSecondaryStateCommand*(self: CreatingAccountOldSeedPhraseState, controller: Controller) = | ||
## Secondary action is called after each async action during migration process, in this case after `addMigratedKeyPair`. | ||
if self.flowType == FlowType.SetupNewKeycardOldSeedPhrase: | ||
if controller.getAddingMigratedKeypairSuccess(): | ||
self.runStoreMetadataFlow(controller) | ||
|
||
method getNextSecondaryState*(self: CreatingAccountOldSeedPhraseState, controller: Controller): State = | ||
if self.flowType == FlowType.SetupNewKeycardOldSeedPhrase: | ||
if not controller.getAddingMigratedKeypairSuccess(): | ||
return createState(StateType.CreatingAccountOldSeedPhraseFailure, self.flowType, nil) | ||
|
||
method resolveKeycardNextState*(self: CreatingAccountOldSeedPhraseState, keycardFlowType: string, keycardEvent: KeycardEvent, | ||
controller: Controller): State = | ||
let state = ensureReaderAndCardPresenceAndResolveNextState(self, keycardFlowType, keycardEvent, controller) | ||
if not state.isNil: | ||
return state | ||
if self.flowType == FlowType.SetupNewKeycardOldSeedPhrase: | ||
if controller.getCurrentKeycardServiceFlow() == KCSFlowType.ExportPublic: | ||
if keycardFlowType == ResponseTypeValueKeycardFlowResult and | ||
keycardEvent.error.len == 0: | ||
if not self.resolveAddresses(controller, keycardEvent): | ||
return createState(StateType.CreatingAccountOldSeedPhraseFailure, self.flowType, nil) | ||
if not self.addAccountsToWallet(controller): | ||
return createState(StateType.CreatingAccountOldSeedPhraseFailure, self.flowType, nil) | ||
self.doMigration(controller) | ||
return nil # returning nil, cause we need to remain in this state | ||
if controller.getCurrentKeycardServiceFlow() == KCSFlowType.StoreMetadata: | ||
if keycardFlowType == ResponseTypeValueKeycardFlowResult and | ||
keycardEvent.error.len == 0: | ||
return createState(StateType.CreatingAccountOldSeedPhraseSuccess, self.flowType, nil) | ||
return createState(StateType.CreatingAccountOldSeedPhraseFailure, self.flowType, nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.