35 lines
1.0 KiB
Swift
35 lines
1.0 KiB
Swift
//
|
|
// 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 ?? "")")
|
|
}
|
|
}
|