
Networks
bấm next qua 2 cái trên ảnh1. đáp án CALLBACK FUNCTIONS/** * Runs a callback function immediately * @param {function} callbackFunction */ function runCallback(callbackFunction) { callbackFunction(); } module.exports = runCallback; /** * Runs a callback function immediately * @param {function} callbackFunction */ function runCallback(callbackFunction) { setTimeout(callbackFunction, 1000); } module.exports = runCallback; class Dialog { onClose(callbackFunction) { this.carr = this.carr || []; th...

Sui Name Service ($SNS)
twitter :discord :Discord - Group Chat That's All Fun & GamesDiscord is great for playing games and chilling with friends, or even building a worldwide community. Customize your own space to talk, play, and hang out.https://discord.comIt's finally here! Join our Crew3 and earn your spot in the $SNS DAO ➤ http://snsdomains.crew3.xyz ➤ New Quests added Daily! 10 people to Like/RT this tweet will also be selected for the Stork role - Winners selected below. #Sui isn't ready. Let&a...

tương tác Harpie
liên kết víxem ví mình dùng cái nào thì chọn - ví hold token thì chọn cái trên , ví chơi nft thì chọn cái dưới hoặc chọn cả 2 cũng đcchọn cái nào muốn bảo vệđợi cho nó load xong và import token mạng eth hoặc nft nào mà mình muốn bảo vệadd bn token hay nft tùy thích - sau đó chọn protecttốn chút fee ethnhư vậy là xong bước 1tiếp theo chọn setupnhập địa chỉ ví - tốn fee eth - nhập 1 địa chỉ ví khác ví đang dùngtiếp theo đến bước 3chọn mint FAU - tốn fee ethBẤM protect faucet token - tốn fee eth...
vui vẻ

Networks
bấm next qua 2 cái trên ảnh1. đáp án CALLBACK FUNCTIONS/** * Runs a callback function immediately * @param {function} callbackFunction */ function runCallback(callbackFunction) { callbackFunction(); } module.exports = runCallback; /** * Runs a callback function immediately * @param {function} callbackFunction */ function runCallback(callbackFunction) { setTimeout(callbackFunction, 1000); } module.exports = runCallback; class Dialog { onClose(callbackFunction) { this.carr = this.carr || []; th...

Sui Name Service ($SNS)
twitter :discord :Discord - Group Chat That's All Fun & GamesDiscord is great for playing games and chilling with friends, or even building a worldwide community. Customize your own space to talk, play, and hang out.https://discord.comIt's finally here! Join our Crew3 and earn your spot in the $SNS DAO ➤ http://snsdomains.crew3.xyz ➤ New Quests added Daily! 10 people to Like/RT this tweet will also be selected for the Stork role - Winners selected below. #Sui isn't ready. Let&a...

tương tác Harpie
liên kết víxem ví mình dùng cái nào thì chọn - ví hold token thì chọn cái trên , ví chơi nft thì chọn cái dưới hoặc chọn cả 2 cũng đcchọn cái nào muốn bảo vệđợi cho nó load xong và import token mạng eth hoặc nft nào mà mình muốn bảo vệadd bn token hay nft tùy thích - sau đó chọn protecttốn chút fee ethnhư vậy là xong bước 1tiếp theo chọn setupnhập địa chỉ ví - tốn fee eth - nhập 1 địa chỉ ví khác ví đang dùngtiếp theo đến bước 3chọn mint FAU - tốn fee ethBẤM protect faucet token - tốn fee eth...
vui vẻ

Subscribe to meteosrds

Subscribe to meteosrds
Share Dialog
Share Dialog
<100 subscribers
<100 subscribers


chọn bài học ETH nhé


const { sha256 } = require("ethereum-cryptography/sha256");
const { toHex, utf8ToBytes } = require("ethereum-cryptography/utils");
// the possible colors that the hash could represent
const COLORS = ['red', 'green', 'blue', 'yellow', 'pink', 'orange'];
// given a hash, return the color that created the hash
function findColor(hash) {
hex = toHex(hash);
for (let i = 0; i < COLORS.length; i++) {
b = utf8ToBytes(COLORS[i]);
h = sha256(b);
if (toHex(h) === hex) {
return COLORS[i];
}
}
}
module.exports = findColor;


const { keccak256 } = require("ethereum-cryptography/keccak");
const { utf8ToBytes } = require("ethereum-cryptography/utils");
function hashMessage(message) {
return keccak256(utf8ToBytes(message));
}
module.exports = hashMessage;

const secp = require("ethereum-cryptography/secp256k1");
const hashMessage = require('./hashMessage');
const PRIVATE_KEY = "6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e";
async function signMessage(msg) {
return secp.sign(hashMessage(msg), PRIVATE_KEY, { recovered: true });
}
module.exports = signMessage;

const secp = require("ethereum-cryptography/secp256k1");
const hashMessage = require("./hashMessage");
async function recoverKey(message, signature, recoveryBit) {
return secp.recoverPublicKey(hashMessage(message), signature, recoveryBit);
}
module.exports = recoverKey;

const secp = require("ethereum-cryptography/secp256k1");
const { keccak256 } = require("ethereum-cryptography/keccak");
function getAddress(publicKey) {
return keccak256(publicKey.slice(1)).slice(-20);
}
module.exports = getAddress;
Proof of Work


const SHA256 = require('crypto-js/sha256');
const TARGET_DIFFICULTY = BigInt(0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
const MAX_TRANSACTIONS = 10;
const mempool = [];
const blocks = [];
function addTransaction(transaction) {
mempool.push(transaction);
}
function mine() {
}
module.exports = {
TARGET_DIFFICULTY,
MAX_TRANSACTIONS,
addTransaction,
mine,
blocks,
mempool
};

const SHA256 = require('crypto-js/sha256');
const TARGET_DIFFICULTY = BigInt(0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
const MAX_TRANSACTIONS = 10;
const mempool = [];
const blocks = [];
function addTransaction(transaction) {
mempool.push(transaction);
}
function mine() {
let block = { id: blocks.length };
blocks.push(block);
}
module.exports = {
TARGET_DIFFICULTY,
MAX_TRANSACTIONS,
addTransaction,
mine,
blocks,
mempool
};

const SHA256 = require('crypto-js/sha256');
const TARGET_DIFFICULTY = BigInt(0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
const MAX_TRANSACTIONS = 10;
const mempool = [];
const blocks = [];
function addTransaction(transaction) {
mempool.push(transaction);
}
function mine() {
let block = { id: blocks.length };
block.hash = SHA256(JSON.stringify(block));
blocks.push(block);
}
module.exports = {
TARGET_DIFFICULTY,
MAX_TRANSACTIONS,
addTransaction,
mine,
blocks,
mempool
};

const SHA256 = require('crypto-js/sha256');
const TARGET_DIFFICULTY = BigInt(0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
const MAX_TRANSACTIONS = 10;
const mempool = [];
const blocks = [];
function addTransaction(transaction) {
mempool.push(transaction);
}
function mine() {
let block = { id: blocks.length, transactions: [] };
if (mempool.length > 0) {
for (let i = 0; i < MAX_TRANSACTIONS; i++) {
block.transactions.push(mempool[0]);
mempool.shift();
if (mempool.length <= 0) {
break;
}
}
}
block.hash = SHA256(JSON.stringify(block));
blocks.push(block);
}
module.exports = {
TARGET_DIFFICULTY,
MAX_TRANSACTIONS,
addTransaction,
mine,
blocks,
mempool
};

const SHA256 = require('crypto-js/sha256');
const TARGET_DIFFICULTY = BigInt(0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
const MAX_TRANSACTIONS = 10;
const mempool = [];
const blocks = [];
function addTransaction(transaction) {
mempool.push(transaction);
}
function mine() {
let block = { id: blocks.length, transactions: [] };
if (mempool.length > 0) {
for (let i = 0; i < MAX_TRANSACTIONS; i++) {
block.transactions.push(mempool[0]);
mempool.shift();
if (mempool.length <= 0) {
break;
}
}
}
block.nonce = 0;
block.hash = SHA256(JSON.stringify(block));
let hash = block.hash;
while (BigInt(`0x${hash}`) >= TARGET_DIFFICULTY) {
block.nonce = block.nonce + 1;
block.hash = SHA256(JSON.stringify(block));
hash = block.hash;
}
blocks.push(block);
}
module.exports = {
TARGET_DIFFICULTY,
MAX_TRANSACTIONS,
addTransaction,
mine,
blocks,
mempool
};


const SHA256 = require('crypto-js/sha256');
class Block {
constructor() {
}
toHash() {
return SHA256("a string");
}
}
module.exports = Block;

const SHA256 = require('crypto-js/sha256');
class Block {
constructor(data) {
this.data = data;
}
toHash() {
return SHA256(this.data.toString());
}
}
module.exports = Block;

const Block = require('./Block');
class Blockchain {
constructor() {
this.chain = [new Block("Genesis")];
}
}
module.exports = Blockchain;

const Block = require('./Block');
class Blockchain {
constructor() {
this.chain = [new Block("Genesis")];
}
addBlock(block) {
this.chain.push(block);
}
}
module.exports = Blockchain;

cái này có 2 code ở 2 mục nhé
mục Blockchain.js
const Block = require('./Block');
class Blockchain {
constructor() {
this.chain = [new Block("Genesis")];
}
addBlock(block) {
block.previousHash = this.chain[this.chain.length - 1].toHash();
this.chain.push(block);
}
}
module.exports = Blockchain;
mục Block.js
const SHA256 = require('crypto-js/sha256');
class Block {
constructor(data) {
this.data = data;
}
toHash() {
if (this.previousHash) {
return SHA256(this.data.toString() + this.previousHash.toString());
} else {
return SHA256(this.data.toString());
}
}
}
module.exports = Block;

const Block = require('./Block');
class Blockchain {
constructor() {
this.chain = [new Block("Genesis")];
}
addBlock(block) {
block.previousHash = this.chain[this.chain.length - 1].toHash();
this.chain.push(block);
}
isValid() {
for (let i = 0; i < this.chain.length; i++) {
if ((i + 1) >= this.chain.length) {
break;
}
if (this.chain[i].toHash().toString() !== this.chain[i + 1].previousHash.toString()) {
return false;
}
}
return true;
}
}
module.exports = Blockchain;
Keeping Track of Blockchain User State


class TXO {
constructor(owner, amount) {
this.owner = owner;
this.amount = amount;
this.spent = false;
}
spend() {
this.spent = true;
}
}
module.exports = TXO;

class Transaction {
constructor(inputUTXOs, outputUTXOs) {
this.inputUTXOs = inputUTXOs;
this.outputUTXOs = outputUTXOs;
}
execute() {
for (let i = 0; i < this.inputUTXOs.length; i++) {
if (this.inputUTXOs[i].spent) {
throw new Error("already spent");
}
}
}
}
module.exports = Transaction;

class Transaction {
constructor(inputUTXOs, outputUTXOs) {
this.inputUTXOs = inputUTXOs;
this.outputUTXOs = outputUTXOs;
}
execute() {
for (let i = 0; i < this.inputUTXOs.length; i++) {
if (this.inputUTXOs[i].spent) {
throw new Error("already spent");
}
}
let inn = 0;
for (let i = 0; i < this.inputUTXOs.length; i++) {
inn = inn + this.inputUTXOs[i].amount;
}
let out = 0;
for (let i = 0; i < this.outputUTXOs.length; i++) {
out = out + this.outputUTXOs[i].amount;
}
if (inn < out) {
throw new Error("less than");
}
}
}
module.exports = Transaction;

class Transaction {
constructor(inputUTXOs, outputUTXOs) {
this.inputUTXOs = inputUTXOs;
this.outputUTXOs = outputUTXOs;
}
execute() {
for (let i = 0; i < this.inputUTXOs.length; i++) {
if (this.inputUTXOs[i].spent) {
throw new Error("already spent");
}
}
let inn = 0;
for (let i = 0; i < this.inputUTXOs.length; i++) {
inn = inn + this.inputUTXOs[i].amount;
}
let out = 0;
for (let i = 0; i < this.outputUTXOs.length; i++) {
out = out + this.outputUTXOs[i].amount;
}
if (inn < out) {
throw new Error("less than");
}
for (let i = 0; i < this.inputUTXOs.length; i++) {
this.inputUTXOs[i].spend();
}
}
}
module.exports = Transaction;

class Transaction {
constructor(inputUTXOs, outputUTXOs) {
this.inputUTXOs = inputUTXOs;
this.outputUTXOs = outputUTXOs;
}
execute() {
for (let i = 0; i < this.inputUTXOs.length; i++) {
if (this.inputUTXOs[i].spent) {
throw new Error("already spent");
}
}
let inn = 0;
for (let i = 0; i < this.inputUTXOs.length; i++) {
inn = inn + this.inputUTXOs[i].amount;
}
let out = 0;
for (let i = 0; i < this.outputUTXOs.length; i++) {
out = out + this.outputUTXOs[i].amount;
}
if (inn < out) {
throw new Error("less than");
}
for (let i = 0; i < this.inputUTXOs.length; i++) {
this.inputUTXOs[i].spend();
}
this.fee = inn - out;
}
}
module.exports = Transaction;


class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
module.exports = Node;

class Tree {
constructor() {
this.root = null;
}
}
module.exports = Tree;

class Tree {
constructor() {
this.root = null;
}
addNode(node) {
this.root = node;
}
}
module.exports = Tree;

class Tree {
constructor() {
this.root = null;
}
addNode(node) {
if (this.root === null) {
this.root = node;
} else if (this.root.data > node.data) {
this.root.left = node;
} else {
this.root.right = node;
}
}
}
module.exports = Tree;

class Tree {
constructor() {
this.root = null;
}
addNode(node) {
if (this.root === null) {
this.root = node;
} else {
let p = this.root;
while (true) {
if (p.data > node.data) {
if (p.left) {
p = p.left;
} else {
p.left = node;
break;
}
} else {
if (p.right) {
p = p.right;
} else {
p.right = node;
break;
}
}
}
}
}
}
module.exports = Tree;

class Tree {
constructor() {
this.root = null;
}
addNode(node) {
if (this.root === null) {
this.root = node;
} else {
let p = this.root;
while (true) {
if (p.data > node.data) {
if (p.left) {
p = p.left;
} else {
p.left = node;
break;
}
} else {
if (p.right) {
p = p.right;
} else {
p.right = node;
break;
}
}
}
}
}
hasNode(number) {
let n = this.root;
while (n) {
if (n.data === number) {
return true;
} else {
if (n.data > number) {
n = n.left;
} else {
n = n.right;
}
}
}
return false;
}
}
module.exports = Tree;


class MerkleTree {
constructor(leaves, concat) {
this.leaves = leaves;
this.concat = concat;
}
getRoot() {
return this.concat(this.leaves[0], this.leaves[1]);
}
}
module.exports = MerkleTree;

class MerkleTree {
constructor(leaves, concat) {
this.leaves = leaves;
this.concat = concat;
}
getRoot(next = this.leaves) {
while (true) {
if (next.length <= 1) {
return next;
}
let level = [];
for (let i = 0; i < next.length; i += 2) {
level.push(this.concat(next[i], next[i + 1]));
}
next = level;
}
}
}
module.exports = MerkleTree;

class MerkleTree {
constructor(leaves, concat) {
this.leaves = leaves;
this.concat = concat;
}
getRoot(next = this.leaves) {
while (true) {
if (next.length <= 1) {
return next;
}
let level = [];
for (let i = 0; i < next.length; i += 2) {
if ((i + 1) >= next.length) {
level.push(next[i]);
} else {
level.push(this.concat(next[i], next[i + 1]));
}
}
next = level;
}
}
}
module.exports = MerkleTree;

class MerkleTree {
constructor(leaves, concat) {
this.leaves = leaves;
this.concat = concat;
}
getRoot(next = this.leaves) {
while (true) {
if (next.length <= 1) {
return next;
}
let level = [];
for (let i = 0; i < next.length; i += 2) {
if ((i + 1) >= next.length) {
level.push(next[i]);
} else {
level.push(this.concat(next[i], next[i + 1]));
}
}
next = level;
}
}
getProof(index, next = this.leaves) {
let proof = [];
while (true) {
if (next.length <= 1) {
return proof;
}
let level = [];
for (let i = 0; i < next.length; i += 2) {
if ((i + 1) >= next.length) {
level.push(next[i]);
} else {
level.push(this.concat(next[i], next[i + 1]));
if (index === i || i === (index - 1)) {
if (index === i) {
proof.push({ data: next[i + 1], left: false });
} else {
proof.push({ data: next[i], left: true });
}
}
}
}
index = Math.floor(index / 2);
next = level;
}
}
}
module.exports = MerkleTree;

function verifyProof(proof, node, root, concat) {
let n = node;
proof.forEach((e, _) => {
if (e.left) {
n = concat(e.data, n);
} else {
n = concat(n, e.data);
}
});
if (n === root) {
return true;
} else {
return false;
}
}
module.exports = verifyProof;


có 2 mục nhé chú ý :
mục TrieNode.js
class TrieNode {
constructor(key) {
this.key = key;
this.children = {};
this.isWord = false;
}
}
module.exports = TrieNode;
mục Trie.js
const TrieNode = require('./TrieNode');
class Trie {
constructor() {
this.root = new TrieNode(null);
}
}
module.exports = Trie;

const TrieNode = require('./TrieNode');
class Trie {
constructor() {
this.root = new TrieNode(null);
}
insert(data) {
let n = this.root;
for (let i = 0; i < data.length; i++) {
let node = new TrieNode(data[i]);
n.children[data[i]] = node;
if ((i + 1) === data.length) {
node.isWord = true;
}
n = node;
}
}
}
module.exports = Trie;

const TrieNode = require('./TrieNode');
class Trie {
constructor() {
this.root = new TrieNode(null);
}
insert(data) {
let n = this.root;
for (let i = 0; i < data.length; i++) {
let node = n.children[data[i]];
if (!node) {
node = new TrieNode(data[i]);
}
n.children[data[i]] = node;
if ((i + 1) === data.length) {
node.isWord = true;
}
n = node;
}
}
}
module.exports = Trie;

const TrieNode = require('./TrieNode');
class Trie {
constructor() {
this.root = new TrieNode(null);
}
insert(data) {
let n = this.root;
for (let i = 0; i < data.length; i++) {
let node = n.children[data[i]];
if (!node) {
node = new TrieNode(data[i]);
}
n.children[data[i]] = node;
if ((i + 1) === data.length) {
node.isWord = true;
}
n = node;
}
}
contains(word) {
let n = this.root.children[word[0]];
if (n === null) {
return false;
}
let i = 1;
while (n) {
if (i >= word.length) {
if (n.isWord) {
return true;
} else {
return false;
}
}
n = n.children[word[i]];
if (n === null) {
return false;
}
i = i + 1;
}
}
}
module.exports = Trie;
next qua các phần k phải điền code
chọn bài học ETH nhé


const { sha256 } = require("ethereum-cryptography/sha256");
const { toHex, utf8ToBytes } = require("ethereum-cryptography/utils");
// the possible colors that the hash could represent
const COLORS = ['red', 'green', 'blue', 'yellow', 'pink', 'orange'];
// given a hash, return the color that created the hash
function findColor(hash) {
hex = toHex(hash);
for (let i = 0; i < COLORS.length; i++) {
b = utf8ToBytes(COLORS[i]);
h = sha256(b);
if (toHex(h) === hex) {
return COLORS[i];
}
}
}
module.exports = findColor;


const { keccak256 } = require("ethereum-cryptography/keccak");
const { utf8ToBytes } = require("ethereum-cryptography/utils");
function hashMessage(message) {
return keccak256(utf8ToBytes(message));
}
module.exports = hashMessage;

const secp = require("ethereum-cryptography/secp256k1");
const hashMessage = require('./hashMessage');
const PRIVATE_KEY = "6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e";
async function signMessage(msg) {
return secp.sign(hashMessage(msg), PRIVATE_KEY, { recovered: true });
}
module.exports = signMessage;

const secp = require("ethereum-cryptography/secp256k1");
const hashMessage = require("./hashMessage");
async function recoverKey(message, signature, recoveryBit) {
return secp.recoverPublicKey(hashMessage(message), signature, recoveryBit);
}
module.exports = recoverKey;

const secp = require("ethereum-cryptography/secp256k1");
const { keccak256 } = require("ethereum-cryptography/keccak");
function getAddress(publicKey) {
return keccak256(publicKey.slice(1)).slice(-20);
}
module.exports = getAddress;
Proof of Work


const SHA256 = require('crypto-js/sha256');
const TARGET_DIFFICULTY = BigInt(0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
const MAX_TRANSACTIONS = 10;
const mempool = [];
const blocks = [];
function addTransaction(transaction) {
mempool.push(transaction);
}
function mine() {
}
module.exports = {
TARGET_DIFFICULTY,
MAX_TRANSACTIONS,
addTransaction,
mine,
blocks,
mempool
};

const SHA256 = require('crypto-js/sha256');
const TARGET_DIFFICULTY = BigInt(0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
const MAX_TRANSACTIONS = 10;
const mempool = [];
const blocks = [];
function addTransaction(transaction) {
mempool.push(transaction);
}
function mine() {
let block = { id: blocks.length };
blocks.push(block);
}
module.exports = {
TARGET_DIFFICULTY,
MAX_TRANSACTIONS,
addTransaction,
mine,
blocks,
mempool
};

const SHA256 = require('crypto-js/sha256');
const TARGET_DIFFICULTY = BigInt(0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
const MAX_TRANSACTIONS = 10;
const mempool = [];
const blocks = [];
function addTransaction(transaction) {
mempool.push(transaction);
}
function mine() {
let block = { id: blocks.length };
block.hash = SHA256(JSON.stringify(block));
blocks.push(block);
}
module.exports = {
TARGET_DIFFICULTY,
MAX_TRANSACTIONS,
addTransaction,
mine,
blocks,
mempool
};

const SHA256 = require('crypto-js/sha256');
const TARGET_DIFFICULTY = BigInt(0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
const MAX_TRANSACTIONS = 10;
const mempool = [];
const blocks = [];
function addTransaction(transaction) {
mempool.push(transaction);
}
function mine() {
let block = { id: blocks.length, transactions: [] };
if (mempool.length > 0) {
for (let i = 0; i < MAX_TRANSACTIONS; i++) {
block.transactions.push(mempool[0]);
mempool.shift();
if (mempool.length <= 0) {
break;
}
}
}
block.hash = SHA256(JSON.stringify(block));
blocks.push(block);
}
module.exports = {
TARGET_DIFFICULTY,
MAX_TRANSACTIONS,
addTransaction,
mine,
blocks,
mempool
};

const SHA256 = require('crypto-js/sha256');
const TARGET_DIFFICULTY = BigInt(0x0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
const MAX_TRANSACTIONS = 10;
const mempool = [];
const blocks = [];
function addTransaction(transaction) {
mempool.push(transaction);
}
function mine() {
let block = { id: blocks.length, transactions: [] };
if (mempool.length > 0) {
for (let i = 0; i < MAX_TRANSACTIONS; i++) {
block.transactions.push(mempool[0]);
mempool.shift();
if (mempool.length <= 0) {
break;
}
}
}
block.nonce = 0;
block.hash = SHA256(JSON.stringify(block));
let hash = block.hash;
while (BigInt(`0x${hash}`) >= TARGET_DIFFICULTY) {
block.nonce = block.nonce + 1;
block.hash = SHA256(JSON.stringify(block));
hash = block.hash;
}
blocks.push(block);
}
module.exports = {
TARGET_DIFFICULTY,
MAX_TRANSACTIONS,
addTransaction,
mine,
blocks,
mempool
};


const SHA256 = require('crypto-js/sha256');
class Block {
constructor() {
}
toHash() {
return SHA256("a string");
}
}
module.exports = Block;

const SHA256 = require('crypto-js/sha256');
class Block {
constructor(data) {
this.data = data;
}
toHash() {
return SHA256(this.data.toString());
}
}
module.exports = Block;

const Block = require('./Block');
class Blockchain {
constructor() {
this.chain = [new Block("Genesis")];
}
}
module.exports = Blockchain;

const Block = require('./Block');
class Blockchain {
constructor() {
this.chain = [new Block("Genesis")];
}
addBlock(block) {
this.chain.push(block);
}
}
module.exports = Blockchain;

cái này có 2 code ở 2 mục nhé
mục Blockchain.js
const Block = require('./Block');
class Blockchain {
constructor() {
this.chain = [new Block("Genesis")];
}
addBlock(block) {
block.previousHash = this.chain[this.chain.length - 1].toHash();
this.chain.push(block);
}
}
module.exports = Blockchain;
mục Block.js
const SHA256 = require('crypto-js/sha256');
class Block {
constructor(data) {
this.data = data;
}
toHash() {
if (this.previousHash) {
return SHA256(this.data.toString() + this.previousHash.toString());
} else {
return SHA256(this.data.toString());
}
}
}
module.exports = Block;

const Block = require('./Block');
class Blockchain {
constructor() {
this.chain = [new Block("Genesis")];
}
addBlock(block) {
block.previousHash = this.chain[this.chain.length - 1].toHash();
this.chain.push(block);
}
isValid() {
for (let i = 0; i < this.chain.length; i++) {
if ((i + 1) >= this.chain.length) {
break;
}
if (this.chain[i].toHash().toString() !== this.chain[i + 1].previousHash.toString()) {
return false;
}
}
return true;
}
}
module.exports = Blockchain;
Keeping Track of Blockchain User State


class TXO {
constructor(owner, amount) {
this.owner = owner;
this.amount = amount;
this.spent = false;
}
spend() {
this.spent = true;
}
}
module.exports = TXO;

class Transaction {
constructor(inputUTXOs, outputUTXOs) {
this.inputUTXOs = inputUTXOs;
this.outputUTXOs = outputUTXOs;
}
execute() {
for (let i = 0; i < this.inputUTXOs.length; i++) {
if (this.inputUTXOs[i].spent) {
throw new Error("already spent");
}
}
}
}
module.exports = Transaction;

class Transaction {
constructor(inputUTXOs, outputUTXOs) {
this.inputUTXOs = inputUTXOs;
this.outputUTXOs = outputUTXOs;
}
execute() {
for (let i = 0; i < this.inputUTXOs.length; i++) {
if (this.inputUTXOs[i].spent) {
throw new Error("already spent");
}
}
let inn = 0;
for (let i = 0; i < this.inputUTXOs.length; i++) {
inn = inn + this.inputUTXOs[i].amount;
}
let out = 0;
for (let i = 0; i < this.outputUTXOs.length; i++) {
out = out + this.outputUTXOs[i].amount;
}
if (inn < out) {
throw new Error("less than");
}
}
}
module.exports = Transaction;

class Transaction {
constructor(inputUTXOs, outputUTXOs) {
this.inputUTXOs = inputUTXOs;
this.outputUTXOs = outputUTXOs;
}
execute() {
for (let i = 0; i < this.inputUTXOs.length; i++) {
if (this.inputUTXOs[i].spent) {
throw new Error("already spent");
}
}
let inn = 0;
for (let i = 0; i < this.inputUTXOs.length; i++) {
inn = inn + this.inputUTXOs[i].amount;
}
let out = 0;
for (let i = 0; i < this.outputUTXOs.length; i++) {
out = out + this.outputUTXOs[i].amount;
}
if (inn < out) {
throw new Error("less than");
}
for (let i = 0; i < this.inputUTXOs.length; i++) {
this.inputUTXOs[i].spend();
}
}
}
module.exports = Transaction;

class Transaction {
constructor(inputUTXOs, outputUTXOs) {
this.inputUTXOs = inputUTXOs;
this.outputUTXOs = outputUTXOs;
}
execute() {
for (let i = 0; i < this.inputUTXOs.length; i++) {
if (this.inputUTXOs[i].spent) {
throw new Error("already spent");
}
}
let inn = 0;
for (let i = 0; i < this.inputUTXOs.length; i++) {
inn = inn + this.inputUTXOs[i].amount;
}
let out = 0;
for (let i = 0; i < this.outputUTXOs.length; i++) {
out = out + this.outputUTXOs[i].amount;
}
if (inn < out) {
throw new Error("less than");
}
for (let i = 0; i < this.inputUTXOs.length; i++) {
this.inputUTXOs[i].spend();
}
this.fee = inn - out;
}
}
module.exports = Transaction;


class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
module.exports = Node;

class Tree {
constructor() {
this.root = null;
}
}
module.exports = Tree;

class Tree {
constructor() {
this.root = null;
}
addNode(node) {
this.root = node;
}
}
module.exports = Tree;

class Tree {
constructor() {
this.root = null;
}
addNode(node) {
if (this.root === null) {
this.root = node;
} else if (this.root.data > node.data) {
this.root.left = node;
} else {
this.root.right = node;
}
}
}
module.exports = Tree;

class Tree {
constructor() {
this.root = null;
}
addNode(node) {
if (this.root === null) {
this.root = node;
} else {
let p = this.root;
while (true) {
if (p.data > node.data) {
if (p.left) {
p = p.left;
} else {
p.left = node;
break;
}
} else {
if (p.right) {
p = p.right;
} else {
p.right = node;
break;
}
}
}
}
}
}
module.exports = Tree;

class Tree {
constructor() {
this.root = null;
}
addNode(node) {
if (this.root === null) {
this.root = node;
} else {
let p = this.root;
while (true) {
if (p.data > node.data) {
if (p.left) {
p = p.left;
} else {
p.left = node;
break;
}
} else {
if (p.right) {
p = p.right;
} else {
p.right = node;
break;
}
}
}
}
}
hasNode(number) {
let n = this.root;
while (n) {
if (n.data === number) {
return true;
} else {
if (n.data > number) {
n = n.left;
} else {
n = n.right;
}
}
}
return false;
}
}
module.exports = Tree;


class MerkleTree {
constructor(leaves, concat) {
this.leaves = leaves;
this.concat = concat;
}
getRoot() {
return this.concat(this.leaves[0], this.leaves[1]);
}
}
module.exports = MerkleTree;

class MerkleTree {
constructor(leaves, concat) {
this.leaves = leaves;
this.concat = concat;
}
getRoot(next = this.leaves) {
while (true) {
if (next.length <= 1) {
return next;
}
let level = [];
for (let i = 0; i < next.length; i += 2) {
level.push(this.concat(next[i], next[i + 1]));
}
next = level;
}
}
}
module.exports = MerkleTree;

class MerkleTree {
constructor(leaves, concat) {
this.leaves = leaves;
this.concat = concat;
}
getRoot(next = this.leaves) {
while (true) {
if (next.length <= 1) {
return next;
}
let level = [];
for (let i = 0; i < next.length; i += 2) {
if ((i + 1) >= next.length) {
level.push(next[i]);
} else {
level.push(this.concat(next[i], next[i + 1]));
}
}
next = level;
}
}
}
module.exports = MerkleTree;

class MerkleTree {
constructor(leaves, concat) {
this.leaves = leaves;
this.concat = concat;
}
getRoot(next = this.leaves) {
while (true) {
if (next.length <= 1) {
return next;
}
let level = [];
for (let i = 0; i < next.length; i += 2) {
if ((i + 1) >= next.length) {
level.push(next[i]);
} else {
level.push(this.concat(next[i], next[i + 1]));
}
}
next = level;
}
}
getProof(index, next = this.leaves) {
let proof = [];
while (true) {
if (next.length <= 1) {
return proof;
}
let level = [];
for (let i = 0; i < next.length; i += 2) {
if ((i + 1) >= next.length) {
level.push(next[i]);
} else {
level.push(this.concat(next[i], next[i + 1]));
if (index === i || i === (index - 1)) {
if (index === i) {
proof.push({ data: next[i + 1], left: false });
} else {
proof.push({ data: next[i], left: true });
}
}
}
}
index = Math.floor(index / 2);
next = level;
}
}
}
module.exports = MerkleTree;

function verifyProof(proof, node, root, concat) {
let n = node;
proof.forEach((e, _) => {
if (e.left) {
n = concat(e.data, n);
} else {
n = concat(n, e.data);
}
});
if (n === root) {
return true;
} else {
return false;
}
}
module.exports = verifyProof;


có 2 mục nhé chú ý :
mục TrieNode.js
class TrieNode {
constructor(key) {
this.key = key;
this.children = {};
this.isWord = false;
}
}
module.exports = TrieNode;
mục Trie.js
const TrieNode = require('./TrieNode');
class Trie {
constructor() {
this.root = new TrieNode(null);
}
}
module.exports = Trie;

const TrieNode = require('./TrieNode');
class Trie {
constructor() {
this.root = new TrieNode(null);
}
insert(data) {
let n = this.root;
for (let i = 0; i < data.length; i++) {
let node = new TrieNode(data[i]);
n.children[data[i]] = node;
if ((i + 1) === data.length) {
node.isWord = true;
}
n = node;
}
}
}
module.exports = Trie;

const TrieNode = require('./TrieNode');
class Trie {
constructor() {
this.root = new TrieNode(null);
}
insert(data) {
let n = this.root;
for (let i = 0; i < data.length; i++) {
let node = n.children[data[i]];
if (!node) {
node = new TrieNode(data[i]);
}
n.children[data[i]] = node;
if ((i + 1) === data.length) {
node.isWord = true;
}
n = node;
}
}
}
module.exports = Trie;

const TrieNode = require('./TrieNode');
class Trie {
constructor() {
this.root = new TrieNode(null);
}
insert(data) {
let n = this.root;
for (let i = 0; i < data.length; i++) {
let node = n.children[data[i]];
if (!node) {
node = new TrieNode(data[i]);
}
n.children[data[i]] = node;
if ((i + 1) === data.length) {
node.isWord = true;
}
n = node;
}
}
contains(word) {
let n = this.root.children[word[0]];
if (n === null) {
return false;
}
let i = 1;
while (n) {
if (i >= word.length) {
if (n.isWord) {
return true;
} else {
return false;
}
}
n = n.children[word[i]];
if (n === null) {
return false;
}
i = i + 1;
}
}
}
module.exports = Trie;
next qua các phần k phải điền code
No activity yet