public void FundRawTransactionWithValidTxsThenReturnsCorrectResponse()
        {
            var k  = new Key();
            var tx = new Transaction();

            tx.Outputs.Add(new TxOut(Money.Coins(1), k));
            RPCClient rpc = this.rpcTestFixture.RpcClient;
            FundRawTransactionResponse result = rpc.FundRawTransaction(tx);

            TestFundRawTransactionResult(tx, result);

            result = rpc.FundRawTransaction(tx, new FundRawTransactionOptions());
            TestFundRawTransactionResult(tx, result);
            FundRawTransactionResponse result1 = result;

            BitcoinAddress change  = rpc.GetNewAddress();
            BitcoinAddress change2 = rpc.GetRawChangeAddress();

            result = rpc.FundRawTransaction(tx, new FundRawTransactionOptions()
            {
                FeeRate         = new FeeRate(Money.Satoshis(50), 1),
                IncludeWatching = true,
                ChangeAddress   = change,
            });
            TestFundRawTransactionResult(tx, result);
            Assert.True(result1.Fee < result.Fee);
            Assert.Contains(result.Transaction.Outputs, o => o.ScriptPubKey == change.ScriptPubKey);
        }
Example #2
0
        public Transaction FundTransaction(TxOut txOut, FeeRate feeRate)
        {
            Transaction tx = new Transaction();

            tx.Outputs.Add(txOut);

            var changeAddress = _RPCClient.GetRawChangeAddress();

            FundRawTransactionResponse response = null;

            try
            {
                response = _RPCClient.FundRawTransaction(tx, new FundRawTransactionOptions()
                {
                    ChangeAddress = changeAddress,
                    FeeRate       = feeRate,
                    LockUnspents  = true
                });
            }
            catch (RPCException)
            {
                var balance = _RPCClient.GetBalance(0, false);
                var needed  = tx.Outputs.Select(o => o.Value).Sum()
                              + feeRate.GetFee(2000);
                var missing = needed - balance;
                if (missing > Money.Zero)
                {
                    throw new NotEnoughFundsException("Not enough funds", "", missing);
                }
                throw;
            }
            var result = _RPCClient.SendCommand("signrawtransaction", response.Transaction.ToHex());

            return(new Transaction(((JObject)result.Result)["hex"].Value <string>()));
        }
        public Transaction FundTransaction(TxOut txOut, FeeRate feeRate)
        {
            Transaction tx = new Transaction();

            tx.Outputs.Add(txOut);

            var changeAddress = BitcoinAddress.Create(_RPCClient.SendCommand("getrawchangeaddress").ResultString, _RPCClient.Network);

            FundRawTransactionResponse response = null;

            try
            {
                response = _RPCClient.FundRawTransaction(tx, new FundRawTransactionOptions()
                {
                    ChangeAddress = changeAddress,
                    FeeRate       = feeRate,
                    LockUnspents  = true
                });
            }
            catch (RPCException ex)
            {
                if (ex.Message.Equals("Insufficient funds", StringComparison.OrdinalIgnoreCase))
                {
                    return(null);
                }
                throw;
            }
            var result = _RPCClient.SendCommand("signrawtransaction", response.Transaction.ToHex());

            return(new Transaction(((JObject)result.Result)["hex"].Value <string>()));
        }
        public void CanSignRawTransaction()
        {
            using (NodeBuilder builder = NodeBuilder.Create(this))
            {
                CoreNode node = builder.CreateBitcoinCoreNode(version: "0.18.0", useNewConfigStyle: true).Start();

                CoreNode sfn = builder.CreateStratisPowNode(this.regTest).WithWallet().Start();

                TestHelper.ConnectAndSync(node, sfn);

                RPCClient rpcClient = node.CreateRPCClient();
                RPCClient sfnRpc    = sfn.CreateRPCClient();

                // Need one block per node so they can each fund a transaction.
                rpcClient.Generate(1);

                TestHelper.ConnectAndSync(node, sfn);

                sfnRpc.Generate(1);

                TestHelper.ConnectAndSync(node, sfn);

                // And then enough blocks mined on top for the coinbases to mature.
                rpcClient.Generate(101);

                TestHelper.ConnectAndSync(node, sfn);

                var tx = new Transaction();
                tx.Outputs.Add(new TxOut(Money.Coins(1.0m), new Key()));
                FundRawTransactionResponse funded = rpcClient.FundRawTransaction(tx);

                // signrawtransaction was removed in 0.18. So just use its equivalent so that we can test SFN's ability to call signrawtransaction.
                RPCResponse response = rpcClient.SendCommand("signrawtransactionwithwallet", tx.ToHex());

                Assert.NotNull(response.Result["hex"]);

                sfnRpc.WalletPassphrase(sfn.WalletPassword, 60);

                tx = new Transaction();
                tx.Outputs.Add(new TxOut(Money.Coins(1.0m), new Key()));
                funded = sfnRpc.FundRawTransaction(tx);
                Transaction signed = sfnRpc.SignRawTransaction(funded.Transaction);
                rpcClient.SendRawTransaction(signed);
            }
        }