Added Bitcoin Cash db fields

This commit is contained in:
Rick Falkvinge
2017-11-15 12:36:28 +01:00
parent d602fb5ed6
commit 7c2cb4fc7c
13 changed files with 142 additions and 27 deletions
+2 -2
View File
@@ -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)
{
@@ -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; }
+1
View File
@@ -45,6 +45,7 @@
<Compile Include="Constants.cs" />
<Compile Include="Enums\AddressValidationResult.cs" />
<Compile Include="Enums\AutoMailType.cs" />
<Compile Include="Enums\BitcoinChain.cs" />
<Compile Include="Enums\CommunicationType.cs" />
<Compile Include="Enums\DocumentType.cs" />
<Compile Include="Enums\EventTypeSource.cs" />
+26
View File
@@ -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
{
/// <summary>
/// Undefined chain -- should never happen, but could probably be resolved by asking block explorers
/// </summary>
Unknown = 0,
/// <summary>
/// The "Core" chain, referring to the Blockstream-hijacked chain of 2014
/// </summary>
Core = 1,
/// <summary>
/// The "Cash" chain, referring to the fork of August 1 2017 following the original Bitcoin whitepaper
/// </summary>
Cash = 2
}
}
+13 -8
View File
@@ -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());
}
+1
View File
@@ -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]
+69
View File
@@ -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
+2 -1
View File
@@ -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<int, List<string>> notificationSpecLookup = new Dictionary<int, List<string>>();
Dictionary<int, List<Int64>> notificationAmountLookup = new Dictionary<int, List<Int64>>();
Payout masterPayoutPrototype = Payout.Empty;
HotBitcoinAddress changeAddress = HotBitcoinAddress.OrganizationWalletZero(organization);
HotBitcoinAddress changeAddress = HotBitcoinAddress.OrganizationWalletZero(organization, BitcoinChain.Core);
// Add the test payment
+7 -5
View File
@@ -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
+5 -2
View File
@@ -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<int, List<string>> notificationSpecLookup = new Dictionary<int, List<string>>();
Dictionary<int, List<Int64>> notificationAmountLookup = new Dictionary<int, List<Int64>>();
Payout masterPayoutPrototype = Payout.Empty;
HotBitcoinAddress changeAddress = HotBitcoinAddress.OrganizationWalletZero (organization);
HotBitcoinAddress changeAddress = HotBitcoinAddress.OrganizationWalletZero (organization, BitcoinChain.Core); // TODO: CHAIN!
foreach (Payout payout in bitcoinPayouts)
{
+1 -1
View File
@@ -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
+3 -2
View File
@@ -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 &, =
+3 -2
View File
@@ -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 &, =