Enabled Donations; Work on bitcoin payouts

This commit is contained in:
Rick Falkvinge
2015-10-08 13:56:30 +02:00
parent 9cc1008f4e
commit 8c61a8d2ff
14 changed files with 188 additions and 89 deletions
+1 -1
View File
@@ -170,7 +170,7 @@ namespace Swarmops.Backend
// Check for existence of bitcoin hotwallet root
FinancialAccounts.VerifyBitcoinHotWallet();
BitcoinUtility.VerifyBitcoinHotWallet();
// Begin maintenance loop
+34
View File
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
@@ -12,11 +13,42 @@ using NBitcoin;
using NBitcoin.BouncyCastle.Asn1.Ocsp;
using Newtonsoft.Json.Linq;
using Swarmops.Database;
using Swarmops.Logic.Support;
namespace Swarmops.Logic.Financial
{
public class BitcoinUtility
{
public static void VerifyBitcoinHotWallet()
{
// Make sure there's always a private hotwallet root, regardless of whether it's used or not
if (!File.Exists(SystemSettings.EtcFolder + Path.DirectorySeparatorChar + "hotwallet"))
{
ExtKey privateRoot = new ExtKey();
File.WriteAllText(SystemSettings.EtcFolder + Path.DirectorySeparatorChar + "hotwallet", privateRoot.GetWif(Network.Main).ToWif(), Encoding.ASCII);
File.WriteAllText(SystemSettings.EtcFolder + Path.DirectorySeparatorChar + "hotwallet-created-" + DateTime.UtcNow.ToString("yyyy-MM-dd--HH-mm-ss--fff.backup"), privateRoot.GetWif(Network.Main).ToWif(), Encoding.ASCII); // an extra backup
Persistence.Key["BitcoinHotPublicRoot"] = privateRoot.Neuter().GetWif(Network.Main).ToWif();
}
}
public static ExtKey BitcoinHotPrivateRoot
{
get
{
return
ExtKey.Parse(File.ReadAllText(SystemSettings.EtcFolder + Path.DirectorySeparatorChar + "hotwallet")); // will throw if hotwallet file does not exist - intentional
}
}
public static ExtPubKey BitcoinHotPublicRoot
{
get
{
return ExtPubKey.Parse((string)Persistence.Key["BitcoinHotPublicRoot"]);
}
}
static public Coin[] GetSpendableCoin (BitcoinSecret secretKey) // we're using BitcoinSecret as arg just to reinforce caller must have privkey to spend funds
{
// This function queries the Blockchain API for the unspent coin.
@@ -115,6 +147,8 @@ namespace Swarmops.Logic.Financial
public const int BitcoinAccountsReceivableIndex = 4;
public const string BitcoinTestAddress = "1JMpU3D6c5sruunMwzkt6p6PQzLcUYcL26";
public const long FeeSatoshisPerThousandBytes = 10000;
}
internal class BitcoinUnspentTransactionOutput
-30
View File
@@ -217,37 +217,7 @@ namespace Swarmops.Logic.Financial
}
public static void VerifyBitcoinHotWallet()
{
// Make sure there's always a private hotwallet root, regardless of whether it's used or not
if (!File.Exists (SystemSettings.EtcFolder + Path.DirectorySeparatorChar + "hotwallet"))
{
ExtKey privateRoot = new ExtKey();
File.WriteAllText(SystemSettings.EtcFolder + Path.DirectorySeparatorChar + "hotwallet", privateRoot.GetWif(Network.Main).ToWif(), Encoding.ASCII);
File.WriteAllText(SystemSettings.EtcFolder + Path.DirectorySeparatorChar + "hotwallet-created-" + DateTime.UtcNow.ToString("yyyy-MM-dd--HH-mm-ss--fff.backup"), privateRoot.GetWif(Network.Main).ToWif(), Encoding.ASCII); // an extra backup
Persistence.Key["BitcoinHotPublicRoot"] = privateRoot.Neuter().GetWif(Network.Main).ToWif();
}
}
public static ExtKey BitcoinHotPrivateRoot
{
get
{
VerifyBitcoinHotWallet();
return
ExtKey.Parse (File.ReadAllText (SystemSettings.EtcFolder + Path.DirectorySeparatorChar + "hotwallet"));
}
}
public static ExtPubKey BitcoinHotPublicRoot
{
get
{
return ExtPubKey.Parse ((string) Persistence.Key["BitcoinHotPublicRoot"]);
}
}
}
}
+33 -1
View File
@@ -51,7 +51,7 @@ namespace Swarmops.Logic.Financial
public static HotBitcoinAddress Create (Organization organization, params int[] derivationPath)
{
ExtPubKey extPubKey = FinancialAccounts.BitcoinHotPublicRoot;
ExtPubKey extPubKey = BitcoinUtility.BitcoinHotPublicRoot;
extPubKey = extPubKey.Derive ((uint) organization.Identity);
string derivationPathString = string.Empty;
@@ -76,9 +76,41 @@ namespace Swarmops.Logic.Financial
return FromBasic(SwarmDb.GetDatabaseForReading().GetHotBitcoinAddress(bitcoinAddress));
}
public BitcoinSecret PrivateKey
{
get
{
// This will throw if the private root key is not available to the running code, and that's as designed
ExtKey secretExtKey = BitcoinUtility.BitcoinHotPrivateRoot.Derive ((uint) base.OrganizationId);
foreach (string derivationString in DerivationPath.Split (' '))
{
secretExtKey = secretExtKey.Derive (UInt32.Parse (derivationString));
}
return secretExtKey.PrivateKey.GetBitcoinSecret (Network.Main);
}
}
public Organization Organization
{
get { return Organization.FromIdentity (base.OrganizationId); }
}
public HotBitcoinAddressUnspents Unspents
{
get { return HotBitcoinAddressUnspents.ForAddress (this); }
}
public Int64 UnspentSatoshis
{
get
{
HotBitcoinAddressUnspents unspents = Unspents;
return unspents.Sum (unspent => unspent.AmountSatoshis);
}
}
}
}
+2
View File
@@ -117,6 +117,8 @@ namespace Swarmops.Logic.Financial
public Salaries DependentSalariesNet;
public Salaries DependentSalariesTax;
public Person RecipientPerson { get; set; }
public decimal Amount
{
set
+3
View File
@@ -102,6 +102,7 @@ namespace Swarmops.Logic.Financial
claim.Claimer.BankClearing + " / " + claim.Claimer.BankAccount, string.Empty, 0,
DateTime.MinValue, false, DateTime.Now, 0);
Payout payout = Payout.FromBasic (basicPayout);
payout.RecipientPerson = claim.Claimer;
payout.DependentExpenseClaims.Add (claim);
@@ -206,6 +207,7 @@ namespace Swarmops.Logic.Financial
advance.Person.BankClearing + " / " + advance.Person.BankAccount, string.Empty, 0,
DateTime.MinValue, false, DateTime.Now, 0);
Payout payout = Payout.FromBasic (basicPayout);
payout.RecipientPerson = advance.Person;
payout.DependentCashAdvancesPayout.Add (advance);
@@ -297,6 +299,7 @@ namespace Swarmops.Logic.Financial
salary.PayoutDate.ToString (CultureInfo.InvariantCulture),
salary.NetSalaryCents, salary.PayoutDate, false, DateTime.Now, 0);
Payout payout = Payout.FromBasic (basicPayout);
payout.RecipientPerson = employee;
payout.DependentSalariesNet.Add (salary);
+3
View File
@@ -414,4 +414,7 @@
<data name="Menu5_People_MyLocalOrganization" xml:space="preserve">
<value>My Local Organization</value>
</data>
<data name="Menu5_Financial_DonateFunds" xml:space="preserve">
<value>Donate Funds</value>
</data>
</root>
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

+28 -12
View File
@@ -154,21 +154,37 @@
<Type>Link</Type>
<Children />
</MainMenuItem>
<MainMenuItem>
<ResourceKey>Financial_RequestCashAdvance</ResourceKey>
<UserLevel>1</UserLevel>
<Permissions />
<ImageUrl>iconshock-walletmoney</ImageUrl>
<NavigateUrl>/Pages/v5/Financial/RequestCashAdvance.aspx</NavigateUrl>
<Type>Link</Type>
<Children />
<ResourceKey>Financial_RequestCashAdvance</ResourceKey>
<UserLevel>1</UserLevel>
<Permissions />
<ImageUrl>iconshock-walletmoney</ImageUrl>
<NavigateUrl>/Pages/v5/Financial/RequestCashAdvance.aspx</NavigateUrl>
<Type>Link</Type>
<Children />
</MainMenuItem>
<MainMenuItem>
<UserLevel>2</UserLevel>
<Type>Separator</Type>
<Children />
<UserLevel>1</UserLevel>
<Type>Separator</Type>
<Children />
</MainMenuItem>
<MainMenuItem>
<ResourceKey>Financial_DonateFunds</ResourceKey>
<UserLevel>1</UserLevel>
<Permissions />
<ImageUrl>iconshock-gift</ImageUrl>
<NavigateUrl>/Pages/v5/Financial/Donate.aspx</NavigateUrl>
<Type>Link</Type>
<Children />
</MainMenuItem>
<MainMenuItem>
<UserLevel>2</UserLevel>
<Type>Separator</Type>
<Children />
</MainMenuItem>
<MainMenuItem>
+19 -7
View File
@@ -6,8 +6,10 @@
// Initialize socket to wait for transaction
initializeSocket();
if (enable) {
initializeSocket();
}
// TODO: Also poll regularly in case socket fails
});
@@ -40,7 +42,7 @@
// transaction received at address
// call page to check if funds received server side
alertify.log("Tx received on socket, checking...");
alertify.log("Detected transaction, verifying...");
checkTransactionReceived(message.x.hash);
}
};
@@ -70,15 +72,25 @@
var guid = '<asp:Literal runat="server" ID="LiteralGuid" />';
var socket = null;
var completed = false;
var enable = <asp:Literal ID="LiteralEnable" runat="server">true</asp:Literal>;
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderMain" Runat="Server">
<h2><asp:Label runat="server" ID="BoxTitle" /></h2>
<p id="paraIntro"><asp:Label runat="server" ID="LabelExplainBitcoinDonation" /></p>
<p style="font-size:200%" id="paraStatus"><asp:Label runat="server" ID="LabelStatus" /></p>
<div align="center" id="divQr"><asp:Image ID="ImageBitcoinQr" runat="server"/></div>
<asp:Panel runat="server" ID="PanelEnabled">
<h2><asp:Label runat="server" ID="BoxTitle" /></h2>
<p id="paraIntro"><asp:Label runat="server" ID="LabelExplainBitcoinDonation" /></p>
<p style="font-size:200%" id="paraStatus"><asp:Label runat="server" ID="LabelStatus" /></p>
<div align="center" id="divQr"><asp:Image ID="ImageBitcoinQr" runat="server"/></div>
</asp:Panel>
<asp:Panel runat="server" ID="PanelDisabled" Visible="false">
<h2>Bitcoin hotwallet is not enabled</h2>
<img alt="Fail symbol" src="/Images/Icons/iconshock-cross-96px.png" align="left" /><br/>
<p>This organization has not enabled its bitcoin hotwallet in Swarmops. Therefore, direct donations are not available.</p>
<p>Contact the administrator for <%=this.CurrentOrganization.Name %> and ask them to enable the bitcoin hotwallet.</p>
<div style="clear:both"></div>
</asp:Panel>
</asp:Content>
+25 -10
View File
@@ -12,6 +12,8 @@ using NBitcoin;
using Swarmops.Logic.Cache;
using Swarmops.Logic.Financial;
using Swarmops.Logic.Security;
using Swarmops.Logic.Structure;
using Satoshis = NBitcoin.Money; // Sets it apart from Swarmops' Money class
namespace Swarmops.Frontend.Pages.v5.Financial
{
@@ -21,13 +23,32 @@ namespace Swarmops.Frontend.Pages.v5.Financial
{
this.PageAccessRequired = new Access (this.CurrentOrganization, AccessAspect.Participant);
// TODO: This page needs a check on whether currentorganization.financialaccounts.assetbitcoinhot is null
/* TEMP TEMP TEMP - REMOVE THIS CODE */
/*
Organization fwn = Organization.FromIdentity (2);
Salaries salaries = Salaries.ForOrganization (fwn);
foreach (Salary salary in salaries)
{
if (salary.PayrollItem.Person.BitcoinPayoutAddress.Length > 0 && salary.Attested == false)
{
salary.Attest (salary.PayrollItem.Person); // null for system apparently isn't allowed here
}
}*/
// TODO: This page needs a check on whether currentorganization.financialaccounts.assetbitcoinhot is null
this.PageTitle = Resources.Pages.Financial.Donate_PageTitle;
this.InfoBoxLiteral = Resources.Pages.Financial.Donate_Info;
this.LabelStatus.Text = Resources.Pages.Financial.Donate_StatusInitial;
if (this.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot == null)
{
this.PanelDisabled.Visible = true;
this.PanelEnabled.Visible = false;
this.LiteralEnable.Text = @"false";
}
HotBitcoinAddress address = HotBitcoinAddress.Create (this.CurrentOrganization,
BitcoinUtility.BitcoinDonationsIndex, this.CurrentUser.Identity);
@@ -38,20 +59,14 @@ namespace Swarmops.Frontend.Pages.v5.Financial
// TEST TEST TEST
/*
BitcoinSecret secretKey =
FinancialAccounts.BitcoinHotPrivateRoot.Derive ((uint) this.CurrentOrganization.Identity)
.Derive (BitcoinUtility.BitcoinDonationsIndex)
.Derive ((uint) this.CurrentUser.Identity)
.PrivateKey
.GetBitcoinSecret (Network.Main);
BitcoinSecret secretKey = address.PrivateKey;
Coin[] spendableCoin = BitcoinUtility.GetSpendableCoin (secretKey);
TransactionBuilder txBuild = new TransactionBuilder();
Transaction tx = txBuild.AddCoins (spendableCoin)
.AddKeys (secretKey)
.Send (new BitcoinAddress (BitcoinUtility.BitcoinTestAddress), "0.001")
.SendFees ("0.0001")
.Send (new BitcoinAddress (BitcoinUtility.BitcoinTestAddress), new Satoshis (address.UnspentSatoshis - BitcoinUtility.FeeSatoshisPerThousandBytes))
.SendFees (new Satoshis(BitcoinUtility.FeeSatoshisPerThousandBytes))
.SetChange (secretKey.GetAddress())
.BuildTransaction (true);
@@ -39,9 +39,17 @@ namespace Swarmops.Frontend.Pages.Financial
StringBuilder result = new StringBuilder (16384);
DateTime today = DateTime.Today;
bool bitcoinHotWalletActive = (this.CurrentOrganization.FinancialAccounts.AssetsBitcoinHot != null
? true
: false);
foreach (Payout payout in payouts)
{
if (bitcoinHotWalletActive && payout.RecipientPerson != null && payout.RecipientPerson.BitcoinPayoutAddress.Length > 0 && payout.Account.Length < 2)
{
continue;
}
result.Append ("{");
result.AppendFormat (
"\"itemId\":\"{0}\"," +
+32 -28
View File
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
@@ -14,77 +14,81 @@
</system.Web>
-->
<system.web>
<machineKey configSource="machineKey.config" />
<compilation debug="true" batch="false" targetFramework="4.5" />
<customErrors mode="Off" />
<httpRuntime maxRequestLength="40960" executionTimeout="180" targetFramework="4.5" />
<machineKey configSource="machineKey.config"/>
<compilation debug="true" batch="false" targetFramework="4.5">
<assemblies>
<add assembly="System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
<customErrors mode="Off"/>
<httpRuntime maxRequestLength="40960" executionTimeout="180" targetFramework="4.5"/>
<authentication mode="Forms">
<forms loginUrl="~/Security/Login" name=".ActzAuth" path="/" protection="All" timeout="600" defaultUrl="/" cookieless="UseDeviceProfile" />
<forms loginUrl="~/Security/Login" name=".ActzAuth" path="/" protection="All" timeout="600" defaultUrl="/" cookieless="UseDeviceProfile"/>
</authentication>
<authorization>
<deny users="?" />
<deny users="?"/>
</authorization>
<pages controlRenderingCompatibilityVersion="4.0" clientIDMode="AutoID">
<controls>
<add tagPrefix="Swarmops5" tagName="DropDown" src="~/Controls/v5/Base/DropDown.ascx" />
<add tagPrefix="Swarmops5" tagName="ComboPeople" src="~/Controls/v5/Swarm/ComboPeople.ascx" />
<add tagPrefix="Swarmops5" tagName="AjaxTextBox" src="~/Controls/v5/Base/AjaxTextBox.ascx" />
<add tagPrefix="Swarmops5" tagName="AjaxComboPeople" src="~/Controls/v5/Swarm/ComboPeople.ascx" />
<add tagPrefix="Swarmops5" tagName="ModalDialog" src="~/Controls/v5/Base/ModalDialog.ascx" />
<add tagPrefix="Swarmops5" tagName="ExternalScripts" src="~/Controls/v5/UI/ExternalScripts.ascx" />
<add tagPrefix="Swarmops5" tagName="IncludedScripts" src="~/Controls/v5/UI/IncludedScripts.ascx" />
<add tagPrefix="Swarmops5" tagName="MainMenu" src="~/Controls/v5/Base/MainMenu.ascx" />
<add tagPrefix="Swarmops5" tagName="DropDown" src="~/Controls/v5/Base/DropDown.ascx"/>
<add tagPrefix="Swarmops5" tagName="ComboPeople" src="~/Controls/v5/Swarm/ComboPeople.ascx"/>
<add tagPrefix="Swarmops5" tagName="AjaxTextBox" src="~/Controls/v5/Base/AjaxTextBox.ascx"/>
<add tagPrefix="Swarmops5" tagName="AjaxComboPeople" src="~/Controls/v5/Swarm/ComboPeople.ascx"/>
<add tagPrefix="Swarmops5" tagName="ModalDialog" src="~/Controls/v5/Base/ModalDialog.ascx"/>
<add tagPrefix="Swarmops5" tagName="ExternalScripts" src="~/Controls/v5/UI/ExternalScripts.ascx"/>
<add tagPrefix="Swarmops5" tagName="IncludedScripts" src="~/Controls/v5/UI/IncludedScripts.ascx"/>
<add tagPrefix="Swarmops5" tagName="MainMenu" src="~/Controls/v5/Base/MainMenu.ascx"/>
</controls>
</pages>
</system.web>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000" />
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
<system.data>
<DbProviderFactories>
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.2.2.0, Culture=neutral" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.2.2.0, Culture=neutral"/>
</DbProviderFactories>
</system.data>
<appSettings>
<add key="aspnet:UseLegacyEncryption" value="false" />
<add key="aspnet:UseLegacyMachineKeyEncryption" value="false" />
<add key="aspnet:UseLegacyFormsAuthenticationTicketCompatibility" value="false" />
<add key="MonoServerDefaultIndexFiles" value="/" />
<add key="aspnet:UseLegacyEncryption" value="false"/>
<add key="aspnet:UseLegacyMachineKeyEncryption" value="false"/>
<add key="aspnet:UseLegacyFormsAuthenticationTicketCompatibility" value="false"/>
<add key="MonoServerDefaultIndexFiles" value="/"/>
</appSettings>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<modules runAllManagedModulesForAllRequests="true"/>
<handlers>
<remove name="ChartImage_axd" />
<remove name="ChartImage_axd"/>
</handlers>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="41943040" />
<requestLimits maxAllowedContentLength="41943040"/>
</requestFiltering>
</security>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Dataflow" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.5.9.0" newVersion="4.5.9.0" />
<assemblyIdentity name="System.Threading.Tasks.Dataflow" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.5.9.0" newVersion="4.5.9.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<location path="WebResource.axd">
<system.web>
<authorization>
<allow users="*" />
<allow users="*"/>
</authorization>
</system.web>
</location>
<location path="ScriptResource.axd">
<system.web>
<authorization>
<allow users="*" />
<allow users="*"/>
</authorization>
</system.web>
</location>