diff --git a/Logic/Financial/BitcoinUtility.cs b/Logic/Financial/BitcoinUtility.cs new file mode 100644 index 00000000..fac7ddea --- /dev/null +++ b/Logic/Financial/BitcoinUtility.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using NBitcoin; +using Newtonsoft.Json.Linq; + +namespace Swarmops.Logic.Financial +{ + public class BitcoinUtility + { + static public Coin[] GetSpendableCoin (BitcoinSecret secretKey) // we're using BitcoinSecret as arg just to reinforce caller must have privkey to spend funds + { + // This function queries the Blockchain API for the unspent coin. + + string addressString = secretKey.PubKey.GetAddress (Network.Main).ToString(); + var unspentJsonResult = + JObject.Parse (new WebClient().DownloadString ("https://blockchain.info/unspent?active=" + addressString)); + + List coinList = new List(); + + foreach (var unspentJson in unspentJsonResult["unspent_outputs"]) + { + BitcoinUnspentTransactionOutput txUnspent = new BitcoinUnspentTransactionOutput() + { + BitcoinAddress = addressString, + ConfirmationCount = (UInt32) unspentJson["confirmations"], + Satoshis = (UInt64) unspentJson["value"], + TransactionHash = (string) unspentJson["tx_hash_big_endian"], + TransactionOutputIndex = (UInt32) unspentJson["tx_output_n"] + }; + + coinList.Add (txUnspent); + } + + return coinList.ToArray(); + } + } + + internal class BitcoinUnspentTransactionOutput + { + public UInt64 Satoshis { get; set; } + public string TransactionHash { get; set; } + public UInt32 TransactionOutputIndex { get; set; } + public string BitcoinAddress { get; set; } + public UInt32 ConfirmationCount { get; set; } + + public static implicit operator NBitcoin.Coin (BitcoinUnspentTransactionOutput source) + { + return new Coin(new OutPoint() { Hash= uint256.Parse (source.TransactionHash), N=source.TransactionOutputIndex }, new TxOut(new NBitcoin.Money (source.Satoshis), new BitcoinAddress (source.BitcoinAddress, Network.Main) )); + } + } +} diff --git a/Logic/Financial/FinancialAccounts.cs b/Logic/Financial/FinancialAccounts.cs index 8586f857..629a4b9f 100644 --- a/Logic/Financial/FinancialAccounts.cs +++ b/Logic/Financial/FinancialAccounts.cs @@ -249,7 +249,9 @@ namespace Swarmops.Logic.Financial } } - public const int RootWalletIndex = 1; - public const int RootDonationsIndex = 2; + public const int BitcoinWalletIndex = 1; + public const int BitcoinDonationsIndex = 2; + + public const string BitcoinTestAddress = "1JMpU3D6c5sruunMwzkt6p6PQzLcUYcL26"; } } \ No newline at end of file diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index 80d4622d..22e4f14e 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -96,6 +96,7 @@ + False ..\packages\Microsoft.Tpl.Dataflow.4.5.9\lib\net45\System.Threading.Tasks.Dataflow.dll @@ -117,6 +118,7 @@ + True diff --git a/Site5/Pages/v5/Financial/Donate.aspx.cs b/Site5/Pages/v5/Financial/Donate.aspx.cs index 966cbf2e..5a54a8ce 100644 --- a/Site5/Pages/v5/Financial/Donate.aspx.cs +++ b/Site5/Pages/v5/Financial/Donate.aspx.cs @@ -1,4 +1,5 @@ using System; +using System.Activities.Validation; using System.Collections.Generic; using System.Linq; using System.Web; @@ -14,22 +15,49 @@ namespace Swarmops.Frontend.Pages.v5.Financial { protected void Page_Load (object sender, EventArgs e) { - this.PageAccessRequired = new Access (this.CurrentOrganization, AccessAspect.Null); + this.PageAccessRequired = new Access (this.CurrentOrganization, AccessAspect.Participant); this.PageTitle = Resources.Pages.Financial.Donate_PageTitle; this.InfoBoxLiteral = Resources.Pages.Financial.FileExpenseClaim_Info; string bitcoinAddress = FinancialAccounts.BitcoinHotPublicRoot .Derive ((uint) this.CurrentOrganization.Identity) - .Derive (FinancialAccounts.RootDonationsIndex) + .Derive (FinancialAccounts.BitcoinDonationsIndex) + .Derive ((uint) this.CurrentUser.Identity) .PubKey.GetAddress (Network.Main) .ToString(); + // TEST TEST TEST + /* + BitcoinSecret secretKey = + FinancialAccounts.BitcoinHotPrivateRoot.Derive ((uint) this.CurrentOrganization.Identity) + .Derive (FinancialAccounts.BitcoinDonationsIndex) + .PrivateKey + .GetBitcoinSecret (Network.Main); + + Coin[] spendableCoin = BitcoinUtility.GetSpendableCoin (secretKey); + + TransactionBuilder txBuild = new TransactionBuilder(); + Transaction tx = txBuild.AddCoins (spendableCoin) + .AddKeys (secretKey) + .Send (new BitcoinAddress (FinancialAccounts.BitcoinTestAddress), "0.001") + .SendFees ("0.0001") + .SetChange (secretKey.GetAddress()) + .BuildTransaction (true); + + bool test = txBuild.Verify (tx); + if (!test) + { + throw new InvalidOperationException("Tx is not properly signed"); + } + + string txHex = tx.ToHex();*/ + this.BoxTitle.Text = Resources.Pages.Financial.Donate_PageTitle; this.LabelExplainBitcoinDonation.Text = String.Format(Resources.Pages.Financial.Donate_Explain, CurrentOrganization.Name, bitcoinAddress); this.ImageBitcoinQr.ImageUrl = - "https://chart.googleapis.com/chart?cht=qr&chs=400x400&chl=bitcoin:" + bitcoinAddress + "&label=" + CurrentOrganization.Name.Replace ("&","et").Replace ("=", string.Empty); // URI scheme doesn't like &, = + "https://chart.googleapis.com/chart?cht=qr&chs=400x400&chl=bitcoin:" + HttpUtility.UrlEncode(bitcoinAddress + "?label=" + CurrentOrganization.Name.Replace ("&","et").Replace ("=", string.Empty)); // URI scheme doesn't like &, = } } } \ No newline at end of file