/// <summary> /// Gets the extended private key for the given address. /// </summary> /// <param name="password">The password used to encrypt/decrypt sensitive info.</param> /// <param name="address">The address to get the private key for.</param> /// <returns>The extended private key.</returns> public ISecret GetExtendedPrivateKeyForAddress(string password, HdAddress address) { Guard.NotEmpty(password, nameof(password)); Guard.NotNull(address, nameof(address)); // Check if the wallet contains the address. if (!this.ContainsAddress(address)) { throw new WalletException("Address not found on wallet."); } // get extended private key Key privateKey = HdOperations.DecryptSeed(this.EncryptedSeed, password, this.Network); return(HdOperations.GetExtendedPrivateKey(privateKey, this.ChainCode, address.HdPath, this.Network)); }
/// <summary> /// Creates a number of additional addresses in the current account. /// </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 at least one transaction. /// </remarks> /// <param name="network">The network these addresses will be for.</param> /// <param name="addressesQuantity">The number of addresses to create.</param> /// <param name="isChange">Whether the addresses added are change (internal) addresses or receiving (external) addresses.</param> /// <returns>A list of addresses in Base58 format.</returns> public List <string> CreateAddresses(Network network, int addressesQuantity, bool isChange = false) { List <string> addressesCreated = new List <string>(); var addresses = isChange ? this.InternalAddresses : this.ExternalAddresses; // Get the index of the last address that contains transactions. int firstNewAddressIndex = 0; if (addresses.Any()) { firstNewAddressIndex = addresses.Max(add => add.Index) + 1; } for (int i = firstNewAddressIndex; i < firstNewAddressIndex + addressesQuantity; i++) { // Generate a new address. PubKey pubkey = HdOperations.GeneratePublicKey(this.ExtendedPubKey, i, isChange); BitcoinPubKeyAddress address = pubkey.GetAddress(network); // Add the new address details to the list of addresses. addresses.Add(new HdAddress { Index = i, HdPath = HdOperations.CreateHdPath((int)this.GetCoinType(), this.Index, i, isChange), ScriptPubKey = address.ScriptPubKey, Pubkey = pubkey.ScriptPubKey, Address = address.ToString(), Transactions = new List <TransactionData>() }); addressesCreated.Add(address.ToString()); } if (isChange) { this.InternalAddresses = addresses; } else { this.ExternalAddresses = addresses; } return(addressesCreated); }
/// <summary> /// Adds an account to the current account root. /// </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)); // Get the current collection of accounts. var accounts = this.Accounts.ToList(); int newAccountIndex = 0; if (accounts.Any()) { newAccountIndex = accounts.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 }; accounts.Add(newAccount); this.Accounts = accounts; return(newAccount); }
/// <summary> /// Determines whether this is a change address or a receive address. /// </summary> /// <returns> /// <c>true</c> if it is a change address; otherwise, <c>false</c>. /// </returns> public bool IsChangeAddress() { return(HdOperations.IsChangeAddress(this.HdPath)); }
/// <summary> /// Gets the type of coin this account is for. /// </summary> /// <returns>A <see cref="CoinType"/>.</returns> public CoinType GetCoinType() { return((CoinType)HdOperations.GetCoinType(this.HdPath)); }