/// <summary> /// Adds an account to the current account root using encrypted seed and password. /// </summary> /// <remarks>The name given to the account is of the form "account (i)" by default, where (i) is an incremental index starting at 0. /// According to BIP44, an account at index (i) can only be created when the account at index (i - 1) contains transactions. /// <seealso cref="https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki"/></remarks> /// <param name="password">The password used to decrypt the wallet's encrypted seed.</param> /// <param name="encryptedSeed">The encrypted private key for this wallet.</param> /// <param name="chainCode">The chain code for this wallet.</param> /// <param name="network">The network for which this account will be created.</param> /// <param name="accountCreationTime">Creation time of the account to be created.</param> /// <param name="accountIndex">The index at which an account will be created. If left null, a new account will be created after the last used one.</param> /// <param name="accountName">The name of the account to be created. If left null, an account will be created according to the <see cref="AccountNamePattern"/>.</param> /// <returns>A new HD account.</returns> public HdAccount AddNewAccount(string password, string encryptedSeed, byte[] chainCode, Network network, DateTimeOffset accountCreationTime, int?accountIndex = null, string accountName = null) { Guard.NotEmpty(password, nameof(password)); Guard.NotEmpty(encryptedSeed, nameof(encryptedSeed)); Guard.NotNull(chainCode, nameof(chainCode)); WalletAccounts hdAccounts = this.Accounts; // If an account needs to be created at a specific index or with a specific name, make sure it doesn't already exist. if (hdAccounts.Any(a => a.Index == accountIndex || a.Name == accountName)) { throw new Exception($"An account at index {accountIndex} or with name {accountName} already exists."); } if (accountIndex == null) { if (hdAccounts.Any()) { // Hide account indexes used for cold staking from the "Max" calculation. accountIndex = hdAccounts.Where(a => a.Index < 100_000_000).Max(a => a.Index) + 1; } else { accountIndex = 0; } } HdAccount newAccount = this.CreateAccount(password, encryptedSeed, chainCode, network, accountCreationTime, accountIndex.Value, accountName); hdAccounts.Add(newAccount); this.Accounts = hdAccounts; return(newAccount); }
public HdAccount(WalletAccounts walletAccounts) : this() { this.WalletAccounts = walletAccounts; walletAccounts.Add(this); }