Skip to content

Instantly share code, notes, and snippets.

View gabrocheleau's full-sized avatar
🐕

Gabriel Rocheleau gabrocheleau

🐕
View GitHub Profile
@gabrocheleau
gabrocheleau / Markdium-JSX.jsx
Created June 20, 2020 02:52
Markdium-Ethereum's Merkle Patricia Trees
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')
@gabrocheleau
gabrocheleau / Markdium-JSX.jsx
Created June 20, 2020 02:52
Markdium-Ethereum's Merkle Patricia Trees
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.
@gabrocheleau
gabrocheleau / Markdium-JSX.jsx
Created June 20, 2020 02:52
Markdium-Ethereum's Merkle Patricia Trees
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())
@gabrocheleau
gabrocheleau / Markdium-JSX.jsx
Created June 20, 2020 02:52
Markdium-Ethereum's Merkle Patricia Trees
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 {
@gabrocheleau
gabrocheleau / Markdium-JSX.jsx
Created June 20, 2020 02:52
Markdium-Ethereum's Merkle Patricia Trees
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,
@gabrocheleau
gabrocheleau / Markdium-JSX.jsx
Created June 20, 2020 02:52
Markdium-Ethereum's Merkle Patricia Trees
hex char bits | node type path length
----------------------------------------------------------
0 0000 | extension even
1 0001 | extension odd
2 0010 | leaf even
3 0011 | leaf odd
@gabrocheleau
gabrocheleau / Markdium-JSX.jsx
Created June 20, 2020 02:52
Markdium-Ethereum's Merkle Patricia Trees
console.log('Node branches: ', node1.node._branches)
// RESULT
Node branches: [
,
,
,
[ , ],
[ , ],
,
@gabrocheleau
gabrocheleau / Markdium-JSX.jsx
Created June 20, 2020 02:52
Markdium-Ethereum's Merkle Patricia Trees
var node3 = await trie._lookupNode(Buffer.from(node2._value))
console.log(node3)
// RESULT
BranchNode {
_branches: [
,
,
,
[ , ],
@gabrocheleau
gabrocheleau / Markdium-JSX.jsx
Created June 20, 2020 02:52
Markdium-Ethereum's Merkle Patricia Trees
const rlp = require('rlp')
console.log('RLP encoding of 127: ', rlp.encode('127'))
// RESULT
RLP encoding of 127:
@gabrocheleau
gabrocheleau / Markdium-JSX.jsx
Created June 20, 2020 02:52
Markdium-Ethereum's Merkle Patricia Trees
// <--path--> <------------- value ----------------->
[ , ] // leaf node down the path of "testKey0"
[ , ] // leaf node down the path of "testKeyA"