From 73fe7d4e42ff42078113b35f70c5675491521c88 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sat, 10 Oct 2015 21:56:18 +0200 Subject: [PATCH] Work on hotwallets and payouts --- .../BasicHotBitcoinAddressUnspent.cs | 6 +- Database/Database-HotBitcoinAddresses.cs | 14 +++-- Logic/Financial/BitcoinTransactionInput.cs | 23 ++++++++ Logic/Financial/BitcoinUtility.cs | 12 ++++ Logic/Financial/HotBitcoinAddressUnspent.cs | 5 ++ Logic/Financial/HotBitcoinAddressUnspents.cs | 25 ++++++++ Logic/Financial/HotBitcoinAddresses.cs | 58 ++++++++++++++++++- Logic/Financial/Payouts.cs | 46 +++++++++++++++ Logic/Logic.csproj | 1 + .../App_GlobalResources/Pages.Financial.resx | 3 + Site5/Pages/v5/Financial/Donate.aspx | 2 +- Site5/Pages/v5/Financial/Donate.aspx.cs | 1 + 12 files changed, 186 insertions(+), 10 deletions(-) create mode 100644 Logic/Financial/BitcoinTransactionInput.cs diff --git a/Basic/Types/Financial/BasicHotBitcoinAddressUnspent.cs b/Basic/Types/Financial/BasicHotBitcoinAddressUnspent.cs index 5441676f..edba6a31 100644 --- a/Basic/Types/Financial/BasicHotBitcoinAddressUnspent.cs +++ b/Basic/Types/Financial/BasicHotBitcoinAddressUnspent.cs @@ -9,10 +9,11 @@ namespace Swarmops.Basic.Types.Financial { public class BasicHotBitcoinAddressUnspent: IHasIdentity { - public BasicHotBitcoinAddressUnspent (int hotBitcoinAddressUnspentId, string transactionHash, + public BasicHotBitcoinAddressUnspent (int hotBitcoinAddressUnspentId, int hotBitcoinAddressId, string transactionHash, int transactionOutputIndex, Int64 amountSatoshis, int confirmationCount) { this.HotBitcoinAddressUnspentId = hotBitcoinAddressUnspentId; + this.HotBitcoinAddressId = hotBitcoinAddressId; this.TransactionHash = transactionHash; this.TransactionOutputIndex = transactionOutputIndex; this.AmountSatoshis = amountSatoshis; @@ -21,13 +22,14 @@ namespace Swarmops.Basic.Types.Financial public BasicHotBitcoinAddressUnspent (BasicHotBitcoinAddressUnspent original) : this ( - original.HotBitcoinAddressUnspentId, original.TransactionHash, original.TransactionOutputIndex, + original.HotBitcoinAddressUnspentId, original.HotBitcoinAddressId, original.TransactionHash, original.TransactionOutputIndex, original.AmountSatoshis, original.ConfirmationCount) { // copy ctor } public int HotBitcoinAddressUnspentId { get; private set; } + public int HotBitcoinAddressId { get; private set; } public string TransactionHash { get; private set; } public int TransactionOutputIndex { get; private set; } public Int64 AmountSatoshis { get; private set; } diff --git a/Database/Database-HotBitcoinAddresses.cs b/Database/Database-HotBitcoinAddresses.cs index 01eec39b..55d21664 100644 --- a/Database/Database-HotBitcoinAddresses.cs +++ b/Database/Database-HotBitcoinAddresses.cs @@ -20,7 +20,8 @@ namespace Swarmops.Database " FROM HotBitcoinAddresses "; private const string hotBitcoinAddressUnspentFieldSequence = - " HotBitcoinAddressUnspentId,TransactionHash,TransactionOutputIndex,AmountSatoshis,ConfirmationCount" + // 0-4 + " HotBitcoinAddressUnspentId,HotBitcoinAddressId,TransactionHash,TransactionOutputIndex,AmountSatoshis," + // 0-4 + " ConfirmationCount" + // 5 " FROM HotBitcoinAddressUnspents "; private BasicHotBitcoinAddress ReadHotBitcoinAddressFromDataReader(IDataRecord reader) @@ -38,12 +39,13 @@ namespace Swarmops.Database private BasicHotBitcoinAddressUnspent ReadHotBitcoinAddressUnspentFromDataReader(IDataRecord reader) { int hotBitcoinAddressUnspentId = reader.GetInt32 (0); - string transactionHash = reader.GetString (1); - int transactionOutputIndex = reader.GetInt32 (2); - Int64 amountSatoshis = reader.GetInt64 (3); - int confirmationCount = reader.GetInt32 (4); + int hotBitcoinAddressId = reader.GetInt32 (1); + string transactionHash = reader.GetString (2); + int transactionOutputIndex = reader.GetInt32 (3); + Int64 amountSatoshis = reader.GetInt64 (4); + int confirmationCount = reader.GetInt32 (5); - return new BasicHotBitcoinAddressUnspent (hotBitcoinAddressUnspentId, transactionHash, transactionOutputIndex, amountSatoshis, confirmationCount); + return new BasicHotBitcoinAddressUnspent (hotBitcoinAddressUnspentId, hotBitcoinAddressId, transactionHash, transactionOutputIndex, amountSatoshis, confirmationCount); } #endregion diff --git a/Logic/Financial/BitcoinTransactionInput.cs b/Logic/Financial/BitcoinTransactionInput.cs new file mode 100644 index 00000000..70679d66 --- /dev/null +++ b/Logic/Financial/BitcoinTransactionInput.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NBitcoin; + +namespace Swarmops.Logic.Financial +{ + public class BitcoinTransactionInput + { + public BitcoinSecret PrivateKey { get; set; } + public Int64 AmountSatoshis { get; set; } + public string TransactionHash { get; set; } + public Int32 TransactionOutputIndex { get; set; } + public string BitcoinAddress { get; set; } + } + + public class BitcoinTransactionInputs: List + { + // typedef the name + } +} diff --git a/Logic/Financial/BitcoinUtility.cs b/Logic/Financial/BitcoinUtility.cs index 2b3e56e9..23dd8e64 100644 --- a/Logic/Financial/BitcoinUtility.cs +++ b/Logic/Financial/BitcoinUtility.cs @@ -13,6 +13,7 @@ using NBitcoin; using NBitcoin.BouncyCastle.Asn1.Ocsp; using Newtonsoft.Json.Linq; using Swarmops.Database; +using Swarmops.Logic.Structure; using Swarmops.Logic.Support; namespace Swarmops.Logic.Financial @@ -165,6 +166,17 @@ namespace Swarmops.Logic.Financial } } + static public BitcoinTransactionInputs GetInputsForAmount (Organization organization, Int64 satoshis) + { + // TODO: Verify inputs up to date? + + // TODO: What's the most efficient way to do this? + + HotBitcoinAddresses addresses = HotBitcoinAddresses.ForOrganization (organization); + + return addresses.FindAmount (satoshis); // Will throw on a number of circumstances + } + public const int BitcoinWalletIndex = 1; public const int BitcoinDonationsIndex = 2; public const int BitcoinLoansIndex = 3; diff --git a/Logic/Financial/HotBitcoinAddressUnspent.cs b/Logic/Financial/HotBitcoinAddressUnspent.cs index 04110f20..f280c1d3 100644 --- a/Logic/Financial/HotBitcoinAddressUnspent.cs +++ b/Logic/Financial/HotBitcoinAddressUnspent.cs @@ -18,5 +18,10 @@ namespace Swarmops.Logic.Financial { return new HotBitcoinAddressUnspent (basic); } + + public HotBitcoinAddress Address + { + get { return HotBitcoinAddress.FromIdentity (base.HotBitcoinAddressId); } + } } } diff --git a/Logic/Financial/HotBitcoinAddressUnspents.cs b/Logic/Financial/HotBitcoinAddressUnspents.cs index 7cfd4335..773341d7 100644 --- a/Logic/Financial/HotBitcoinAddressUnspents.cs +++ b/Logic/Financial/HotBitcoinAddressUnspents.cs @@ -15,5 +15,30 @@ namespace Swarmops.Logic.Financial { return FromArray (SwarmDb.GetDatabaseForReading().GetHotBitcoinAddressUnspents (address)); } + + public BitcoinTransactionInputs AsInputs + { + get + { + BitcoinTransactionInputs result = new BitcoinTransactionInputs(); + foreach (HotBitcoinAddressUnspent unspent in this) + { + HotBitcoinAddress address = unspent.Address; + + BitcoinTransactionInput input = new BitcoinTransactionInput() + { + AmountSatoshis = unspent.AmountSatoshis, + BitcoinAddress = address.Address, + PrivateKey = address.PrivateKey, + TransactionHash = unspent.TransactionHash, + TransactionOutputIndex = unspent.TransactionOutputIndex + }; + + result.Add (input); + } + + return result; + } + } } } diff --git a/Logic/Financial/HotBitcoinAddresses.cs b/Logic/Financial/HotBitcoinAddresses.cs index e6dd771e..d9018a88 100644 --- a/Logic/Financial/HotBitcoinAddresses.cs +++ b/Logic/Financial/HotBitcoinAddresses.cs @@ -1,15 +1,71 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using Swarmops.Basic.Types.Financial; +using Swarmops.Database; +using Swarmops.Logic.Structure; using Swarmops.Logic.Support; namespace Swarmops.Logic.Financial { public class HotBitcoinAddresses: PluralBase { - // to be implemented + public static HotBitcoinAddresses ForOrganization (Organization organization) + { + return FromArray (SwarmDb.GetDatabaseForReading().GetHotBitcoinAddresses (organization)); + } + + public HotBitcoinAddresses InMainWallet + { + get + { + HotBitcoinAddresses subset = new HotBitcoinAddresses(); + string startMatch = BitcoinUtility.BitcoinWalletIndex.ToString (CultureInfo.InvariantCulture) + " "; + foreach (HotBitcoinAddress address in this) + { + if (address.DerivationPath.StartsWith (startMatch)) + { + subset.Add (address); + } + } + + return subset; + } + } + + public BitcoinTransactionInputs FindAmount (Int64 satoshisRequired) + { + // Sort addresses by known balance, richest first + + HotBitcoinAddresses workCopy = this; + + workCopy.Sort((a,b) => Math.Sign(b.BalanceSatoshis - a.BalanceSatoshis)); // Descending order + + // Do linear search from top for last address with sufficient money on its own. TODO: Binary search. + + int lastSufficientIndex = -1; + for (int index = 0; index < workCopy.Count; index++) + { + if (workCopy[index].BalanceSatoshis >= satoshisRequired) + { + lastSufficientIndex = index; + } + } + + if (lastSufficientIndex >= 0) + { + // There exists at least one single input which is sufficient to meet the requirement. + // The lowest input fulfilling this criterion has been chosen as return value. + + return workCopy[lastSufficientIndex].Unspents.AsInputs; + } + + throw new NotImplementedException("This fundfinding path is not completed"); // TODO + } + + } } diff --git a/Logic/Financial/Payouts.cs b/Logic/Financial/Payouts.cs index 3522a847..cd77cb7d 100644 --- a/Logic/Financial/Payouts.cs +++ b/Logic/Financial/Payouts.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using NBitcoin; using Swarmops.Basic.Types; using Swarmops.Basic.Types.Financial; using Swarmops.Database; @@ -465,5 +466,50 @@ namespace Swarmops.Logic.Financial } } } + + public void PerformAutomated() + { + // Perform all waiting hot payouts for all orgs in the installation + + foreach (Organization organization in Organizations.GetAll()) + { + Payouts orgPayouts = Payouts.Construct (organization); + Payouts bitcoinPayouts = new Payouts(); + Dictionary satoshiLookup = new Dictionary(); + Int64 satoshisTotal = 0; + + // For each ready payout that can automate, add an output to a constructed transaction + + TransactionBuilder txBuilder = new TransactionBuilder(); + foreach (Payout payout in orgPayouts) + { + if (payout.RecipientPerson != null && payout.RecipientPerson.BitcoinPayoutAddress.Length > 2 && + payout.Account.Length < 4) + { + bitcoinPayouts.Add (payout); + + // Find the amount of satoshis for this payout + + if (organization.Currency.IsBitcoin) + { + satoshiLookup[payout.ProtoIdentity] = payout.AmountCents; + satoshisTotal += payout.AmountCents; + } + else + { + // Convert currency + Money payoutAmount = new Money(payout.AmountCents, organization.Currency); + Int64 satoshis = payoutAmount.ToCurrency(Currency.Bitcoin).Cents; + satoshiLookup[payout.ProtoIdentity] = satoshis; + satoshisTotal += satoshis; + } + } + } + + // 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 = BitcoinUtility.GetInputsForAmount (organization, satoshisTotal + BitcoinUtility.FeeSatoshisPerThousandBytes * 20); // assume max 20k transaction size + } + } } } \ No newline at end of file diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index f992995e..de32f0bf 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -118,6 +118,7 @@ + diff --git a/Site5/App_GlobalResources/Pages.Financial.resx b/Site5/App_GlobalResources/Pages.Financial.resx index f2403500..eaad7470 100644 --- a/Site5/App_GlobalResources/Pages.Financial.resx +++ b/Site5/App_GlobalResources/Pages.Financial.resx @@ -346,6 +346,9 @@ Waiting for transaction... + + Transaction detected, verifying... + Donation to {0} The {0} is the organization name diff --git a/Site5/Pages/v5/Financial/Donate.aspx b/Site5/Pages/v5/Financial/Donate.aspx index 8bbf7364..f85ada57 100644 --- a/Site5/Pages/v5/Financial/Donate.aspx +++ b/Site5/Pages/v5/Financial/Donate.aspx @@ -42,7 +42,7 @@ // transaction received at address // call page to check if funds received server side - alertify.log("Detected transaction, verifying..."); + alertify.log(decodeURIComponent('')); checkTransactionReceived(message.x.hash); } }; diff --git a/Site5/Pages/v5/Financial/Donate.aspx.cs b/Site5/Pages/v5/Financial/Donate.aspx.cs index 9e85c3e8..f92d0f0d 100644 --- a/Site5/Pages/v5/Financial/Donate.aspx.cs +++ b/Site5/Pages/v5/Financial/Donate.aspx.cs @@ -41,6 +41,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial this.PageTitle = Resources.Pages.Financial.Donate_PageTitle; this.InfoBoxLiteral = Resources.Pages.Financial.Donate_Info; this.LabelStatus.Text = Resources.Pages.Financial.Donate_StatusInitial; + this.LiteralTxDetected.Text = JavascriptEscape (Resources.Pages.Financial.Donate_TransactionDetected); if (this.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot == null) {