Bitcoin Forum

Bitcoin => Wallet software => Topic started by: Nicolas Dorier on April 17, 2014, 10:51:09 PM



Title: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on April 17, 2014, 10:51:09 PM
Hi all,

I passed the two last weeks, full time, porting the C++ code of bitcoin on C#.
It is the most complete and faithful porting I know of bitcoin.
Here is a copy paste of the info I put on the github page.

For those familiar with visual studio, it is available on nuget:
Install-Package NBitcoin

Why is it a big deal ? Because you can run it and debug into it without any linux-voodoo-setup to make it run.
Visual studio express for free, XUnit and you are up to go.

  • Full port of the test suite of bitcoin core with their own data
  • Full script evaluation and parsing
  • Recognize standard script and permit to create them
  • Object model faithful to the C++ API but with C# goodness
  • Simpler API (here is how to generate a key and get the address : new Key().PubKey.Address.ToString())
  • Bloom filter, partial merkle tree
  • Serialization of Blocks, Transactions, Script
  • Signing/verification with private keys, support compact signature for prooving ownership
  • Deterministic wallet : BIP 32

Then the RPC client part. Which I need your help because I don't have any satoshi myself, so I can't emit transactions for testing. If you like my work, submit the testing satoshis to 15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe ;)

Public classes are clean and easy to use, but the implementation is a little messy due to the fact that I'm using C# convention and C++ and the same time. (I will clean that up after the RPC client implementation)

I ported directly from the C++, except the OpenSSL part where I'm using BouncyCaslte instead. (BitcoinJ helped me a lot on the implementation)
I also ported OpenSSL bugs (you can't believe how much time it took me) ;)

Please, use the code to explore/learn/debug/play/sharing/create the licence is LGPL v3, so you should be good to go.
This is the simple way and most complete way to see the internal of bitcoin without going to C++ madness.

With no so much work, it should be Mono compliant. I don't have a lot of dependency on the Windows.

If you like my work, send some satoshi I can crucify for the testing of the RPC client. ;)

Info :
github : https://github.com/NicolasDorier/NBitcoin
bitcoin address : 15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe

Useful link :
Nuget site : https://www.nuget.org/packages/NBitcoin/
Visual studio express : http://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx
XUnit runner (only if using vs express) : http://xunit.codeplex.com/releases


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: DeathAndTaxes on April 18, 2014, 01:25:01 AM
Wow got to take a look at this.

Still you are kidding about needing coins to do testing right?
https://tpfaucet.appspot.com/


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on April 18, 2014, 10:37:48 AM
I did not know about the test network .(seen in the code that we could change, but had no idea about an existing test network)
Thanks for the tips, I can now start implementing the rpc client. :)

Even if I ported the code, I am relatively new to bitcoin, I only knew the basics before starting to coding.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: DeathAndTaxes on April 18, 2014, 04:32:40 PM
Well glad I could help.   Testnet exists for that exact purpose.  Depending on your testing needs you may also want to look into "testnet in a box" which is a self contained testnet that exists only on your system.

BTW I have no problem with you asking for tips just didn't want you wasting them thinking they need to be used for testing.  Glad to see some .net development.  Will take a look at your code this weekend.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: mcaizgk2 on April 18, 2014, 08:57:48 PM
Very nice initiative and as @DeathAndTaxes said it's good to see more people getting involved with C# for Bitcoin.

I can now start implementing the rpc client. :)

Before you decide to reinvent the wheel I would suggest you take a look at this C# RPC API library & wrapper for Bitcoin, Litecoin and Bitcoin-based ALTs:

https://github.com/GeorgeKimionis/BitcoinLib

Some of BitcoinLib's features:

- Fully compatible and up-to-date with Bitcoin 0.9.1 RPC API.
- Design-by-contract, service-oriented architecture.
- Strongly-typed structures for complex RPC requests and responses.
- Implicit JSON casting for all RPC messages.
- Extended methods for every-day scenarios where the built-in methods fall short.
- Exposure of all RPC API's functionality as well as the extended methods through a single interface.
- Fallback mechanism for timed-out RPC requests.
- Custom RPC exceptions.
- Supports all Bitcoin clones.
- Can operate on unlimited daemons with a single library reference.
- Litecoin integration included.
- Each coin instance can be fully parametrized at run-time and implement its own constants.
- Console and web test clients out of the box with demo methods implemented in them.
- Testnet ready.
- Fully configurable.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on April 18, 2014, 09:31:03 PM
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.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on April 19, 2014, 01:00:26 PM
Just added different network support.
Implementing bitcoin and sharing with this forum is the best way to learn for sure. :)


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: AirBreather on May 05, 2014, 10:50:39 PM
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 (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 (http://"https://github.com/airbreather/Evercoin/blob/5bc9e47931d6475bb08f5ca7d59d35519e96b444/source/Evercoin.Algorithms/ECDSASignatureChecker.cs") (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 (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.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on May 06, 2014, 10:21:43 AM
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 (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 (http://"https://github.com/airbreather/Evercoin/blob/5bc9e47931d6475bb08f5ca7d59d35519e96b444/source/Evercoin.Algorithms/ECDSASignatureChecker.cs") (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 (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) :(
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 (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.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: 5flags on May 07, 2014, 03:11:02 PM
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?


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: DeathAndTaxes on May 07, 2014, 03:12:36 PM
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.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: DeathAndTaxes on May 07, 2014, 03:19:21 PM
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.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: 5flags on May 07, 2014, 03:31:59 PM
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 (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.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on May 07, 2014, 03:41:36 PM
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)


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: PeanutPower on May 29, 2014, 11:48:16 PM
Nicolas Dorier you have my respect, thank you!


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on May 29, 2014, 11:55:34 PM
Nicolas Dorier you have my respect, thank you!
Thanks, don't hesitate to share, ask features or tell issues on github :)


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: tprogex on July 02, 2014, 10:11:01 AM
Great work thanks.I'll join to project in github.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: DeathAndTaxes on July 03, 2014, 09:49:14 PM
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? 


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on July 03, 2014, 10:18:19 PM
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 :)

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 (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 (http://www.codeproject.com/Articles/775226/NBitcoin-Cryptography-Part)
Scanning of Blockchain http://www.codeproject.com/Articles/784519/NBitcoin-How-to-scan-the-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 ! :)

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.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: DeathAndTaxes on July 05, 2014, 04:28:37 PM
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.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on July 05, 2014, 04:47:42 PM
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 (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.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: DeathAndTaxes on July 05, 2014, 04:59:59 PM
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.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on July 05, 2014, 05:13:47 PM
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. :)


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: DeathAndTaxes on July 06, 2014, 04:52:09 PM
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.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on July 06, 2014, 06:00:51 PM
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<BitcoinExtKey>(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)


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: DeathAndTaxes on July 07, 2014, 08:53:48 PM
Yeah I got it working like that but I found it easier to modify BitcoinExtKey and BitcoinExtPubKey to have the Neuter and Derive methods to avoid excess swapping between types.  Also I think it would be more clear if the "Key" property in BitcoinExtKey (and "PubKey" in BitcoinExtPubKey) were named ExtKey and ExtPubKey as they do refer to the Extended Key and ExtPubKey.    Otherwise the accessor for the actual Key or PubKey becomes bitcoinExtKey.Key.Key and bitcoinExtPubKey.PubKey.PubKey.

Code:
            var masterPubKey = new BitcoinExtPubKey(masterPubKeyString, Network.Main);
            var masterServerWalletPubChain = masterPubKey.Derive(0);

...
       private BitcoinScriptAddress GenerateDepositAddresses(BitcoinExtPubKey parentExtPubKey, uint index)
        {
            BitcoinExtPubKey serverDepositPubChain1 = parentExtPubKey.Derive(1001);
            BitcoinExtPubKey serverDepositPubChain2 = parentExtPubKey.Derive(1002);
            BitcoinExtPubKey serverDepositPubChain3 = parentExtPubKey.Derive(1003);
           
            BitcoinExtPubKey extendedDepositPubKey1 = serverDepositPubChain1.Derive(index);
            BitcoinExtPubKey extendedDepositPubKey2 = serverDepositPubChain2.Derive(index);
            BitcoinExtPubKey extendedDepositPubKey3 = serverDepositPubChain3.Derive(index);

            var template = new PayToMultiSigTemplate();
            Script scriptPubKey = template.GenerateScriptPubKey(2, new[] { extendedDepositPubKey1.ExtPubKey.PubKey, extendedDepositPubKey2.ExtPubKey.PubKey, extendedDepositPubKey3.ExtPubKey.PubKey });
            return scriptPubKey.GetAddress(Network.Main);




Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: DeathAndTaxes on July 07, 2014, 09:31:13 PM
On an unrelated issue.  There are some unspendable but still valid outputs in the UTXO which the library does not seem to handle properly.  The constructor for the Script class should probably check for cases where there is a OP_PUSHDATA1, OP_PUSHDATA2, or OP_PUSHDATA4 and insufficient bytes to complete the push. A good test case is TxID  ebc9fa1196a59e192352d76c0f6e73167046b9d37b8302b6bb6968dfd279b767 as it is a complete mess.

For example index 4 has a PkScript (in hex) of 0x4d.  Just an OP_PUSHDATA2 with no data following.  This code runs infinitely

Code:
            var script = new Script(new Byte[]{0x4d}); // raw bytes from ebc9fa1196a59e192352d76c0f6e73167046b9d37b8302b6bb6968dfd279b767:4
   string asm = scrip.ToString();

Yeah it would have been nice if since the genesis blocks invalid pushes produced invalid txns and thus this txn would never have made it into a block.  I haven't had a chance to take a close look but I believe the issue is in Op.ReadData().

Quote
      internal static byte[] ReadData(Op op, Stream stream, bool ignoreWrongPush = false)
      {
         var opcode = op.Code;
         uint len = 0;
         BitcoinStream bitStream = new BitcoinStream(stream, false);
         if(opcode == 0)
            return new byte[0];

         if((byte)OpcodeType.OP_1 <= (byte)opcode && (byte)opcode <= (byte)OpcodeType.OP_16)
         {
            return new byte[] { (byte)(opcode - OpcodeType.OP_1 + 1) };
         }

         if(opcode == OpcodeType.OP_1NEGATE)
         {
            return new byte[] { 0x81 };
         }

         if(0x01 <= (byte)opcode && (byte)opcode <= 0x4b)
            len = (uint)opcode;
         else if(opcode == OpcodeType.OP_PUSHDATA1) //fails to return if there is <1 bytes after opcode
            len = bitStream.ReadWrite((byte)0);
         else if(opcode == OpcodeType.OP_PUSHDATA2) //fails to return if there is <2 bytes after opcode
            len = bitStream.ReadWrite((ushort)0);
         else if(opcode == OpcodeType.OP_PUSHDATA4) //fails to return if there is <4 bytes after opcode
            len = bitStream.ReadWrite((uint)0);
         else
            throw new FormatException("Invalid opcode for pushing data : " + opcode);

         byte[] data = new byte[len];
         var readen = stream.Read(data, 0, data.Length);
         if(readen != data.Length && !ignoreWrongPush)
            throw new FormatException("Not enough bytes pushed with " + opcode.ToString() + " expected " + len + " but got " + readen);
         else if(readen != data.Length)
         {
            op.IncompleteData = true;
            Array.Resize(ref data, readen);
         }
         return data;
      }


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on July 08, 2014, 11:44:34 AM
You are correct, I will fix this bug tonight, you'll just have to Update-Package NBitcoin

I accept invalid script as parameters, because the coinbase script is arbritrary data.
Trying to .ToString an invalid script can't return something sensible, but at least, I will add a Script.IsValid on the Script class, and make .ToString() terminate correctly.



Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on July 08, 2014, 05:57:37 PM
Here you go. (Also on github, so you can just merge with your clone)
Quote
Update-Package NBitcoin
http://docs.nuget.org/docs/start-here/using-the-package-manager-console (http://docs.nuget.org/docs/start-here/using-the-package-manager-console)

The new version is 1.4.0.9
I fixed the bug where invalid script can get you into infinite loop. (Did not renamed the Key and PubKey properties though, I will surely do it later on)


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: DeathAndTaxes on July 10, 2014, 01:17:43 AM
Nice taking a look at it now.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: TYDIRocks on July 10, 2014, 05:50:38 AM
Great work, I'm surprised I haven't seen more replies here, as I remember reading this thread a few months ago.

Once I start getting my lazy ass motivated on furthering my programming knowledge I'll definitely be using your library. I've always wanted to make my own Bitcoin client, no matter how basic it is.



Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on July 12, 2014, 04:54:07 PM
Great work, I'm surprised I haven't seen more replies here, as I remember reading this thread a few months ago.

Once I start getting my lazy ass motivated on furthering my programming knowledge I'll definitely be using your library. I've always wanted to make my own Bitcoin client, no matter how basic it is.



Spread the word ;)


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: ThePok on July 15, 2014, 11:51:09 AM
Oh i love it!

And so i donated :)

Finaly something that just works after hitting f5 :D


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on July 15, 2014, 05:15:24 PM
Oh i love it!

And so i donated :)

Finaly something that just works after hitting f5 :D

You forgot Install-Package NBitcoin ... except if you cloned the repo where it compiles directly with F5 :p
I never managed to compile the Bitcoin Core source code by the way... ;)
Thanks a lot for the tip !


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on July 15, 2014, 08:27:15 PM
Pushed new small version update, some helper methods ToString(Network) or static Parse methods to get and parse wif format instead of having to pass by Base58Data classes. (For ExtKey, ExtPubKey, and Key)
DeathAndTaxes, I renamed BitcoinExtKey.Key to ExtKey and same for the BitcoinPubExtKey.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: ThePok on July 16, 2014, 09:39:49 AM
Are you planning on including a actual Client to show off this nice BitcoinLib?


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on July 16, 2014, 06:08:48 PM
Actually, I'm a little busy to create an indexer.
So that will probably be a blockchain.info or blockexplorer.com backed by Microsoft Azure. (which is pure awesomeness)
I will release it entirely open source.

Then my second project will be a WPF client app to track Colored Coins emission and trading.

If you intend to create a client with NBitcoin, I'll be glad to help you if any problem.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: mcaizgk2 on July 16, 2014, 08:46:11 PM
Are you planning on including a actual Client to show off this nice BitcoinLib?

@ThePok I'm not sure whether your refer to BitcoinLib (https://github.com/GeorgeKimionis/BitcoinLib), but there's another thread for it: https://bitcointalk.org/index.php?topic=456322


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: TYDIRocks on July 17, 2014, 04:32:57 AM
Are you planning on including a actual Client to show off this nice BitcoinLib?

I have begun making a simple client using this library and will surely make it open source once it is completed more.

However, you can figure a lot out by playing with the library and checking out all the tests.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Zombie123 on July 21, 2014, 09:48:44 PM
Great Work I am reading the code now!!


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: ThePok on July 22, 2014, 02:55:06 PM
The Payloadclass for reject is missing :(

https://en.bitcoin.it/wiki/Protocol_specification#reject

Maybe add it when you have time :)


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on July 22, 2014, 02:56:56 PM
Making your own Payload is not complicated,
I'll add it, but if you are stuck because of that take a look at the implementation of others. It takes literally 5 minutes.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: ThePok on October 13, 2014, 08:36:35 PM
The lib now can handle ColoredCoins!


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on October 21, 2014, 11:09:03 PM
Yes from the OpenAsset protocol, I'm working right now on NBitcoin Indexer that will also support queries over colored coin wallets and balances ! :)
It already support indexing of ColoredTransactions.

Here some intro on the indexer (before I implemented CC) : http://www.codeproject.com/Articles/819567/NBitcoin-Indexer-A-scalable-and-fault-tolerant-blo (http://www.codeproject.com/Articles/819567/NBitcoin-Indexer-A-scalable-and-fault-tolerant-blo)


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: illodin on November 13, 2014, 06:03:56 AM
Exciting news:

http://news.microsoft.com/2014/11/12/microsoft-takes-net-open-source-and-cross-platform-adds-new-development-capabilities-with-visual-studio-2015-net-2015-and-visual-studio-online/
http://www.visualstudio.com/en-us/news/vs2013-community-vs.aspx


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on November 13, 2014, 12:15:35 PM
Exciting news:

http://news.microsoft.com/2014/11/12/microsoft-takes-net-open-source-and-cross-platform-adds-new-development-capabilities-with-visual-studio-2015-net-2015-and-visual-studio-online/
http://www.visualstudio.com/en-us/news/vs2013-community-vs.aspx

Exciting, I wonder what is the trick, because I don't see any reason to by a 5K $ msdn subscription for VS now...


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: thirdprize on December 16, 2014, 12:04:47 PM
If you ask, M$ will give you a free "Biz Spark" MSDN subscription, if you have a web site and pretend to be a start up.  I got one of the back of a couple of iPhone apps i wrote which i said i wanted to port to WinPhone. 

http://www.microsoft.com/bizspark/default.aspx


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: hhanh00 on December 28, 2014, 06:26:42 PM
Is it possible to run it as a full node implementation? Basically with tx relay and wallet maintenance.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on December 28, 2014, 08:14:59 PM
Is it possible to run it as a full node implementation? Basically with tx relay and wallet maintenance.


Yes it is, here to get started (assuming you run bitcoin core locally)
Obviously, you can also connect to other node as long as you know the IP.

Quote
using(var node = Node.ConnectToLocal(Network.Main))
{
   node.VersionHandshake();
   node.MessageReceived += (node,message) => //Do your stuff when new message here;
}

If you want to make a server,

Quote
using(var server = new NodeServer(Network.Main))
{
   server.Listen();
   server.NodeAdded += (s,node)=>//do stuff when node connect (Handshakes and Ping/Pong are handled automatically)
}


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: hhanh00 on December 29, 2014, 03:23:32 AM
Thanks for your quick response, another question - is there support for the maintenance of the mempool and the set of unspent outputs? I couldn't find where transactions are checked for double spends for example.



Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on December 29, 2014, 01:28:48 PM
Thanks for your quick response, another question - is there support for the maintenance of the mempool and the set of unspent outputs? I couldn't find where transactions are checked for double spends for example.



No, this is yours to do.
NodeServer and Node are the class to send message and respond to messages you receive. (as a server, or as a client)
I have not coded a full node with this, but someone can.

In my code, I usually use NodeServer for mocking a fake bitcoin server (for unit testing), and I use Node to get notified from new Transactions, blocks, get the Headers chain, and download blocks.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: hhanh00 on December 29, 2014, 02:28:32 PM
Got it - thanks


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on December 29, 2014, 02:44:23 PM
Got it - thanks

One advice, there is several test that are using these class.
If you have the xunit test running, they either the Trait "NodeServer" or "Network". It assume you have a local bitcoin instance on testnet.
You might learn how these class work with the tests.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: doof on January 29, 2015, 02:34:10 AM
Actually, I'm a little busy to create an indexer.
So that will probably be a blockchain.info or blockexplorer.com backed by Microsoft Azure. (which is pure awesomeness)
I will release it entirely open source.

Doing a similar thing.  PM me :)


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: doof on January 29, 2015, 02:40:19 AM
Is it possible to run it as a full node implementation? Basically with tx relay and wallet maintenance.


Yes it is, here to get started (assuming you run bitcoin core locally)
Obviously, you can also connect to other node as long as you know the IP.

Quote
using(var node = Node.ConnectToLocal(Network.Main))
{
   node.VersionHandshake();
   node.MessageReceived += (node,message) => //Do your stuff when new message here;
}

If you want to make a server,

Quote
using(var server = new NodeServer(Network.Main))
{
   server.Listen();
   server.NodeAdded += (s,node)=>//do stuff when node connect (Handshakes and Ping/Pong are handled automatically)
}

When trying to start as server, I'm getting this exception thrown on server.Listen()

An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in NBitcoin.dll

Additional information: Only one usage of each socket address (protocol/network address/port) is normally permitted

Any ideas?
 ::) I had bitcoind running using that port.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: DGulari on April 28, 2015, 01:24:05 PM
Is there a new thread somewhere?  How could 4 months go by without anyone discussing NBitcoin? 


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on April 29, 2015, 12:07:54 PM
Is there a new thread somewhere?  How could 4 months go by without anyone discussing NBitcoin?  
There was some about discussion on threads about Blockchain Programming in C# (https://blockchainprogramming.azurewebsites.net/) about it.
The rest happens on github/stackoverflow/mails. (no specific forum thread)


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: lontivero on July 07, 2015, 05:03:54 AM
If you take a look at the NBitcoin repo https://github.com/NicolasDorier/NBitcoin  (https://github.com/NicolasDorier/NBitcoin) you will see that it is a very active project. In fact, yesterday there was a new release with many improvements. For example, the latest version allows you to use the bitcoin core consensus library to validate transactions.

There are new features (see the TransactionBuilder and MoneyBag classes), improvements and, at the same time, NBitcoin is on sync with bitcoin core project. So, summarizing: there are a lot of news.      



Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: bananas on July 08, 2015, 07:59:37 AM
If you take a look at the NBitcoin repo https://github.com/NicolasDorier/NBitcoin  (https://github.com/NicolasDorier/NBitcoin) you will see that it is a very active project. In fact, yesterday there was a new release with many improvements. For example, the latest version allows you to use the bitcoin core consensus library to validate transactions.

There are new features (see the TransactionBuilder and MoneyBag classes), improvements and, at the same time, NBitcoin is on sync with bitcoin core project. So, summarizing: there are a lot of news.      



I wonder if it does have some kind of coin selector and a wallet file format?


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on July 08, 2015, 08:11:42 AM
Yes, you can check the book https://blockchainprogramming.azurewebsites.net/ (https://blockchainprogramming.azurewebsites.net/) to see how I to use coin selector to build transactions and a sample of SPV implementation on https://github.com/NicolasDorier/NBitcoin.SPVSample (https://github.com/NicolasDorier/NBitcoin.SPVSample)


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: piotrgal on January 14, 2016, 02:38:06 PM
Hi!

Assuming all the similarities between bitcoin and litecoin, would it be feasible / doable to adapt NBitcoin to also handle litecoin?
I mean not necessary in one codebase, but maybe as a child / side project.
How much work would that be?





Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: Nicolas Dorier on January 16, 2016, 03:37:57 PM
Yes it would be possible, but it depends on the differences. I don't know the differences though.
NBitcoin is only for Bitcoin though, but I'm not against adding things in the code to make it extensible for other altcoins.
I won't do it myself though, so I'm waiting PR.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: nopara73 on May 25, 2016, 07:12:53 AM
UPDATE: We have just came out with the second edition of NBitcoin's documentation, what is a book, called "Programming The Blockchain in C#". 
Now everything has been moved to GitHub, contributions are welcomed! 
https://github.com/ProgrammingBlockchain/ProgrammingBlockchain


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: doof on June 24, 2016, 05:48:37 AM
Is there any info on the Fee Policy implementation?  Not sure how to use it.  Im hoping it sets the miners fee in the TX builder.


Title: Re: NBitcoin : Almost full bitcoin implementation in .NET
Post by: IIpeBeD_MeDBeD on January 31, 2017, 12:39:16 PM
https://twitter.com/NicolasDorier/status/826382810478899200