Work on bitcoin hotwallet

This commit is contained in:
Rick Falkvinge
2015-10-06 13:05:20 +02:00
parent 87cc3e716c
commit 3b9bb52486
4 changed files with 94 additions and 5 deletions
+57
View File
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using NBitcoin;
using Newtonsoft.Json.Linq;
namespace Swarmops.Logic.Financial
{
public class BitcoinUtility
{
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.
string addressString = secretKey.PubKey.GetAddress (Network.Main).ToString();
var unspentJsonResult =
JObject.Parse (new WebClient().DownloadString ("https://blockchain.info/unspent?active=" + addressString));
List<Coin> coinList = new List<Coin>();
foreach (var unspentJson in unspentJsonResult["unspent_outputs"])
{
BitcoinUnspentTransactionOutput txUnspent = new BitcoinUnspentTransactionOutput()
{
BitcoinAddress = addressString,
ConfirmationCount = (UInt32) unspentJson["confirmations"],
Satoshis = (UInt64) unspentJson["value"],
TransactionHash = (string) unspentJson["tx_hash_big_endian"],
TransactionOutputIndex = (UInt32) unspentJson["tx_output_n"]
};
coinList.Add (txUnspent);
}
return coinList.ToArray();
}
}
internal class BitcoinUnspentTransactionOutput
{
public UInt64 Satoshis { get; set; }
public string TransactionHash { get; set; }
public UInt32 TransactionOutputIndex { get; set; }
public string BitcoinAddress { get; set; }
public UInt32 ConfirmationCount { get; set; }
public static implicit operator NBitcoin.Coin (BitcoinUnspentTransactionOutput source)
{
return new Coin(new OutPoint() { Hash= uint256.Parse (source.TransactionHash), N=source.TransactionOutputIndex }, new TxOut(new NBitcoin.Money (source.Satoshis), new BitcoinAddress (source.BitcoinAddress, Network.Main) ));
}
}
}
+4 -2
View File
@@ -249,7 +249,9 @@ namespace Swarmops.Logic.Financial
}
}
public const int RootWalletIndex = 1;
public const int RootDonationsIndex = 2;
public const int BitcoinWalletIndex = 1;
public const int BitcoinDonationsIndex = 2;
public const string BitcoinTestAddress = "1JMpU3D6c5sruunMwzkt6p6PQzLcUYcL26";
}
}
+2
View File
@@ -96,6 +96,7 @@
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Numerics" />
<Reference Include="System.Threading.Tasks.Dataflow, Version=4.5.9.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.Tpl.Dataflow.4.5.9\lib\net45\System.Threading.Tasks.Dataflow.dll</HintPath>
@@ -117,6 +118,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Financial\BitcoinUtility.cs" />
<Compile Include="Financial\IPayable.cs" />
<Compile Include="Resources\Logic.Financial.FinancialAccount.Designer.cs">
<AutoGen>True</AutoGen>
+31 -3
View File
@@ -1,4 +1,5 @@
using System;
using System.Activities.Validation;
using System.Collections.Generic;
using System.Linq;
using System.Web;
@@ -14,22 +15,49 @@ namespace Swarmops.Frontend.Pages.v5.Financial
{
protected void Page_Load (object sender, EventArgs e)
{
this.PageAccessRequired = new Access (this.CurrentOrganization, AccessAspect.Null);
this.PageAccessRequired = new Access (this.CurrentOrganization, AccessAspect.Participant);
this.PageTitle = Resources.Pages.Financial.Donate_PageTitle;
this.InfoBoxLiteral = Resources.Pages.Financial.FileExpenseClaim_Info;
string bitcoinAddress =
FinancialAccounts.BitcoinHotPublicRoot
.Derive ((uint) this.CurrentOrganization.Identity)
.Derive (FinancialAccounts.RootDonationsIndex)
.Derive (FinancialAccounts.BitcoinDonationsIndex)
.Derive ((uint) this.CurrentUser.Identity)
.PubKey.GetAddress (Network.Main)
.ToString();
// TEST TEST TEST
/*
BitcoinSecret secretKey =
FinancialAccounts.BitcoinHotPrivateRoot.Derive ((uint) this.CurrentOrganization.Identity)
.Derive (FinancialAccounts.BitcoinDonationsIndex)
.PrivateKey
.GetBitcoinSecret (Network.Main);
Coin[] spendableCoin = BitcoinUtility.GetSpendableCoin (secretKey);
TransactionBuilder txBuild = new TransactionBuilder();
Transaction tx = txBuild.AddCoins (spendableCoin)
.AddKeys (secretKey)
.Send (new BitcoinAddress (FinancialAccounts.BitcoinTestAddress), "0.001")
.SendFees ("0.0001")
.SetChange (secretKey.GetAddress())
.BuildTransaction (true);
bool test = txBuild.Verify (tx);
if (!test)
{
throw new InvalidOperationException("Tx is not properly signed");
}
string txHex = tx.ToHex();*/
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:" + 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=" + CurrentOrganization.Name.Replace ("&","et").Replace ("=", string.Empty)); // URI scheme doesn't like &, =
}
}
}