Pages:
Author

Topic: NBitcoin : Almost full bitcoin implementation in .NET - page 3. (Read 30756 times)

hero member
Activity: 714
Merit: 662
BitcoinExtKey and BitcoinExtPubKey inherit from Base58Data as all base58Data it contains also information about the Network you are using (Main, TestNet or Reg).
ExtKey on the other hand, only contains the key information.

You can get the ExtKey from BitcoinExtKey by using the property BitcoinExtKey.Key.
However, to do the reverse, ExtKey need information about which Network to use to generate your base58 string.

So here is how your do to go back and forth :

Quote
String keyStr = "xprv9s21ZrQH143K4XUJvv1fQcoDGhLdtQUsfqX7zX4p5XvMtbh8bQxjjMRSTdeVakjmMtg52no13wj g16eSgKA32i1WRxSNYpDaRkF2GkG2NZc";
BitcoinExtKey base58key = new BitcoinExtKey(keyStr, Network.Main); //Would throw FormatException if the base58 is for Network.TestNet
ExtKey key = base58key.Key;
ExtPubKey pubkey = key.Neuter();

//Or, if you want NBitcoin to deduce the network from the base58 directly, this does exactly the same thing

base58key = Network.CreateFromBase58Data(keyStr);
key = base58key.Key;
pubkey = key.Neuter();

//Now, if you want to transform back in BitcoinExtKey

base58key = new BitcoinExtKey(key, Network.Main);  //Or Network.Main.CreateBitcoinExtKey(key)
string base58Str = base58key.ToString(); //base58Str == key

//If you want the PubKey in base58 :
var base58PubKey = new BitcoinExtPubKey(pubkey, Network.Main);  //Or Network.Main.CreateBitcoinExtPubKey(key)
base58Str = base58PubKey.ToString(); //xpub661MyMwAqRbcH1Yn2wYfmkjwpjB8HsCj34SinuURdsTLmQ2H8xGzH9jvJxMYRRKchqEYF3xEB1sXsnQTRKU8oNYDChLuYPgyBneJvdzhc1J
//BitcoinExtPubKey.PubKey to get back the ExtPubKey

So to respond to your question, the reason why there is both BitcoinExtKey and ExtKey is because BitcoinExtKey represents a base58 data, which contains more information that simply the ExtKey. (that has no Network information attached to ExtKey, just the strict cryptographic data)
This works the same for all base58Data. (Key vs BitcoinSecret for example)
donator
Activity: 1218
Merit: 1079
Gerald Davis
Glad to hear it.  I have a question on the difference between ExtKey and Bitcoin ExtKey (and likewise ExtPubKey and BitcoinExtPubKey).  If the BitcoinX class simply a wrapper around a generic ExtKey which contains the Bitcoin specific (i.e. BIP32) features?

I find myself needing to swap between them excessively.  That usually means I am missing something obvious.  My first thought is that you would be able to derive extended keys directly from the BitcoinExtKey class but those methods (Derive & Neuter are not implemented).  My assumption is that you are using these classes differently than I assumed they would be used.

Is there a reason that BitcoinExtKey and BitcoinExtPubKey do not implement derive and neuter or is it just that it hasn't been completed.  If it is the later I would gladly do a pull but I want to make sure I am not missing something obvious before I go down that road.
hero member
Activity: 714
Merit: 662
I will continue to maintain it. I intend to build my own project on top of it, also I am currently working with some bitcoin startup, and the community is very supportive, that get me a great incentive. Smiley
donator
Activity: 1218
Merit: 1079
Gerald Davis
Quote
Thanks for the tip, highly appreciate !!

No problem.  It is a very solid library.  I hope you continue to develop it.

Quote
You can use xUnit test runner in visual studio via extension gallery in vs or with http://visualstudiogallery.msdn.microsoft.com/463c5987-f82b-46c8-a97e-b1cde42b9099

Yeah I grabbed the runner and it works fine in VS 2013.  

Quote
yes I develop in C# professionally. ... Yes, I am a proponent of Test Driven Development.
I had a feeling.  Glad to see another C# developer involved in Bitcoin.  We are a rare breed but I think a solid .Net library will be essential as Bitcoin moves more into the mainstream.  

Quote
I'm using xUnit just because it was easier to run in a build environment in the past than mstest, and I never looked back.
I hear you.  The only times in recent history that I used mstest was when I was working for a company which required it (because they had been using it for years).  I have always used NUnit but have been meaning to learn about XUnit.  This will give me a good excuse.
hero member
Activity: 714
Merit: 662
Thanks for the tip, highly appreciate !! yes I develop in C# professionnally. Did NBitcoin so entrepreneurs can build business upon Bitcoin more easily.
You can use xUnit test runner in visual studio via extension gallery in vs or with http://visualstudiogallery.msdn.microsoft.com/463c5987-f82b-46c8-a97e-b1cde42b9099
Then it works as MSTest, and you can run/debug them directly in VS.

Yes, I am a proponent of Test Driven Development.
I ported the tests of Bitcoin Core or other bitcoin libraries before making its implementation.
The reason is that I wanted a class model similar to Bitcoin Core, this make porting easier and learning curve smooth.

I'm using xUnit just because it was easier to run in a build environment in the past than mstest, and I never looked back.
donator
Activity: 1218
Merit: 1079
Gerald Davis
A tip sent.  Please continue to development you got something good started here.  The library is very robust for something that was only started a few months ago.  I am also glad to see some good use of C#.  I prefer the syntax of C# or Java C++.  I assume you write C# professionally?   I was also glad to see unit testing, I assume you are a proponent of test driven development?  I don't have much experience with xUnit (mainly used NUnit and when forced Microsoft's test platform) but the syntax was easy to pick up.  Well at least the basis using a little google-fu as needed.
hero member
Activity: 714
Merit: 662
So I finally got around to taking a look at this library as I need to do some BIP32 work.  All I can say is I wish I had taken a closer look earlier.   This is solid work man.  I am surprised it hasn't been given more attention.  Bonus points for making it a nuget package.  

Is the tip address in your profile valid?  

Yes, this is valid Smiley

This lib is very new I started in April I think.

Here is some article to get you started. (some methods might be outdated)
Basic of Bitcoin  http://www.codeproject.com/Articles/768412/NBitcoin-The-most-complete-Bitcoin-port-Part-Crypt (maybe not for you)
Usage of Stealth Address and BIP38 http://www.codeproject.com/Articles/775226/NBitcoin-Cryptography-Part
Scanning of Blockchain http://www.codeproject.com/Articles/784519/NBitcoin-How-to-scan-the-Blockchain

I am working nowadays on a open source JSON service that would permit you to track any Account you want on the blockchain from the simple implementation of a "Scanner" talked in the article. (Colored Coin, stealth payment included)
This will be awesome ! Smiley

For the BIP32, look at the bip32_tests.cs tests, I'm using the same tests vectors as core.
The base58 representation is called BitcoinExtKey.
donator
Activity: 1218
Merit: 1079
Gerald Davis
So I finally got around to taking a look at this library as I need to do some BIP32 work.  All I can say is I wish I had taken a closer look earlier.   This is solid work man.  I am surprised it hasn't been given more attention.  Bonus points for making it a nuget package. 

Is the tip address in your profile valid? 
member
Activity: 104
Merit: 10
Great work thanks.I'll join to project in github.
hero member
Activity: 714
Merit: 662
Nicolas Dorier you have my respect, thank you!
Thanks, don't hesitate to share, ask features or tell issues on github Smiley
member
Activity: 89
Merit: 10
Nicolas Dorier you have my respect, thank you!
hero member
Activity: 714
Merit: 662
What problem are you trying to solve here? What does your .Net implementation give us that that Bitcoin-QT or talking to bitcoind over RPC doesn't?

I'm totally in favour of alternative clients, but all of them seem to solve a particular problem. Are you planning to simply keep up to date with the bitcoin-core changes? Are you planning to head in a different direction?

It depends on if you are talking about the crypto part goal, the node implementation goal, or the overall goal.

For the overall goal :
Actually Bitcoin code is in C++. Compiling it on a windows computer, or even linux, and playing by debugging tests is a big battle that will cost you lots of time.
In other words, you can't participate into the bitcoin code if you are not a linux wizard in C++.

With NBitcoin, you clone the project, open with visual studio, and you can directly compile and run tests.
There is no dependencies to compile and fetch on the net.
To use the NBitcoin library, you just use the Nuget Package manager that will automatically download and add reference to NBitcoin with dependencies so you can directly code without any hassle.
In clear : If you have visual studio, it takes 1 minutes to start using the lib in your code, and 1 minute to compile it yourself and run tets.
Also the C++ code is not always obvious to read for business developers (I guess that's ok enough for hackers though), the C# have a cleaner model. (But I try to get as close as I can to the C++ model)

For the crypto part :
You can generate keys or use BIP 32 without the need of any node server to install, which is huge depending on where you deploy your code. (Hosted website will not permit you to run a node server, and you should not need that if you just want to generate keys)

For the node client part :
I think that developers need an easy way to talk to the network.
Why should I be obliged to install bitcoind to use a JSON API, download the whole blockchain, to get informations about a block, when I can just connect to the network and ask for it by using the raw protocol ?
Why if I want just to parse a raw transaction, I need to host a server ?

For the node server part :
Well, this come for free once you coded the client part.
I just permit you to create a indexes on blocks, and soon, transactions without the need to install a dedicated server.
The goal will be to manually create transactions in code from your bitcoin wallet.

Anyway, for now, I'm always a little late on the node part behind bitcoin core, and I intend to follow their direction because they will always be used by the majority. (NBitcoin only limits to C# developers)
full member
Activity: 224
Merit: 100
Professional anarchist
It isn't a client, it is a library.

Aar, I see.

For the benefit of other .Net developers, I'm currently using BitcoinJ in a .Net application (https://github.com/XCoinr/Metrobit) by using IKVM to create a .Net dll from a BitcoinJ bundled jar. I will be writing a how-to when I've nailed the very basic functionality, but happy to share tips with anyone interested.
donator
Activity: 1218
Merit: 1079
Gerald Davis
Keep your implementation, maybe I am lacking feature that only you have. The ECDSA alternative is very intesting, I might try to make it works on x86. I add that to my check list..

I wouldn't kill yourself on x86 compatibility.  Honestly who has a machine that is running .net which isn't x64 at this point. Even in QT devs started using x64 builds because of performance and memory issues.  As the blockchain grows the memory demands will also grow with it.
donator
Activity: 1218
Merit: 1079
Gerald Davis
What problem are you trying to solve here? What does your .Net implementation give us that that Bitcoin-QT or talking to bitcoind over RPC doesn't?

I'm totally in favour of alternative clients, but all of them seem to solve a particular problem. Are you planning to simply keep up to date with the bitcoin-core changes? Are you planning to head in a different direction?

It isn't a client, it is a library.
full member
Activity: 224
Merit: 100
Professional anarchist
What problem are you trying to solve here? What does your .Net implementation give us that that Bitcoin-QT or talking to bitcoind over RPC doesn't?

I'm totally in favour of alternative clients, but all of them seem to solve a particular problem. Are you planning to simply keep up to date with the bitcoin-core changes? Are you planning to head in a different direction?
hero member
Activity: 714
Merit: 662
Air
Ahh damn, you beat me to it.  I wanted to have the first (mostly) independent full-node reimplementation in .NET.  I guess I'll have to focus on making mine the best.

Some things I've learned that may be relevant to your experiences too:
  • If you're willing to experiment with an x64-specific build, I would highly recommend checking out https://github.com/joshlang/Secp256k1.NET.  It made a world of difference in the time for my client to do the initial sync.  My super early WIP code uses it, with BouncyCastle as a fallback, like so (obviously doesn't have any special hashtype-related behavior).
  • There's no good LevelDB library on .NET; I think it would be a really big value-add to be able to run off the standard database from the Satoshi client, so just a couple of days ago, I dipped my feet into bitcoin's leveldb tree and started working on https://github.com/airbreather/LevelDB.NET.  It won't work on Mono, it's just a C++/CLI bridge, so I've got a fallback for some other things for the IL-only version.

EDIT: I accidentally a word.

Keep your implementation, maybe I am lacking feature that only you have. The ECDSA alternative is very intesting, I might try to make it works on x86. I add that to my check list..

For the DB, I am using SqLite as a NoSql store, it works well. The indexing of the 300 000 blocks take only 5 minutes. (Compared to the 5 hours of the core implementation when lvldb is corrupt) Sad
The size of the db afterward is 35 MB. I would have a problem though if I intend to index transactions. (It would take 170 GB given that 1 block = 5000 Tx)
The block files of the satoshi client are not from LevelDB, but raw blocks with a small header. I created the code to parse it (StoredBlock) as well to write it.

I created an article on only the crypto part I have coded : http://www.codeproject.com/Articles/768412/NBitcoin-The-most-complete-Bitcoin-port-Part-Crypt, maybe it can interest you.

Today, I am banging my head on the parsing of their wallet which is not a leveldb structure. I also need to make the RPC client/server part.

For LevelDB... I stopped to try when I could not compile that stuff on windows without installing X dependency.
newbie
Activity: 4
Merit: 0
Ahh damn, you beat me to it.  I wanted to have the first (mostly) independent full-node reimplementation in .NET.  I guess I'll have to focus on making mine the best.

Some things I've learned that may be relevant to your experiences too:
  • If you're willing to experiment with an x64-specific build, I would highly recommend checking out https://github.com/joshlang/Secp256k1.NET.  It made a world of difference in the time for my client to do the initial sync.  My super early WIP code uses it, with BouncyCastle as a fallback, like so (obviously doesn't have any special hashtype-related behavior).
  • There's no good LevelDB library on .NET; I think it would be a really big value-add to be able to run off the standard database from the Satoshi client, so just a couple of days ago, I dipped my feet into bitcoin's leveldb tree and started working on https://github.com/airbreather/LevelDB.NET.  It won't work on Mono, it's just a C++/CLI bridge, so I've got a fallback for some other things for the IL-only version.

EDIT: I accidentally a word.
hero member
Activity: 714
Merit: 662
Just added different network support.
Implementing bitcoin and sharing with this forum is the best way to learn for sure. Smiley
hero member
Activity: 714
Merit: 662
Thanks, it will definitively help me for the implementation. I note that.
I will keep reimplementing that in NBitcoin, since my goal is to have a full implementation of bitcoin, and not just the RPC part.
Pages:
Jump to: