/// <summary> /// Create an account for a specific account index and account name pattern. /// </summary> /// <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="newAccountIndex">The optional account index to use.</param> /// <param name="newAccountName">The optional account name to use.</param> /// <returns>A new HD account.</returns> public HdAccount CreateAccount(string password, string encryptedSeed, byte[] chainCode, Network network, DateTimeOffset accountCreationTime, int newAccountIndex, string newAccountName = null) { if (string.IsNullOrEmpty(newAccountName)) { newAccountName = string.Format(Wallet.AccountNamePattern, newAccountIndex); } // Get the extended pub key used to generate addresses for this account. string accountHdPath = HdOperations.GetAccountHdPath((int)this.CoinType, newAccountIndex); Key privateKey = HdOperations.DecryptSeed(encryptedSeed, password, network); ExtPubKey accountExtPubKey = HdOperations.GetExtendedPublicKey(privateKey, chainCode, accountHdPath); return(new HdAccount { Index = newAccountIndex, ExtendedPubKey = accountExtPubKey.ToString(network), ExternalAddresses = new List <HdAddress>(), InternalAddresses = new List <HdAddress>(), Name = newAccountName, HdPath = accountHdPath, CreationTime = accountCreationTime }); }
/// <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> /// <returns>A new HD account.</returns> public HdAccount AddNewAccount(string password, string encryptedSeed, byte[] chainCode, Network network, DateTimeOffset accountCreationTime) { Guard.NotEmpty(password, nameof(password)); Guard.NotEmpty(encryptedSeed, nameof(encryptedSeed)); Guard.NotNull(chainCode, nameof(chainCode)); int newAccountIndex = 0; ICollection <HdAccount> hdAccounts = this.Accounts.ToList(); if (hdAccounts.Any()) { newAccountIndex = hdAccounts.Max(a => a.Index) + 1; } // Get the extended pub key used to generate addresses for this account. string accountHdPath = HdOperations.GetAccountHdPath((int)this.CoinType, newAccountIndex); Key privateKey = HdOperations.DecryptSeed(encryptedSeed, password, network); ExtPubKey accountExtPubKey = HdOperations.GetExtendedPublicKey(privateKey, chainCode, accountHdPath); var newAccount = new HdAccount { Index = newAccountIndex, ExtendedPubKey = accountExtPubKey.ToString(network), ExternalAddresses = new List <HdAddress>(), InternalAddresses = new List <HdAddress>(), Name = $"account {newAccountIndex}", HdPath = accountHdPath, CreationTime = accountCreationTime }; hdAccounts.Add(newAccount); this.Accounts = hdAccounts; return(newAccount); }