This project is a toy project aimed at simulating a simple UTXO-based blockchain.
It is implemented in TypeScript and provides the following features:
- Generating private keys, public keys, and addresses through a wallet
- UTXO model (transaction inputs and outputs)
- ECDSA signing and verification
- Simple P2P simulation
- Proof-of-Work mining (block hash and difficulty)
- Coinbase transaction
dive-into-bitcoin/
├── src/
│ ├── main.ts
│ ├── core/
│ │ ├── blockchain.ts
│ │ ├── block.ts
│ │ ├── utxo.ts
│ │ ├── miner.ts
│ │ ├── p2pnode.ts
│ │ └── wallet.ts
│ ├── utils/
│ │ └── crypto_utils.ts
│ └── config/
│ └── index.ts
├── package.json
└── tsconfig.json
Install typescript and ts-node
npm install -g typescript
npm install -g ts-nodeInstall dependencies
yarnRun the main file
ts-node src/main.tsManages the entire state of the blockchain and provides various functions. It handles adding blocks and validating the chain.
Defines the block structure as an interface.
Implements the UTXO (Unspent Transaction Output) model.
- TxIn: An input transaction that uses coins by referencing a specific output of a previous transaction
- TxOut: Ownership of newly created coins
- Transaction: Has one or more
TxInandTxOut, and has a unique transaction hash
Implements a simple Proof-of-Work mining logic.
Calculates the block hash repeatedly to meet a certain DIFFICULTY.
A node class that handles the P2P simulation.
Generates and stores the user's private key, public key, and address.
Uses the secp256k1 elliptic curve from the elliptic library to create key pairs and sign data.
Provides utility functions for hashing (SHA-256), signing, and verification.
Calculates block hashes with (createBlockHash) and verifies transactions with (verifyTxIn).