5 ms·
I wrote a toy (but working) implementation of the Bitcoin algorithm here: https://github.com/tlrobinson/tomcoin https://github.com/tlrobinson/tomcoin The main
by tlrobinson 6y ago
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.js https://github.com/tlrobinson/tomcoin/blob/master/src/node.j...
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 6y agoconst { 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()?
- robjan 6y agoYou can see what your functions do by opening the file called util.js in the same directory. And, yes you can run this in the browser by either polyfilling "require" or transpiling it
- TekMol 6y agoBy that logic, everything runs in a browser. The principle of computational equivalence tells us that one turing complete language can do what any other can. So yes, everything can be polyfilled or transpiled. But that does not really meet my understanding of "Runs in the browser".
- sillysaurusx 6y agoIf you run browserify on an index.js file, it will usually spit out a single .js file that will run in the browser.
- tlrobinson 6y agoWhen I open https://tomcoin.herokuapp.com/ https://tomcoin.herokuapp.com/ in a browser I see it running. That meets my definition of "runs in the browser". Would you have been happier if I wrote "written in a dialect of JavaScript which is transpiled to ES5 using Webpack and Babel so it can run in the browser"?
- ric2b 6y agoComputational equivalence doesn't include things like networking and resource access if you run in a sandboxed environment.
- quickthrower2 6y agoEs6 destructuring- should run in most modern browsers although I’m a Es5 transpiler fan still!
- MuffinFlavored 6y agoMost modern browsers have require()? I thought they only have import?
- monokh 6y agoThis is great. I especially like that it follows the protocol's implementation for the most part. i.e. you still have utxos and script verification even if it doesn't actually support the array of script op codes. There is a fully compatible JavaScript implementation that you could use as reference to extend yours: https://github.com/bcoin-org/bcoin https://github.com/bcoin-org/bcoin With my post, I tried to boil down the concepts as much as possible to save some mental bandwidth and make understanding the overall easier. We will see how that turns out