Bitcoin Forum
December 14, 2024, 06:40:39 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: BitcoinRpcSharp - A C# wrapper for the Bitcoin JSON RPC interface  (Read 4278 times)
BitKoot (OP)
Member
**
Offline Offline

Activity: 113
Merit: 10



View Profile
September 15, 2013, 05:03:56 PM
 #1

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


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.
vual
Hero Member
*****
Offline Offline

Activity: 714
Merit: 500



View Profile
September 29, 2013, 09:36:46 AM
 #2

really really nice thanks a ton!
Honestly great work!
BitKoot (OP)
Member
**
Offline Offline

Activity: 113
Merit: 10



View Profile
September 29, 2013, 12:03:24 PM
 #3

Thanks Smiley. Let me know if you encounter any problems.
IsaacGoldbourne
Member
**
Offline Offline

Activity: 112
Merit: 10

Looking to start various enterprises


View Profile
January 22, 2014, 12:53:29 PM
 #4

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.

Vote for me for CEO/CNO of MemoryCoin!
CEO: MVTEceoa86dYRsxc2rWCexBMjJmaawMkHZ
CNO: MVTEcno2tbsJWj7AQEyEjgk72j94hbPHFm
bitcoinprogrammer
Newbie
*
Offline Offline

Activity: 1
Merit: 0


View Profile
January 23, 2014, 02:46:22 PM
 #5

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.
BitKoot (OP)
Member
**
Offline Offline

Activity: 113
Merit: 10



View Profile
January 25, 2014, 04:39:21 PM
 #6

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.
BitKoot (OP)
Member
**
Offline Offline

Activity: 113
Merit: 10



View Profile
January 25, 2014, 04:40:39 PM
 #7

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?
doof
Hero Member
*****
Offline Offline

Activity: 765
Merit: 503


View Profile WWW
January 30, 2014, 02:51:53 AM
 #8

FYI, getting wrong account when using GetAccount(btcaddress) method, and account/label is null.
cosmoken
Newbie
*
Offline Offline

Activity: 2
Merit: 0


View Profile
January 30, 2014, 02:52:44 AM
 #9

Nice wrapper! looking forward to it  Cheesy
andrehorta
Legendary
*
Offline Offline

Activity: 1261
Merit: 1000


View Profile WWW
February 08, 2014, 02:22:42 PM
 #10

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
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!