This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const INFURIA_ENDPOINT = require('./infura_endpoint') | |
const Web3 = require('web3') | |
const web3 = new Web3(new Web3.providers.HttpProvider(INFURIA_ENDPOINT)) | |
// Looking up an individual transaction | |
async function checkTransaction(transactionHash) { | |
let transaction = await web3.eth.getTransaction(transactionHash) | |
console.log('Transaction: ', transaction) | |
} | |
checkTransaction('0x2f81c59fb4f0c3146483e72c1315833af79b6ea9323b647101645dc7ebe04074') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function test() { | |
await trie.put('testKey', 'testValue') // We update (using "put") the trie with the key-value pair "testKey": "testValue" | |
var value = await trie.get('testKey') // We retrieve (using "get") the value at key "testKey" | |
console.log('Value (String): ', value.toString()) // We retrieve our value | |
console.log('Updated trie root:', trie.root) // The new trie root | |
await trie.del('testKey') | |
value = await trie.get('testKey') // We try to retrieve the value at (deleted) key "testKey" | |
console.log('Value at key "testKey": ', value) // Key not found. Value is therefore null. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Trie = require('merkle-patricia-tree').SecureTrie // We import the library required to create a secure Merkle Patricia Tree | |
var trie = new Trie() // We create an empty Merkle Patricia Tree | |
console.log('Empty trie root (Bytes): ', trie.root) // The trie root (32 bytes) | |
async function test() { | |
await trie.put(Buffer.from('testKey'), Buffer.from('testValue')) // We update (using "put") the trie with the key-value pair "testKey": "testValue" | |
const value = await trie.get(Buffer.from('testKey')) // We retrieve (using "get") the value at key "testKey" | |
console.log('Value (Bytes): ', value) | |
console.log('Value (String): ', value.toString()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
await trie.put(Buffer.from('testKey'), Buffer.from('testValue')) | |
await trie.put(Buffer.from('testKey0001'), Buffer.from('testValue0')) | |
await trie.put(Buffer.from('testKey000A'), Buffer.from('testValueA')) | |
var node1 = await trie.findPath(Buffer.from('testKey')) // Looking up our branch node | |
console.log(node1.node) // The branch node | |
// RESULT | |
BranchNode { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function createTransactionHash(transactionHash) { | |
let transaction = await web3.eth.getTransaction(transactionHash) | |
transactionNode = [ | |
web3.utils.toHex(transaction.nonce), | |
web3.utils.toHex(transaction.gasPrice), | |
web3.utils.toHex(transaction.gas), | |
transaction.to, | |
web3.utils.toHex(transaction.value), | |
transaction.input, | |
transaction.v, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hex char bits | node type path length | |
---------------------------------------------------------- | |
0 0000 | extension even | |
1 0001 | extension odd | |
2 0010 | leaf even | |
3 0011 | leaf odd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('Node branches: ', node1.node._branches) | |
// RESULT | |
Node branches: [ | |
, | |
, | |
, | |
[ , ], | |
[ , ], | |
, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var node3 = await trie._lookupNode(Buffer.from(node2._value)) | |
console.log(node3) | |
// RESULT | |
BranchNode { | |
_branches: [ | |
, | |
, | |
, | |
[ , ], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rlp = require('rlp') | |
console.log('RLP encoding of 127: ', rlp.encode('127')) | |
// RESULT | |
RLP encoding of 127: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <--path--> <------------- value -----------------> | |
[ , ] // leaf node down the path of "testKey0" | |
[ , ] // leaf node down the path of "testKeyA" |