
Bitcoin From Scratch – Part 1 - monokh
https://monokh.com/posts/bitcoin-from-scratch-part-1
======
tlrobinson
I wrote a toy (but working) implementation of the Bitcoin algorithm here:
[https://github.com/tlrobinson/tomcoin](https://github.com/tlrobinson/tomcoin)

The main logic is a few hundred lines of code:
[https://github.com/tlrobinson/tomcoin/blob/master/src/node.j...](https://github.com/tlrobinson/tomcoin/blob/master/src/node.js)

A somewhat unique aspect is since it's written in JavaScript a node can run on
the server or the browser, which makes for easy demos:

1\. Open [https://tomcoin.herokuapp.com/](https://tomcoin.herokuapp.com/) and
[https://tomcoin1.herokuapp.com/](https://tomcoin1.herokuapp.com/) in two
browser windows (the private key is stored in localStorage so using different
domains ensures each gets a different wallet)

2\. Click "Start Mining" on one or both nodes

3\. Once you've mined some TomCoin (should just take a few seconds unless more
people start mining) copy the public key from one node to the other's
"publicKey" field, enter an amount, and click "Transfer"

4\. Wait for another block to be mined and you should see the balances
transfer

(There's no persistence so the chain will reset when all the Heroku nodes idle
out and other nodes close)

(It also just uses WebSockets to connect to the instances on Heroku, or in
theory other server instances people are running. I meant to implement WebRTC
for P2P between browser nodes but never got around to it)

~~~
TekMol

        const {
          mine,
          sign,
          verify,
          hash,
          validateProofOfWork,
          stripTxSignatures,
          generatePrivateKey,
          getPublicKey
        } = require("./util");
    

What does this do?

And will this really run in a browser? I think brosers don't have require()?

~~~
quickthrower2
Es6 destructuring- should run in most modern browsers although I’m a Es5
transpiler fan still!

~~~
MuffinFlavored
Most modern browsers have require()? I thought they only have import?

------
nemo1618
> The PoW is suffice if the hash begins with a certain number of 0s

This is a common explanation of PoW, but is actually incorrect. If you think
about it, this would mean that the PoW difficulty could only increase (or
decrease) by a factor of two. In reality, the block hash is simply interpreted
as a (very large) number, and this number must be less than some other very
large number (the "target"). So you _do_ end up with a lot of leading zeros --
but these are just a side effect, not the thing being measured.

~~~
monokh
(author here) Absolutely. Thanks for pointing this out. If difficulty adjusted
in factors of two, the ability of the network to accurately maintain its 10
mins blocktime become impaired.

I try to keep these concepts rather simplified to ease the reading and
learning process. Perhaps there should be a note for these situations.

~~~
sowbug
In defense of the explanation, sometimes it's asking for an unnecessary mental
leap of the reader to think of a bit array as a number, especially if the
reader is not accustomed to thinking in base-2 or base-16 number systems.

~~~
nullc
The hash is just a number regardless-- surely no one has any problem
understanding any other number in the protocol as a number! :)

~~~
bouncycastle
Even this sentence is just a number.

~~~
yaktubi
Really it’s not because it’s a sequence of glyphs first and foremost. It can
be mapped to a number though.

~~~
bouncycastle
Nope, no mapping necessary. 36 octets, so a single 288 bit number in base 2.
(Big-endian, unsigned, I guess) Then convert to any base you want.

(I should have probably used 32 octets so that it fits neatly in 256 bits)

~~~
yaktubi
Isn’t that a mapping? Because the primary purpose of the string of glyphs is
readable by humans. Such representation is mapped to the numerical
representation for storage in the computer. It may also be mapped to lines of
ink written on a piece of paper for transmission

~~~
bouncycastle
Oh, in my example, it's just a coincidence that the number was readable by
humans! Other numbers may come out as complete gibberish.

The "mapping" you may be referring to is happening outside and subject to how
your brain /your browser interpret it. (How do you know that when I posted the
comment, that I didn't supply the raw bits of the number?! ;-))

Anyway, it was just a joke for low level-programmers I guess. Have a good day
;-)

------
ggrrhh_ta
For people that need or want to know (really understand) in depth about
Bitcoin without any previous background in Computer Science (for example, in
Finance, investors, journalists, historians, curious people in general) I have
to recommend Ted Nelson's "How Bitcoin actually works" (2014) from his series
"Computers for Cynics" [0]. I enjoyed it. (+)

[0] [https://youtu.be/3CMucDjJQ4E](https://youtu.be/3CMucDjJQ4E)

(+) For the people with background in CS: it might be that the video at some
point talks about the number of zeros instead of the comparison, but really,
it does not detract a 'bit' from its value.

edit: formatting

------
obilgic
Here is a simple bitcoin implementation in ruby including the networking for
interested, that I did some time ago:

[https://github.com/oguzbilgic/zincir](https://github.com/oguzbilgic/zincir)

------
ozankabak
Clear, concise and to the point. Looking forward to reading the rest. If you
finish the whole series and fix/improve the posts in this sequence using the
feedback from here, I think it might serve as one of the go-to pages for
technically inclined people who are interested to learn about Bitcoin and
blockchains.

~~~
person_of_color
Except they’re polarising as hell. Expect someone to chime in and state how
they are a complete waste of time and energy.

~~~
dang
That's generally true. The trouble is that your post more or less does to the
thread what you're predicting that people will do to the thread—the polarity
of the high-order bit doesn't matter much. It's best to abstain.

[https://news.ycombinator.com/newsguidelines.html](https://news.ycombinator.com/newsguidelines.html)

------
aciswhat
[https://github.com/lhartikk/naivechain](https://github.com/lhartikk/naivechain)
is a rather decent introductory implementation as well.

------
ur-whale
Love this way of learning about tech. : build one yourself (the truly noble
way would be to try and build without having seen the specs, just from a
description of what it does, but that'd take ages).

However: when it comes to implementations, the real hard part of the whole
Bitcoin affair is where the gnarly details hide, namely consensu, and it is
missing at this point.

------
rammy1234
Mining part is confusing for me. Is POW is essentially to achieve difficulty
level ? You do in this while loop until you have achieved this difficulty
level ? Is this the primary factor that differentiates having various protocol
in crypto ?

~~~
jstanley
Yes. Proof of Work is required so that there is a global limit on the rate at
which new coins are created.

~~~
monokh
It also:

\- Acts as the primary mechanism to make the data in the chain immutable, as
the cumulation of the work (chain of valid blocks) becomes harder and harder
to reproduce.

\- Anti spam, i.e. you need to put something at stake (electricity and
hardware cost) to participate in the issuance and transaction ordering

------
cchance
Please finish this series! Especially the networking and consensus models it's
an interesting read, really like that it's in Rust as I'm also learning Rust
so it's nice to see the concepts in that language :)

------
fogetti
> the transaction needs to be "Signed" by the `from` public key

Is that so? The code actually signs with the private key. Although I might
have misunderstood what that sentence refer to.

~~~
monokh
Thanks, that was a mistake. I've corrected it.

------
mmahemoff
Thanks for this info Monokh. Just want to raise a minor typo, which is the
first line of code in "Signed transactions". I think it's missing two of the
map keys.

------
shuringai
I really like the intention and good will behind the post but almost every
part is incorrect (even on an abstract level not restricting to bitcoin). I
feel like OP is yet another someone who needed the keyword "blockchain expert"
on his/her linkedin profile and trying to justify it now with misleading and
incomplete information.

~~~
tluyben2
This is HN; you are supposed to point out (like some people have in this
thread about the leading 000 count) the incorrect parts, providing some links
or explanation why they are incorrect. Many people probably clicked here to
learn something and so it's helpful to point out what they should be learning
instead of this if it is 'incorrect'.

------
Koshkin
It would be nice to be told what programming language this page is using.

------
deevolution
Bookmarking this one.

