Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi account support #403

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DashSyncCurrentCommit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
08f34489b64061aa549735dd79a19bfb266f5e31
11837d26130f55c25f763109878920920b039b34
12 changes: 10 additions & 2 deletions DashWallet/Sources/Models/DWEnvironment.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#import "DWEnvironment.h"

#import "DWGlobalOptions.h"

#define CURRENT_CHAIN_TYPE_KEY @"CURRENT_CHAIN_TYPE_KEY"

NSNotificationName const DWCurrentNetworkDidChangeNotification = @"DWCurrentNetworkDidChangeNotification";
Expand Down Expand Up @@ -88,7 +90,10 @@ - (DSWallet *)currentWallet {
}

- (DSAccount *)currentAccount {
return [[self.currentWallet accounts] firstObject];
NSDictionary<NSNumber *, DSAccount *> *accounts = self.currentWallet.orderedAccounts;
NSInteger accountIndex = [DWGlobalOptions sharedInstance].currentAccountIndex;
DSAccount *account = accounts[@(accountIndex)];
return account;
}

- (NSArray *)allWallets {
Expand Down Expand Up @@ -187,7 +192,10 @@ - (void)switchToNetwork:(DSChainType)chainType withIdentifier:(NSString *)identi
protocolVersion:70216
minProtocolVersion:70216
sporkAddress:@"yQuAu9YAMt4yEiXBeDp3q5bKpo7jsC2eEj"
sporkPrivateKey:@"cVk6u16fT1Pwd9MugowSt7VmNzN8ozE4wJjfJGC97Hf43oxRMjar"];
sporkPrivateKey:@"cVk6u16fT1Pwd9MugowSt7VmNzN8ozE4wJjfJGC97Hf43oxRMjar"
instantSendLockQuorumType:DSLLMQType_5_60
chainLockQuorumType:DSLLMQType_5_60
platformQuorumType:DSLLMQType_5_60];
[destinationChain setDevnetNetworkName:@"Evonet"];
}
break;
Expand Down
2 changes: 2 additions & 0 deletions DashWallet/Sources/Models/DWGlobalOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, assign, getter=isResyncingWallet) BOOL resyncingWallet;

@property (nonatomic, assign) NSInteger currentAccountIndex;

// Non-dynamic

- (BOOL)lockScreenDisabled;
Expand Down
3 changes: 3 additions & 0 deletions DashWallet/Sources/Models/DWGlobalOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ @implementation DWGlobalOptions
@dynamic dashpayRegistrationCompleted;
@dynamic mostRecentViewedNotificationDate;
@dynamic resyncingWallet;
@dynamic currentAccountIndex;

#pragma mark - Init

Expand All @@ -51,6 +52,7 @@ - (instancetype)init {
DW_KEYPATH(self, localNotificationsEnabled) : @YES,
DW_KEYPATH(self, autoLockAppInterval) : @60, // 1 min
DW_KEYPATH(self, shouldDisplayOnboarding) : @YES,
DW_KEYPATH(self, currentAccountIndex) : @0,
};

self = [super initWithUserDefaults:nil defaults:defaults];
Expand Down Expand Up @@ -119,6 +121,7 @@ - (void)restoreToDefaults {
self.dashpayRegistrationCompleted = NO;
self.mostRecentViewedNotificationDate = nil;
self.resyncingWallet = NO;
self.currentAccountIndex = 0;
}

@end
Expand Down
6 changes: 5 additions & 1 deletion DashWallet/Sources/UI/Home/Models/DWHomeModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ - (void)forceStartSyncingActivity {
DWSyncModel *syncModel = (DWSyncModel *)self.syncModel;
NSAssert([syncModel isKindOfClass:DWSyncModel.class], @"Internal inconsistency");
[syncModel forceStartSyncingActivity];

[self updateBalance];
[self reloadTxDataSource];
}

- (void)walletDidWipe {
Expand Down Expand Up @@ -467,7 +470,8 @@ - (void)reloadTxDataSource {
return [(NSNumber *)obj1 compare:obj2];
}
}];
NSArray<DSTransaction *> *transactions = [wallet.allTransactions sortedArrayUsingDescriptors:@[ sortDescriptor ]];
DSAccount *account = [DWEnvironment sharedInstance].currentAccount;
NSArray<DSTransaction *> *transactions = [account.allTransactions sortedArrayUsingDescriptors:@[ sortDescriptor ]];

BOOL shouldAnimate = YES;
DSTransaction *prevTransaction = self.dataSource.items.firstObject;
Expand Down
2 changes: 2 additions & 0 deletions DashWallet/Sources/UI/Menu/Settings/DWSettingsMenuModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ NS_ASSUME_NONNULL_BEGIN
@property (readonly, copy, nonatomic) NSString *networkName;
@property (readonly, copy, nonatomic) NSString *localCurrencyCode;

@property (assign, nonatomic) NSInteger accountIndex;

@property (assign, nonatomic) BOOL notificationsEnabled;

+ (void)switchToMainnetWithCompletion:(void (^)(BOOL success))completion;
Expand Down
13 changes: 13 additions & 0 deletions DashWallet/Sources/UI/Menu/Settings/DWSettingsMenuModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ - (NSString *)localCurrencyCode {
return [DSPriceManager sharedInstance].localCurrencyCode;
}

- (NSInteger)accountIndex {
return [DWGlobalOptions sharedInstance].currentAccountIndex;
}

- (void)setAccountIndex:(NSInteger)accountIndex {
BOOL hasChanged = [DWGlobalOptions sharedInstance].currentAccountIndex != accountIndex;
[DWGlobalOptions sharedInstance].currentAccountIndex = accountIndex;
if (hasChanged) {
[[NSNotificationCenter defaultCenter] postNotificationName:DWCurrentNetworkDidChangeNotification
object:nil];
}
}

- (BOOL)notificationsEnabled {
return [DWGlobalOptions sharedInstance].localNotificationsEnabled;
}
Expand Down
102 changes: 102 additions & 0 deletions DashWallet/Sources/UI/Menu/Settings/DWSettingsMenuViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#import <DashSync/DashSync.h>

#import "DWAboutViewController.h"
#import "DWEnvironment.h"
#import "DWFormTableViewController.h"
#import "DWLocalCurrencyViewController.h"
#import "DWSettingsMenuModel.h"
Expand All @@ -33,6 +34,7 @@ @interface DWSettingsMenuViewController () <DWLocalCurrencyViewControllerDelegat
@property (nonatomic, strong) DWFormTableViewController *formController;
@property (strong, nonatomic) DWSelectorFormCellModel *localCurrencyCellModel;
@property (strong, nonatomic) DWSelectorFormCellModel *switchNetworkCellModel;
@property (strong, nonatomic) DWSelectorFormCellModel *switchAccountCellModel;

@end

Expand Down Expand Up @@ -90,6 +92,24 @@ - (DWSettingsMenuModel *)model {
[items addObject:cellModel];
}

{
DWSelectorFormCellModel *cellModel = [[DWSelectorFormCellModel alloc] initWithTitle:NSLocalizedString(@"Account Index", nil)];
self.switchAccountCellModel = cellModel;
[self updateSwitchAccountCellModel];
cellModel.accessoryType = DWSelectorFormAccessoryType_DisclosureIndicator;
cellModel.didSelectBlock = ^(DWSelectorFormCellModel *_Nonnull cellModel, NSIndexPath *_Nonnull indexPath) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) {
return;
}

UITableView *tableView = self.formController.tableView;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[strongSelf showChangeAccountFromSourceView:tableView sourceRect:cell.frame];
};
[items addObject:cellModel];
}

{
DWSelectorFormCellModel *cellModel = [[DWSelectorFormCellModel alloc] initWithTitle:NSLocalizedString(@"Network", nil)];
self.switchNetworkCellModel = cellModel;
Expand Down Expand Up @@ -185,6 +205,10 @@ - (void)updateSwitchNetworkCellModel {
self.switchNetworkCellModel.subTitle = self.model.networkName;
}

- (void)updateSwitchAccountCellModel {
self.switchAccountCellModel.subTitle = [NSString stringWithFormat:@"%ld", self.model.accountIndex];
}

- (void)rescanBlockchainActionFromSourceView:(UIView *)sourceView sourceRect:(CGRect)sourceRect {
[DWSettingsMenuModel rescanBlockchainActionFromController:self
sourceView:sourceView
Expand All @@ -207,6 +231,50 @@ - (void)showAboutController {
[self.navigationController pushViewController:aboutViewController animated:YES];
}

- (void)showChangeAccountFromSourceView:(UIView *)sourceView sourceRect:(CGRect)sourceRect {
DSWallet *wallet = [DWEnvironment sharedInstance].currentWallet;
uint32_t lastAccount = wallet.lastAccountNumber;

UIAlertController *actionSheet = [UIAlertController
alertControllerWithTitle:NSLocalizedString(@"Select Account", nil)
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];

for (uint32_t i = 0; i <= lastAccount; i++) {
NSString *title = [NSString stringWithFormat:NSLocalizedString(@"Account # %u", nil), i];
UIAlertAction *selectAccount = [UIAlertAction
actionWithTitle:title
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) {
self.model.accountIndex = i;
[self updateSwitchAccountCellModel];
}];
[actionSheet addAction:selectAccount];
}

UIAlertAction *addAccount = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Add New Account", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self addNewAccount];
}];
[addAccount setValue:[UIColor dw_greenColor]
forKey:[@"title" stringByAppendingString:@"TextColor"]];
[actionSheet addAction:addAccount];

UIAlertAction *cancel = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Cancel", nil)
style:UIAlertActionStyleCancel
handler:nil];
[actionSheet addAction:cancel];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
actionSheet.popoverPresentationController.sourceView = sourceView;
actionSheet.popoverPresentationController.sourceRect = sourceRect;
}
[self presentViewController:actionSheet animated:YES completion:nil];
}

- (void)showChangeNetworkFromSourceView:(UIView *)sourceView sourceRect:(CGRect)sourceRect {
UIAlertController *actionSheet = [UIAlertController
alertControllerWithTitle:NSLocalizedString(@"Network", nil)
Expand All @@ -218,6 +286,7 @@ - (void)showChangeNetworkFromSourceView:(UIView *)sourceView sourceRect:(CGRect)
handler:^(UIAlertAction *action) {
[DWSettingsMenuModel switchToMainnetWithCompletion:^(BOOL success) {
if (success) {
self.model.accountIndex = 0;
[self updateSwitchNetworkCellModel];
}
}];
Expand All @@ -228,6 +297,7 @@ - (void)showChangeNetworkFromSourceView:(UIView *)sourceView sourceRect:(CGRect)
handler:^(UIAlertAction *action) {
[DWSettingsMenuModel switchToTestnetWithCompletion:^(BOOL success) {
if (success) {
self.model.accountIndex = 0;
[self updateSwitchNetworkCellModel];
}
}];
Expand Down Expand Up @@ -259,6 +329,38 @@ - (void)showChangeNetworkFromSourceView:(UIView *)sourceView sourceRect:(CGRect)
[self presentViewController:actionSheet animated:YES completion:nil];
}

- (void)addNewAccount {
DSWallet *wallet = [DWEnvironment sharedInstance].currentWallet;
DSChain *chain = wallet.chain;
uint32_t addAccountNumber = wallet.lastAccountNumber + 1;
NSArray *derivationPaths = [wallet.chain standardDerivationPathsForAccountNumber:addAccountNumber];
DSAccount *addAccount = [DSAccount accountWithAccountNumber:addAccountNumber
withDerivationPaths:derivationPaths
inContext:wallet.chain.chainManagedObjectContext];
[wallet seedPhraseAfterAuthenticationWithPrompt:NSLocalizedString(@"Confirm adding new account", nil)
completion:^(NSString *_Nullable seedPhrase) {
if (seedPhrase == nil) {
return;
}

NSData *derivedKeyData = [[DSBIP39Mnemonic sharedInstance] deriveKeyFromPhrase:seedPhrase withPassphrase:nil];
for (DSDerivationPath *derivationPath in addAccount.fundDerivationPaths) {
[derivationPath generateExtendedPublicKeyFromSeed:derivedKeyData
storeUnderWalletUniqueId:wallet.uniqueIDString];
}
if ([chain isEvolutionEnabled]) {
[addAccount.masterContactsDerivationPath generateExtendedPublicKeyFromSeed:derivedKeyData
storeUnderWalletUniqueId:wallet.uniqueIDString];
}

[wallet addAccount:addAccount];
[addAccount loadDerivationPaths];

self.model.accountIndex = addAccountNumber;
[self updateSwitchAccountCellModel];
}];
}

@end

NS_ASSUME_NONNULL_END
15 changes: 15 additions & 0 deletions DashWallet/bg.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
/* No comment provided by engineer. */
"Accept" = "Accept";

/* No comment provided by engineer. */
"Account # %u" = "Account # %u";

/* No comment provided by engineer. */
"Account Index" = "Account Index";

/* No comment provided by engineer. */
"Activity" = "Activity";

Expand All @@ -70,6 +76,9 @@
/* No comment provided by engineer. */
"Add a New Contact" = "Add a New Contact";

/* No comment provided by engineer. */
"Add New Account" = "Add New Account";

/* Translate it as short as possible! (24 symbols max) */
"Add Shortcut" = "Добави пряк път";

Expand Down Expand Up @@ -199,6 +208,9 @@
/* No comment provided by engineer. */
"Confirm & Pay" = "Confirm & Pay";

/* No comment provided by engineer. */
"Confirm adding new account" = "Confirm adding new account";

/* No comment provided by engineer. */
"Confirm PIN" = "Потвърди ПИН";

Expand Down Expand Up @@ -820,6 +832,9 @@
/* No comment provided by engineer. */
"See on Uphold" = "Виж Uphold";

/* No comment provided by engineer. */
"Select Account" = "Select Account";

/* No comment provided by engineer. */
"Send" = "Изпрати";

Expand Down
15 changes: 15 additions & 0 deletions DashWallet/cs.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
/* No comment provided by engineer. */
"Accept" = "Přijmout";

/* No comment provided by engineer. */
"Account # %u" = "Account # %u";

/* No comment provided by engineer. */
"Account Index" = "Account Index";

/* No comment provided by engineer. */
"Activity" = "Aktivita";

Expand All @@ -70,6 +76,9 @@
/* No comment provided by engineer. */
"Add a New Contact" = "Přidat nový kontakt";

/* No comment provided by engineer. */
"Add New Account" = "Add New Account";

/* Translate it as short as possible! (24 symbols max) */
"Add Shortcut" = "Přidat zkratku";

Expand Down Expand Up @@ -199,6 +208,9 @@
/* No comment provided by engineer. */
"Confirm & Pay" = "Potvrdit a zaplatit";

/* No comment provided by engineer. */
"Confirm adding new account" = "Confirm adding new account";

/* No comment provided by engineer. */
"Confirm PIN" = "Potvrdit PIN";

Expand Down Expand Up @@ -820,6 +832,9 @@
/* No comment provided by engineer. */
"See on Uphold" = "Prohlédnout na Uphold";

/* No comment provided by engineer. */
"Select Account" = "Select Account";

/* No comment provided by engineer. */
"Send" = "Odeslat";

Expand Down
Loading