Пример #1
0
        /// <inheritdoc />
        public List <UnspentOutputReference> GetSpendableTransactionsInAccount(WalletAccountReference walletAccountReference, int confirmations = 0)
        {
            Guard.NotNull(walletAccountReference, nameof(walletAccountReference));

            Wallet    wallet  = this.GetWalletByName(walletAccountReference.WalletName);
            HdAccount account = wallet.GetAccountByCoinType(walletAccountReference.AccountName, this.coinType);

            if (account == null)
            {
                throw new WalletException($"Account '{walletAccountReference.AccountName}' in wallet '{walletAccountReference.WalletName}' not found.");
            }

            return(account.GetSpendableTransactions(this.chain.Tip.Height, confirmations));
        }
Пример #2
0
        /// <inheritdoc />
        public (string hex, uint256 transactionId, Money fee) BuildTransaction(WalletAccountReference accountReference, string password, Script destinationScript, Money amount, FeeType feeType, int minConfirmations)
        {
            if (amount == Money.Zero)
            {
                throw new WalletException($"Cannot send transaction with 0 {this.coinType}");
            }

            // get the wallet and the account
            Wallet    wallet  = this.GetWalletByName(accountReference.WalletName);
            HdAccount account = this.GetAccounts(wallet).GetAccountByName(accountReference.AccountName);

            // get a list of transactions outputs that have not been spent
            var spendableTransactions = account.GetSpendableTransactions().ToList();

            // remove whats under min confirmations
            var currentHeight = this.chain.Height;

            spendableTransactions = spendableTransactions.Where(s => currentHeight - s.BlockHeight >= minConfirmations).ToList();

            // get total spendable balance in the account.
            var balance = spendableTransactions.Sum(t => t.Amount);

            // make sure we have enough funds
            if (balance < amount)
            {
                throw new WalletException("Not enough funds.");
            }

            // calculate which addresses needs to be used as well as the fee to be charged
            var calculationResult = this.CalculateFees(spendableTransactions, amount, feeType.ToConfirmations());

            // get extended private key
            var privateKey = Key.Parse(wallet.EncryptedSeed, password, wallet.Network);
            var seedExtKey = new ExtKey(privateKey, wallet.ChainCode);

            var signingKeys = new HashSet <ISecret>();
            var coins       = new List <Coin>();

            foreach (var transactionToUse in calculationResult.transactionsToUse)
            {
                var           address           = account.FindAddressesForTransaction(t => t.Id == transactionToUse.Id && t.Index == transactionToUse.Index && t.Amount > 0).Single();
                ExtKey        addressExtKey     = seedExtKey.Derive(new KeyPath(address.HdPath));
                BitcoinExtKey addressPrivateKey = addressExtKey.GetWif(wallet.Network);
                signingKeys.Add(addressPrivateKey);

                coins.Add(new Coin(transactionToUse.Id, (uint)transactionToUse.Index, transactionToUse.Amount, transactionToUse.ScriptPubKey));
            }

            // get address to send the change to
            var changeAddress = account.GetFirstUnusedChangeAddress();

            // build transaction
            var         builder = new TransactionBuilder();
            Transaction tx      = builder
                                  .AddCoins(coins)
                                  .AddKeys(signingKeys.ToArray())
                                  .Send(destinationScript, amount)
                                  .SetChange(changeAddress.ScriptPubKey)
                                  .SendFees(calculationResult.fee)
                                  .BuildTransaction(true);

            if (!builder.Verify(tx))
            {
                throw new WalletException("Could not build transaction, please make sure you entered the correct data.");
            }

            return(tx.ToHex(), tx.GetHash(), calculationResult.fee);
        }