Lots of work on unique HBAs, OInvs

This commit is contained in:
Rick Falkvinge
2017-11-29 17:52:23 +01:00
parent e130ae0cbb
commit a6e333aa3f
17 changed files with 210 additions and 38 deletions
@@ -11,12 +11,13 @@ namespace Swarmops.Basic.Types.Financial
public class BasicHotBitcoinAddress: IHasIdentity
{
public BasicHotBitcoinAddress (int hotBitcoinAddressId, int organizationId, BitcoinChain chain, string derivationPath,
string address, string addressFallback, Int64 balanceSatoshis, Int64 throughputSatoshis)
int uniqueDerive, string address, string addressFallback, Int64 balanceSatoshis, Int64 throughputSatoshis)
{
this.HotBitcoinAddressId = hotBitcoinAddressId;
this.OrganizationId = organizationId;
this.Chain = chain;
this.DerivationPath = derivationPath;
this.UniqueDerive = uniqueDerive;
this.Address = address;
this.AddressFallback = addressFallback;
this.BalanceSatoshis = balanceSatoshis;
@@ -26,7 +27,7 @@ namespace Swarmops.Basic.Types.Financial
public BasicHotBitcoinAddress (BasicHotBitcoinAddress original)
: this (
original.HotBitcoinAddressId, original.OrganizationId, original.Chain, original.DerivationPath,
original.Address, original.AddressFallback, original.BalanceSatoshis, original.ThroughputSatoshis)
original.UniqueDerive, original.Address, original.AddressFallback, original.BalanceSatoshis, original.ThroughputSatoshis)
{
// copy ctor
}
@@ -35,6 +36,7 @@ namespace Swarmops.Basic.Types.Financial
public int OrganizationId { get; private set; }
public BitcoinChain Chain { get; private set; }
public string DerivationPath { get; private set; }
public int UniqueDerive { get; private set; }
public string Address { get; private set; }
public string AddressFallback { get; private set; }
public Int64 BalanceSatoshis { get; protected set; }
@@ -6,7 +6,7 @@ namespace Swarmops.Basic.Types.Financial
public class BasicOutboundInvoice : IHasIdentity
{
public BasicOutboundInvoice (int outboundInvoiceId, string customerName, string invoiceAddressPaper,
string invoiceAddressMail, int currencyId, int organizationId, int budgetId,
string invoiceAddressMail, int currencyId, int organizationId, int organizationSequenceId, int budgetId,
DateTime createdDateTime, int createdByPersonId, DateTime dueDate, int reminderCount, string reference,
bool domestic, bool open, bool sent, string securityCode, string theirReference)
{
@@ -16,6 +16,7 @@ namespace Swarmops.Basic.Types.Financial
this.InvoiceAddressMail = invoiceAddressMail;
this.CurrencyId = currencyId;
this.OrganizationId = organizationId;
this.OrganizationSequenceId = organizationSequenceId;
this.BudgetId = budgetId;
this.CreatedDateTime = createdDateTime;
this.CreatedByPersonId = createdByPersonId;
@@ -33,7 +34,7 @@ namespace Swarmops.Basic.Types.Financial
public BasicOutboundInvoice (BasicOutboundInvoice original) :
this (
original.OutboundInvoiceId, original.CustomerName, original.InvoiceAddressPaper, original.InvoiceAddressMail,
original.CurrencyId, original.OrganizationId, original.BudgetId, original.CreatedDateTime,
original.CurrencyId, original.OrganizationId, original.OrganizationSequenceId, original.BudgetId, original.CreatedDateTime,
original.CreatedByPersonId, original.DueDate, original.ReminderCount, original.Reference, original.Domestic,
original.Open, original.Sent, original.SecurityCode, original.TheirReference)
{
@@ -46,6 +47,7 @@ namespace Swarmops.Basic.Types.Financial
public string InvoiceAddressMail { get; private set; }
public int CurrencyId { get; private set; }
public int OrganizationId { get; private set; }
public int OrganizationSequenceId { get; protected set; }
public int BudgetId { get; protected set; }
public DateTime CreatedDateTime { get; private set; }
public int CreatedByPersonId { get; private set; }
+1 -1
View File
@@ -12,7 +12,7 @@ namespace Swarmops.Database
/// <summary>
/// This is the IN-CODE revision of the database. The version we expect to be running against.
/// </summary>
public static int DbVersionExpected => 61;
public static int DbVersionExpected => 63;
// This class is in horrible need of serious refactoring. For its simplicity in task, it's a horrible bowl of spaghetti.
+65 -9
View File
@@ -16,8 +16,8 @@ namespace Swarmops.Database
#region Field reading code
private const string hotBitcoinAddressFieldSequence =
" HotBitcoinAddressId,OrganizationId,BitcoinChainId,DerivationPath,AddressString," + // 0-4
" AddressStringFallback,BalanceSatoshis,ThroughputSatoshis" + // 5-7
" HotBitcoinAddressId,OrganizationId,BitcoinChainId,DerivationPath,UniqueDerive," + // 0-4
" AddressString,AddressStringFallback,BalanceSatoshis,ThroughputSatoshis" + // 5-7
" FROM HotBitcoinAddresses ";
private const string hotBitcoinAddressUnspentFieldSequence =
@@ -31,12 +31,13 @@ namespace Swarmops.Database
int organizationId = reader.GetInt32 (1);
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);
int uniqueDerive = reader.GetInt32(4);
string addressString = reader.GetString(5);
string addressStringFallback = reader.GetString(6);
Int64 balanceSatoshis = reader.GetInt64 (7);
Int64 throughputSatoshis = reader.GetInt64 (8);
return new BasicHotBitcoinAddress (hotBitcoinAddressId, organizationId, chain, derivationPath, addressString, addressStringFallback, balanceSatoshis, throughputSatoshis);
return new BasicHotBitcoinAddress (hotBitcoinAddressId, organizationId, chain, derivationPath, uniqueDerive, addressString, addressStringFallback, balanceSatoshis, throughputSatoshis);
}
private BasicHotBitcoinAddressUnspent ReadHotBitcoinAddressUnspentFromDataReader(IDataRecord reader)
@@ -177,6 +178,25 @@ namespace Swarmops.Database
#region Creation and manipulation
public int CreateHotBitcoinAddress(int organizationId, BitcoinChain chain, string derivationPath)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
using (DbCommand command = GetDbCommand("CreateHotBitcoinAddress", connection))
{
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "organizationId", organizationId);
AddParameterWithName(command, "bitcoinChainId", (int)chain);
AddParameterWithName(command, "derivationPath", derivationPath);
return Convert.ToInt32(command.ExecuteScalar());
}
}
}
public int CreateHotBitcoinAddressConditional(int organizationId, BitcoinChain chain, string derivationPath, string address, string addressFallback = "")
{
using (DbConnection connection = GetMySqlDbConnection())
@@ -188,7 +208,7 @@ namespace Swarmops.Database
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "organizationId", organizationId);
AddParameterWithName(command, "bitcoinChainId", (int) chain);
AddParameterWithName(command, "bitcoinChainId", (int)chain);
AddParameterWithName(command, "derivationPath", derivationPath);
AddParameterWithName(command, "addressString", address);
AddParameterWithName(command, "addressStringFallback", addressFallback);
@@ -198,7 +218,43 @@ namespace Swarmops.Database
}
}
public int SetHotBitcoinAddressBalance (int hotBitcoinAddressId, Int64 balanceSatoshis)
public int SetHotBitcoinAddressAddress(int hotBitcoinAddressId, string addressString)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
using (DbCommand command = GetDbCommand("SetHotBitcoinAddressAddress", connection))
{
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "hotBitcoinAddressId", hotBitcoinAddressId);
AddParameterWithName(command, "addressString", addressString);
return Convert.ToInt32(command.ExecuteScalar());
}
}
}
public int SetHotBitcoinAddressFallbackAddress(int hotBitcoinAddressId, string fallbackAddressString)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
using (DbCommand command = GetDbCommand("SetHotBitcoinAddressFallbackAddress", connection))
{
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "hotBitcoinAddressId", hotBitcoinAddressId);
AddParameterWithName(command, "balanceSatoshis", fallbackAddressString);
return Convert.ToInt32(command.ExecuteScalar());
}
}
}
public int SetHotBitcoinAddressBalance(int hotBitcoinAddressId, Int64 balanceSatoshis)
{
using (DbConnection connection = GetMySqlDbConnection())
{
+22 -4
View File
@@ -13,7 +13,7 @@ namespace Swarmops.Database
private const string outboundInvoiceFieldSequence =
" OutboundInvoiceId,CustomerName,InvoiceAddressPaper,InvoiceAddressMail,CurrencyId," + // 0-4
"0,OrganizationId,BudgetId,CreatedDateTime,CreatedByPersonId," + // 5-9
"OrganizationId,OrganizationSequenceId,BudgetId,CreatedDateTime,CreatedByPersonId," + // 5-9
"DueDate,ReminderCount,Reference,Domestic,Open, " + // 10-14
"Sent,SecurityCode,TheirReference " +
"FROM OutboundInvoices ";
@@ -32,8 +32,8 @@ namespace Swarmops.Database
string invoiceAddressPaper = reader.GetString (2);
string invoiceAddressMail = reader.GetString (3);
int currencyId = reader.GetInt32 (4);
// double amount = reader.GetDouble(5);
int organizationId = reader.GetInt32 (6);
int organizationId = reader.GetInt32 (5);
int organizationSequenceId = reader.GetInt32(6);
int budgetId = reader.GetInt32 (7);
DateTime createdDateTime = reader.GetDateTime (8);
int createdByPersonId = reader.GetInt32 (9);
@@ -47,7 +47,7 @@ namespace Swarmops.Database
string theirReference = reader.GetString (17);
return new BasicOutboundInvoice (outboundInvoiceId, customerName, invoiceAddressPaper, invoiceAddressMail,
currencyId, organizationId, budgetId, createdDateTime,
currencyId, organizationId, organizationSequenceId, budgetId, createdDateTime,
createdByPersonId, dueDate, reminderCount, reference, domestic, open, sent, securityCode, theirReference);
}
@@ -210,6 +210,24 @@ namespace Swarmops.Database
}
public int SetOutboundInvoiceSequence(int outboundInvoiceId)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand("SetOutboundInvoiceOrganizationSequenceId", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName(command, "outboundInvoiceId", outboundInvoiceId);
return Convert.ToInt32(command.ExecuteScalar());
}
}
public int CreateOutboundInvoiceItem (int outboundInvoiceId, string description, double amount)
{
using (DbConnection connection = GetMySqlDbConnection())
+2
View File
@@ -59,3 +59,5 @@
0059 - Added BackendServiceOrders, quite a bit of code too, for returning echo UTXOs from backend [RF]
0060 - Dumb fix for BSO tables: forgot autoincrement on the primary keys [RF]
0061 - And CreatedDateTime can't be an INT, while we're at it; also renaming dateTime => nowUtc [RF]
0062 - Added OrganizationSequenceId to OutboundInvoices (like 0041 but outbound) [RF]
0063 - Added DeriveUnique to HotBitcoinAddresses for ability to create unique HBAs [RF]
+41 -7
View File
@@ -50,27 +50,56 @@ namespace Swarmops.Logic.Financial
throw new NotImplementedException();
}
public static HotBitcoinAddress Create (Organization organization, BitcoinChain chain, params int[] derivationPath)
public static HotBitcoinAddress Create(Organization organization, BitcoinChain chain, params int[] derivationPath)
{
ExtPubKey extPubKey = BitcoinUtility.BitcoinHotPublicRoot;
extPubKey = extPubKey.Derive ((uint) organization.Identity);
extPubKey = extPubKey.Derive((uint)organization.Identity);
string derivationPathString = string.Empty;
foreach (int derivation in derivationPath) // requires that order is consistent from 0 to n-1
{
extPubKey = extPubKey.Derive ((uint) derivation);
derivationPathString += " " + derivation.ToString (CultureInfo.InvariantCulture);
extPubKey = extPubKey.Derive((uint)derivation);
derivationPathString += " " + derivation.ToString(CultureInfo.InvariantCulture);
}
derivationPathString = derivationPathString.TrimStart();
string bitcoinAddress = extPubKey.PubKey.GetAddress (Network.Main).ToString(); // TODO: CHANGE NETWORK.MAIN TO NEW LOOKUP
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, chain, derivationPathString, bitcoinAddress);
.CreateHotBitcoinAddressConditional(organization.Identity, chain, derivationPathString, bitcoinAddress);
return FromIdentityAggressive (hotBitcoinAddressId);
return FromIdentityAggressive(hotBitcoinAddressId);
}
public static HotBitcoinAddress CreateUnique (Organization organization, BitcoinChain chain, params int[] derivationPath)
{
ExtPubKey extPubKey = BitcoinUtility.BitcoinHotPublicRoot;
extPubKey = extPubKey.Derive((uint)organization.Identity);
string derivationPathString = string.Empty;
foreach (int derivation in derivationPath) // requires that order is consistent from 0 to n-1
{
extPubKey = extPubKey.Derive((uint)derivation);
derivationPathString += " " + derivation.ToString(CultureInfo.InvariantCulture);
}
int hotBitcoinAddressId =
SwarmDb.GetDatabaseForWriting()
.CreateHotBitcoinAddress(organization.Identity, chain, derivationPathString);
HotBitcoinAddress addressTemp = FromIdentityAggressive(hotBitcoinAddressId);
// Derive the last step with the now-assigned unique identifier, then set address, read again, and return
extPubKey = extPubKey.Derive((uint) addressTemp.UniqueDerive);
string bitcoinAddressString = extPubKey.PubKey.GetAddress(Network.Main).ToString();
SwarmDb.GetDatabaseForWriting().SetHotBitcoinAddressAddress(hotBitcoinAddressId, bitcoinAddressString);
return FromIdentityAggressive(hotBitcoinAddressId);
}
public static HotBitcoinAddress FromAddress (BitcoinChain chain, string bitcoinAddress)
@@ -113,6 +142,11 @@ namespace Swarmops.Logic.Financial
secretExtKey = secretExtKey.Derive (UInt32.Parse (derivationString));
}
if (this.UniqueDerive > -1)
{
secretExtKey = secretExtKey.Derive((uint) this.UniqueDerive);
}
return secretExtKey.PrivateKey.GetBitcoinSecret (Network.Main);
}
}
+24
View File
@@ -17,6 +17,26 @@ namespace Swarmops.Logic.Financial
// empty ctor
}
public new int OrganizationSequenceId
{
get
{
if (base.OrganizationSequenceId == 0)
{
// This case is for legacy installations before DbVersion 63, when
// OrganizationSequenceId was added for each new outbound invoice
SwarmDb db = SwarmDb.GetDatabaseForWriting();
base.OrganizationSequenceId = db.SetInboundInvoiceSequence(this.Identity);
return base.OrganizationSequenceId;
}
return base.OrganizationSequenceId;
}
}
public OutboundInvoiceItems Items
{
get { return OutboundInvoiceItems.ForInvoice (this); }
@@ -68,6 +88,10 @@ namespace Swarmops.Logic.Financial
{
get { return FinancialAccount.FromIdentity (BudgetId); }
}
public Documents Documents
{
get { return Documents.ForObject(this); }
}
public new bool Sent
{
+1 -1
View File
@@ -84,7 +84,7 @@ namespace Swarmops.Logic.Support
order.HasTerminated = true;
}
order.Close();
SwarmDb.GetDatabaseForWriting().SetBackendServiceOrderException(this.Identity, exception);
order.ThrewException(exception);
}
finally
{
@@ -89,6 +89,11 @@ namespace Swarmops.Logic.Support.BackendServices
SwarmDb.GetDatabaseForWriting().SetBackendServiceOrderClosed(this.ServiceOrderIdentity);
}
public void ThrewException(Exception exception)
{
SwarmDb.GetDatabaseForWriting().SetBackendServiceOrderException(this.ServiceOrderIdentity, exception);
}
/// <summary>
/// This creates the order and serializes it into the database for execution.
/// </summary>
@@ -32,8 +32,14 @@ namespace Swarmops.Logic.Support.BackendServices
{
RasterizeDocumentHiresOrder order = (RasterizeDocumentHiresOrder) orderObject;
Document document = Document.FromIdentity(order.DocumentId);
PdfProcessor.Rerasterize((Document) document, PdfProcessor.PdfProcessorOptions.HighQuality | PdfProcessor.PdfProcessorOptions.ForceOrphans);
try
{
PdfProcessor.Rerasterize((Document)document, PdfProcessor.PdfProcessorOptions.HighQuality | PdfProcessor.PdfProcessorOptions.ForceOrphans);
}
catch (Exception exception)
{
order.ThrewException(exception);
}
order.Close();
order.HasTerminated = true;
}
@@ -20,6 +20,7 @@ namespace Swarmops.Logic.Support
Thread WorkerThread { get; set; }
void Terminate();
bool HasTerminated { get; set; }
void ThrewException(Exception exception);
int ServiceOrderIdentity { get; set; }
+9
View File
@@ -582,6 +582,15 @@ namespace Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Outbound Invoice #{0:N0} to {1}.
/// </summary>
internal static string Financial_OutboundInvoiceSpecificationWithCustomer {
get {
return ResourceManager.GetString("Financial_OutboundInvoiceSpecificationWithCustomer", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Conference.
/// </summary>
+3
View File
@@ -950,4 +950,7 @@
<value>Chain</value>
<comment>Short for "Blockchain fork", as in "what chain are you following"</comment>
</data>
<data name="Financial_OutboundInvoiceSpecificationWithCustomer" xml:space="preserve">
<value>Outbound Invoice #{0:N0} to {1}</value>
</data>
</root>
@@ -546,20 +546,16 @@ namespace Swarmops.Frontend.Pages.v5.Financial
private void PopulateInboundInvoices()
{
InboundInvoices invoices = InboundInvoices.ForOrganization (CurrentOrganization).WhereUnattested;
InboundInvoices invoices = InboundInvoices.ForOrganization (CurrentOrganization, true);
foreach (InboundInvoice invoice in invoices)
{
Documents dox = invoice.Documents;
bool hasDox = (dox.Count > 0 ? true : false);
if (this._attestationRights.ContainsKey (invoice.BudgetId) ||
invoice.Budget.OwnerPersonId == Person.NobodyId)
{
AddDocuments (invoice.Documents, "I" + invoice.Identity.ToString (CultureInfo.InvariantCulture),
String.Format (Global.Financial_InboundInvoiceSpecificationWithSender + " - ",
invoice.Identity, invoice.Supplier) + Global.Global_ImageSpecification);
}
AddDocuments (invoice.Documents, "I" + invoice.Identity.ToString (CultureInfo.InvariantCulture),
String.Format (Global.Financial_InboundInvoiceSpecificationWithSender + " - ",
invoice.OrganizationSequenceId, invoice.Supplier) + Global.Global_ImageSpecification);
}
}
-2
View File
@@ -74,8 +74,6 @@ namespace Swarmops.Pages.Security
string requestHost = Request.Url.Host;
// If this is the Dev Sandbox, autologin
if ((requestHost == "sandbox.swarmops.com" || requestHost == "dev.swarmops.com") &&
+16
View File
@@ -216,6 +216,13 @@
<Compile Include="Pages\v5\Comms\SendMassMessage.aspx.designer.cs">
<DependentUpon>SendMassMessage.aspx</DependentUpon>
</Compile>
<Compile Include="Pages\v5\Financial\Json-ListInvoicesOutbound.aspx.cs">
<DependentUpon>Json-ListInvoicesOutbound.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
</Compile>
<Compile Include="Pages\v5\Financial\Json-ListInvoicesOutbound.aspx.designer.cs">
<DependentUpon>Json-ListInvoicesOutbound.aspx</DependentUpon>
</Compile>
<Compile Include="Pages\v5\Financial\Json-ListInvoicesInbound.aspx.cs">
<DependentUpon>Json-ListInvoicesInbound.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
@@ -223,6 +230,13 @@
<Compile Include="Pages\v5\Financial\Json-ListInvoicesInbound.aspx.designer.cs">
<DependentUpon>Json-ListInvoicesInbound.aspx</DependentUpon>
</Compile>
<Compile Include="Pages\v5\Financial\ListInvoicesOutbound.aspx.cs">
<DependentUpon>ListInvoicesOutbound.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
</Compile>
<Compile Include="Pages\v5\Financial\ListInvoicesOutbound.aspx.designer.cs">
<DependentUpon>ListInvoicesOutbound.aspx</DependentUpon>
</Compile>
<Compile Include="Pages\v5\Financial\ListInvoicesInbound.aspx.cs">
<DependentUpon>ListInvoicesInbound.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
@@ -1096,7 +1110,9 @@
<Content Include="Pages\v5\Admin\OrgStaffing.aspx" />
<Content Include="Pages\v5\Admin\SystemSettings.aspx" />
<Content Include="Pages\v5\Comms\SendMassMessage.aspx" />
<Content Include="Pages\v5\Financial\Json-ListInvoicesOutbound.aspx" />
<Content Include="Pages\v5\Financial\Json-ListInvoicesInbound.aspx" />
<Content Include="Pages\v5\Financial\ListInvoicesOutbound.aspx" />
<Content Include="Pages\v5\Financial\ListInvoicesInbound.aspx" />
<Content Include="Pages\v5\Financial\AttestCosts.aspx" />
<Content Include="Pages\v5\Financial\BitcoinHotwalletNotEnabled.aspx" />