Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: BitKoot on September 15, 2013, 05:03:56 PM



Title: BitcoinRpcSharp - A C# wrapper for the Bitcoin JSON RPC interface
Post by: BitKoot on September 15, 2013, 05:03:56 PM
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 examples
Let's get some information:
Code:
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:
Code:
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:
Code:
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 Application
https://dl.dropboxusercontent.com/u/6522303/BitcoinRpcSharp_console.png

The 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.


Title: Re: BitcoinRpcSharp - A C# wrapper for the Bitcoin JSON RPC interface
Post by: vual on September 29, 2013, 09:36:46 AM
really really nice thanks a ton!
Honestly great work!


Title: Re: BitcoinRpcSharp - A C# wrapper for the Bitcoin JSON RPC interface
Post by: BitKoot on September 29, 2013, 12:03:24 PM
Thanks :). Let me know if you encounter any problems.


Title: Re: BitcoinRpcSharp - A C# wrapper for the Bitcoin JSON RPC interface
Post by: IsaacGoldbourne on January 22, 2014, 12:53:29 PM
Anyone looking at this you need to download http://james.newtonking.com/json and reference it in the project before building it. Then in your own project reference the DLL from the debug folder.


Title: Re: BitcoinRpcSharp - A C# wrapper for the Bitcoin JSON RPC interface
Post by: bitcoinprogrammer on January 23, 2014, 02:46:22 PM
Hi!
I'm new in bitcoin system and i want to do some tests.
I've tried your application choosing method 17 (GetDifficulty).
But at the  MakeHttpRequest() method i received an exception : "System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly."

I set in app.config my ip and port number:
    <add key="rpc_host" value="http://myIP:8333"/>
    <add key="rpc_user" value="test"/>
    <add key="rpc_pw" value="123"/>

Do you have an idea why i received this error? Exists another configurations to set?

Thank in advance!

Have a nice day.


Title: Re: BitcoinRpcSharp - A C# wrapper for the Bitcoin JSON RPC interface
Post by: BitKoot on January 25, 2014, 04:39:21 PM
Anyone looking at this you need to download http://james.newtonking.com/json and reference it in the project before building it. Then in your own project reference the DLL from the debug folder.

Thanks for the heads up. In Visual Studio the NuGet package manager should automaticly install the needed DLL's. But for people who don't use Visual Studio or the package manager I added the link to the Json.NET site in the readme.


Title: Re: BitcoinRpcSharp - A C# wrapper for the Bitcoin JSON RPC interface
Post by: BitKoot on January 25, 2014, 04:40:39 PM
Hi!
I'm new in bitcoin system and i want to do some tests.
I've tried your application choosing method 17 (GetDifficulty).
But at the  MakeHttpRequest() method i received an exception : "System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly."

I set in app.config my ip and port number:
    <add key="rpc_host" value="http://myIP:8333"/>
    <add key="rpc_user" value="test"/>
    <add key="rpc_pw" value="123"/>

Do you have an idea why i received this error? Exists another configurations to set?

Thank in advance!

Have a nice day.


Hi, you should only have to set those three settings.

Is it possible a firewall is blocking access to your bitcoind instance?


Title: Re: BitcoinRpcSharp - A C# wrapper for the Bitcoin JSON RPC interface
Post by: doof on January 30, 2014, 02:51:53 AM
FYI, getting wrong account when using GetAccount(btcaddress) method, and account/label is null.


Title: Re: BitcoinRpcSharp - A C# wrapper for the Bitcoin JSON RPC interface
Post by: cosmoken on January 30, 2014, 02:52:44 AM
Nice wrapper! looking forward to it  :D


Title: Re: BitcoinRpcSharp - A C# wrapper for the Bitcoin JSON RPC interface
Post by: andrehorta on February 08, 2014, 02:22:42 PM
Congratualtions!

How can i donate in Bitcoin?

Why im getting this number?

...
add1= wallet.GetNewAddress();
             wallet.SetAccount(add1, "andre22");
             balance = wallet.GetBalance("andre22");


Is showing to me:

balance = 415855183323463.75M