From 64ab8792b79d18979c75012945feca7528c596cd Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sat, 16 Jan 2016 23:27:51 +0100 Subject: [PATCH] Checkpoint commit --- Logic/Financial/BitcoinUtility.cs | 138 ++++++++++++++++++++++--- Logic/Logic.csproj | 1 + Logic/Support/BlockchainTransaction.cs | 68 ++++++++++++ 3 files changed, 195 insertions(+), 12 deletions(-) create mode 100644 Logic/Support/BlockchainTransaction.cs diff --git a/Logic/Financial/BitcoinUtility.cs b/Logic/Financial/BitcoinUtility.cs index 84878e6c..724f789c 100644 --- a/Logic/Financial/BitcoinUtility.cs +++ b/Logic/Financial/BitcoinUtility.cs @@ -340,20 +340,24 @@ namespace Swarmops.Logic.Financial return; // not a bitcoin address but something else } + Organization organization = parent.Organization; + bool organizationLedgerUsesBitcoin = organization.Currency.IsBitcoin; + JObject addressData = JObject.Parse( new WebClient().DownloadString("https://blockchain.info/address/" + address + "?format=json&api_key=" + SystemSettings.BlockchainSwarmopsApiKey)); int transactionCount = (int) (addressData["n_tx"]); - foreach (var tx in addressData["txs"]) + foreach (JObject txJson in addressData["txs"]) { - string blockchainTransactionId = (string) tx["hash"]; FinancialTransaction ourTx = null; + Dictionary satoshisLookup = new Dictionary(); // map from ledgercents to satoshis + BlockchainTransaction blockchainTx = BlockchainTransaction.FromBlockchainInfoJson (txJson); try { - ourTx = FinancialTransaction.FromBlockchainHash (parent.Organization, blockchainTransactionId); + ourTx = FinancialTransaction.FromBlockchainHash (parent.Organization, blockchainTx.TransactionHash); // If the transaction was fetched fine, we have already seen this transaction, but need to re-check it } @@ -361,21 +365,131 @@ namespace Swarmops.Logic.Financial { // We didn't have this transaction, so we need to create it - System.DateTime transactionDateTimeUtc = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc); - transactionDateTimeUtc = transactionDateTimeUtc.AddSeconds (Int64.Parse ((string) tx["time"])); - - ourTx = FinancialTransaction.Create (parent.Organization, transactionDateTimeUtc, "Blockchain tx"); + ourTx = FinancialTransaction.Create (parent.Organization, blockchainTx.TransactionDateTimeUtc, "Blockchain tx"); // Did we lose or gain money? // Find all in- and outpoints, determine which are ours (hot and cold wallet) and which aren't } + + Dictionary transactionReconstructedRows = new Dictionary(); + + // Note the non-blockchain rows in this tx, keep them for reconstruction + + foreach (FinancialTransactionRow row in ourTx.Rows) + { + if (!addressAccountLookup.ContainsValue (row.FinancialAccountId)) // not optimal but n is small + { + // This is not a bitcoin address account, so note it for reconstruction + transactionReconstructedRows[row.FinancialAccountId] += row.AmountCents; + } + else + { + // this is a known blockchain row, note its ledgered value in satoshis + if (!organizationLedgerUsesBitcoin) + { + Money nativeMoney = row.NativeAmountCents; + if (nativeMoney.Currency.IsBitcoin) // it damn well should be, but just checking + { + satoshisLookup[row.AmountCents] = row.NativeAmountCents.Cents; + } + } + } + } + + // Reconstruct the blockchain rows: input, output, fees, in that order + + // -- inputs + + foreach (BlockchainTransactionRow inputRow in blockchainTx.Inputs) + { + if (addressAccountLookup.ContainsKey (inputRow.Address)) + { + // this input is ours + + if (organizationLedgerUsesBitcoin) + { + transactionReconstructedRows[addressAccountLookup[inputRow.Address]] += + -inputRow.ValueSatoshis; // note the negation! + } + else + { + Int64 ledgerCents = + new Money (inputRow.ValueSatoshis, Currency.Bitcoin, ourTx.DateTime).ToCurrency ( + organization.Currency).Cents; + transactionReconstructedRows[addressAccountLookup[inputRow.Address]] += + -ledgerCents; // note the negation! + satoshisLookup[ledgerCents] = inputRow.ValueSatoshis; + } + } + } + + // -- outputs + + foreach (BlockchainTransactionRow outputRow in blockchainTx.Outputs) + { + if (addressAccountLookup.ContainsKey(outputRow.Address)) + { + // this input is ours + + if (organizationLedgerUsesBitcoin) + { + transactionReconstructedRows[addressAccountLookup[outputRow.Address]] += + outputRow.ValueSatoshis; + } + else + { + Int64 ledgerCents = + new Money(outputRow.ValueSatoshis, Currency.Bitcoin, ourTx.DateTime).ToCurrency( + organization.Currency).Cents; + transactionReconstructedRows[addressAccountLookup[outputRow.Address]] += + ledgerCents; + satoshisLookup[ledgerCents] = outputRow.ValueSatoshis; + } + } + } + + // -- fees + + if (addressAccountLookup.ContainsKey (blockchainTx.Inputs[0].Address)) + { + // if the first input is ours, we're paying the fee (is there any case where this is not true?) + + if (organizationLedgerUsesBitcoin) + { + transactionReconstructedRows[organization.FinancialAccounts.CostsBitcoinFees.Identity] = + blockchainTx.FeeSatoshis; + } + else + { + Int64 feeLedgerCents = + new Money (blockchainTx.FeeSatoshis, Currency.Bitcoin, + blockchainTx.TransactionDateTimeUtc).ToCurrency (organization.Currency).Cents; + transactionReconstructedRows[organization.FinancialAccounts.CostsBitcoinFees.Identity] = + feeLedgerCents; + } + } + + + // Rewrite the transaction (called always, but the function won't do anything if everything matches) + + ourTx.RecalculateTransaction (transactionReconstructedRows, /* loggingPerson*/ null); + + if (!organizationLedgerUsesBitcoin) + { + // Add native cents, if any + + foreach (FinancialTransactionRow row in ourTx.Rows) + { + if (addressAccountLookup.ContainsValue (row.Identity)) // "ContainsValue" is bad, but n is low + { + // Do we have a foreign amount for this row already? + + + } + } + } } - // TODO: Note the non-blockchain rows in this tx, keep them - // TODO: Reconstruct the blockchain rows - // TODO: Rewrite the tx - - // TODO: CONTINUE HERE } diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index 1f4095fc..0d323882 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -196,6 +196,7 @@ + diff --git a/Logic/Support/BlockchainTransaction.cs b/Logic/Support/BlockchainTransaction.cs new file mode 100644 index 00000000..e2dd6072 --- /dev/null +++ b/Logic/Support/BlockchainTransaction.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json.Linq; + +namespace Swarmops.Logic.Support +{ + public class BlockchainTransaction + { + static public BlockchainTransaction FromBlockchainInfoJson (JObject json) + { + BlockchainTransaction thisTx = new BlockchainTransaction(); + + System.DateTime transactionDateTimeUtc = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); + thisTx.TransactionDateTimeUtc = transactionDateTimeUtc.AddSeconds(Int64.Parse((string)json["time"])); + thisTx.TransactionHash = (string) json["hash"]; + + Int64 satoshiCount = 0; + thisTx.Inputs = new List(); + foreach (JObject inputRow in json["inputs"]) + { + BlockchainTransactionRow newRow = + BlockchainTransactionRow.FromBlockchainInfoJson ((JObject) inputRow["prev_out"]); + thisTx.Inputs.Add (newRow); + satoshiCount += newRow.ValueSatoshis; + } + foreach (JObject outputRow in json["out"]) + { + BlockchainTransactionRow newRow = + BlockchainTransactionRow.FromBlockchainInfoJson(outputRow); + thisTx.Outputs.Add(newRow); + satoshiCount -= newRow.ValueSatoshis; + } + + thisTx.FeeSatoshis = satoshiCount; // inputs minus outputs equals fee + + return thisTx; + } + + public DateTime TransactionDateTimeUtc { get; private set; } + public Int64 FeeSatoshis { get; private set; } + public string TransactionHash { get; private set; } + + public List Inputs { get; private set; } + public List Outputs { get; private set; } + } + + public class BlockchainTransactionRow + { + static public BlockchainTransactionRow FromBlockchainInfoJson (JObject json) + { + BlockchainTransactionRow newRow = new BlockchainTransactionRow(); + newRow.Address = (string) json["addr"]; + newRow.ValueSatoshis = Int64.Parse ((string) json["value"]); + newRow.Index = Int32.Parse ((string) json["n"]); + newRow.Spent = Boolean.Parse ((string) json["spent"]); + + return newRow; + } + + public string Address { get; private set; } + public bool Spent { get; private set; } + public Int64 ValueSatoshis { get; private set; } + public int Index { get; private set; } + } +}