diff --git a/Logic/Financial/BitcoinTransactionInput.cs b/Logic/Financial/BitcoinTransactionInput.cs index 70679d66..52dad22e 100644 --- a/Logic/Financial/BitcoinTransactionInput.cs +++ b/Logic/Financial/BitcoinTransactionInput.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; +using System.Collections.Specialized; using System.Linq; using System.Text; using System.Threading.Tasks; using NBitcoin; +using Satoshis=NBitcoin.Money; namespace Swarmops.Logic.Financial { @@ -18,6 +20,46 @@ namespace Swarmops.Logic.Financial public class BitcoinTransactionInputs: List { - // typedef the name + public BitcoinSecret[] PrivateKeys + { + get + { + List keyList = new List(); + + foreach (BitcoinTransactionInput input in this) + { + keyList.Add (input.PrivateKey); + } + + return keyList.ToArray(); + } + } + + public Coin[] Coins + { + get + { + List coinList = new List(); + + foreach (BitcoinTransactionInput input in this) + { + coinList.Add (new Coin ( + new OutPoint() + { + Hash = uint256.Parse (input.TransactionHash), + N = (uint) input.TransactionOutputIndex + }, + new TxOut (new Satoshis (input.AmountSatoshis), + new BitcoinAddress (input.BitcoinAddress, Network.Main)))); + } + + return coinList.ToArray(); + } + } + + public Int64 AmountSatoshisTotal + { + get { return this.Sum (input => input.AmountSatoshis); } + } } } diff --git a/Logic/Financial/BitcoinUtility.cs b/Logic/Financial/BitcoinUtility.cs index 23dd8e64..59165549 100644 --- a/Logic/Financial/BitcoinUtility.cs +++ b/Logic/Financial/BitcoinUtility.cs @@ -80,7 +80,7 @@ namespace Swarmops.Logic.Financial string addressString = secretKey.PubKey.GetAddress (Network.Main).ToString(); var unspentJsonResult = - JObject.Parse(new WebClient().DownloadString("https://blockchain.info/unspent?active=" + addressString + "&api_key=I_am_testing_the_format_please_ignore")); + JObject.Parse(new WebClient().DownloadString("https://blockchain.info/unspent?active=" + addressString + "&api_key=" + SystemSettings.BlockchainSwarmopsApiKey)); List coinList = new List(); @@ -110,7 +110,7 @@ namespace Swarmops.Logic.Financial var addressInfoResult = JObject.Parse ( new WebClient().DownloadString ( - "https://blockchain.info/address/" + address + "?format=json&api_key=I_am_testing_the_format_please_ignore")); + "https://blockchain.info/address/" + address + "?format=json&api_key=" + SystemSettings.BlockchainSwarmopsApiKey)); if ((int) addressInfoResult["final_balance"] == 0) { @@ -118,7 +118,9 @@ namespace Swarmops.Logic.Financial } var unspentJsonResult = - JObject.Parse(new WebClient().DownloadString("https://blockchain.info/unspent?active=" + address + "&api_key=I_am_testing_the_format_please_ignore")); // warn: will 500 if no unspent outpoints + JObject.Parse ( + new WebClient().DownloadString ("https://blockchain.info/unspent?active=" + address + "&api_key=" + + SystemSettings.BlockchainSwarmopsApiKey)); // warn: will 500 if no unspent outpoints foreach (var unspentJson in unspentJsonResult["unspent_outputs"]) { diff --git a/Logic/Financial/HotBitcoinAddress.cs b/Logic/Financial/HotBitcoinAddress.cs index 866fd1ba..48a398d5 100644 --- a/Logic/Financial/HotBitcoinAddress.cs +++ b/Logic/Financial/HotBitcoinAddress.cs @@ -76,6 +76,11 @@ namespace Swarmops.Logic.Financial return FromBasic(SwarmDb.GetDatabaseForReading().GetHotBitcoinAddress(bitcoinAddress)); } + public static HotBitcoinAddress OrganizationWalletZero (Organization organization) + { + return Create (organization, BitcoinUtility.BitcoinWalletIndex, 0); + } + public BitcoinSecret PrivateKey { get diff --git a/Logic/Financial/Payouts.cs b/Logic/Financial/Payouts.cs index 95556da9..a7d9d28b 100644 --- a/Logic/Financial/Payouts.cs +++ b/Logic/Financial/Payouts.cs @@ -523,9 +523,11 @@ namespace Swarmops.Logic.Financial // We now have our desired payments. The next step is to find enough inputs to reach the required amount (plus fees; we're working a little blind here still). + BitcoinTransactionInputs inputs = null; + try { - BitcoinTransactionInputs inputs = BitcoinUtility.GetInputsForAmount(organization, satoshisTotal + BitcoinUtility.FeeSatoshisPerThousandBytes * 20); // assume max 20k transaction size + inputs = BitcoinUtility.GetInputsForAmount(organization, satoshisTotal + BitcoinUtility.FeeSatoshisPerThousandBytes * 20); // assume max 20k transaction size } catch (NotEnoughFundsException) { @@ -536,7 +538,7 @@ namespace Swarmops.Logic.Financial Int64 satoshisAvailable = HotBitcoinAddresses.ForOrganization (organization).BalanceSatoshisTotal; secondaryStrings["AmountMissingMicrocoinsFloat"] = - ((satoshisAvailable - satoshisTotal)/100.0).ToString ("N2"); + ((satoshisTotal - satoshisAvailable)/100.0).ToString ("N2"); if (organization.Currency.IsBitcoin) { @@ -545,19 +547,47 @@ namespace Swarmops.Logic.Financial } else { + // convert to org native currency + secondaryStrings["AmountNeededFloat"] = (new Money (satoshisTotal, Currency.Bitcoin).ToCurrency (organization.Currency).Cents/100.0).ToString ("N2"); secondaryStrings["AmountWalletFloat"] = (new Money (satoshisAvailable, Currency.Bitcoin).ToCurrency (organization.Currency).Cents/100.0).ToString ("N2"); - - // convert to org native currency } OutboundComm.CreateNotification (organization, NotificationResource.Bitcoin_Shortage_Critical, primaryStrings, secondaryStrings, People.FromSingle (Person.FromIdentity (1))); } - // If we arrive at this point, the previous function didn't throw, and we have enough money. + // If we arrive at this point, the previous function didn't throw, and we have enough money. Add the inputs to the transaction. + + txBuilder = txBuilder.AddCoins (inputs.Coins); + txBuilder = txBuilder.AddKeys (inputs.PrivateKeys); + Int64 satoshisInput = inputs.AmountSatoshisTotal; + + // Add outputs and prepare notifications + + Int64 satoshisUsed = 0; + Dictionary notificationLookup = new Dictionary(); + + + + // Add fee + + // Add change to main wallet zero-slush address + + // Sign transaction - ready to execute + + // Prepare notifications after execution + + // Broadcast transaction + + // Delete inputs, adjust balance for addresses + + // Send notifications + + // Ledger entries + } } } diff --git a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs index f78a758c..c63156d5 100644 --- a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs +++ b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs @@ -61,7 +61,11 @@ namespace Swarmops.Logic.Resources { } /// - /// Looks up a localized string similar to [BitcoinAmount] millicoin was just transferred to your address by [OrganizationName].. + /// Looks up a localized string similar to [BitcoinAmount] microcoin ("bits") was just transferred to your bitcoin address ([BitcoinAddress]) by [OrganizationName] for the following: + /// + ///[PreformattedText] + /// + ///You should already see the transaction pending confirmations and it should achieve spendability within about an hour (six normal confirmations).. /// internal static string Bitcoin_PaidOut_Body { get { @@ -70,7 +74,7 @@ namespace Swarmops.Logic.Resources { } /// - /// Looks up a localized string similar to [[OrganizationName]] Funds paid using bitcoin: [CurrencyCode] [BudgetAmountFloat]. + /// Looks up a localized string similar to [[OrganizationName]] Funds paid using bitcoin: [CurrencyCode] [AmountFloat]. /// internal static string Bitcoin_PaidOut_Subject { get { diff --git a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx index a3fd356a..9be7ed28 100644 --- a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx +++ b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx @@ -150,10 +150,14 @@ If you did not set this payout address (it may be worth double checking the addr [[OrganizationName]] Bitcoin payout address set - [BitcoinAmount] millicoin was just transferred to your address by [OrganizationName]. + [BitcoinAmount] microcoin ("bits") was just transferred to your bitcoin address ([BitcoinAddress]) by [OrganizationName] for the following: + +[PreformattedText] + +You should already see the transaction pending confirmations and it should achieve spendability within about an hour (six normal confirmations). - [[OrganizationName]] Funds paid using bitcoin: [CurrencyCode] [BudgetAmountFloat] + [[OrganizationName]] Funds paid using bitcoin: [CurrencyCode] [AmountFloat] Swarmops is trying to make payments of [CurrencyCode] [AmountNeededFloat], but only [CurrencyCode] [AmountWalletFloat] is available in the bitcoin hotwallet. Swarmops is unable to make further payments at this time. diff --git a/Logic/Support/SystemSettings.cs b/Logic/Support/SystemSettings.cs index 5f2cbaf1..8df0c2e4 100644 --- a/Logic/Support/SystemSettings.cs +++ b/Logic/Support/SystemSettings.cs @@ -216,6 +216,12 @@ namespace Swarmops.Logic.Support } + public static string BlockchainSwarmopsApiKey + { + get { return "77e11665-14a9-4d72-9111-7dce7a6748f6"; } + } + + public static string EtcFolder { get diff --git a/Site5/Pages/v5/Financial/Donate.aspx.cs b/Site5/Pages/v5/Financial/Donate.aspx.cs index ea57e7d2..60e409cc 100644 --- a/Site5/Pages/v5/Financial/Donate.aspx.cs +++ b/Site5/Pages/v5/Financial/Donate.aspx.cs @@ -24,7 +24,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial this.PageAccessRequired = new Access (this.CurrentOrganization, AccessAspect.Participant); // TEMP - Payouts.PerformAutomated(); + // Payouts.PerformAutomated(); /* TEMP TEMP TEMP - REMOVE THIS CODE */ /* @@ -39,8 +39,6 @@ namespace Swarmops.Frontend.Pages.v5.Financial } }*/ - // 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;