forked from CUITBlockchain/blockchain-in-js-workshop-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6bbc3b
commit 5e9c74a
Showing
10 changed files
with
335 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { createHash } from 'crypto'; | ||
class MerkleTree { | ||
constructor(data) { | ||
this.leaves = data.map(item => this.hash(item)); | ||
this.tree = this.buildTree(this.leaves.slice()); | ||
this.root = this.tree[0]; | ||
} | ||
|
||
hash(data) { | ||
return createHash('sha256').update(data).digest('hex'); | ||
} | ||
|
||
buildTree(leaves) { | ||
let tree = leaves; | ||
while (tree.length > 1) { | ||
let parentLevel = []; | ||
for (let i = 0; i < tree.length; i += 2) { | ||
let left = tree[i]; | ||
let right = tree[i + 1] || left; | ||
parentLevel.push(this.hash(left + right)); | ||
} | ||
tree = parentLevel; | ||
} | ||
return tree; | ||
} | ||
|
||
// 添加节点 | ||
addNode(data) { | ||
this.leaves.push(this.hash(data)); | ||
this.tree = this.buildTree(this.leaves.slice()); | ||
this.root = this.tree[0]; | ||
} | ||
|
||
// 删除节点 | ||
removeNode(data) { | ||
const hashedData = this.hash(data); | ||
const index = this.leaves.findIndex(leaf => leaf === hashedData); | ||
if (index !== -1) { | ||
this.leaves.splice(index, 1); | ||
this.tree = this.buildTree(this.leaves.slice()); | ||
this.root = this.tree[0]; | ||
} | ||
} | ||
|
||
// 验证函数 | ||
verify(data) { | ||
const hashedData = this.hash(data); | ||
let tree = this.leaves.slice(); | ||
while (tree.length > 1) { | ||
let parentLevel = []; | ||
for (let i = 0; i < tree.length; i += 2) { | ||
let left = tree[i]; | ||
let right = tree[i + 1] || left; | ||
if (hashedData === left || hashedData === right) { | ||
return true; | ||
} | ||
parentLevel.push(this.hash(left + right)); | ||
} | ||
tree = parentLevel; | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
const data = ['A', 'B', 'C', 'D']; | ||
const merkleTree = new MerkleTree(data); | ||
console.log('Merkle Root:', merkleTree.root); | ||
|
||
// 验证数据块 'A' 是否在 Merkle Tree 中 | ||
console.log('Verify A:', merkleTree.verify('A')); // 应该输出 true | ||
|
||
// 添加一个新的数据块 'E' | ||
merkleTree.addNode('E'); | ||
console.log('Merkle Root after adding E:', merkleTree.root); // Merkle Root 应该发生变化 | ||
|
||
// 删除数据块 'E' | ||
merkleTree.removeNode('E'); | ||
console.log('Merkle Root after removing E:', merkleTree.root); // Merkle Root 应该恢复到最初的值 | ||
|
||
export default MerkleTree |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import sha256 from 'crypto-js/sha256.js' | ||
import t from "ramda/src/T.js"; | ||
|
||
|
||
class Transaction { | ||
constructor(miner, receiverPubKey, value, hash) { | ||
this.miner = miner | ||
this.receiverPubKey = receiverPubKey | ||
this.value = value | ||
this.hash = hash || this._calculateHash() | ||
} | ||
|
||
// 更新交易 hash | ||
_setHash() { | ||
this.hash = this._calculateHash() | ||
} | ||
|
||
// 计算交易 hash 的摘要函数 | ||
_calculateHash() { | ||
return sha256(this.miner + this.receiverPubKey + this.value).toString() | ||
} | ||
} | ||
|
||
export default Transaction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.