diff --git a/Backend/Program.cs b/Backend/Program.cs index c43f8279..b2ac823f 100644 --- a/Backend/Program.cs +++ b/Backend/Program.cs @@ -170,7 +170,7 @@ namespace Swarmops.Backend // Check for existence of bitcoin hotwallet root - FinancialAccounts.VerifyBitcoinHotWallet(); + BitcoinUtility.VerifyBitcoinHotWallet(); // Begin maintenance loop diff --git a/Logic/Financial/BitcoinUtility.cs b/Logic/Financial/BitcoinUtility.cs index 379f231a..711f81de 100644 --- a/Logic/Financial/BitcoinUtility.cs +++ b/Logic/Financial/BitcoinUtility.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; using System.Diagnostics; +using System.IO; using System.Linq; using System.Net; using System.Text; @@ -12,11 +13,42 @@ using NBitcoin; using NBitcoin.BouncyCastle.Asn1.Ocsp; using Newtonsoft.Json.Linq; using Swarmops.Database; +using Swarmops.Logic.Support; namespace Swarmops.Logic.Financial { public class BitcoinUtility { + public static void VerifyBitcoinHotWallet() + { + // Make sure there's always a private hotwallet root, regardless of whether it's used or not + + if (!File.Exists(SystemSettings.EtcFolder + Path.DirectorySeparatorChar + "hotwallet")) + { + ExtKey privateRoot = new ExtKey(); + File.WriteAllText(SystemSettings.EtcFolder + Path.DirectorySeparatorChar + "hotwallet", privateRoot.GetWif(Network.Main).ToWif(), Encoding.ASCII); + File.WriteAllText(SystemSettings.EtcFolder + Path.DirectorySeparatorChar + "hotwallet-created-" + DateTime.UtcNow.ToString("yyyy-MM-dd--HH-mm-ss--fff.backup"), privateRoot.GetWif(Network.Main).ToWif(), Encoding.ASCII); // an extra backup + Persistence.Key["BitcoinHotPublicRoot"] = privateRoot.Neuter().GetWif(Network.Main).ToWif(); + } + } + + public static ExtKey BitcoinHotPrivateRoot + { + get + { + return + ExtKey.Parse(File.ReadAllText(SystemSettings.EtcFolder + Path.DirectorySeparatorChar + "hotwallet")); // will throw if hotwallet file does not exist - intentional + } + } + + public static ExtPubKey BitcoinHotPublicRoot + { + get + { + return ExtPubKey.Parse((string)Persistence.Key["BitcoinHotPublicRoot"]); + } + } + 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. @@ -115,6 +147,8 @@ namespace Swarmops.Logic.Financial public const int BitcoinAccountsReceivableIndex = 4; public const string BitcoinTestAddress = "1JMpU3D6c5sruunMwzkt6p6PQzLcUYcL26"; + + public const long FeeSatoshisPerThousandBytes = 10000; } internal class BitcoinUnspentTransactionOutput diff --git a/Logic/Financial/FinancialAccounts.cs b/Logic/Financial/FinancialAccounts.cs index 8a5b1c28..76c9ba0d 100644 --- a/Logic/Financial/FinancialAccounts.cs +++ b/Logic/Financial/FinancialAccounts.cs @@ -217,37 +217,7 @@ namespace Swarmops.Logic.Financial } - public static void VerifyBitcoinHotWallet() - { - // Make sure there's always a private hotwallet root, regardless of whether it's used or not - if (!File.Exists (SystemSettings.EtcFolder + Path.DirectorySeparatorChar + "hotwallet")) - { - ExtKey privateRoot = new ExtKey(); - File.WriteAllText(SystemSettings.EtcFolder + Path.DirectorySeparatorChar + "hotwallet", privateRoot.GetWif(Network.Main).ToWif(), Encoding.ASCII); - File.WriteAllText(SystemSettings.EtcFolder + Path.DirectorySeparatorChar + "hotwallet-created-" + DateTime.UtcNow.ToString("yyyy-MM-dd--HH-mm-ss--fff.backup"), privateRoot.GetWif(Network.Main).ToWif(), Encoding.ASCII); // an extra backup - Persistence.Key["BitcoinHotPublicRoot"] = privateRoot.Neuter().GetWif(Network.Main).ToWif(); - } - } - - public static ExtKey BitcoinHotPrivateRoot - { - get - { - VerifyBitcoinHotWallet(); - - return - ExtKey.Parse (File.ReadAllText (SystemSettings.EtcFolder + Path.DirectorySeparatorChar + "hotwallet")); - } - } - - public static ExtPubKey BitcoinHotPublicRoot - { - get - { - return ExtPubKey.Parse ((string) Persistence.Key["BitcoinHotPublicRoot"]); - } - } } } \ No newline at end of file diff --git a/Logic/Financial/HotBitcoinAddress.cs b/Logic/Financial/HotBitcoinAddress.cs index 81de1cd8..866fd1ba 100644 --- a/Logic/Financial/HotBitcoinAddress.cs +++ b/Logic/Financial/HotBitcoinAddress.cs @@ -51,7 +51,7 @@ namespace Swarmops.Logic.Financial public static HotBitcoinAddress Create (Organization organization, params int[] derivationPath) { - ExtPubKey extPubKey = FinancialAccounts.BitcoinHotPublicRoot; + ExtPubKey extPubKey = BitcoinUtility.BitcoinHotPublicRoot; extPubKey = extPubKey.Derive ((uint) organization.Identity); string derivationPathString = string.Empty; @@ -76,9 +76,41 @@ namespace Swarmops.Logic.Financial return FromBasic(SwarmDb.GetDatabaseForReading().GetHotBitcoinAddress(bitcoinAddress)); } + public BitcoinSecret PrivateKey + { + get + { + // This will throw if the private root key is not available to the running code, and that's as designed + + ExtKey secretExtKey = BitcoinUtility.BitcoinHotPrivateRoot.Derive ((uint) base.OrganizationId); + + foreach (string derivationString in DerivationPath.Split (' ')) + { + secretExtKey = secretExtKey.Derive (UInt32.Parse (derivationString)); + } + + return secretExtKey.PrivateKey.GetBitcoinSecret (Network.Main); + } + } + + public Organization Organization + { + get { return Organization.FromIdentity (base.OrganizationId); } + } + public HotBitcoinAddressUnspents Unspents { get { return HotBitcoinAddressUnspents.ForAddress (this); } } + + public Int64 UnspentSatoshis + { + get + { + HotBitcoinAddressUnspents unspents = Unspents; + + return unspents.Sum (unspent => unspent.AmountSatoshis); + } + } } } diff --git a/Logic/Financial/Payout.cs b/Logic/Financial/Payout.cs index 8cff08f8..6c9fc24b 100644 --- a/Logic/Financial/Payout.cs +++ b/Logic/Financial/Payout.cs @@ -117,6 +117,8 @@ namespace Swarmops.Logic.Financial public Salaries DependentSalariesNet; public Salaries DependentSalariesTax; + public Person RecipientPerson { get; set; } + public decimal Amount { set diff --git a/Logic/Financial/Payouts.cs b/Logic/Financial/Payouts.cs index fba816f1..3522a847 100644 --- a/Logic/Financial/Payouts.cs +++ b/Logic/Financial/Payouts.cs @@ -102,6 +102,7 @@ namespace Swarmops.Logic.Financial claim.Claimer.BankClearing + " / " + claim.Claimer.BankAccount, string.Empty, 0, DateTime.MinValue, false, DateTime.Now, 0); Payout payout = Payout.FromBasic (basicPayout); + payout.RecipientPerson = claim.Claimer; payout.DependentExpenseClaims.Add (claim); @@ -206,6 +207,7 @@ namespace Swarmops.Logic.Financial advance.Person.BankClearing + " / " + advance.Person.BankAccount, string.Empty, 0, DateTime.MinValue, false, DateTime.Now, 0); Payout payout = Payout.FromBasic (basicPayout); + payout.RecipientPerson = advance.Person; payout.DependentCashAdvancesPayout.Add (advance); @@ -297,6 +299,7 @@ namespace Swarmops.Logic.Financial salary.PayoutDate.ToString (CultureInfo.InvariantCulture), salary.NetSalaryCents, salary.PayoutDate, false, DateTime.Now, 0); Payout payout = Payout.FromBasic (basicPayout); + payout.RecipientPerson = employee; payout.DependentSalariesNet.Add (salary); diff --git a/Site5/App_GlobalResources/Menu5.resx b/Site5/App_GlobalResources/Menu5.resx index c8f5cfb8..1fc6b9bb 100644 --- a/Site5/App_GlobalResources/Menu5.resx +++ b/Site5/App_GlobalResources/Menu5.resx @@ -414,4 +414,7 @@ My Local Organization + + Donate Funds + \ No newline at end of file diff --git a/Site5/Images/PageIcons/iconshock-gift-40px.png b/Site5/Images/PageIcons/iconshock-gift-40px.png new file mode 100644 index 00000000..c3cdacc1 Binary files /dev/null and b/Site5/Images/PageIcons/iconshock-gift-40px.png differ diff --git a/Site5/Images/PageIcons/iconshock-gift-96px.png b/Site5/Images/PageIcons/iconshock-gift-96px.png new file mode 100644 index 00000000..57e6b1bb Binary files /dev/null and b/Site5/Images/PageIcons/iconshock-gift-96px.png differ diff --git a/Site5/MainMenu-v5.xml b/Site5/MainMenu-v5.xml index ca8561dc..eeaf7199 100644 --- a/Site5/MainMenu-v5.xml +++ b/Site5/MainMenu-v5.xml @@ -154,21 +154,37 @@ Link - + - Financial_RequestCashAdvance - 1 - - iconshock-walletmoney - /Pages/v5/Financial/RequestCashAdvance.aspx - Link - + Financial_RequestCashAdvance + 1 + + iconshock-walletmoney + /Pages/v5/Financial/RequestCashAdvance.aspx + Link + - + - 2 - Separator - + 1 + Separator + + + + + Financial_DonateFunds + 1 + + iconshock-gift + /Pages/v5/Financial/Donate.aspx + Link + + + + + 2 + Separator + diff --git a/Site5/Pages/v5/Financial/Donate.aspx b/Site5/Pages/v5/Financial/Donate.aspx index 2fbd7e70..8bbf7364 100644 --- a/Site5/Pages/v5/Financial/Donate.aspx +++ b/Site5/Pages/v5/Financial/Donate.aspx @@ -6,8 +6,10 @@ // Initialize socket to wait for transaction - initializeSocket(); - + if (enable) { + initializeSocket(); + } + // TODO: Also poll regularly in case socket fails }); @@ -40,7 +42,7 @@ // transaction received at address // call page to check if funds received server side - alertify.log("Tx received on socket, checking..."); + alertify.log("Detected transaction, verifying..."); checkTransactionReceived(message.x.hash); } }; @@ -70,15 +72,25 @@ var guid = ''; var socket = null; var completed = false; + var enable = true; -

-

-

-
+ +

+

+

+
+
+ +

Bitcoin hotwallet is not enabled

+ Fail symbol
+

This organization has not enabled its bitcoin hotwallet in Swarmops. Therefore, direct donations are not available.

+

Contact the administrator for <%=this.CurrentOrganization.Name %> and ask them to enable the bitcoin hotwallet.

+
+
diff --git a/Site5/Pages/v5/Financial/Donate.aspx.cs b/Site5/Pages/v5/Financial/Donate.aspx.cs index db127e2e..9e85c3e8 100644 --- a/Site5/Pages/v5/Financial/Donate.aspx.cs +++ b/Site5/Pages/v5/Financial/Donate.aspx.cs @@ -12,6 +12,8 @@ using NBitcoin; using Swarmops.Logic.Cache; using Swarmops.Logic.Financial; using Swarmops.Logic.Security; +using Swarmops.Logic.Structure; +using Satoshis = NBitcoin.Money; // Sets it apart from Swarmops' Money class namespace Swarmops.Frontend.Pages.v5.Financial { @@ -21,13 +23,32 @@ namespace Swarmops.Frontend.Pages.v5.Financial { this.PageAccessRequired = new Access (this.CurrentOrganization, AccessAspect.Participant); - // TODO: This page needs a check on whether currentorganization.financialaccounts.assetbitcoinhot is null + /* TEMP TEMP TEMP - REMOVE THIS CODE */ + /* + Organization fwn = Organization.FromIdentity (2); + Salaries salaries = Salaries.ForOrganization (fwn); + foreach (Salary salary in salaries) + { + if (salary.PayrollItem.Person.BitcoinPayoutAddress.Length > 0 && salary.Attested == false) + { + salary.Attest (salary.PayrollItem.Person); // null for system apparently isn't allowed here + } + }*/ + + // TODO: This page needs a check on whether currentorganization.financialaccounts.assetbitcoinhot is null this.PageTitle = Resources.Pages.Financial.Donate_PageTitle; this.InfoBoxLiteral = Resources.Pages.Financial.Donate_Info; this.LabelStatus.Text = Resources.Pages.Financial.Donate_StatusInitial; + if (this.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot == null) + { + this.PanelDisabled.Visible = true; + this.PanelEnabled.Visible = false; + this.LiteralEnable.Text = @"false"; + } + HotBitcoinAddress address = HotBitcoinAddress.Create (this.CurrentOrganization, BitcoinUtility.BitcoinDonationsIndex, this.CurrentUser.Identity); @@ -38,20 +59,14 @@ namespace Swarmops.Frontend.Pages.v5.Financial // TEST TEST TEST /* - BitcoinSecret secretKey = - FinancialAccounts.BitcoinHotPrivateRoot.Derive ((uint) this.CurrentOrganization.Identity) - .Derive (BitcoinUtility.BitcoinDonationsIndex) - .Derive ((uint) this.CurrentUser.Identity) - .PrivateKey - .GetBitcoinSecret (Network.Main); - + BitcoinSecret secretKey = address.PrivateKey; Coin[] spendableCoin = BitcoinUtility.GetSpendableCoin (secretKey); TransactionBuilder txBuild = new TransactionBuilder(); Transaction tx = txBuild.AddCoins (spendableCoin) .AddKeys (secretKey) - .Send (new BitcoinAddress (BitcoinUtility.BitcoinTestAddress), "0.001") - .SendFees ("0.0001") + .Send (new BitcoinAddress (BitcoinUtility.BitcoinTestAddress), new Satoshis (address.UnspentSatoshis - BitcoinUtility.FeeSatoshisPerThousandBytes)) + .SendFees (new Satoshis(BitcoinUtility.FeeSatoshisPerThousandBytes)) .SetChange (secretKey.GetAddress()) .BuildTransaction (true); diff --git a/Site5/Pages/v5/Financial/Json-PayableCosts.aspx.cs b/Site5/Pages/v5/Financial/Json-PayableCosts.aspx.cs index a911c906..730d6711 100644 --- a/Site5/Pages/v5/Financial/Json-PayableCosts.aspx.cs +++ b/Site5/Pages/v5/Financial/Json-PayableCosts.aspx.cs @@ -39,9 +39,17 @@ namespace Swarmops.Frontend.Pages.Financial StringBuilder result = new StringBuilder (16384); DateTime today = DateTime.Today; + bool bitcoinHotWalletActive = (this.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot != null + ? true + : false); foreach (Payout payout in payouts) { + if (bitcoinHotWalletActive && payout.RecipientPerson != null && payout.RecipientPerson.BitcoinPayoutAddress.Length > 0 && payout.Account.Length < 2) + { + continue; + } + result.Append ("{"); result.AppendFormat ( "\"itemId\":\"{0}\"," + diff --git a/Site5/web.config b/Site5/web.config index 36c3118f..204dfa3c 100644 --- a/Site5/web.config +++ b/Site5/web.config @@ -1,4 +1,4 @@ - + - - - - + + + + + + + + - + - + - - - - - - - - + + + + + + + + - + - + - - - - + + + + - + - + - + - - + + - + - +