initial
This commit is contained in:
commit
74ae93a96e
4 changed files with 94691 additions and 0 deletions
|
|
@ -0,0 +1,165 @@
|
|||
Practical 1
|
||||
|
||||
Setting up environment
|
||||
- We need to connect our machine to etherium network.
|
||||
- To able to do transaction to etherium network we need to pay fees called gas which is distributed to miners
|
||||
- As buying ether is so expensive we can connect to test network instead of main network for learning purpose.
|
||||
- So to turn our browser into node for the test network use the metamask extenstion
|
||||
- get free ethers for sepolia network from these faucets https://faucetlink.to/sepolia
|
||||
- to use polygon amoy test network use this to add in metamask config
|
||||
https://support.polygon.technology/support/solutions/articles/82000907114-how-to-add-amoy-network-in-your-wallet-
|
||||
```text
|
||||
Network Name: POLYGON AMOY TESTNET
|
||||
New RPC URL: https://rpc-amoy.polygon.technology/
|
||||
Chain ID: 80002
|
||||
Currency Symbol: MATIC
|
||||
Block Explorer URL: https://www.oklink.com/amoy
|
||||
```
|
||||
faucet for polygon amoy : https://faucet.polygon.technology/
|
||||
- to buy real one
|
||||
https://medium.com/prepo/setting-up-metamask-and-getting-eth-matic-on-polygon-step-by-step-guide-fd55147a0f05
|
||||
|
||||
### Coding
|
||||
- Use remix.etherium.org IDE
|
||||
Create a file named `Bank.sol`
|
||||
```sol
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity 0.8.25;
|
||||
|
||||
contract Bank {
|
||||
int bal;
|
||||
|
||||
constructor() public {
|
||||
bal = 1;
|
||||
}
|
||||
|
||||
function getBalance() view public returns(int) {
|
||||
return bal;
|
||||
}
|
||||
|
||||
function withdraw(int amt) public {
|
||||
bal = bal - amt;
|
||||
}
|
||||
|
||||
function deposit(int amt) public {
|
||||
bal = bal + amt;
|
||||
}
|
||||
}
|
||||
```
|
||||
- This Bank.sol should be under contracts folder. This file is to be compiled
|
||||
- Also the file named `deploy_with_web3.ts` should be in scripts folder which will deploy it
|
||||
```js
|
||||
// This script can be used to deploy the "Storage" contract using Web3 library.
|
||||
// Please make sure to compile "./contracts/1_Storage.sol" file before running this script.
|
||||
// And use Right click -> "Run" from context menu of the file to run the script. Shortcut: Ctrl+Shift+S
|
||||
|
||||
import { deploy } from './web3-lib'
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const result = await deploy('Bank', [])
|
||||
console.log(`address: ${result.address}`)
|
||||
} catch (e) {
|
||||
console.log(e.message)
|
||||
}
|
||||
})()
|
||||
```
|
||||
- Now run it like this
|
||||

|
||||
- switch to injected metamask instead of remix vm to deploy transaction on our network
|
||||
|
||||
|
||||
### Front end
|
||||
Instead of running it and using it inside remix lets create a separate front end
|
||||
- Two things are required ie ABI from compiler section and Address of Bank(not ours) from 'Deploy and run transaction' section.
|
||||
- Now create html file and type
|
||||
```html
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="">
|
||||
<input type="text" id="amount">
|
||||
<p id="balance"></p>
|
||||
<button id="deposit">Deposit</button>
|
||||
<button id="withdraw">Withdraw</button>
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.34/dist/web3.min.js"></script>
|
||||
|
||||
|
||||
<script>
|
||||
let contract;
|
||||
|
||||
|
||||
web3= new Web3(web3.currentProvider);
|
||||
// web3= new Web3(ethereum);
|
||||
|
||||
// initialize ABI and address first
|
||||
|
||||
// Contract method will accept 2 parameters ie ABI and contract
|
||||
contract = new web3.eth.Contract(ABI,address);
|
||||
|
||||
// as getBalance is a function so we need to call it and use the call method for that which will return the promise
|
||||
contract.methods.getBalance().call().then( (bal)=>{
|
||||
console.log('balance')
|
||||
document.querySelector("#balance").innerHTML= bal;
|
||||
} )
|
||||
|
||||
depositBtn.addEventListener('click',async()=>{
|
||||
let amt=0;
|
||||
amt = parseInt(inputEle.value);
|
||||
|
||||
await window.ethereum.enable();
|
||||
// this will return all the promise of accounts of metamask
|
||||
web3.eth.getAccounts().then( accounts=>{
|
||||
|
||||
console.log('accounts: ',accounts)
|
||||
// taking first account
|
||||
const acc = accounts[0];
|
||||
|
||||
// instead of using call method send method is used
|
||||
return contract.methods.deposit(amt).send({from : acc});
|
||||
} )
|
||||
.then(data=>{
|
||||
console.log('data: ',data)
|
||||
})
|
||||
.catch(err=>{
|
||||
console.log('err: ',err)
|
||||
})
|
||||
})
|
||||
|
||||
withdrawBtn.addEventListener('click',async()=>{
|
||||
let amt=0;
|
||||
amt = parseInt(inputEle.value);
|
||||
|
||||
await window.ethereum.enable();
|
||||
// this will return all the promise of accounts of metamask
|
||||
web3.eth.getAccounts().then( accounts=>{
|
||||
|
||||
console.log('accounts: ',accounts)
|
||||
// taking first account
|
||||
const acc = accounts[0];
|
||||
|
||||
// instead of using call method send method is used
|
||||
return contract.methods.withdraw(amt).send({from : acc});
|
||||
} )
|
||||
.then(data=>{
|
||||
console.log('data: ',data)
|
||||
})
|
||||
.catch(err=>{
|
||||
console.log('err: ',err)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
Theory 2
|
||||
|
||||
## Etherium
|
||||
Developed by Vitalik Buterin. Eherium used to use POW but now from sep 2022 it uses POS.
|
||||
**Why Etherium?**
|
||||
Unlike bitcoin which is decentralized cash app(cryptocurrency), etherium if for decentralized apps(dApps). It has concept like EVM(Etherium Virtual Machine). To run softwares in etherium network, EVM is needed. Here Ether is cryptocurrency in etherium network
|
||||
### Smart Contract
|
||||
If there is a company say B from which user A wanna order product. That company may or may not be trusted. Also comapnay B will give commision to delivery service C. But what if delivery C is not trustable and dont deliver product at all.
|
||||
So using **smart contract** means automating the payment so the money will get transferred automatically once the product is delievered to B from A and to C from B
|
||||

|
||||
Benefits
|
||||
- Fast
|
||||
- Secure bcz of blockchain
|
||||
- Trust
|
||||
|
||||
|
||||
### Drawbacks of Blockchain
|
||||
1. Complex : So many terms like consensus,chain, nodes and miners and complex technology, providing coordination.
|
||||
2. Slow : Bitcoin transaction speed is about 7-10 transactions/sec wheras visa is 1000 transactions/sec.
|
||||
3. Bitcoin and etherium is using proof of work which waste so much resources/energy.
|
||||
4. In case of public blockchain it is true that name will be anonymous but how much transaction has taken place will not be anonymous and known to everyone
|
||||
5. 51% attack : if more than 50% of miners r malacious then they can attack the whole blockchain.
|
||||
6. Here is an example about transactions being public
|
||||
https://www.blockchain.com/explorer/addresses/btc/3M6XXpqUc8G8GuHYKQhjAWobo5aGpkdV7u
|
||||
7. Law enforcement requests information from Coinbase all the time. See https://www.coinbase.com/blog/transparency-report-2023
|
||||
|
||||
### Hyperledger
|
||||
Hyperledger is an open source project having different frameworks and tools to build blockchain. It is hosted in the linux foundation. As public blockchain has its drawbacks so companies and transactions especially B2B instead of B2C want their own blockchain so they start making open source tools for that.
|
||||
Frameworks
|
||||
- Iroha
|
||||
- Sawtooth
|
||||
- Fabric
|
||||
- Indy
|
||||
- Burrow
|
||||
|
||||
Tools
|
||||
- Hyperledger Composer
|
||||
- Hyperledger Cello
|
||||
- Hyperledger Quilt
|
||||
- Hyperledger Explorer
|
||||
121
joplin notion extension/Web Development/Blockchain/Theory.md
Normal file
121
joplin notion extension/Web Development/Blockchain/Theory.md
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
Theory
|
||||
|
||||
- A blockchain is a distributed ledger with growing lists of records (blocks) that are securely linked together via cryptographic hashes.
|
||||
|
||||
### Web versions
|
||||
- Web 1.0 (1994-2004) : Websites r made by programmers, normal users can read the content only and nothing cant be changed by user. only programmer can write. Also knows as static sites
|
||||
- Web 2.0 (2004-2022) : Websites which allows normal users to both read and write the content and companies provide hosting platform. Also known as dynamic sites. Eg: social media like youtube, facebook. They have centralized servers. Client server architecture. Users dont have control over flow of thir data or ownership
|
||||
- Web 3.0 (2022 - ) : Unlike web 2.0, we have decentralized servers. Peer to peer network. Like torrent services. Applications run on decentralized nodes so those apps are called dapps. Blockchain is the main building block of web 3.0.
|
||||
|
||||
Summary
|
||||
Web 1.0 - Read
|
||||
Web 2.0 - Read and Write
|
||||
Web 3.0 - Own your data
|
||||
|
||||
### Prerequisites
|
||||
|
||||
Someone who made transaction that transaction is stored in ledger and it is immutable
|
||||
|
||||
- Cryptography
|
||||
- Distributed computing : Distributed computing is the method of making multiple computers work together to solve a common problem. It makes a computer network appear as a powerful single computer that provides large-scale resources to deal with complex challenges.
|
||||
- Game theory : game theory, branch of applied mathematics that provides tools for analyzing situations in which parties, called players, make decisions that are interdependent. This interdependence causes each player to consider the other player's possible decisions, or strategies, in formulating strategy.
|
||||
|
||||
### Blockchain
|
||||
If A sends 10$ to B then if ledger is with A or B only then they both may change it for their advantage but if its with all other peers sychronized then they cant change it as others will say hey u changed it
|
||||

|
||||
|
||||
Each block are having data and hash of next node similar to linked list
|
||||

|
||||
Hash algorithms like sha-3 is used to calculate hash of the data so if data changes hash also changes thus breaking the link and helps to identify malicious node
|
||||
|
||||
Miners
|
||||
Miners get the reward for adding blocks in block chain to provide incentivization. But to control the demand as everyone wants to do that for a simple task so in order to choose miners they r given a challenge and that challenge requires computational power and money. Other people who r using block chain r called nodes
|
||||
Summary
|
||||
So basically blockchain is a chain of blocks and each blocks are having multiple transaction and the one who is responsible to add blocks in block chain are called miners(in case of bitcoin). All the people in the blockchain network are called nodes
|
||||
|
||||
### Cryptography
|
||||
Blockchain is very similar to P2P network with the exception of crytpography
|
||||
Reasons for need of crytograpy in block chain
|
||||
1. Confidentiality : A sends a sensitive information to B which should not be read by anyone else
|
||||
2. Integrity : Data sent should not be changed in between by anyone or get corrupted
|
||||
3. Non repudiation : Taking acknowlegement of transaction/message. Eg: If A sends message to B then A cant be able to say that A has not sent it
|
||||
4. Authentication : Means someone else say C cant send message to B inpersonating as A.
|
||||
|
||||
#### Digital signature
|
||||
It is possible to have both digital signature and confidentiality by first encrypting the data from sender's private key then encrypt it again with reciever's public key. When data has reached then first it is decrypted by reciever private key then decrypted by sender public key to verify authenticity.
|
||||

|
||||
|
||||
### Nodes in blockchain
|
||||
- Full Node : Verify blocks which get added. Full node store whole block chain which is like 5450 of GBs in 2024.
|
||||
- Partial Node : lightweight node having partial information. when we use blockchain for wallet so our device becomes node itself
|
||||
- Full node + miner : in this case miner needs huge computational poower lie stack of multiple CPU and GPUs
|
||||
|
||||
### Hashing in blockchain
|
||||
- Input of any length/bit when converted to has using hash function, the hash generated is of fixed bit/size. So even if hige chunk data ie 5gb is passed then only fixed size hash which is merely in bytes will be generated.
|
||||

|
||||
- Unlike encryption hash cant be converted back to input
|
||||
- Some exampples are MD(MD5 was discontinued due to security issue) and SHA where SHA 256 bit is famous. MD stands for Message Digest and SHA stands for Secure Hash Algorithm. SHA is made by NSA
|
||||
- For 2 different inputs we never get same hash and if in rare case if it happens then it is called collision
|
||||
- If we r downloading ubuntu iso from untrusted site then we can generate hash of iso file and compare it with hash posted in original site and if that mismatch then it means it is modified.
|
||||
- For packages having GPG keys, where digital signature is used, hash is signed by encrypting the hash with the key the actual package is not encrypted only its hash is encrypted
|
||||
so to validate it you do 3 steps
|
||||
- you hash it yourself
|
||||
- you decrypt the hash it came with using the key the maintainer gave you
|
||||
- you check if the 2 hashes are the same
|
||||
### Merkle Tree
|
||||
We have nodes and we have transaction in nodes too. As there are multiple transaction we cant store the hash of all transactions so using merkle tree we try to generate single hash for all the transaction.
|
||||
Let's say we have 10 transactions so lets try to merge them in pairs. H1 and H2, H3 and H4 ... and so we will combine hashes.
|
||||

|
||||
If there r even number of nodes then we will just repeat the last one. (9,10) was the odd one out so a copy is created and it become (9,10,9,10).
|
||||
|
||||
|
||||
### Blockchain Architecture
|
||||
- Blocks will contain data but each block will have block header which is like meta-data. Blockheader will contain
|
||||
- Timestamp
|
||||
- Version
|
||||
- Merkle root
|
||||
- Difficulty Target
|
||||
- Nonce
|
||||
- Previous Hash
|
||||
|
||||
Difficulty and Nonce comes under proof of work(POW) section.
|
||||
- Data is immutable bcz of POW as hash will change
|
||||

|
||||
POW
|
||||
- block has to be validated before making change
|
||||
- takes atleast 10 min to add block. So if there r 5 blocks which needs to be changed then it will take atleast 50 min
|
||||
- blockchain stored in multiple machines. Consensus algorithm will be used as it is verified by everyone on blockchain
|
||||
- need supercomputer to change block
|
||||
- need 50% majority in blockchain network which is almost impossible
|
||||
- First block is known as genesis block and is created by the one who started the chain. Its previous hash value is 0.
|
||||
### Types of Blockchain
|
||||
- Public Blockchain
|
||||
1. Open : Means data about is available to public(not secrets but meta-data and other whose visibility is public to everyone)
|
||||
2. Anyone can join that blockchain
|
||||
- Private Blockchain
|
||||
1. For single company
|
||||
- Federated Blockchain
|
||||
1. Group of people/ companies but not to everyone in world
|
||||
|
||||
Advantage of private over public is that there is no need to implement POW to verify nodes as all nodes are known and no need to wait for 10 min.
|
||||
|
||||
### Consensus Blockchain
|
||||
- In order to add a block consensus algorithm is used to determine who will add the block. Some examples are
|
||||
1. POW(Proof of Work) : To solve computational program requiring huge computational power. This algo is used by bitcoin and whoever wins the puzzle will given the opportuity as they have spent computational power.
|
||||
2. POS(Proof of Stake) : Unlike proof of work which is resposible for so much energy consumption. Before applying for that, they put some stake like spending some coins, Used by Etherium
|
||||
3. POET(Prooof of Elapsed Time) : By intel
|
||||
4. Proof of Depsit : Like POS but depositing amount before entering block chain
|
||||
5. Proof of Capacity(POC) : one with huge capacity will win
|
||||
### Proof of Work
|
||||
Why is it useless to alter blocks in bitcoin?
|
||||
|
||||
In a blockchain, each block contains a cryptographic hash of the previous block, creating a chain of blocks linked together. This ensures the integrity and immutability of the blockchain because altering any block would require changing all subsequent blocks, which becomes increasingly difficult as more blocks are added due to the computational effort required (proof of work).
|
||||
|
||||
Let's consider your scenario where you want to change the 25th block in a chain of 30 blocks. To successfully alter the 25th block, you would need to recalculate its hash and the hash of all subsequent blocks in the chain. This is because each block's hash includes the hash of the previous block.
|
||||
|
||||
Additionally, in a proof-of-work system like Bitcoin's, miners compete to solve complex mathematical puzzles to add new blocks to the blockchain. Changing historical blocks would require redoing all the proof-of-work for those blocks and then outpacing the current network's proof-of-work to have the altered version of the blockchain accepted by the majority of nodes.
|
||||
|
||||
The reason it would be considered useless is that the computational power required to achieve this, especially for a significant number of blocks deep in the chain, is prohibitively high. The Bitcoin blockchain, for example, has an immense amount of computational power securing it, making it practically infeasible to alter past transactions without expending an exorbitant amount of resources.
|
||||
|
||||
Even if someone were to succeed in altering the 25th block, the rest of the network would reject it because the altered chain would not have the longest proof-of-work chain, which is the criterion for consensus in Bitcoin and many other blockchain networks. This is why attempting to alter past blocks in a blockchain is often referred to as "useless" because of the immense computational effort required and the near-impossibility of success.
|
||||
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
Theory3
|
||||
|
||||
### NFT
|
||||
- Non Fungible Token
|
||||
- Anyone can create NFT like first twitter tweet or song. If someone tries to copy it then it would be of no use as new hash will be generated as NFT is unique and have digital signature so others wont have private key so cant really generate same token
|
||||
Loading…
Reference in a new issue