From 7c2cb4fc7cd1aa258e418d34050774779b5c5028 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Wed, 15 Nov 2017 12:36:28 +0100 Subject: [PATCH] Added Bitcoin Cash db fields --- Backend/BackendLoop.cs | 4 +- .../Types/Financial/BasicHotBitcoinAddress.cs | 13 ++-- Common/Common.csproj | 1 + Common/Enums/BitcoinChain.cs | 26 +++++++ Database/Database-HotBitcoinAddresses.cs | 21 +++--- Database/Schemata/revisions.txt | 1 + Database/Schemata/upgrade-0052.sql | 69 +++++++++++++++++++ Logic/Financial/BitcoinUtility.cs | 3 +- Logic/Financial/HotBitcoinAddress.cs | 12 ++-- Logic/Financial/Payouts.cs | 7 +- README.md | 2 +- Site/Pages/v5/Admin/BitcoinEchoTest.aspx.cs | 5 +- Site/Pages/v5/Financial/Donate.aspx.cs | 5 +- 13 files changed, 142 insertions(+), 27 deletions(-) create mode 100644 Common/Enums/BitcoinChain.cs create mode 100644 Database/Schemata/upgrade-0052.sql diff --git a/Backend/BackendLoop.cs b/Backend/BackendLoop.cs index 6d46b54b..28d02590 100644 --- a/Backend/BackendLoop.cs +++ b/Backend/BackendLoop.cs @@ -466,8 +466,8 @@ namespace Swarmops.Backend try { - BotLog.Write(1, "FiveMinute", "Starting automated payout processing"); - Payouts.PerformAutomated(); + // BotLog.Write(1, "FiveMinute", "Starting automated payout processing"); + // Payouts.PerformAutomated(); // TODO: Re-enable with Bitcoin Cash } catch (Exception e) { diff --git a/Basic/Types/Financial/BasicHotBitcoinAddress.cs b/Basic/Types/Financial/BasicHotBitcoinAddress.cs index a9141321..64486bbd 100644 --- a/Basic/Types/Financial/BasicHotBitcoinAddress.cs +++ b/Basic/Types/Financial/BasicHotBitcoinAddress.cs @@ -3,35 +3,40 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Swarmops.Common.Enums; using Swarmops.Common.Interfaces; namespace Swarmops.Basic.Types.Financial { public class BasicHotBitcoinAddress: IHasIdentity { - public BasicHotBitcoinAddress (int hotBitcoinAddressId, int organizationId, string derivationPath, - string address, Int64 balanceSatoshis, Int64 throughputSatoshis) + public BasicHotBitcoinAddress (int hotBitcoinAddressId, int organizationId, BitcoinChain chain, string derivationPath, + string address, string addressFallback, Int64 balanceSatoshis, Int64 throughputSatoshis) { this.HotBitcoinAddressId = hotBitcoinAddressId; this.OrganizationId = organizationId; + this.Chain = chain; this.DerivationPath = derivationPath; this.Address = address; + this.AddressFallback = addressFallback; this.BalanceSatoshis = balanceSatoshis; this.ThroughputSatoshis = throughputSatoshis; } public BasicHotBitcoinAddress (BasicHotBitcoinAddress original) : this ( - original.HotBitcoinAddressId, original.OrganizationId, original.DerivationPath, original.Address, original.BalanceSatoshis, - original.ThroughputSatoshis) + original.HotBitcoinAddressId, original.OrganizationId, original.Chain, original.DerivationPath, + original.Address, original.AddressFallback, original.BalanceSatoshis, original.ThroughputSatoshis) { // copy ctor } public int HotBitcoinAddressId { get; private set; } public int OrganizationId { get; private set; } + public BitcoinChain Chain { get; private set; } public string DerivationPath { get; private set; } public string Address { get; private set; } + public string AddressFallback { get; private set; } public Int64 BalanceSatoshis { get; protected set; } public Int64 ThroughputSatoshis { get; protected set; } diff --git a/Common/Common.csproj b/Common/Common.csproj index c5acac8c..6d681984 100644 --- a/Common/Common.csproj +++ b/Common/Common.csproj @@ -45,6 +45,7 @@ + diff --git a/Common/Enums/BitcoinChain.cs b/Common/Enums/BitcoinChain.cs new file mode 100644 index 00000000..10aef653 --- /dev/null +++ b/Common/Enums/BitcoinChain.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Swarmops.Common.Enums +{ + public enum BitcoinChain + { + /// + /// Undefined chain -- should never happen, but could probably be resolved by asking block explorers + /// + Unknown = 0, + + /// + /// The "Core" chain, referring to the Blockstream-hijacked chain of 2014 + /// + Core = 1, + + /// + /// The "Cash" chain, referring to the fork of August 1 2017 following the original Bitcoin whitepaper + /// + Cash = 2 + } +} diff --git a/Database/Database-HotBitcoinAddresses.cs b/Database/Database-HotBitcoinAddresses.cs index c2f6d50f..b472dc2f 100644 --- a/Database/Database-HotBitcoinAddresses.cs +++ b/Database/Database-HotBitcoinAddresses.cs @@ -7,6 +7,7 @@ using System.Text; using System.Threading.Tasks; using Swarmops.Basic.Types.Financial; using Swarmops.Basic.Types.Governance; +using Swarmops.Common.Enums; namespace Swarmops.Database { @@ -15,8 +16,8 @@ namespace Swarmops.Database #region Field reading code private const string hotBitcoinAddressFieldSequence = - " HotBitcoinAddressId,OrganizationId,DerivationPath,AddressString,BalanceSatoshis," + // 0-4 - " ThroughputSatoshis " + // 5 + " HotBitcoinAddressId,OrganizationId,BitcoinChainId,DerivationPath,AddressString," + // 0-4 + " AddressStringFallback,BalanceSatoshis,ThroughputSatoshis" + // 5-7 " FROM HotBitcoinAddresses "; private const string hotBitcoinAddressUnspentFieldSequence = @@ -28,12 +29,14 @@ namespace Swarmops.Database { int hotBitcoinAddressId = reader.GetInt32 (0); int organizationId = reader.GetInt32 (1); - string derivationPath = reader.GetString (2); - string addressString = reader.GetString (3); - Int64 balanceSatoshis = reader.GetInt64 (4); - Int64 throughputSatoshis = reader.GetInt64 (5); + BitcoinChain chain = (BitcoinChain) reader.GetInt32(2); + string derivationPath = reader.GetString (3); + string addressString = reader.GetString(4); + string addressStringFallback = reader.GetString(5); + Int64 balanceSatoshis = reader.GetInt64 (6); + Int64 throughputSatoshis = reader.GetInt64 (7); - return new BasicHotBitcoinAddress (hotBitcoinAddressId, organizationId, derivationPath, addressString, balanceSatoshis, throughputSatoshis); + return new BasicHotBitcoinAddress (hotBitcoinAddressId, organizationId, chain, derivationPath, addressString, addressStringFallback, balanceSatoshis, throughputSatoshis); } private BasicHotBitcoinAddressUnspent ReadHotBitcoinAddressUnspentFromDataReader(IDataRecord reader) @@ -174,7 +177,7 @@ namespace Swarmops.Database #region Creation and manipulation - public int CreateHotBitcoinAddressConditional(int organizationId, string derivationPath, string address) + public int CreateHotBitcoinAddressConditional(int organizationId, BitcoinChain chain, string derivationPath, string address, string addressFallback = "") { using (DbConnection connection = GetMySqlDbConnection()) { @@ -185,8 +188,10 @@ namespace Swarmops.Database command.CommandType = CommandType.StoredProcedure; AddParameterWithName(command, "organizationId", organizationId); + AddParameterWithName(command, "bitcoinChainId", (int) chain); AddParameterWithName(command, "derivationPath", derivationPath); AddParameterWithName(command, "addressString", address); + AddParameterWithName(command, "addressStringFallback", addressFallback); return Convert.ToInt32(command.ExecuteScalar()); } diff --git a/Database/Schemata/revisions.txt b/Database/Schemata/revisions.txt index 60593e87..33c5f827 100644 --- a/Database/Schemata/revisions.txt +++ b/Database/Schemata/revisions.txt @@ -49,3 +49,4 @@ 0049 - Another fix for CreateVatReportItem 0050 - Added VatReports.OpenTransaction, .CloseTransaction 0051 - Fix for CreateFinancialTransactionStub with Amount field gone (caused in 0043) [RF] +0052 - Added database support for multiple Bitcoin chain forks [RF] \ No newline at end of file diff --git a/Database/Schemata/upgrade-0052.sql b/Database/Schemata/upgrade-0052.sql new file mode 100644 index 00000000..dce16196 --- /dev/null +++ b/Database/Schemata/upgrade-0052.sql @@ -0,0 +1,69 @@ +ALTER TABLE `HotBitcoinAddresses` +ADD COLUMN `BitcoinChainId` INT NOT NULL DEFAULT 1 AFTER `OrganizationId`, +ADD COLUMN `AddressStringFallback` VARCHAR(48) NOT NULL DEFAULT '' AFTER `AddressString`, +ADD INDEX `Ix_Chain` (`BitcoinChainId` ASC), +ADD INDEX `Ix_Address2` (`AddressStringFallback` ASC) + + +# + + +DROP PROCEDURE IF EXISTS `CreateHotBitcoinAddressConditional` + +# + + +DROP PROCEDURE IF EXISTS `CreateHotBitcoinAddress` + +# + + +CREATE PROCEDURE `CreateHotBitcoinAddressConditional`( + IN organizationId INT, + IN bitcoinChainId INT, + IN derivationPath VARCHAR(256), + IN addressString VARCHAR(48), + IN addressStringFallback VARCHAR(48) +) +BEGIN + + IF ((SELECT COUNT(*) FROM HotBitcoinAddresses WHERE HotBitcoinAddresses.AddressString=addressString AND HotBitcoinAddresses.BitcoinChainId=bitcoinChainId) = 0) + THEN + + INSERT INTO HotBitcoinAddresses + (OrganizationId,BitcoinChainId,DerivationPath,AddressString,BalanceSatoshis,ThroughputSatoshis) + VALUES + (organizationId,bitcoinChainId,derivationPath,addressString,addressStringFallback,0,0); + + SELECT LAST_INSERT_ID() AS Identity; + + ELSE + + SELECT HotBitcoinAddresses.HotBitcoinAddressId AS Identity + FROM HotBitcoinAddresses + WHERE HotBitcoinAddresses.AddressString=addressString AND HotBitcoinAddresses.BitcoinChainId=bitcoinChainId; + + END IF; + +END + + +# + + +CREATE PROCEDURE `CreateHotBitcoinAddress`( + IN organizationId INT, + IN bitcoinChainId INT, + IN derivationPath VARCHAR(256), + IN addressString VARCHAR(48), + IN addressStringFallback VARCHAR(48) +) +BEGIN + INSERT INTO HotBitcoinAddresses + (OrganizationId,BitcoinChainId,DerivationPath,AddressString,AddressStringFallback,BalanceSatoshis,ThroughputSatoshis) + VALUES + (organizationId,bitcoinChainId,derivationPath,addressString,addressStringFallback,0,0); + + SELECT LAST_INSERT_ID() AS Identity; + +END diff --git a/Logic/Financial/BitcoinUtility.cs b/Logic/Financial/BitcoinUtility.cs index 869996b7..1a1222ee 100644 --- a/Logic/Financial/BitcoinUtility.cs +++ b/Logic/Financial/BitcoinUtility.cs @@ -16,6 +16,7 @@ using System.Web.Configuration; using NBitcoin; using Newtonsoft.Json.Linq; using Swarmops.Common; +using Swarmops.Common.Enums; using Swarmops.Database; using Swarmops.Logic.Communications; using Swarmops.Logic.Communications.Payload; @@ -682,7 +683,7 @@ namespace Swarmops.Logic.Financial Dictionary> notificationSpecLookup = new Dictionary>(); Dictionary> notificationAmountLookup = new Dictionary>(); Payout masterPayoutPrototype = Payout.Empty; - HotBitcoinAddress changeAddress = HotBitcoinAddress.OrganizationWalletZero(organization); + HotBitcoinAddress changeAddress = HotBitcoinAddress.OrganizationWalletZero(organization, BitcoinChain.Core); // Add the test payment diff --git a/Logic/Financial/HotBitcoinAddress.cs b/Logic/Financial/HotBitcoinAddress.cs index 48a398d5..2e776093 100644 --- a/Logic/Financial/HotBitcoinAddress.cs +++ b/Logic/Financial/HotBitcoinAddress.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; using NBitcoin; using Swarmops.Basic.Types.Financial; +using Swarmops.Common.Enums; using Swarmops.Database; using Swarmops.Logic.Structure; @@ -49,7 +50,7 @@ namespace Swarmops.Logic.Financial throw new NotImplementedException(); } - public static HotBitcoinAddress Create (Organization organization, params int[] derivationPath) + public static HotBitcoinAddress Create (Organization organization, BitcoinChain chain, params int[] derivationPath) { ExtPubKey extPubKey = BitcoinUtility.BitcoinHotPublicRoot; extPubKey = extPubKey.Derive ((uint) organization.Identity); @@ -62,11 +63,12 @@ namespace Swarmops.Logic.Financial } derivationPathString = derivationPathString.TrimStart(); - string bitcoinAddress = extPubKey.PubKey.GetAddress (Network.Main).ToString(); + string bitcoinAddress = extPubKey.PubKey.GetAddress (Network.Main).ToString(); // TODO: CHANGE NETWORK.MAIN TO NEW LOOKUP + string bitcoinAddressFallback = extPubKey.PubKey.GetAddress(Network.Main).ToString(); // The fallback address would be the main address int hotBitcoinAddressId = SwarmDb.GetDatabaseForWriting() - .CreateHotBitcoinAddressConditional (organization.Identity, derivationPathString, bitcoinAddress); + .CreateHotBitcoinAddressConditional (organization.Identity, chain, derivationPathString, bitcoinAddress); return FromIdentityAggressive (hotBitcoinAddressId); } @@ -76,9 +78,9 @@ namespace Swarmops.Logic.Financial return FromBasic(SwarmDb.GetDatabaseForReading().GetHotBitcoinAddress(bitcoinAddress)); } - public static HotBitcoinAddress OrganizationWalletZero (Organization organization) + public static HotBitcoinAddress OrganizationWalletZero (Organization organization, BitcoinChain chain) { - return Create (organization, BitcoinUtility.BitcoinWalletIndex, 0); + return Create (organization, chain, BitcoinUtility.BitcoinWalletIndex, 0); } public BitcoinSecret PrivateKey diff --git a/Logic/Financial/Payouts.cs b/Logic/Financial/Payouts.cs index d335d683..5b36676c 100644 --- a/Logic/Financial/Payouts.cs +++ b/Logic/Financial/Payouts.cs @@ -6,6 +6,7 @@ using NBitcoin; using Swarmops.Basic.Types; using Swarmops.Basic.Types.Financial; using Swarmops.Common; +using Swarmops.Common.Enums; using Swarmops.Database; using Swarmops.Logic.Communications; using Swarmops.Logic.Communications.Payload; @@ -458,10 +459,12 @@ namespace Swarmops.Logic.Financial } } - public static void PerformAutomated() + public static void PerformAutomated(BitcoinChain chain) { // Perform all waiting hot payouts for all orgs in the installation + throw new NotImplementedException("Waiting for rewrite for Bitcoin Cash"); + DateTime utcNow = DateTime.UtcNow; foreach (Organization organization in Organizations.GetAll()) @@ -653,7 +656,7 @@ namespace Swarmops.Logic.Financial Dictionary> notificationSpecLookup = new Dictionary>(); Dictionary> notificationAmountLookup = new Dictionary>(); Payout masterPayoutPrototype = Payout.Empty; - HotBitcoinAddress changeAddress = HotBitcoinAddress.OrganizationWalletZero (organization); + HotBitcoinAddress changeAddress = HotBitcoinAddress.OrganizationWalletZero (organization, BitcoinChain.Core); // TODO: CHAIN! foreach (Payout payout in bitcoinPayouts) { diff --git a/README.md b/README.md index 77e46177..81e17d88 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ Beta-2 features progress Beta-2 will be released on December 5, with string freeze on December 2. Its focus is to retool for Bitcoin Cash ~~and/or the Bitcoin 2x fork. This goal is fluid and may change as the strength of the respective forks become clearer.~~ -- [ ] Database update for Bitcoin Cash balances for addresses +- [x] Database update for Bitcoin Cash balances for addresses - [ ] Update NBitcoin to handle dual-mode - [ ] Rewrite the Bitcoin Hotwallet page to display Bitcoin Cash balances (part 1, without conversion to fiat) - [ ] Rewrite the Bitcoin Echo page to use Bitcoin Cash diff --git a/Site/Pages/v5/Admin/BitcoinEchoTest.aspx.cs b/Site/Pages/v5/Admin/BitcoinEchoTest.aspx.cs index 86ef4551..5b31c1c8 100644 --- a/Site/Pages/v5/Admin/BitcoinEchoTest.aspx.cs +++ b/Site/Pages/v5/Admin/BitcoinEchoTest.aspx.cs @@ -8,6 +8,7 @@ using System.Web.Services; using System.Web.UI; using System.Web.UI.WebControls; using NBitcoin; +using Swarmops.Common.Enums; using Swarmops.Logic.Cache; using Swarmops.Logic.Financial; using Swarmops.Logic.Security; @@ -50,7 +51,7 @@ namespace Swarmops.Frontend.Pages.v5.Admin this.LiteralEnable.Text = @"false"; } - HotBitcoinAddress address = HotBitcoinAddress.Create (this.CurrentOrganization, + HotBitcoinAddress address = HotBitcoinAddress.Create (this.CurrentOrganization, BitcoinChain.Cash, BitcoinUtility.BitcoinDonationsIndex, this.CurrentUser.Identity); this.LiteralBitcoinAddress.Text = address.Address; @@ -84,7 +85,7 @@ namespace Swarmops.Frontend.Pages.v5.Admin CurrentOrganization.Name, address.Address); this.ImageBitcoinQr.ImageUrl = - "https://chart.googleapis.com/chart?cht=qr&chs=400x400&chl=bitcoin:" + + "https://chart.googleapis.com/chart?cht=qr&chs=400x400&chl=bitcoincash:" + HttpUtility.UrlEncode (address.Address + "?label=" + Uri.EscapeDataString (String.Format (Resources.Pages.Financial.Donate_TxLabel, CurrentOrganization.Name))); // URI scheme doesn't like &, = diff --git a/Site/Pages/v5/Financial/Donate.aspx.cs b/Site/Pages/v5/Financial/Donate.aspx.cs index fc968714..1ae5ed26 100644 --- a/Site/Pages/v5/Financial/Donate.aspx.cs +++ b/Site/Pages/v5/Financial/Donate.aspx.cs @@ -9,6 +9,7 @@ using System.Web.UI; using System.Web.UI.WebControls; using NBitcoin; using Newtonsoft.Json.Linq; +using Swarmops.Common.Enums; using Swarmops.Logic.Cache; using Swarmops.Logic.Financial; using Swarmops.Logic.Security; @@ -50,7 +51,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial this.PanelEnabled.Visible = false; } - HotBitcoinAddress address = HotBitcoinAddress.Create (this.CurrentOrganization, + HotBitcoinAddress address = HotBitcoinAddress.Create (this.CurrentOrganization, BitcoinChain.Cash, BitcoinUtility.BitcoinDonationsIndex, this.CurrentUser.Identity); this.LiteralBitcoinAddress.Text = address.Address; @@ -80,7 +81,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial CurrentOrganization.Name, address.Address); this.ImageBitcoinQr.ImageUrl = - "https://chart.googleapis.com/chart?cht=qr&chs=400x400&chl=bitcoin:" + + "https://chart.googleapis.com/chart?cht=qr&chs=400x400&chl=bitcoincash:" + HttpUtility.UrlEncode (address.Address + "?label=" + Uri.EscapeDataString (String.Format (Resources.Pages.Financial.Donate_TxLabel, CurrentOrganization.Name))); // URI scheme doesn't like &, =