Goothereum: Cryptocurrency in 160 lines of JavaScript

Build your own blockchain currency in a matter of minutes.

Introduction

  • Transaction: In this section I will describe basic transaction structures and how they are signed and verified.
  • Block: This section will be dedicated to transaction blocks — how they are linked and mined.
  • State: In this section we are going to implement a small Ethereum-like state container.
  • Blockhain: Final section will tie everything up and create a proper block and transaction verification mechanisms.

Wallet

  • Wallet Creator: Generates a new asymmetric key pair and turns it into a brand new wallet with properly populated fields.
  • Wallet Converters: Utility that converts raw HEX keys stored in the wallet into a PEM-encoded certificates used by Node. Long mysterious constant is an ASN-encoded binary header used to tag private and public parts of the resulting key.

Transaction

  • To: Receiver wallet.
  • Value: Amount of the Goo that is going to be taken from the sender account and sent to the receiver.
  • Nonce: Number of successful transactions made by the sender before, used to prevent multiple usage of the same transaction.
  • Signature: Digital signature made by signing transaction body hash with a sender private key.
  • Sign: Consumes sender private key and returns a new transaction with a digital signature based on it.
  • Test: Uses sender public key to verify transaction signature status and returns it.

Block

  • State Hash: Contains the hash of the latest state just before block was pushed into the chain.
  • Miner: Address that should be associated with the block miner, reward is going to be sent here.
  • Nonce: A completely meaningless value produced by mining — block hash starts with a required number of zeroes with it.
  • Transactions: List of transactions packed into the block and ordered by their nonce.
  • Mine: Synchronous miner implementation — increments nonce value until the proof of work condition is met. Returns block with an appropriate nonce as a result.
  • Test: Returns mining status defined by counting number of leading zeroes in its hash. Does not check transactions, needs to be performed manually.

State

  • Hash: Iterates over existing accounts and uses them to get the current state hash.
  • With (for transactions): Consumes transaction and returns updated state — with a proper coin amount transferred from one account to another.
  • With (for blocks): Consumes block and returns updated state — with appropriate reward sent to the the miner account. Does not process block transactions.

Blockchain

  • Block verifier: Compares block parent and state hashes with current ones, checks mining status and runs verification process for its transactions.
  • Balance: Returns current account balance if it exists, returns zero balance otherwise.
  • Push: Verifies block and pushes it into the current blockchain by updating its state and history.
  • Mine: A handy wrapper for the previous method that automatically mines block and pushes it into the blockchain.

Conclusion

We have a small fully functional cryptocurrency now. Lets take a closer look at the results:

  • Goothereum can create, sign and verify transactions.
  • Goothereum is able to pack transactions into blocks and mine them.
  • Goothereum can track state changes and present them in a handy way.
  • Goothereum fits into 160 lines of uncompressed JavaScript code.
  • Goothereum doesn’t use merke trees for hashing state.
  • Goothereum uses public key as wallet address instead of hashing it.
  • Goothereum has no network functionality — at least, yet.

Developer @ Kiev, Ukraine. Check my site for details: https://marlow.dev

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store