For a personal .net project of mine I was looking around for a way to interact with the Bitcoin wallet. I found some projects, but they were all outdated or writtin in the wrong language.
So as any programmer would do, I made my own C# wrapper of the JSON RPC interface which is offered by the standard Bitcoin wallet software. The project can be found on GitHub:
https://github.com/BitKoot/BitcoinRpcSharp. I plan to add more features as I need them for my own projects.
I tried to keep to interface as similar to the JSON RPC interface as possible. I have provided a summary with every method offered. Most of it comes from the wiki, and I filled in the gaps where things were not immediatly clear.
The solution contains two projects.
- BitcoinRpcSharp: this is the library which contains the actual implementation (see below for examples). I have implemented about 95% of all RPC calls. Some calls are deprected, others are not relevant (like help). There are two methods which I need to look into more before I can complete the list of implemented methods: listlockunspent and lockunspent.
- TestConsoleApplication: this console application let's you invoke most of the methods from the command line (see below for an example). This application is to try out the Bitcoin wallet methods. It is not intended as a full fledged command line interface to the bitcoin wallet.
Library use examplesLet's get some information:
BitcoinWallet wallet = new BitcoinWallet("http://192.168.56.1:19001", "test", "123", false);
Info info = wallet.GetInfo();
Console.WriteLine("Current difficulty: {0}", info.Difficulty);
If we want to move some bitcoins to a newly generated address associated with a new account:
BitcoinWallet wallet = new BitcoinWallet("http://192.168.56.1:19001", "test", "123", false);
string newAddress = wallet.GetNewAddress("NewAccount");
bool success = wallet.Move("NewAccount", "OldAccount", 1m);
Or let's try something a bit more complicated: create a raw transaction, and sign it:
BitcoinWallet wallet = new BitcoinWallet("http://192.168.56.1:19001", "test", "123", false);
// Create an object containing the inputs and outputs of the transaction.
var createRawTransaction = new CreateRawTransaction();
createRawTransaction.AddInput("bfe0d11bdb73df37709a9f84fabf272576136bcd80589d52ab8ef35f25b48eda", 1); // Transaction id and output
createRawTransaction.AddOutput("n4AiGgWQvZtbo6vjnHWhXTB3ayYF3CFa55", 0.0001m); // Destination address and amount
createRawTransaction.AddOutput("mxDuX7VAPVEhDLtiMy5YgZSt1tLkss8e6G", 0.0001m); // Destination address and amount
// Get the hex string of the raw transaction
var unsignedHex = wallet.CreateRawTransaction(createRawTransaction);
// Sign the transaction, and get the hex string of the signed transaction
var signRawTransaction = new SignRawTransaction(unsignedHex); // This object can hold more information for complex scenario's
var signedTransaction = wallet.SignRawTransaction(signRawTransaction);
// Sent the signed transaction
wallet.SentRawTransaction(signedTransaction.Hex);
Test Console ApplicationThe way to invoke a method is to enter it's number from the menu, followed by a comma separated list of arguments. For example, if we want to backup the wallet to D:\Backups, we would enter: 2,D:\Backups.
I would like to hear if people find this usefull, have suggestions or feature requests. Please be carefull when using this library, as some functions can really mess up your wallet. I have used and recommend the testnet in a box (
https://github.com/freewil/bitcoin-testnet-box) for development.
I you are going to use my software, drop me a message. I always like to hear what you are using it for and if you have any requests or recommendations. And if you are feeling very filantropic, drop me a small tip at: 16rC9F5f5gWc3BNVDSX8Z9MRhcjWeRQ8uU.