• Stars
    star
    179
  • Rank 214,039 (Top 5 %)
  • Language
    JavaScript
  • Created about 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Project logo

LuotuoCoin

GitHub Issues GitHub Pull Requests Downloads HitCount License


⚠️ 本项目仅作为了解区块链及区块链入门教学使用 For educational purpose only

测试覆盖率(Test Coverage)

File % Stmts % Branch % Funcs % Lines Uncovered Line #s
All files 96.15 88.46 100 96.05
blockChain.js 96.15 88.46 100 96.05 76,133,168

功能(Features)

  • 工作量证明机制(Proof of Work, PoW)的简单实现, A simple implementation of the Proof of Work algorithm.
  • 区块链的验证, Block chain validation to prevent it from being maliciously tampered.
  • Transaction 的签名. Sign transactions.
  • 钱包地址生成. Wallet generation.
  • 挖矿. Mining

🦊 开始 Let's get started

祝学习愉快。 Happy Learning. All the demo can be found in bilibiliDemo folder. 所有的视频里的示例都可以在 bilibiliDemo 的文件夹里找到

安装依赖(Install dependencies)

npm install --save
yarn

如果作为依赖安装, it's published to npm, so if you want to install it as an dependency, please use

npm install --save luotuocoin
or
yarn add luotuocoin

运行测试用例(Run tests)

npm run test
or
yarn test

生成一个密钥对(Generate your wallet)(bilibiliDemo/encryption/keygen.js)

密钥对讲用于发起转账,公钥(public key)会用作你的钱包地址,私钥(private key)会用来生成转账的数字签名。 The keypair will be used for signing transactions(private key) and receving miner rewards/transactions, public key will be your wallet address.

const ecLib = require("elliptic").ec;
const ec = new ecLib("secp256k1"); // curve name
const sha256 = require("crypto-js/sha256");
// 生成我们的密钥对 generate the keypair
const key = ec.genKeyPair();
// 拿到我们的私钥和公钥 希望拿到hex string格式的
console.log("private", key.getPrivate("hex"));
console.log("public", key.getPublic("hex"));

创建区块链(Create Blockchain)

const { chain, transaction } = require("luotuocoin");
const myCoin = new chain();

发起转账(Create Transaction)

利用上述步骤生成的keypair object, use the keypair object generated above

// 发起一笔转给to 10块钱的交易
// create a transaction that transfer 10 coin to address 'to'
const transaction = new Transaction(key.getPublic("hex"), "to", 10);
transaction.sign(key);
myCoin.addTransaction(transaction);

挖矿(Mine)

同样需要利用之前生成的 key object,将公钥作为参数传入挖矿的方法,如果你最后挖矿成功,区块链会向这个地址发放一个矿工奖励。 Using the same keypair object, passin the public key to receive the minder reward.

myCoin.mineTransactionPool(key.getPublic("hex"));

📽 视频教程(Video tutorial)

本项目均可在 b 站找到视频教程 我的 B 站区块链视频. 第一列视频均为理论讲解,第二列视频均为代码实现。

视频 1-1: 区块链概念讲解 视频 1-2: 简单区块链的实现
视频 2-1: 工作量证明机制(pow)概念讲解 视频 2-2: 整合 Proof of Work 到区块链
视频 3-1: 数字货币是怎么产生的 视频 3-2: 把区块链变成数字货币
视频 4-1: 比特币中的数字签名是什么 视频 4-2: 向区块链中添加数字签名