diff --git a/Blockchain.xcodeproj/project.pbxproj b/Blockchain.xcodeproj/project.pbxproj index ec86c69..b3ae72c 100644 --- a/Blockchain.xcodeproj/project.pbxproj +++ b/Blockchain.xcodeproj/project.pbxproj @@ -8,6 +8,8 @@ /* Begin PBXBuildFile section */ B5E66EBD2407FDE000E89D17 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E66EBC2407FDE000E89D17 /* main.swift */; }; + B5E66EC52407FE0900E89D17 /* Block.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E66EC42407FE0900E89D17 /* Block.swift */; }; + B5E66EC72407FEC800E89D17 /* Blockchain.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E66EC62407FEC800E89D17 /* Blockchain.swift */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -25,6 +27,8 @@ /* Begin PBXFileReference section */ B5E66EB92407FDE000E89D17 /* Blockchain */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Blockchain; sourceTree = BUILT_PRODUCTS_DIR; }; B5E66EBC2407FDE000E89D17 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; + B5E66EC42407FE0900E89D17 /* Block.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Block.swift; sourceTree = ""; }; + B5E66EC62407FEC800E89D17 /* Blockchain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Blockchain.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -57,11 +61,21 @@ B5E66EBB2407FDE000E89D17 /* Blockchain */ = { isa = PBXGroup; children = ( + B5E66EC32407FDEC00E89D17 /* Models */, B5E66EBC2407FDE000E89D17 /* main.swift */, ); path = Blockchain; sourceTree = ""; }; + B5E66EC32407FDEC00E89D17 /* Models */ = { + isa = PBXGroup; + children = ( + B5E66EC42407FE0900E89D17 /* Block.swift */, + B5E66EC62407FEC800E89D17 /* Blockchain.swift */, + ); + path = Models; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -121,6 +135,8 @@ buildActionMask = 2147483647; files = ( B5E66EBD2407FDE000E89D17 /* main.swift in Sources */, + B5E66EC52407FE0900E89D17 /* Block.swift in Sources */, + B5E66EC72407FEC800E89D17 /* Blockchain.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Blockchain.xcodeproj/xcuserdata/vbodinaud.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Blockchain.xcodeproj/xcuserdata/vbodinaud.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..27d5527 --- /dev/null +++ b/Blockchain.xcodeproj/xcuserdata/vbodinaud.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,6 @@ + + + diff --git a/Blockchain/Models/Block.swift b/Blockchain/Models/Block.swift new file mode 100644 index 0000000..4573654 --- /dev/null +++ b/Blockchain/Models/Block.swift @@ -0,0 +1,25 @@ +// +// Block.swift +// Blockchain +// +// Created by Victor BODINAUD on 27/02/2020. +// Copyright © 2020 Victor BODINAUD. All rights reserved. +// + +import Foundation +import CryptoKit + +class Block { + var hash: String! + var data: String! + var previousHash: String! + var index: Int! + + func generateHash() -> String { + if let hashData = data.data(using: .utf8) { + _ = SHA256.hash(data: hashData) + } + + return NSUUID().uuidString.replacingOccurrences(of: "-", with: "") + } +} diff --git a/Blockchain/Models/Blockchain.swift b/Blockchain/Models/Blockchain.swift new file mode 100644 index 0000000..d0ded70 --- /dev/null +++ b/Blockchain/Models/Blockchain.swift @@ -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 ?? "")") + } +} diff --git a/Blockchain/main.swift b/Blockchain/main.swift index 6010d29..e0d7c02 100644 --- a/Blockchain/main.swift +++ b/Blockchain/main.swift @@ -8,5 +8,25 @@ import Foundation -print("Hello, World!") +let blockchain = Blockchain() +var command: String? +blockchain.createGenesisBlock(data: "") + +repeat { + print("Enter command:") + command = readLine() + + if command == "create" { + print("data for the new block:") + let data = readLine() + blockchain.createBlock(data: data ?? "") + } + + if command == "spam" { + for _ in 1...1000000 { + blockchain.createBlock(data: "\((blockchain.chain.last?.index ?? 0)+1)") + } + } + +} while command != "exit"