Basic concept of blockchain
This commit is contained in:
34
Blockchain/Models/Blockchain.swift
Normal file
34
Blockchain/Models/Blockchain.swift
Normal file
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// Blockchain.swift
|
||||
// Blockchain
|
||||
//
|
||||
// Created by Victor BODINAUD on 27/02/2020.
|
||||
// Copyright © 2020 Victor BODINAUD. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class Blockchain {
|
||||
var chain = [Block]()
|
||||
|
||||
func createGenesisBlock(data: String) {
|
||||
let genesisBlock = Block()
|
||||
genesisBlock.data = data
|
||||
genesisBlock.previousHash = "0000"
|
||||
genesisBlock.index = 0
|
||||
genesisBlock.hash = genesisBlock.generateHash()
|
||||
chain.append(genesisBlock)
|
||||
print("Genesis block created -- hash: \(genesisBlock.hash ?? "")")
|
||||
}
|
||||
|
||||
func createBlock(data: String) {
|
||||
let newBlock = Block()
|
||||
newBlock.data = data
|
||||
newBlock.previousHash = chain[chain.count-1].hash
|
||||
newBlock.index = chain.count
|
||||
newBlock.hash = newBlock.generateHash()
|
||||
chain.append(newBlock)
|
||||
|
||||
print("Block \(newBlock.index ?? 0) created -- hash: \(newBlock.hash ?? "") previous hash: \(newBlock.previousHash ?? "") data: \(newBlock.data ?? "")")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user