0% found this document useful (0 votes)
28 views4 pages

BCT Exp - No.01

Creating Smart Contract using Solidity (Lottery application)

Uploaded by

gaming47more04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views4 pages

BCT Exp - No.01

Creating Smart Contract using Solidity (Lottery application)

Uploaded by

gaming47more04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Universal College of Engineering, Kaman

Department of AIML/DE Engineering Subject:


Blockchain Technologies

Experiment No: 01

Aim:- Local Blockchain: Introduction to Truffle, establishing local Blockchain using Truffle
a) Cryptography in Blockchain and Merkle root tree hash

Code:-
/ SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract MerkleProof {
function verify(
bytes32[] memory proof,
bytes32 root,
bytes32 leaf,
uint256 index
) public pure returns (bool) {
bytes32 hash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
bytes32 proofElement = proof[i];
if (index % 2 == 0) {
hash = keccak256(abi.encodePacked(hash, proofElement));
} else {
hash = keccak256(abi.encodePacked(proofElement, hash));
}
index = index / 2;
}
return hash == root;
}
}
contract TestMerkleProof is MerkleProof
{ bytes32[] public hashes;
constructor() {
string[4] memory transactions =
["alice -> bob", "bob -> dave", "carol ->
alice", "dave -> bob"];
Universal College of Engineering, Kaman
Department of AIML/DE Engineering Subject:
Blockchain Technologies

for (uint256 i = 0; i < transactions.length; i++) {


hashes.push(keccak256(abi.encodePacked(transactions[i])));
}
uint256 n = transactions.length;
uint256 offset = 0;
while (n > 0) {
for (uint256 i = 0; i < n - 1; i += 2) {
hashes.push(
keccak256( abi.enco
dePacked(
hashes[offset + i], hashes[offset + i + 1]

)
)
);
}
offset +=
n; n = n / 2;
}
}
function getRoot() public view returns (bytes32)
{ return hashes[hashes.length - 1];
}
/* verify
3rd leaf
0xdca3326ad7e8121bf9cf9c12333e6b2271abe823ec9edfe42f813b1e768fa57b
root
0xcc086fcc038189b4641db2cc4f1de3bb132aefbd65d510d817591550937818c7
index
2
proof
0x8da9e1c820f9dbd1589fd6585872bc1063588625729e7ab0797cfc63a00bd950
0x995788ffc103b987ad50f5e5707fd094419eb12d9552cc423bd0cd86a3861433
*/
}
Universal College of Engineering, Kaman
Department of AIML/DE Engineering Subject:
Blockchain Technologies

Output:-

(Code Compilation)

(Deploying)
Universal College of Engineering, Kaman
Department of AIML/DE Engineering Subject:
Blockchain Technologies

Conclusion:-The implementation of the MerkleProof and TestMerkleProof contracts was


successful. The MerkleProof contract effectively verifies Merkle proofs, ensuring that a leaf node
belongs to a Merkle tree by reconstructing the hash and comparing it to the root.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy