Commit 56c9a9cf authored by Jeremy Bokobza's avatar Jeremy Bokobza

Added send-transaction

parent 20d4beef
...@@ -287,12 +287,11 @@ namespace Breeze.Wallet.Controllers ...@@ -287,12 +287,11 @@ namespace Breeze.Wallet.Controllers
try try
{ {
var transaction = this.walletManager.BuildTransaction(request.WalletName, request.AccountName, request.CoinType, request.Password, request.DestinationAddress, request.Amount, request.FeeType, request.AllowUnconfirmed); var transactionResult = this.walletManager.BuildTransaction(request.WalletName, request.AccountName, request.CoinType, request.Password, request.DestinationAddress, request.Amount, request.FeeType, request.AllowUnconfirmed);
var fee = transaction.TotalOut - request.Amount;
var model = new WalletBuildTransactionModel var model = new WalletBuildTransactionModel
{ {
Hex = transaction.ToHex(), Hex = transactionResult.hex,
Fee = fee Fee = transactionResult.fee
}; };
return this.Json(model); return this.Json(model);
} }
......
...@@ -135,8 +135,13 @@ namespace Breeze.Wallet ...@@ -135,8 +135,13 @@ namespace Breeze.Wallet
/// <param name="feeType">The type of fee to be included.</param> /// <param name="feeType">The type of fee to be included.</param>
/// <param name="allowUnconfirmed">Whether or not we allow this transaction to rely on unconfirmed outputs.</param> /// <param name="allowUnconfirmed">Whether or not we allow this transaction to rely on unconfirmed outputs.</param>
/// <returns></returns> /// <returns></returns>
NBitcoin.Transaction BuildTransaction(string walletName, string accountName, CoinType coinType, string password, string destinationAddress, Money amount, string feeType, bool allowUnconfirmed); (string hex, Money fee) BuildTransaction(string walletName, string accountName, CoinType coinType, string password, string destinationAddress, Money amount, string feeType, bool allowUnconfirmed);
/// <summary>
/// Sends a transaction to the network.
/// </summary>
/// <param name="transactionHex">The hex of the transaction.</param>
/// <returns></returns>
bool SendTransaction(string transactionHex); bool SendTransaction(string transactionHex);
/// <summary> /// <summary>
......
...@@ -7,7 +7,9 @@ using Breeze.Wallet.Helpers; ...@@ -7,7 +7,9 @@ using Breeze.Wallet.Helpers;
using Breeze.Wallet.Models; using Breeze.Wallet.Models;
using NBitcoin; using NBitcoin;
using NBitcoin.DataEncoders; using NBitcoin.DataEncoders;
using NBitcoin.Protocol;
using Newtonsoft.Json; using Newtonsoft.Json;
using Stratis.Bitcoin.Connection;
using Transaction = NBitcoin.Transaction; using Transaction = NBitcoin.Transaction;
namespace Breeze.Wallet namespace Breeze.Wallet
...@@ -31,12 +33,14 @@ namespace Breeze.Wallet ...@@ -31,12 +33,14 @@ namespace Breeze.Wallet
private readonly CoinType coinType; private readonly CoinType coinType;
private readonly ConnectionManager connectionManager;
/// <summary> /// <summary>
/// Occurs when a transaction is found. /// Occurs when a transaction is found.
/// </summary> /// </summary>
public event EventHandler<TransactionFoundEventArgs> TransactionFound; public event EventHandler<TransactionFoundEventArgs> TransactionFound;
public WalletManager(ConcurrentChain chain, Network netwrok) public WalletManager(ConnectionManager connectionManager, Network netwrok)
{ {
this.Wallets = new List<Wallet>(); this.Wallets = new List<Wallet>();
...@@ -46,13 +50,14 @@ namespace Breeze.Wallet ...@@ -46,13 +50,14 @@ namespace Breeze.Wallet
this.Load(this.DeserializeWallet(path)); this.Load(this.DeserializeWallet(path));
} }
this.connectionManager = connectionManager;
this.coinType = (CoinType)netwrok.Consensus.CoinType; this.coinType = (CoinType)netwrok.Consensus.CoinType;
// load data in memory for faster lookups // load data in memory for faster lookups
// TODO get the coin type from somewhere else // TODO get the coin type from somewhere else
this.PubKeys = this.LoadKeys(this.coinType); this.PubKeys = this.LoadKeys(this.coinType);
this.TrackedTransactions = this.LoadTransactions(this.coinType); this.TrackedTransactions = this.LoadTransactions(this.coinType);
this.TransactionFound += this.OnTransactionFound; this.TransactionFound += this.OnTransactionFound;
} }
/// <inheritdoc /> /// <inheritdoc />
...@@ -322,7 +327,7 @@ namespace Breeze.Wallet ...@@ -322,7 +327,7 @@ namespace Breeze.Wallet
} }
/// <inheritdoc /> /// <inheritdoc />
public Transaction BuildTransaction(string walletName, string accountName, CoinType coinType, string password, string destinationAddress, Money amount, string feeType, bool allowUnconfirmed) public (string hex, Money fee) BuildTransaction(string walletName, string accountName, CoinType coinType, string password, string destinationAddress, Money amount, string feeType, bool allowUnconfirmed)
{ {
if (amount == Money.Zero) if (amount == Money.Zero)
{ {
...@@ -385,7 +390,7 @@ namespace Breeze.Wallet ...@@ -385,7 +390,7 @@ namespace Breeze.Wallet
throw new Exception("Could not build transaction, please make sure you entered the correct data."); throw new Exception("Could not build transaction, please make sure you entered the correct data.");
} }
return tx; return (tx.ToHex(), calculationResult.fee);
} }
/// <summary> /// <summary>
...@@ -396,7 +401,7 @@ namespace Breeze.Wallet ...@@ -396,7 +401,7 @@ namespace Breeze.Wallet
/// <returns>The collection of transactions to be used and the fee to be charged</returns> /// <returns>The collection of transactions to be used and the fee to be charged</returns>
private (List<TransactionData> transactionsToUse, Money fee) CalculateFees(IEnumerable<TransactionData> spendableTransactions, Money amount) private (List<TransactionData> transactionsToUse, Money fee) CalculateFees(IEnumerable<TransactionData> spendableTransactions, Money amount)
{ {
// TODO make this a bit smarter! // TODO make this a bit smarter!
List<TransactionData> transactionsToUse = new List<TransactionData>(); List<TransactionData> transactionsToUse = new List<TransactionData>();
foreach (var transaction in spendableTransactions) foreach (var transaction in spendableTransactions)
{ {
...@@ -411,9 +416,20 @@ namespace Breeze.Wallet ...@@ -411,9 +416,20 @@ namespace Breeze.Wallet
return (transactionsToUse, fee); return (transactionsToUse, fee);
} }
/// <inheritdoc />
public bool SendTransaction(string transactionHex) public bool SendTransaction(string transactionHex)
{ {
throw new System.NotImplementedException(); // TODO move this to a behavior on the full node
// parse transaction
Transaction transaction = Transaction.Parse(transactionHex);
TxPayload payload = new TxPayload(transaction);
foreach (var node in connectionManager.ConnectedNodes)
{
node.SendMessage(payload);
}
return true;
} }
/// <inheritdoc /> /// <inheritdoc />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment