Looking for a simple C++ version of the bitcoin protocol

15 replies 184 views
Posts: 28 · Reputation: 229
#1Mar 2, 2024, 04:25 AM
Hey, I'm on the lookout for a simple C++ implementation of the original bitcoin protocol. By that, I mean just the basics like legacy addresses and maybe some basic multisig stuff. I just need what's essential to create a functioning bitcoin chain. I've been searching around on GitHub for a while but came up empty. Most of the stuff I find is either like btcd, which has a bunch of extra features, or just a small blockchain.
5 Reply Quote Share
im_apeHero Member
Posts: 629 · Reputation: 3824
#2Mar 2, 2024, 09:36 AM
What exactly are you trying to do and what parts of the libraries do you find non-minimalistic and why is that a problem?  A Bitcoin library is either incomplete which is not a good idea to use or is a full implementation of Bitcoin Protocol. It would be pointless for someone to implement like half of the protocol. In any case since you mentioned "original protocol", here is the bitcoin core's first version that can be found on the internet: https://github.com/benjiqq/bitcoinArchive maybe that helps.
1 Reply Quote Share
Posts: 28 · Reputation: 229
#3Mar 2, 2024, 11:25 AM
Well the original early versions are GUI versions with outdated libraries. Any GUI version is not minimalistic... It's not a problem, i just want a minimalistic implementation
5 Reply Quote Share
coin777Senior Member
Posts: 143 · Reputation: 970
#4Mar 4, 2024, 07:23 AM
Then, just take version 0.1.0, and remove things, which you don't need. For example: instead of "scriptSig" and "scriptPubKey", you only need "sig" and "pubKey", and ECDSA implementation. I guess the bare minimum is hashing, so you can start from SHA-256 implementation (because then, you can form a chain). The next thing is public key cryptography, which means secp256k1 implementation. If you have those two, then you can form the basic chain. If you ignore everything else, then you will have "downgraded soft-fork", where your version will only check P2PK, and accept everything else as valid. I was trying to do something like that, but it is not yet ready. I guess it is about recreating the process of making Bitcoin from scratch. Because between empty main function, and the first released version, there are definitely more points, where you have "half-baked coin", and where you can learn, why things are set, the way they are. For example: the bare minimum doesn't need any Script. The bare minimum doesn't need versioning. And there are many other things, which you can remove, and still cover everything, what is described in the whitepaper. So, to sum up: start from SHA-256 implementation, which will give you just "something mineable, without any public keys". Then, add public keys support. And then, you will have the bare minimum. And if you want to test some basic concepts, then you can even downgrade it into SHA-1 with secp160k1, or even CRC-32 with some 32-bit elliptic curve, if you want some weaker test network.
1 Reply Quote Share
hash_bossLegendary
Posts: 1166 · Reputation: 5261
#5Mar 5, 2024, 10:31 PM
Have you come across https://github.com/piotrnar/gocoin? The newest version have wallet, CLI and web UI, but the older version only have CLI and wallet where the wallet's code is separated from full node client. In addition, gocoin is created by only one person, so i expect he make things as simple as possible to make it easier to maintain. But take note that gocoin simply load entire UTXO into RAM.
3 Reply Quote Share
Posts: 28 · Reputation: 229
#6Mar 5, 2024, 10:59 PM
That's something quite close to what I was looking for. Thank you so much!
2 Reply Quote Share
LuckyCoinLegendary
Posts: 832 · Reputation: 4795
#7Mar 6, 2024, 12:22 AM
There is also a similar client written in Python called pycoin, but it's no longer maintained. https://pypi.org/project/pycoin/ Actually, not even 0.1.0. In fact even versions like 0.2 - 0.6 would be suitable as well, not to mention OP will be able to see things like P2PKH and even be able to sync it to the chain fully (at least 0.4.something and up [I forgot exactly which version is the minimum but achow wrote it somewhere]).
1 Reply Quote Share
Posts: 28 · Reputation: 229
#8Mar 6, 2024, 03:14 AM
I think it's better off to start with the qt versions rather than the wxwidgets versions actually, but anyhow these old bitcoin source codes are not as good looking (in the sense of code readability) as a project like gocoin. I actually found out that gocoin doesn't verify the block's hash values, it just trusts whoever sends them. I think there are many more issues with gocoin i'm yet to find. So i choose to go with btcd, I'm now stripping it down to the bare minimum.
4 Reply Quote Share
im_apeHero Member
Posts: 629 · Reputation: 3824
#9Mar 6, 2024, 05:46 AM
What do you mean? Gocoin is a full node which means it should verify the blocks it receives (the hash alone is not verified, the entire block is) and a quick look at the code seems like it does have the block verification code https://github.com/piotrnar/gocoin/blob/master/lib/chain/block_check.go And you can see it is verifying everything including but not limited to PoW, version, weight, transactions, etc.
1 Reply Quote Share
LuckyCoinLegendary
Posts: 832 · Reputation: 4795
#10Mar 6, 2024, 06:42 AM
Alright, but just a warning about the Qt builds: I don't know when they started building with Qt5, but old builds using Qt4 (if any) are going to be extremely hard to compile since most distros stopped shipping Qt4 libraries in their repos. You're better off using an older distribution in that case. Happened to me while I was trying to run Armory (a bitcoin wallet) so this could be relevant here.
2 Reply Quote Share
Posts: 28 · Reputation: 229
#11Mar 7, 2024, 11:25 PM
Thanks for the information, I am currently working on some code based on btcd, however.
4 Reply Quote Share
alex.coinMember
Posts: 13 · Reputation: 193
#12Mar 8, 2024, 01:16 AM
https://github.com/chjj/mako
3 Reply Quote Share
Posts: 43 · Reputation: 203
#13Mar 8, 2024, 07:01 AM
without scriptsig you'll have no first block. https://github.com/alexeyneu/BlockZero/blob/master/BlockZero.cpp#L73-L77
1 Reply Quote Share
sage777Full Member
Posts: 102 · Reputation: 305
#14Mar 8, 2024, 07:28 AM
Why not? You can for example detect, if something is P2PK, and then handle it. And in all other cases, you can just assume it as valid. Then, it will be compatible with the rest of the network, just like a downgraded soft-fork. That's why old nodes have no Taproot or Segwit, and can still follow the same chain. And you can do that too, and throw away the whole Script implementation, then hardcode just some patterns for P2PK, and implement only ECDSA, and nothing else.
2 Reply Quote Share
Posts: 43 · Reputation: 203
#15Mar 9, 2024, 02:14 PM
code i've posted is a collective work of 10 or so people. And it worked only after ~15 tries where we just wanna to reverse engineer satoshi stuff having easy way to test if it does right or not. where you offer to write his own .... I think you can guess the rest . All other projects eth included just copied this part with no-brainer (you can ask buterin why he'd used nbits = 486604799 for eth block #0).
0 Reply Quote Share
GrimMoonMember
Posts: 17 · Reputation: 163
#16Mar 9, 2024, 07:56 PM
This is the most reasonable approach in my opinion
0 Reply Quote Share

Related topics