blockchain 52
blockchain 52
Practical – 1
Aim: 1. Understanding Block using
https://tools.superdatascience.com/Blockchain/block.
The block in the block chain plays an important role during transaction of bitcoins,
storing information related user.
OUTPUT:
Block Chain Technology (CT703D-N) 21BECE30052
OUTPUT:
Block Chain Technology (CT703D-N) 21BECE30052
https://tools.superdatascience.com/Blockchain/Blockchain
OUTPUT:
Block Chain Technology (CT703D-N) 21BECE30052
TOKENS are digital assets defined by a project or smart contract and built on a
specific block chain.
OUTPUT:
Block Chain Technology (CT703D-N) 21BECE30052
Coin base is an easy way for those who are new to cryptocurrency to get started. Its
easy-to-use interface lets people buy and sell crypto in just a few clicks. While not
every type of cryptocurrency is supported, you will find many of the most popular
coins there.
OUTPUT:
Block Chain Technology (CT703D-N) 21BECE30052
Practical – 2
Using JavaScript
Aim: 1. Creating a block chain using Javascript.
class Block {
constructor(index, timestamp, data, previousHash = '') {
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.previousHash = previousHash;
this.hash = this.calculateHash();
}
calculateHash() {
return SHA256(this.index + this.previousHash + this.timestamp +
JSON.stringify(this.data)).toString();
}
}
class Blockchain {
constructor() {
this.chain = [this.createGenesisBlock()];
}
createGenesisBlock() {
return new Block(0, "01/01/2020", "Genesis Block", "0");
}
getLatestBlock() {
Block Chain Technology (CT703D-N) 21BECE30052
class Block {
constructor(index, timestamp, data, previousHash = '') {
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.previousHash = previousHash;
this.hash = this.calculateHash();
this.nonce = 0;
}
calculateHash() {
return SHA256(this.index + this.previousHash + this.timestamp +
JSON.stringify(this.data) + this.nonce).toString();
}
mineBlock(difficulty) {
while (this.hash.substring(0, difficulty) !== Array(difficulty + 1).join("0")) {
this.nonce++;
this.hash = this.calculateHash();
}
console.log("Block mined: " + this.hash);
}
}
class Blockchain {
constructor() {
this.chain = [this.createGenesisBlock()];
this.difficulty = 2;
}
Block Chain Technology (CT703D-N) 21BECE30052
createGenesisBlock() {
return new Block(0, "01/01/2020", "Genesis Block", "0");
}
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
addBlock(newBlock) {
newBlock.previousHash = this.getLatestBlock().hash;
newBlock.mineBlock(this.difficulty);
this.chain.push(newBlock);
}
isChainValid() {
for (let i = 1; i < this.chain.length; i++) {
const currentBlock = this.chain[i];
const previousBlock = this.chain[i - 1];
class Transaction {
constructor(fromAddress, toAddress, amount) {
this.fromAddress = fromAddress;
this.toAddress = toAddress;
this.amount = amount;
}
}
class Block {
constructor(timestamp, transactions, previousHash = '') {
this.timestamp = timestamp;
this.transactions = transactions;
this.previousHash = previousHash;
this.hash = this.calculateHash();
this.nonce = 0;
}
calculateHash() {
return SHA256(this.previousHash + this.timestamp + JSON.stringify(this.transactions)
+ this.nonce).toString();
}
mineBlock(difficulty) {
while (this.hash.substring(0, difficulty) !== Array(difficulty + 1).join("0")) {
this.nonce++;
this.hash = this.calculateHash();
}
console.log("Block mined: " + this.hash);
}
}
Block Chain Technology (CT703D-N) 21BECE30052
class Blockchain {
constructor() {
this.chain = [this.createGenesisBlock()];
this.difficulty = 2;
this.pendingTransactions = [];
this.miningReward = 200;
}
createGenesisBlock() {
return new Block("10/01/2024", [], "0");
}
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
minePendingTransactions(miningRewardAddress) {
const rewardTx = new Transaction(null, miningRewardAddress, this.miningReward);
this.pendingTransactions.push(rewardTx);
OUTPUT:
class KeyPair {
constructor() {
this.key = ec.genKeyPair();
}
getPublic() {
return this.key.getPublic('hex');
}
getPrivate() {
return this.key.getPrivate('hex');
}
}
class Transaction {
constructor(fromAddress, toAddress, amount) {
this.fromAddress = fromAddress;
this.toAddress = toAddress;
this.amount = amount;
this.signature = null;
}
calculateHash() {
return SHA256(this.fromAddress + this.toAddress + this.amount).toString();
}
Block Chain Technology (CT703D-N) 21BECE30052
signTransaction(signingKey) {
if (signingKey.getPublic('hex') !== this.fromAddress) {
throw new Error('You cannot sign transactions for other wallets!');
}
isValid() {
if (this.fromAddress === null) return true;
class Block {
constructor(timestamp, transactions, previousHash = '') {
this.timestamp = timestamp;
this.transactions = transactions;
this.previousHash = previousHash;
this.hash = this.calculateHash();
this.nonce = 0;
}
Block Chain Technology (CT703D-N) 21BECE30052
calculateHash() {
return SHA256(this.previousHash + this.timestamp + JSON.stringify(this.transactions)
+ this.nonce).toString();
}
mineBlock(difficulty) {
while (this.hash.substring(0, difficulty) !== Array(difficulty + 1).join("0")) {
this.nonce++;
this.hash = this.calculateHash();
}
console.log("Block mined: " + this.hash);
}
hasValidTransactions() {
for (const tx of this.transactions) {
if (!tx.isValid()) {
return false;
}
}
return true;
}
}
class Blockchain {
constructor() {
this.chain = [this.createGenesisBlock()];
this.difficulty = 2;
this.pendingTransactions = [];
this.miningReward = 200;
}
createGenesisBlock() {
return new Block("10/01/2024", [], "0");
}
Block Chain Technology (CT703D-N) 21BECE30052
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
minePendingTransactions(miningRewardAddress) {
const rewardTx = new Transaction(null, miningRewardAddress, this.miningReward);
this.pendingTransactions.push(rewardTx);
if (!transaction.isValid()) {
throw new Error('Cannot add invalid transaction to chain');
}
this.pendingTransactions.push(transaction);
}
getBalanceOfAddress(address) {
let balance = 0;
for (const block of this.chain) {
for (const trans of block.transactions) {
if (trans.fromAddress === address) {
balance -= trans.amount;
}
Block Chain Technology (CT703D-N) 21BECE30052
isChainValid() {
for (let i = 1; i < this.chain.length; i++) {
const currentBlock = this.chain[i];
const previousBlock = this.chain[i - 1];
if (!currentBlock.hasValidTransactions()) {
return false;
}
class Block {
constructor(timestamp, transactions, previousHash = '') {
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"savjeecoin-frontend": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {
"@schematics/angular:component": {
"styleext": "scss"
}
},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/savjeecoin-frontend",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
Block Chain Technology (CT703D-N) 21BECE30052
],
"scripts": []
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
Block Chain Technology (CT703D-N) 21BECE30052
"options": {
"browserTarget": "savjeecoin-frontend:build"
},
"configurations": {
"production": {
"browserTarget": "savjeecoin-frontend:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "savjeecoin-frontend:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"src/styles.scss"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
Block Chain Technology (CT703D-N) 21BECE30052
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"savjeecoin-frontend-e2e": {
"root": "e2e/",
"projectType": "application",
"prefix": "",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "savjeecoin-frontend:serve"
},
"configurations": {
"production": {
"devServerTarget": "savjeecoin-frontend:serve:production"
}
}
},
"lint": {
Block Chain Technology (CT703D-N) 21BECE30052
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "savjeecoin-frontend"
}
Practical – 3
Block Chain Technology (CT703D-N) 21BECE30052
be left as ‘0’.
Block Chain Technology (CT703D-N)
• eip155Block/eip158Block: EIP stands for “Ethereum Improvement
Proposals”, these were implemented to release Homestead. In a private
blockchain development, hard forks aren’t needed, hence the parameter value
should be left as ‘0’.
• difficulty: Controls the complexity of the mining puzzle and a lower value
enables quicker mining.
• gasLimit: Establishes an upper limit for executing smart contracts.
• alloc: Allows allocation of Ether to a specific address.
Paste the above code in the genesis.json file and save it in a folder on your
computer.
Next, open the terminal and run the following code snippet. This will instruct
Geth to use genesis.json file to be the first block of your custom blockchain.
We also specify a data directory where our private chain data will be stored.
Choose a location on your computer (separate folder from the public Ethereum
chain folder, if you have one) and Geth will create the data directory for you.
geth --rpc --rpcport "8085" --datadir
/path_to_your_data_directory/TestChain init
/path_to_folder/genesis.json
Once you run this snippet, you can see the following terminal result
There are two ways to get ether to your account. You can mine
blocks and get rewarded with ether or someone sends you
some ether to your account.
Since you are alone in your private network at this point, the
only way isto mine some blocks and get rewarded.
genesis.json file.
Important Note:
/path_to_your_data_directory/TestChain
/path_to_your_data_directory/TestChain2 --
similar to this.
Note:
[::] will be parsed as localhost (127.0.0.1). If your
nodes are on a local network check each individual host
machine and find your IP with ifconfig
If your peers are not on the local network, you need to
f2da64f49c30a0038bba3391f40805d531510c473ec2bcc7
c201631ba003c6f16fa09e03308e48f87d21c0fed1e4e0bc
53428047f6dcf34da344d3f 5bb69373b@[::]:30306?
discport=0")