Skip to content

Latest commit

 

History

History
71 lines (47 loc) · 1.29 KB

File metadata and controls

71 lines (47 loc) · 1.29 KB

jsChain

For Educational Purpose only

Clone repository

git clone https://github.com/hitesharma/jsChain.git

cd jsChain

Generate a keypair

const EC = require("elliptic").ec;
const ec = new EC("secp256k1");

const myKeyPair = ec.genKeyPair();

The myKeyPair object contains the public & private key:

console.log("Public key:", myKeyPair.getPublic("hex"));
console.log("Private key:", myKeyPair.getPrivate("hex"));

Create blockchain instance//Getting hash of latest block

const { Blockchain } = require("./src/blockchain");
const { Transaction } = require("./src/transaction");

const myChain = new Blockchain();

Add transactions

// Transfer 10 coins from my wallet address to `toAddress`
const tx = new Transaction(myKeyPair.getPublic("hex"), "toAddress", 10);

tx.signTransaction(myKeyPair);

myChain.addTransaction(tx);

Complete Pending Transactions and Mine Block

myChain.minePendingTransactions(myKeyPair.getPublic("hex"));

Check wallet Balance

console.log('Wallet's balance: ', myChain.getBalance(myKeyPair));

Get latest Block

console.log(`Latest block's Hash: ${myChain.getLatestBlock()});

Verify Blockchain

console.log("Verify Chain: ", myChain.verifyChain() ? "Yes" : "No");