From 3414d0901a171da632254cfbb911f11f829b9c67 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Tue, 6 Oct 2015 19:20:49 +0200 Subject: [PATCH] Db0026; work on hotwallet --- Database/Configuration.cs | 2 +- Database/Schemata/revisions.txt | 5 +- Database/Schemata/upgrade-0026.sql | 157 ++++++++++++++++++ Logic/Financial/BitcoinUtility.cs | 25 ++- Logic/Financial/FinancialAccounts.cs | 4 - ...ansmission.NotificationPayload.Designer.cs | 18 ++ ...ions.Transmission.NotificationPayload.resx | 6 + .../App_GlobalResources/Pages.Financial.resx | 8 + Site5/Pages/v5/Financial/Donate.aspx.cs | 11 +- 9 files changed, 224 insertions(+), 12 deletions(-) create mode 100644 Database/Schemata/upgrade-0026.sql diff --git a/Database/Configuration.cs b/Database/Configuration.cs index 764481c6..0c0e553b 100644 --- a/Database/Configuration.cs +++ b/Database/Configuration.cs @@ -14,7 +14,7 @@ namespace Swarmops.Database /// public static int DbVersionExpected { - get { return 25; } + get { return 26; } } diff --git a/Database/Schemata/revisions.txt b/Database/Schemata/revisions.txt index a3d8e18a..d8325ea3 100644 --- a/Database/Schemata/revisions.txt +++ b/Database/Schemata/revisions.txt @@ -20,4 +20,7 @@ 0020 - Added concurrency check to TerminatePositionAssignment [RF] 0021 - Added PositionCustomTitles and its stored procedures [RF] 0022 - Added VolunteerPositions and its stored procedures [RF] -0023 - Added procs to change Facebook, Twitter, GPlus IDs [RF] \ No newline at end of file +0023 - Added procs to change Facebook, Twitter, GPlus IDs [RF] +0024 - Maint on CreatePayrollItem [RF] +0025 - Bugfix for 0024 [RF] +0026 - Added first tables, procs for hot bitcoin wallets [RF] diff --git a/Database/Schemata/upgrade-0026.sql b/Database/Schemata/upgrade-0026.sql new file mode 100644 index 00000000..192bf74b --- /dev/null +++ b/Database/Schemata/upgrade-0026.sql @@ -0,0 +1,157 @@ +CREATE TABLE `HotBitcoinAddresses` ( + `HotBitcoinAddressId` INT NOT NULL AUTO_INCREMENT, + `OrganizationId` INT NOT NULL, + `DerivationPath` VARCHAR(256) NOT NULL, + `AddressString` VARCHAR(48) NOT NULL, + `BalanceSatoshis` BIGINT NOT NULL, + `ThroughputSatoshis` BIGINT NOT NULL, + PRIMARY KEY (`HotBitcoinAddressId`), + INDEX `Ix_Organization` (`OrganizationId` ASC), + INDEX `Ix_Balance` (`BalanceSatoshis` ASC), + INDEX `Ix_Used` (`ThroughputSatoshis` ASC)) + + +# + + +CREATE TABLE `HotBitcoinAddressUnspents` ( + `HotBitcoinAddressUnspentId` INT NOT NULL AUTO_INCREMENT, + `AmountSatoshis` BIGINT NOT NULL, + `TransactionHash` VARCHAR(128) NOT NULL, + `TransactionOutputIndex` INT NOT NULL, + `ConfirmationCount` INT NOT NULL, + PRIMARY KEY (`HotBitcoinAddressUnspentId`), + INDEX `Ix_Confirmations` (`ConfirmationCount` ASC), + INDEX `Ix_Amount` (`AmountSatoshis` ASC), + INDEX `Ix_TxHash` (`TransactionHash` ASC)) + + +# + + +DROP procedure IF EXISTS `CreateHotBitcoinAddress` + + +# + + +CREATE PROCEDURE `CreateHotBitcoinAddress` ( + IN organizationId INT, + IN derivationPath VARCHAR(256), + IN addressString VARCHAR(48) +) +BEGIN + INSERT INTO HotBitcoinAddresses + (OrganizationId,DerivationPath,AddressString,BalanceSatoshis,ThroughputSatoshis) + VALUES + (organizationId,derivationPath,addressString,0,0); + + SELECT LAST_INSERT_ID() AS Identity; + +END + + +# + + +DROP procedure IF EXISTS `SetHotBitcoinAddressBalance` + + +# + + +CREATE PROCEDURE `SetHotBitcoinAddressBalance` ( + IN hotBitcoinAddressId INT, + IN balanceSatoshis BIGINT +) +BEGIN + UPDATE HotBitcoinAddresses + SET HotBitcoinAddresses.BalanceSatoshis=balanceSatoshis + WHERE HotBitcoinAddresses.HotBitcoinAddressId=hotBitcoinAddressId; + + SELECT ROW_COUNT() AS RowsUpdated; + +# + + +DROP procedure IF EXISTS `SetHotBitcoinAddressThroughput` + + +# + + +CREATE PROCEDURE `SetHotBitcoinAddressThroughput` ( + IN hotBitcoinAddressId INT, + IN throughputSatoshis BIGINT +) +BEGIN + UPDATE HotBitcoinAddresses + SET HotBitcoinAddresses.ThroughputSatoshis=throughputSatoshis + WHERE HotBitcoinAddresses.HotBitcoinAddressId=hotBitcoinAddressId; + + SELECT ROW_COUNT() AS RowsUpdated; + +# + + +DROP procedure IF EXISTS `CreateHotBitcoinAddressUnspent` + + +# + + +CREATE PROCEDURE `CreateHotBitcoinAddressUnspent` ( + IN amountSatoshis BIGINT, + IN transactionHash VARCHAR(128), + IN transactionOutputIndex INT, + IN confirmationCount INT +) +BEGIN + INSERT INTO HotBitcoinAddressUnspent + (AmountSatoshis,TransactionHash,TransactionOutputIndex,ConfirmationCount) + VALUES + (amountSatoshis,transactionHash,transactionOutputIndex,confirmationCount); + + SELECT LAST_INSERT_ID() AS Identity; + +END + + +# + + +DROP procedure IF EXISTS `SetHotBitcoinAddressUnspentConfirmations` + + +# + + +CREATE PROCEDURE `SetHotBitcoinAddressUnspentConfirmations` ( + IN hotBitcoinAddressUnspentId INT, + IN confirmationCount INT +) +BEGIN + UPDATE HotBitcoinAddressUnspents + SET HotBitcoinAddressUnspents.ConfirmationCount=confirmationCount + WHERE HotBitcoinAddressUnspents.HotBitcoinAddressUnspentId=hotBitcoinAddressUnspentId; + + SELECT ROW_COUNT() AS RowsUpdated; + +# + + +DROP procedure IF EXISTS `DeleteHotBitcoinAddressUnspent` + + +# + + +CREATE PROCEDURE `DeleteHotBitcoinAddressUnspent` ( + IN hotBitcoinAddressUnspentId INT +) +BEGIN + DELETE FROM HotBitcoinAddressUnspents + WHERE HotBitcoinAddressUnspents.HotBitcoinAddressUnspentId=hotBitcoinAddressUnspentId; + + SELECT ROW_COUNT() AS RowsDeleted; + diff --git a/Logic/Financial/BitcoinUtility.cs b/Logic/Financial/BitcoinUtility.cs index fac7ddea..e605a11b 100644 --- a/Logic/Financial/BitcoinUtility.cs +++ b/Logic/Financial/BitcoinUtility.cs @@ -1,12 +1,15 @@ using System; using System.Collections.Generic; +using System.Collections.Specialized; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; +using System.Web; using NBitcoin; +using NBitcoin.BouncyCastle.Asn1.Ocsp; using Newtonsoft.Json.Linq; namespace Swarmops.Logic.Financial @@ -34,11 +37,31 @@ namespace Swarmops.Logic.Financial TransactionOutputIndex = (UInt32) unspentJson["tx_output_n"] }; - coinList.Add (txUnspent); + coinList.Add (txUnspent); // invokes implicit conversion to NBitcoin.Coin } return coinList.ToArray(); } + + static public void BroadcastTransaction (Transaction transaction) + { + using (WebClient client = new WebClient()) + { + byte[] response = client.UploadValues("https://blockchain.info/pushtx?cors=true", new NameValueCollection() + { + { "tx", transaction.ToHex() } + }); + + // TODO: Exception handling + } + } + + public const int BitcoinWalletIndex = 1; + public const int BitcoinDonationsIndex = 2; + public const int BitcoinLoansIndex = 3; + public const int BitcoinAccountsReceivableIndex = 4; + + public const string BitcoinTestAddress = "1JMpU3D6c5sruunMwzkt6p6PQzLcUYcL26"; } internal class BitcoinUnspentTransactionOutput diff --git a/Logic/Financial/FinancialAccounts.cs b/Logic/Financial/FinancialAccounts.cs index 629a4b9f..8a5b1c28 100644 --- a/Logic/Financial/FinancialAccounts.cs +++ b/Logic/Financial/FinancialAccounts.cs @@ -249,9 +249,5 @@ namespace Swarmops.Logic.Financial } } - public const int BitcoinWalletIndex = 1; - public const int BitcoinDonationsIndex = 2; - - public const string BitcoinTestAddress = "1JMpU3D6c5sruunMwzkt6p6PQzLcUYcL26"; } } \ No newline at end of file diff --git a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs index 12d574bf..868800ac 100644 --- a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs +++ b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.Designer.cs @@ -60,6 +60,24 @@ namespace Swarmops.Logic.Resources { } } + /// + /// Looks up a localized string similar to [BitcoinAmount] millicoin was just transferred to your address by [OrganizationName].. + /// + internal static string Bitcoin_PaidOut_Body { + get { + return ResourceManager.GetString("Bitcoin_PaidOut_Body", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to [[OrganizationName]] Funds paid using bitcoin: [CurrencyCode] [BudgetAmountFloat]. + /// + internal static string Bitcoin_PaidOut_Subject { + get { + return ResourceManager.GetString("Bitcoin_PaidOut_Subject", resourceCulture); + } + } + /// /// Looks up a localized string similar to You have changed your bitcoin payout address from [BitcoinOldAddress] to [BitcoinAddress]. As a security precaution, all payouts are quarantied for the next 48 hours. /// diff --git a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx index 5151c90c..0914fe52 100644 --- a/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx +++ b/Logic/Resources/Logic.Communications.Transmission.NotificationPayload.resx @@ -149,6 +149,12 @@ If you did not set this payout address (it may be worth double checking the addr [[OrganizationName]] Bitcoin payout address set + + [BitcoinAmount] millicoin was just transferred to your address by [OrganizationName]. + + + [[OrganizationName]] Funds paid using bitcoin: [CurrencyCode] [BudgetAmountFloat] + Your requested cash advance with [OrganizationName] for [CurrencyCode] [BudgetAmountFloat], concerning [RequestPurpose], has been attested by the budget owner. It will now proceed to payout to you. diff --git a/Site5/App_GlobalResources/Pages.Financial.resx b/Site5/App_GlobalResources/Pages.Financial.resx index 23e0747e..43f9feb5 100644 --- a/Site5/App_GlobalResources/Pages.Financial.resx +++ b/Site5/App_GlobalResources/Pages.Financial.resx @@ -335,6 +335,14 @@ Donate + + Donation to {0} + The {0} is the organization name + + + Loan to {0} + The {0} is the organization name + Expense claim amount (in {0}) {0} is the currency. diff --git a/Site5/Pages/v5/Financial/Donate.aspx.cs b/Site5/Pages/v5/Financial/Donate.aspx.cs index 5a54a8ce..688227dc 100644 --- a/Site5/Pages/v5/Financial/Donate.aspx.cs +++ b/Site5/Pages/v5/Financial/Donate.aspx.cs @@ -22,7 +22,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial string bitcoinAddress = FinancialAccounts.BitcoinHotPublicRoot .Derive ((uint) this.CurrentOrganization.Identity) - .Derive (FinancialAccounts.BitcoinDonationsIndex) + .Derive (BitcoinUtility.BitcoinDonationsIndex) .Derive ((uint) this.CurrentUser.Identity) .PubKey.GetAddress (Network.Main) .ToString(); @@ -31,7 +31,8 @@ namespace Swarmops.Frontend.Pages.v5.Financial /* BitcoinSecret secretKey = FinancialAccounts.BitcoinHotPrivateRoot.Derive ((uint) this.CurrentOrganization.Identity) - .Derive (FinancialAccounts.BitcoinDonationsIndex) + .Derive (BitcoinUtility.BitcoinDonationsIndex) + .Derive ((uint) this.CurrentUser.Identity) .PrivateKey .GetBitcoinSecret (Network.Main); @@ -40,7 +41,7 @@ namespace Swarmops.Frontend.Pages.v5.Financial TransactionBuilder txBuild = new TransactionBuilder(); Transaction tx = txBuild.AddCoins (spendableCoin) .AddKeys (secretKey) - .Send (new BitcoinAddress (FinancialAccounts.BitcoinTestAddress), "0.001") + .Send (new BitcoinAddress (BitcoinUtility.BitcoinTestAddress), "0.001") .SendFees ("0.0001") .SetChange (secretKey.GetAddress()) .BuildTransaction (true); @@ -51,13 +52,13 @@ namespace Swarmops.Frontend.Pages.v5.Financial throw new InvalidOperationException("Tx is not properly signed"); } - string txHex = tx.ToHex();*/ + BitcoinUtility.BroadcastTransaction (tx);*/ this.BoxTitle.Text = Resources.Pages.Financial.Donate_PageTitle; this.LabelExplainBitcoinDonation.Text = String.Format(Resources.Pages.Financial.Donate_Explain, CurrentOrganization.Name, bitcoinAddress); this.ImageBitcoinQr.ImageUrl = - "https://chart.googleapis.com/chart?cht=qr&chs=400x400&chl=bitcoin:" + HttpUtility.UrlEncode(bitcoinAddress + "?label=" + CurrentOrganization.Name.Replace ("&","et").Replace ("=", string.Empty)); // URI scheme doesn't like &, = + "https://chart.googleapis.com/chart?cht=qr&chs=400x400&chl=bitcoin:" + HttpUtility.UrlEncode(bitcoinAddress + "?label=" + Uri.EscapeDataString(String.Format(Resources.Pages.Financial.Donate_TxLabel, CurrentOrganization.Name))); // URI scheme doesn't like &, = } } } \ No newline at end of file