Blockchain at Heart
La Blockchain sans la Cryptomonnaie
@m4d_zLa plus grande révolution technologique depuis Internet (?)
Ce n’est pas que la crypto monnaie
c’est à la fois bien plus simple
et bien plus puissant
Nope
La première Blockchain de l’histoire
date de… 1995
La somme de contrôle est publiée
quotidiennement dans le 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
La blockchain, c’est avant tout
une histoire de Crypto
Une blockchain,
c’est une suite (une chaine)
de structures de données (des blocs)
Une base de données
Immuable
(Immutability)
Décentralisée
Autorité
Publique vs Privée
Obtenir un consensus valide
PoW (Nakamoto consensus)
PoS
pBFT : Practical Byzantine Fault Tolerance
Pourquoi l’utiliser sur réseau fermé ?
Sur un cluster de BDD
Sur une Blockchain
Imaginez
Git vs SVN
« Smart Contracts »
Repartons de zéro
Un bloc
{
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},
]
}
Une Blockchain
class Blockchain {
constructor() {
this.blocks = [{
hash: "000000",
index: 0,
data: null,
timestamp: Date.now()
}]
}
}
Ajouter un bloc
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)
}
}
Ajouter une transaction
class Blockchain {...
transaction(data) {
const transaction = {
data,
timestamp: Date.now()
}
transaction.hash = CryptoJS.SHA256(`
${JSON.stringify(data)};${transaction.timestamp}
`)
return transaction
}
}
En action
const blockchain = new Blockchain
blockchain.addBlock([
blockchain.transaction({
id: 2820628...37, name: 'Jane Doe', note: 'Registration'
})
blockchain.transaction({
id: 2820628...37,
prescription: [{
name: 'Doliprane',
dosage: '3-6/day'
}]
})
])
Intégrité
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
Signer
Ajouter un client
class Blockchain {
constructor() {...
this.users = []
}
register(user) {
const keys = GenKeyPair()
user.pubKey = keys.pubKey
this.users.push(user)
return key.privKey
}
}
Signer la transaction
class Blockchain {...
transaction(data, privKey) {...
transaction.sign = Sign(transaction.hash, privKey)
}
}
blockchain.transaction({
id: 1940269...51,
name: 'John Doe',
note: 'Registration'
}, Storage.privKey)
Vérifier la transaction
class Blockchain {...
findUserForTransaction(transaction) {
return this.users.find(user => Verify(transaction.sign, user.pubKey))
}
checkTransaction(transaction) {
return !!findUserForTransaction(transaction)
}
}
Certifier
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
}
}
Contrôler les blocs
class Blockchain {...
addBlock(data) {
if (!data.every(this.checkTransaction)) {
throw
}
...
}
}
Chiffrer
const pharmacy = blockchain.getUser(pharmacyID)
transaction({
id: 2820628...37,
prescription: Encrypt(JSON.stringify([{
name: 'Doliprane',
dosage: '3-6/day'
}]), pharmacy.pubKey)
})
Synchroniser
Consensus
Consensus de Nakamoto
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
}
}
Exécution
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))
})
}
}
Crypto !
Une blockchain n’est :
Si votre cas d’usage inclut :
La blockchain s’accomode mal du monde réel
Choisissez d’utiliser une blockchain avec précaution :
la techno est brillante en terme de cryptographie,
mais comme tout ce qui brille, elle n’est pas magique.
Ce n’est qu’un outil.
https://talks.m4dz.net/blockchain-at-heart/fr/ 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