Blockchain at Heart
How it works outside of the cryptocurrency trend
@m4d_zhttps://talks.m4dz.net/blockchain-at-heart/en/Blockchain Is the Most Disruptive Invention Since the Internet (?)
It’s not only the cryptocurrency
it’s far simple
and even bigger
Nope
The first Blockchain in History
started in… 1995
The checksum is published
daily in the NYT
The longest running blockchain started in 1995 and is still running strong today. Current hash circled in red. Based on Stuart Haber and W. Scott Stornetta https://t.co/q1VIUvwGUA pic.twitter.com/eGEhqFSl7U
— Ittai Abraham (@ittaia) August 23, 2018
So Blockchain is more
a matter of Cryptography
A Blockchain,
is a series (a chain)
of structured data (blocks)
Immutable
Decentralized & Distributed
Authority
Public vs Private
Trusted Mutual Agreement
PoW (Nakamoto agreement)
PoS
pBFT : Practical Byzantine Fault Tolerance
Why using it on a private network?
With a classical DB cluster
With a Blockchain
Think it like
Git vs SVN
“Smart Contracts”
Restart from scratch
A Block
{
index: 1,
hash: "7A7D12...E62EC2",
parent: "3A1A1D...684E1E",
data: [...],
timestamp: 1548237157237
}
Hash
SHA256({
index: 1,
parent: "3A1A1D...684E1E",
data: [...],
timestamp: 1548237157237
}) === "7A7D12...E62EC2"
Data
{
data: [
{hash:"67c...640", data: ..., timestamp: 1548237157237},
{hash:"82a...503", data: ..., timestamp: 1548237157252},
]
}
A Blockchain
class Blockchain {
constructor() {
this.blocks = [{
hash: "000000",
index: 0,
data: null,
timestamp: Date.now()
}]
}
}
Adding a block
class Blockchain {...
addBlock(data) {
const block = {
index: this.blocks.length,
parent: this.blocks[this.blocks.length -1].hash
data,
timestamp: Date.now()
}
block.hash = CryptoJS.SHA256(`
${block.index};${block.parent};
${JSON.stringify(data)};${block.timestamp}
`)
return this.blocks.push(block)
}
}
Adding a transaction
class Blockchain {...
transaction(data) {
const transaction = {
data,
timestamp: Date.now()
}
transaction.hash = CryptoJS.SHA256(`
${JSON.stringify(data)};${transaction.timestamp}
`)
return transaction
}
}
In action
const blockchain = new Blockchain
blockchain.addBlock([
blockchain.transaction({
id: 2820628...37, name: 'Jane Doe', note: 'Registration'
})
blockchain.transaction({
id: 2820628...37,
prescription: [{
name: 'Paracetamol',
dosage: '3-6/day'
}]
})
])
Integrity
Moaaaar
blockchain.addBlock{[
blockchain.transaction({
id: 1940269...51,
name: 'John Doe',
note: 'Registration'
})
]}
[{
index: 0, hash: "000000", data: null, timestamp: 1548237157237
},{
index: 1,
hash: "7A7D12...E62EC2", parent: "000000",
data: [{
{hash:"67C...640", data: ..., timestamp: 1548237157237},
{hash:"82A...503", data: ..., timestamp: 1548237157252}
}], timestamp: 1548237157237
},{
index: 2,
hash: "6DE54C...87FEB7D", parent: "7A7D12...E62EC2",
data: [{
{hash:"AE4...953", data: ..., timestamp: 1548237157237}
}], timestamp: 1548237157237
}]
Hashes
Signatures
Adding a client
class Blockchain {
constructor() {...
this.users = []
}
register(user) {
const keys = GenKeyPair()
user.pubKey = keys.pubKey
this.users.push(user)
return key.privKey
}
}
Signing the transaction
class Blockchain {...
transaction(data, privKey) {...
transaction.sign = Sign(transaction.hash, privKey)
}
}
blockchain.transaction({
id: 1940269...51,
name: 'John Doe',
note: 'Registration'
}, Storage.privKey)
Verifying the transaction
class Blockchain {...
findUserForTransaction(transaction) {
return this.users.find(user => Verify(transaction.sign, user.pubKey))
}
checkTransaction(transaction) {
return !!findUserForTransaction(transaction)
}
}
Certify
class Blockchain {...
register(user, privKey) {
const keys = GenKeyPair()
user.pubKey = keys.pubKey
user.pubKey.sign = Sign(keys.pubKey, privKey)
this.users.push(user)
return key.privKey
}
checkUserKey(pubKey) {
return !!this.users.find(user => Verify(pubKey.sign, user.pubKey))
}
checkTransaction(transaction) {
const user = findUserForTransaction(transaction)
return user ? checkUserKey(user.pubKey) : false
}
}
Control the blocks
class Blockchain {...
addBlock(data) {
if (!data.every(this.checkTransaction)) {
throw
}
...
}
}
Encrypt
const pharmacy = blockchain.getUser(pharmacyID)
transaction({
id: 2820628...37,
prescription: Encrypt(JSON.stringify([{
name: 'Paracetamol',
dosage: '3-6/day'
}]), pharmacy.pubKey)
})
Synchronize
Agreement
Nakamoto Agreement
class Blockchain {...
const inc = 5, prefix = Array(inc).fill(0).join('')
addBlock(data) {...
while (true) {
block.nonce = Math.random()
block.hash = CryptoJS.SHA256(`
${block.nonce};${block.index};${block.parent};
${JSON.stringify(data)};${block.timestamp}
`)
if (block.hash.substr(0, inc) === prefix) {
this.blocks.push(block); break
}
}
}
}
Code Is Law
Smart Contracts
class SmartContract {
validate (transaction) {
return true
}
exec (transaction) {
return true
}
}
At run
class Blockchain {...
addContract (contract) {
this.contracts.push(contract)
}
addBlock (data, ...) {...
this.blocks.push(block)
block.data.forEach(transaction => {
this.contracts
.filter(contract => contract.validate(transaction))
.each(contract => contract.exec(transaction))
})
}
}
Cryptography!
Blockchain
If your use-case need:
The blockchain doesn’t interact very well IRL
Choose to use a blockchain with caution :
the technology is brilliant in terms of cryptography,
but not all that glitters is gold out there.
It’s just a tool in your toolbox.
Paranoïd Web Dino · Tech Evangelist
https://talks.m4dz.net/blockchain-at-heart/en/ Available under licence CC BY-SA 4.0
m4dz, CC BY-SA 4.0
Courtesy of Unsplash and Pexels contributors
Powered by Reveal.js
Source code available at
https://git.madslab.net/talks