Bitcoin Forum

Bitcoin => BitcoinJ => Topic started by: R2D221 on November 05, 2013, 03:26:57 AM



Title: Using BitcoinJ in .NET (C#)
Post by: R2D221 on November 05, 2013, 03:26:57 AM
Hello! I'm trying to develop a Bitcoin app for the Windows Store. I'm doing this like a hobby, so in many things I'm still new to this.
I've seen that many programs use BitcoinJ, but as the name implies, it's for Java. I've found some .NET ports, but they are from 2011, so I don't think they are that reliable.
But then I found about a program called IKVM (http://www.ikvm.net/), which can convert a Java library to a .NET one.

As this is a very delicate subject (an app that handles money), I would like to ask you: Is it OK if I follow this approach? Or maybe I should manually port it to .NET? (which will take me a long time since I'm still in university)

What are your opinions?


Title: Re: Using BitcoinJ in .NET (C#)
Post by: Mike Hearn on November 05, 2013, 10:28:07 AM
I've never used IKVM, but it's the approach I've been suggesting .NET developers try for a while.

From a language/operating system perspective bitcoinj is very simple. It doesn't do anything fancy like reflection or code synthesis. I've actually successfully transpiled it to C++ (although the result didn't run due to missing runtime stubs). It should be possible to either use IKVM and convert the Java at the bytecode level, or using the tool the Mono guys developed to do a source-to-source translation of the Android sources to C#.

It looks like IKVM is as simple as running a command line tool to convert the JAR to a DLL. What I suggest you try is creating a bundled JAR that contains everything together, and then converting it to a DLL and seeing if you can re-write the ForwardingService example in C#.

To get a bundled JAR, you can just use Maven to compile bitcoinj and then grab the tools/target/bitcoinj-tools-0.11-SNAPSHOT.jar file. That JAR contains the WalletTool app which is kind of useless, but more importantly it's been processed with the "maven shade" plugin that bundles bitcoinj with all its dependencies. That's the JAR you should convert to a DLL.


Title: Re: Using BitcoinJ in .NET (C#)
Post by: R2D221 on November 06, 2013, 02:43:46 AM
Thanks  ;D. I'm already following the tutorials from the website, and everything seems to work well (except some Java-specific things such as the Listeners, but I'm sorting them out). If anything goes wrong, I'll let you know.


Title: Re: Using BitcoinJ in .NET (C#)
Post by: Mike Hearn on November 06, 2013, 11:47:53 AM
If you find ways we can adjust the library to make it more pleasant to use from C#, please do let us know.

Also, if you could write up a short guide once you're done, I'd love to include it on the website.


Title: Re: Using BitcoinJ in .NET (C#)
Post by: R2D221 on November 08, 2013, 02:38:23 PM
Although I could successfully create a console app, when I tried to import the references to the Windows Store app, it gave me compile errors because some references could not be resolved. This means that, since Windows Store has a limited .NET API, it can't use some of the features this library is using (at least the way IKVM generated it).

Either I find a way to generate a Windows Store compatible library, or I'm afraid I'll have to manually port the library.


Title: Re: Using BitcoinJ in .NET (C#)
Post by: Mike Hearn on November 10, 2013, 02:26:27 PM
Or not distribute via the Windows Store? It seems odd that the API would be missing chunks for store vs non-store apps.

Which API's were missing? Perhaps I will understand what it's trying to do that's forbidden. BitcoinJ doesn't use much in the way of the Java API, so I suspect Windows Store places restrictions on file IO or something like that.


Title: Re: Using BitcoinJ in .NET (C#)
Post by: R2D221 on November 11, 2013, 05:05:00 AM
Or not distribute via the Windows Store?
I'm developing a Windows Store app. It needs to be distributed via the Windows Store.

Which API's were missing? Perhaps I will understand what it's trying to do that's forbidden. BitcoinJ doesn't use much in the way of the Java API, so I suspect Windows Store places restrictions on file IO or something like that.

The Windows Store API is somewhat restricted because it's more related to tablet and smartphone development than it is to the desktop, so for things such as file access, geolocation, etc., the app has to ask the user explicit permission to use it. Also, things like opening executables are forbidden for security reasons (although I know that is no problem for this app; it's just interesting to point out).

Thanks anyway for your help, and for writing this library :)
When I have something ready to test, I will show it here.

Just for the record, IKVM works fine when creating desktop apps in .NET. It's only when developing Windows Store apps when issues appear.


Title: Re: Using BitcoinJ in .NET (C#)
Post by: Mike Hearn on November 11, 2013, 11:39:43 AM
OK. Like I said, if you point out which API's are not available, perhaps we can come up with workarounds.


Title: Re: Using BitcoinJ in .NET (C#)
Post by: luvbcs on December 15, 2013, 03:19:33 PM
This seems a great place to ask this question.

I need to build a windows app that can do just a few specific things:

1) create a local wallet with one or more addresses
2) display the public key of those addresses to the user
3) accept a cut-and-paste raw transaction, decode it, and show the user what the transaction does
4) Allow user to sign and send transaction to miners

If the IKVM .dll conversion works as expected, will this be pretty straightforward?


Title: Re: Using BitcoinJ in .NET (C#)
Post by: Mike Hearn on December 15, 2013, 05:49:16 PM
Sure


Title: Re: Using BitcoinJ in .NET (C#)
Post by: luvbcs on December 15, 2013, 07:43:29 PM
sweet.  Because the bitnetclient for .net that was written is great as far as it goes, but it doesn't seem to support signrawtransaction or sendrawtransaction.  I'll be back when I get this far.  Thanks a ton


Title: Re: Using BitcoinJ in .NET (C#)
Post by: R2D221 on December 20, 2013, 05:05:34 PM
Just remember: it only works on desktop applications (WPF, etc.), but not Windows Store applications.


Title: Re: Using BitcoinJ in .NET (C#)
Post by: Bitoy on February 10, 2014, 11:59:07 AM
Is there a BitcoinJ .NET API that can sign and send a raw transaction?




Title: Re: Using BitcoinJ in .NET (C#)
Post by: JPSelzer on March 04, 2014, 08:20:30 AM
Good tutorials on specialized sites always help a lot in such matters


Title: Re: Using BitcoinJ in .NET (C#)
Post by: marsattacks on March 09, 2014, 12:54:09 AM
I can confirm that IKVM works, just compile the bundled jar using ikvmc (ikvm-7.2.4630.5), like so:

ikvmc -target:library bitcoinj-0.11-bundled.jar slf4j-nop-1.7.6.jar

this results in a .net library bitcoinj-0.11-bundled.dll

I had to include slf4j-nop-1.7.6.jar to prevent getting the following warning when running a simple WalletAppKit console app:

Code:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

during the compile IKVMC also warns about some missing org.apache.lucene classes, but the assembly still works.


Title: Re: Using BitcoinJ in .NET (C#)
Post by: Nicolas Dorier on May 27, 2014, 07:09:05 PM
Alternatively, you can try https://github.com/NicolasDorier/NBitcoin (https://github.com/NicolasDorier/NBitcoin)
Support more feature than BitcoinJ, except for the payment (BIP70) that I'm developping right now.
You can sign and send transactions.

I wrote two small article on it.
http://www.codeproject.com/Articles/768412/NBitcoin-The-most-complete-Bitcoin-port-Part-Crypt (http://www.codeproject.com/Articles/768412/NBitcoin-The-most-complete-Bitcoin-port-Part-Crypt)
http://www.codeproject.com/Articles/775226/NBitcoin-Cryptography-Part (http://www.codeproject.com/Articles/775226/NBitcoin-Cryptography-Part)


Title: Re: Using BitcoinJ in .NET (C#)
Post by: R2D221 on May 28, 2014, 01:28:55 PM
Alternatively, you can try https://github.com/NicolasDorier/NBitcoin (https://github.com/NicolasDorier/NBitcoin)
Support more feature than BitcoinJ, except for the payment (BIP70) that I'm developping right now.
You can sign and send transactions.

I wrote two small article on it.
http://www.codeproject.com/Articles/768412/NBitcoin-The-most-complete-Bitcoin-port-Part-Crypt (http://www.codeproject.com/Articles/768412/NBitcoin-The-most-complete-Bitcoin-port-Part-Crypt)
http://www.codeproject.com/Articles/775226/NBitcoin-Cryptography-Part (http://www.codeproject.com/Articles/775226/NBitcoin-Cryptography-Part)
Does it support SPV mode? I was thinking on a Windows Phone app, and I don't think it could (or should) run a full node.


Title: Re: Using BitcoinJ in .NET (C#)
Post by: Nicolas Dorier on May 28, 2014, 02:22:07 PM
Alternatively, you can try https://github.com/NicolasDorier/NBitcoin (https://github.com/NicolasDorier/NBitcoin)
Support more feature than BitcoinJ, except for the payment (BIP70) that I'm developping right now.
You can sign and send transactions.

I wrote two small article on it.
http://www.codeproject.com/Articles/768412/NBitcoin-The-most-complete-Bitcoin-port-Part-Crypt (http://www.codeproject.com/Articles/768412/NBitcoin-The-most-complete-Bitcoin-port-Part-Crypt)
http://www.codeproject.com/Articles/775226/NBitcoin-Cryptography-Part (http://www.codeproject.com/Articles/775226/NBitcoin-Cryptography-Part)
Does it support SPV mode? I was thinking on a Windows Phone app, and I don't think it could (or should) run a full node.

Sadly, not for now ;(
But you can manually connect to the network and send the messages to support it yourself. (Partial Merkle Tree are implemented, and bloom filter also)


Title: Re: Using BitcoinJ in .NET (C#)
Post by: R2D221 on May 28, 2014, 03:40:49 PM
But you can manually connect to the network and send the messages to support it yourself. (Partial Merkle Tree are implemented, and bloom filter also)
OK, I will consider it :)


Title: Re: Using BitcoinJ in .NET (C#)
Post by: Razick on August 08, 2014, 10:55:46 PM
Although I could successfully create a console app, when I tried to import the references to the Windows Store app, it gave me compile errors because some references could not be resolved. This means that, since Windows Store has a limited .NET API, it can't use some of the features this library is using (at least the way IKVM generated it).

Either I find a way to generate a Windows Store compatible library, or I'm afraid I'll have to manually port the library.

I stopped developing for Windows Store because so many important methods are missing from the Windows Store version of .NET. System.Security.Cryptography, which was required for my app is a good example.

I think there may be alternative methods, but they made it very hard to port programs by not using the same ones.  Hmm, I wonder why there are no good Windows Store apps?  ::)


Title: Re: Using BitcoinJ in .NET (C#)
Post by: Nicolas Dorier on August 08, 2014, 11:46:41 PM
Although I could successfully create a console app, when I tried to import the references to the Windows Store app, it gave me compile errors because some references could not be resolved. This means that, since Windows Store has a limited .NET API, it can't use some of the features this library is using (at least the way IKVM generated it).

Either I find a way to generate a Windows Store compatible library, or I'm afraid I'll have to manually port the library.

I stopped developing for Windows Store because so many important methods are missing from the Windows Store version of .NET. System.Security.Cryptography, which was required for my app is a good example.

I think there may be alternative methods, but they made it very hard to port programs by not using the same ones.  Hmm, I wonder why there are no good Windows Store apps?  ::)

Razick, porting NBitcoin to Portable library is not complicated and definitively feasable thanks to the unit tests.
I say that because I did it for a customer of mine, it took approximately 7H.

To do that, you need to fetch the sources of BouncyCastle, and strip every thing that does not compile in portable.
Then, do the same with NBitcoin, strip every class you don't need.
Then, do the same with NBitcoin.Tests, so you can be sure you did not broke anything by porting or stripping too much. Run tests with Core and UnitTests traits.

For my customer, NBitcoin works on xamarin (IOS, Android, Windows Phone). I wanted to make it open source directly, but he pays the bill, so he got a private copy of this work. I might release the code in open source when the project is finished if he agrees.
If not, I will redo the port from scratch.

Some goodies : NBitcoin signature verification and generation, and all crypto ECC operations are manifold more performant thanks to the most recent version of BouncyCastle :)
All security stuff is implemented in BouncyCastle so no dependance on System.Security.Crypto.

One problematic thing is the Random generation, RandomUtils will not compile because generating a random number with enough entropy is plateform specific. Luckily, for the sig creation I implement determinitstic RFC. (http://tools.ietf.org/html/rfc6979)


Title: Re: Using BitcoinJ in .NET (C#)
Post by: garyramsey389 on November 07, 2014, 01:55:40 PM
Hello! I'm trying to develop a Bitcoin app for the Windows Store. I'm doing this like a hobby, so in many things I'm still new to this.
I've seen that many programs use BitcoinJ, but as the name implies, it's for Java. I've found some .NET ports, but they are from 2011, so I don't think they are that reliable.
But then I found about a program called IKVM (http://www.ikvm.net/), which can convert a Java library to a .NET one.

As this is a very delicate subject (an app that handles money), I would like to ask you: Is it OK if I follow this approach? Or maybe I should manually port it to .NET? (which will take me a long time since I'm still in university)

What are your opinions?


Thanks for sharing this nice and informative post...