Ejemplo n.º 1
0
        /// <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>The created addresses.</returns>
        public IEnumerable <HdAddress> CreateAddresses(Network network, int addressesQuantity, bool isChange = false)
        {
            ICollection <HdAddress> addresses = isChange ? this.InternalAddresses : this.ExternalAddresses;

            // Get the index of the last address.
            int firstNewAddressIndex = 0;

            if (addresses.Any())
            {
                firstNewAddressIndex = addresses.Max(add => add.Index) + 1;
            }

            var addressesCreated = new List <HdAddress>();

            for (int i = firstNewAddressIndex; i < firstNewAddressIndex + addressesQuantity; i++)
            {
                // Retrieve the pubkey associated with the private key of this address index.
                PubKey pubkey = HdOperations.GeneratePublicKey(this.ExtendedPubKey, i, isChange);

                // Generate the P2PKH address corresponding to the pubkey.
                BitcoinPubKeyAddress    address    = pubkey.GetAddress(network);
                BitcoinWitPubKeyAddress witAddress = pubkey.GetSegwitAddress(network);

                // Add the new address details to the list of addresses.
                var newAddress = new HdAddress
                {
                    Index         = i,
                    HdPath        = HdOperations.CreateHdPath((int)this.GetCoinType(), this.Index, isChange, i),
                    ScriptPubKey  = address.ScriptPubKey,
                    Pubkey        = pubkey.ScriptPubKey,
                    Bech32Address = witAddress.ToString(),
                    Address       = address.ToString(),
                };

                addresses.Add(newAddress);
                addressesCreated.Add(newAddress);
            }

            if (isChange)
            {
                this.InternalAddresses = addresses;
            }
            else
            {
                this.ExternalAddresses = addresses;
            }

            return(addressesCreated);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <string> > GetDepositAddress()
        {
            string accountIdString = User.FindFirstValue(ClaimTypes.NameIdentifier);

            if (accountIdString == null)
            {
                return(BadRequest("AccountId missing from request"));
            }

            int accountId = int.Parse(accountIdString);

            BitcoinWitPubKeyAddress address = await _bitcoin.GetDepositAddress(accountId);

            return(address.ToString());
        }