Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5db6a5bfe2 | ||
|
|
3d88a95823 | ||
|
|
bf7c7e7144 | ||
|
|
bb6992cadb | ||
|
|
fcf488b065 | ||
|
|
33d508fb44 | ||
|
|
00bc56adc3 | ||
|
|
b8847bd8a9 | ||
|
|
52ea558811 | ||
|
|
b32a6e6bbb | ||
|
|
5c83114197 | ||
|
|
963a9b074a |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/.build
|
||||
*.xcuserstate
|
||||
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>IDEDidComputeMac32BitWarning</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -1,12 +0,0 @@
|
||||
//
|
||||
// main.swift
|
||||
// Blockchain
|
||||
//
|
||||
// Created by Victor BODINAUD on 27/02/2020.
|
||||
// Copyright © 2020 Victor BODINAUD. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
print("Hello, World!")
|
||||
|
||||
@@ -3,15 +3,15 @@
|
||||
archiveVersion = 1;
|
||||
classes = {
|
||||
};
|
||||
objectVersion = 50;
|
||||
objectVersion = 77;
|
||||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
B5E66EBD2407FDE000E89D17 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5E66EBC2407FDE000E89D17 /* main.swift */; };
|
||||
57BA298C2CF77907009E4448 /* CryptoSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 57BA298B2CF77907009E4448 /* CryptoSwift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXCopyFilesBuildPhase section */
|
||||
B5E66EB72407FDE000E89D17 /* CopyFiles */ = {
|
||||
57BA297E2CF778F5009E4448 /* CopyFiles */ = {
|
||||
isa = PBXCopyFilesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
dstPath = /usr/share/man/man1/;
|
||||
@@ -23,118 +23,127 @@
|
||||
/* End PBXCopyFilesBuildPhase section */
|
||||
|
||||
/* 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 = "<group>"; };
|
||||
57BA29802CF778F5009E4448 /* SwiftChain */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = SwiftChain; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFileSystemSynchronizedRootGroup section */
|
||||
57BA29822CF778F5009E4448 /* SwiftChain */ = {
|
||||
isa = PBXFileSystemSynchronizedRootGroup;
|
||||
path = SwiftChain;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
/* End PBXFileSystemSynchronizedRootGroup section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
B5E66EB62407FDE000E89D17 /* Frameworks */ = {
|
||||
57BA297D2CF778F5009E4448 /* Frameworks */ = {
|
||||
isa = PBXFrameworksBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
57BA298C2CF77907009E4448 /* CryptoSwift in Frameworks */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXFrameworksBuildPhase section */
|
||||
|
||||
/* Begin PBXGroup section */
|
||||
B5E66EB02407FDE000E89D17 = {
|
||||
57BA29772CF778F5009E4448 = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
B5E66EBB2407FDE000E89D17 /* Blockchain */,
|
||||
B5E66EBA2407FDE000E89D17 /* Products */,
|
||||
57BA29822CF778F5009E4448 /* SwiftChain */,
|
||||
57BA29812CF778F5009E4448 /* Products */,
|
||||
);
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
B5E66EBA2407FDE000E89D17 /* Products */ = {
|
||||
57BA29812CF778F5009E4448 /* Products */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
B5E66EB92407FDE000E89D17 /* Blockchain */,
|
||||
57BA29802CF778F5009E4448 /* SwiftChain */,
|
||||
);
|
||||
name = Products;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
B5E66EBB2407FDE000E89D17 /* Blockchain */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
B5E66EBC2407FDE000E89D17 /* main.swift */,
|
||||
);
|
||||
path = Blockchain;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
/* End PBXGroup section */
|
||||
|
||||
/* Begin PBXNativeTarget section */
|
||||
B5E66EB82407FDE000E89D17 /* Blockchain */ = {
|
||||
57BA297F2CF778F5009E4448 /* SwiftChain */ = {
|
||||
isa = PBXNativeTarget;
|
||||
buildConfigurationList = B5E66EC02407FDE000E89D17 /* Build configuration list for PBXNativeTarget "Blockchain" */;
|
||||
buildConfigurationList = 57BA29872CF778F5009E4448 /* Build configuration list for PBXNativeTarget "SwiftChain" */;
|
||||
buildPhases = (
|
||||
B5E66EB52407FDE000E89D17 /* Sources */,
|
||||
B5E66EB62407FDE000E89D17 /* Frameworks */,
|
||||
B5E66EB72407FDE000E89D17 /* CopyFiles */,
|
||||
57BA297C2CF778F5009E4448 /* Sources */,
|
||||
57BA297D2CF778F5009E4448 /* Frameworks */,
|
||||
57BA297E2CF778F5009E4448 /* CopyFiles */,
|
||||
);
|
||||
buildRules = (
|
||||
);
|
||||
dependencies = (
|
||||
);
|
||||
name = Blockchain;
|
||||
productName = Blockchain;
|
||||
productReference = B5E66EB92407FDE000E89D17 /* Blockchain */;
|
||||
fileSystemSynchronizedGroups = (
|
||||
57BA29822CF778F5009E4448 /* SwiftChain */,
|
||||
);
|
||||
name = SwiftChain;
|
||||
packageProductDependencies = (
|
||||
57BA298B2CF77907009E4448 /* CryptoSwift */,
|
||||
);
|
||||
productName = SwiftChain;
|
||||
productReference = 57BA29802CF778F5009E4448 /* SwiftChain */;
|
||||
productType = "com.apple.product-type.tool";
|
||||
};
|
||||
/* End PBXNativeTarget section */
|
||||
|
||||
/* Begin PBXProject section */
|
||||
B5E66EB12407FDE000E89D17 /* Project object */ = {
|
||||
57BA29782CF778F5009E4448 /* Project object */ = {
|
||||
isa = PBXProject;
|
||||
attributes = {
|
||||
LastSwiftUpdateCheck = 1130;
|
||||
LastUpgradeCheck = 1130;
|
||||
ORGANIZATIONNAME = "Victor BODINAUD";
|
||||
BuildIndependentTargetsInParallel = 1;
|
||||
LastSwiftUpdateCheck = 1610;
|
||||
LastUpgradeCheck = 1610;
|
||||
TargetAttributes = {
|
||||
B5E66EB82407FDE000E89D17 = {
|
||||
CreatedOnToolsVersion = 11.3.1;
|
||||
57BA297F2CF778F5009E4448 = {
|
||||
CreatedOnToolsVersion = 16.1;
|
||||
};
|
||||
};
|
||||
};
|
||||
buildConfigurationList = B5E66EB42407FDE000E89D17 /* Build configuration list for PBXProject "Blockchain" */;
|
||||
compatibilityVersion = "Xcode 9.3";
|
||||
buildConfigurationList = 57BA297B2CF778F5009E4448 /* Build configuration list for PBXProject "SwiftChain" */;
|
||||
developmentRegion = en;
|
||||
hasScannedForEncodings = 0;
|
||||
knownRegions = (
|
||||
en,
|
||||
Base,
|
||||
);
|
||||
mainGroup = B5E66EB02407FDE000E89D17;
|
||||
productRefGroup = B5E66EBA2407FDE000E89D17 /* Products */;
|
||||
mainGroup = 57BA29772CF778F5009E4448;
|
||||
minimizedProjectReferenceProxies = 1;
|
||||
packageReferences = (
|
||||
57BA298A2CF77907009E4448 /* XCRemoteSwiftPackageReference "CryptoSwift" */,
|
||||
);
|
||||
preferredProjectObjectVersion = 77;
|
||||
productRefGroup = 57BA29812CF778F5009E4448 /* Products */;
|
||||
projectDirPath = "";
|
||||
projectRoot = "";
|
||||
targets = (
|
||||
B5E66EB82407FDE000E89D17 /* Blockchain */,
|
||||
57BA297F2CF778F5009E4448 /* SwiftChain */,
|
||||
);
|
||||
};
|
||||
/* End PBXProject section */
|
||||
|
||||
/* Begin PBXSourcesBuildPhase section */
|
||||
B5E66EB52407FDE000E89D17 /* Sources */ = {
|
||||
57BA297C2CF778F5009E4448 /* Sources */ = {
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
B5E66EBD2407FDE000E89D17 /* main.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXSourcesBuildPhase section */
|
||||
|
||||
/* Begin XCBuildConfiguration section */
|
||||
B5E66EBE2407FDE000E89D17 /* Debug */ = {
|
||||
57BA29852CF778F5009E4448 /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
||||
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
|
||||
CLANG_ANALYZER_NONNULL = YES;
|
||||
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
|
||||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_ENABLE_OBJC_WEAK = YES;
|
||||
@@ -153,6 +162,7 @@
|
||||
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
|
||||
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
|
||||
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
|
||||
CLANG_WARN_STRICT_PROTOTYPES = YES;
|
||||
CLANG_WARN_SUSPICIOUS_MOVE = YES;
|
||||
@@ -163,7 +173,8 @@
|
||||
DEBUG_INFORMATION_FORMAT = dwarf;
|
||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
||||
ENABLE_TESTABILITY = YES;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu11;
|
||||
ENABLE_USER_SCRIPT_SANDBOXING = YES;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu17;
|
||||
GCC_DYNAMIC_NO_PIC = NO;
|
||||
GCC_NO_COMMON_BLOCKS = YES;
|
||||
GCC_OPTIMIZATION_LEVEL = 0;
|
||||
@@ -177,24 +188,25 @@
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
MACOSX_DEPLOYMENT_TARGET = 10.15;
|
||||
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
|
||||
MACOSX_DEPLOYMENT_TARGET = 15.1;
|
||||
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
|
||||
MTL_FAST_MATH = YES;
|
||||
ONLY_ACTIVE_ARCH = YES;
|
||||
SDKROOT = macosx;
|
||||
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
|
||||
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)";
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
B5E66EBF2407FDE000E89D17 /* Release */ = {
|
||||
57BA29862CF778F5009E4448 /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
||||
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
|
||||
CLANG_ANALYZER_NONNULL = YES;
|
||||
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
|
||||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_ENABLE_OBJC_WEAK = YES;
|
||||
@@ -213,6 +225,7 @@
|
||||
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
|
||||
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
|
||||
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
|
||||
CLANG_WARN_STRICT_PROTOTYPES = YES;
|
||||
CLANG_WARN_SUSPICIOUS_MOVE = YES;
|
||||
@@ -223,7 +236,8 @@
|
||||
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
|
||||
ENABLE_NS_ASSERTIONS = NO;
|
||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu11;
|
||||
ENABLE_USER_SCRIPT_SANDBOXING = YES;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu17;
|
||||
GCC_NO_COMMON_BLOCKS = YES;
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
@@ -231,16 +245,16 @@
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
MACOSX_DEPLOYMENT_TARGET = 10.15;
|
||||
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
|
||||
MACOSX_DEPLOYMENT_TARGET = 15.1;
|
||||
MTL_ENABLE_DEBUG_INFO = NO;
|
||||
MTL_FAST_MATH = YES;
|
||||
SDKROOT = macosx;
|
||||
SWIFT_COMPILATION_MODE = wholemodule;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-O";
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
B5E66EC12407FDE000E89D17 /* Debug */ = {
|
||||
57BA29882CF778F5009E4448 /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
@@ -251,7 +265,7 @@
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
B5E66EC22407FDE000E89D17 /* Release */ = {
|
||||
57BA29892CF778F5009E4448 /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
@@ -265,25 +279,44 @@
|
||||
/* End XCBuildConfiguration section */
|
||||
|
||||
/* Begin XCConfigurationList section */
|
||||
B5E66EB42407FDE000E89D17 /* Build configuration list for PBXProject "Blockchain" */ = {
|
||||
57BA297B2CF778F5009E4448 /* Build configuration list for PBXProject "SwiftChain" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
B5E66EBE2407FDE000E89D17 /* Debug */,
|
||||
B5E66EBF2407FDE000E89D17 /* Release */,
|
||||
57BA29852CF778F5009E4448 /* Debug */,
|
||||
57BA29862CF778F5009E4448 /* Release */,
|
||||
);
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
B5E66EC02407FDE000E89D17 /* Build configuration list for PBXNativeTarget "Blockchain" */ = {
|
||||
57BA29872CF778F5009E4448 /* Build configuration list for PBXNativeTarget "SwiftChain" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
B5E66EC12407FDE000E89D17 /* Debug */,
|
||||
B5E66EC22407FDE000E89D17 /* Release */,
|
||||
57BA29882CF778F5009E4448 /* Debug */,
|
||||
57BA29892CF778F5009E4448 /* Release */,
|
||||
);
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
/* End XCConfigurationList section */
|
||||
|
||||
/* Begin XCRemoteSwiftPackageReference section */
|
||||
57BA298A2CF77907009E4448 /* XCRemoteSwiftPackageReference "CryptoSwift" */ = {
|
||||
isa = XCRemoteSwiftPackageReference;
|
||||
repositoryURL = "https://github.com/krzyzanowskim/CryptoSwift.git";
|
||||
requirement = {
|
||||
kind = upToNextMajorVersion;
|
||||
minimumVersion = 1.8.3;
|
||||
};
|
||||
rootObject = B5E66EB12407FDE000E89D17 /* Project object */;
|
||||
};
|
||||
/* End XCRemoteSwiftPackageReference section */
|
||||
|
||||
/* Begin XCSwiftPackageProductDependency section */
|
||||
57BA298B2CF77907009E4448 /* CryptoSwift */ = {
|
||||
isa = XCSwiftPackageProductDependency;
|
||||
package = 57BA298A2CF77907009E4448 /* XCRemoteSwiftPackageReference "CryptoSwift" */;
|
||||
productName = CryptoSwift;
|
||||
};
|
||||
/* End XCSwiftPackageProductDependency section */
|
||||
};
|
||||
rootObject = 57BA29782CF778F5009E4448 /* Project object */;
|
||||
}
|
||||
@@ -2,6 +2,6 @@
|
||||
<Workspace
|
||||
version = "1.0">
|
||||
<FileRef
|
||||
location = "self:Blockchain.xcodeproj">
|
||||
location = "self:">
|
||||
</FileRef>
|
||||
</Workspace>
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"originHash" : "72c2bbc73844ab67fc7d9913c0750200cb8c2703ae8ac776acd11affc367875c",
|
||||
"pins" : [
|
||||
{
|
||||
"identity" : "cryptoswift",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/krzyzanowskim/CryptoSwift.git",
|
||||
"state" : {
|
||||
"revision" : "678d442c6f7828def400a70ae15968aef67ef52d",
|
||||
"version" : "1.8.3"
|
||||
}
|
||||
}
|
||||
],
|
||||
"version" : 3
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
<dict>
|
||||
<key>SchemeUserState</key>
|
||||
<dict>
|
||||
<key>Blockchain.xcscheme_^#shared#^_</key>
|
||||
<key>SwiftChain.xcscheme_^#shared#^_</key>
|
||||
<dict>
|
||||
<key>orderHint</key>
|
||||
<integer>0</integer>
|
||||
18
SwiftChain/Models/Account.swift
Normal file
18
SwiftChain/Models/Account.swift
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Victor BODINAUD on 31/03/2021.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class Account {
|
||||
var id: String
|
||||
var balance: Int
|
||||
|
||||
init(id: String, balance: Int) {
|
||||
self.id = id
|
||||
self.balance = balance
|
||||
}
|
||||
}
|
||||
59
SwiftChain/Models/Block.swift
Normal file
59
SwiftChain/Models/Block.swift
Normal file
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// Block.swift
|
||||
// Blockchain
|
||||
//
|
||||
// Created by Victor BODINAUD on 27/02/2020.
|
||||
// Copyright © 2020 Victor BODINAUD. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CryptoSwift
|
||||
|
||||
class Block {
|
||||
var hash: String
|
||||
var data: String
|
||||
var previousHash: String
|
||||
var index: Int
|
||||
var nonce: Int
|
||||
var timestamp: Int
|
||||
|
||||
/**
|
||||
Initialize a block with the provided parts and specifications.
|
||||
|
||||
- parameters:
|
||||
- hash: The hash of the block
|
||||
- data: The data of the block
|
||||
- previousHash: The hash of the previous block
|
||||
- index: The index of the block
|
||||
- nonce: The nonce of the block
|
||||
- timestamp: The timestamp of the block
|
||||
|
||||
- returns: A beautiful new block for the blockchain.
|
||||
*/
|
||||
init(hash: String = "", data: String = "", previousHash: String = "", index: Int = 0, nonce: Int = 0, timestamp: Int = 0) {
|
||||
self.data = data
|
||||
self.previousHash = previousHash
|
||||
self.index = index
|
||||
self.nonce = nonce
|
||||
self.timestamp = timestamp
|
||||
self.hash = hash
|
||||
}
|
||||
|
||||
/**
|
||||
Generate the hash of the block.
|
||||
|
||||
- returns: The hash of the block
|
||||
*/
|
||||
func generateHash() -> String {
|
||||
return data.sha256()
|
||||
}
|
||||
|
||||
/**
|
||||
Generate the timestamp of the block
|
||||
|
||||
- returns: The timestamp of the block
|
||||
*/
|
||||
func generateTimestamp() -> Int {
|
||||
return Int(Date().timeIntervalSince1970)
|
||||
}
|
||||
}
|
||||
88
SwiftChain/Models/Blockchain.swift
Normal file
88
SwiftChain/Models/Blockchain.swift
Normal file
@@ -0,0 +1,88 @@
|
||||
//
|
||||
// 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]()
|
||||
|
||||
/**
|
||||
Initialize the first block of the blockchain.
|
||||
|
||||
- Parameters:
|
||||
- data: The datas of the block
|
||||
*/
|
||||
func createGenesisBlock(data: String) {
|
||||
let genesisBlock = Block()
|
||||
genesisBlock.data = data
|
||||
genesisBlock.previousHash = "0000"
|
||||
genesisBlock.index = 0
|
||||
genesisBlock.nonce = 0
|
||||
genesisBlock.timestamp = genesisBlock.generateTimestamp()
|
||||
genesisBlock.hash = genesisBlock.generateHash()
|
||||
chain.append(genesisBlock)
|
||||
print("Genesis block created -- hash: \(genesisBlock.hash)")
|
||||
}
|
||||
|
||||
/**
|
||||
Initialize a new block of the blockchain.
|
||||
|
||||
- Parameters:
|
||||
- data: The datas of the block
|
||||
*/
|
||||
func createBlock(data: String) {
|
||||
let newBlock = Block()
|
||||
newBlock.data = data
|
||||
newBlock.previousHash = chain.last!.hash
|
||||
newBlock.index = chain.count
|
||||
newBlock.nonce = 0
|
||||
newBlock.timestamp = newBlock.generateTimestamp()
|
||||
newBlock.hash = newBlock.generateHash()
|
||||
chain.append(newBlock)
|
||||
|
||||
print("-- Block \(newBlock.index) created --\n hash: \(newBlock.hash)\n previous hash: \(newBlock.previousHash)\n data: \(newBlock.data)")
|
||||
}
|
||||
|
||||
/**
|
||||
Insert a corrupted block in the blockhain.
|
||||
(for testing purpose)
|
||||
*/
|
||||
func insertCorruptedBlock() {
|
||||
let newCorruptedBlock = Block()
|
||||
newCorruptedBlock.data = "Corrupted block"
|
||||
newCorruptedBlock.previousHash = "1234567890"
|
||||
newCorruptedBlock.index = chain.count
|
||||
newCorruptedBlock.nonce = 0
|
||||
newCorruptedBlock.timestamp = newCorruptedBlock.generateTimestamp()
|
||||
newCorruptedBlock.hash = newCorruptedBlock.generateHash()
|
||||
chain.append(newCorruptedBlock)
|
||||
|
||||
print("-- Corrupted block \(newCorruptedBlock.index) created --\n hash: \(newCorruptedBlock.hash)\n previous hash: \(newCorruptedBlock.previousHash)\n data: \(newCorruptedBlock.data)")
|
||||
}
|
||||
|
||||
/**
|
||||
Check validity of the blockchain.
|
||||
*/
|
||||
func chainValidity() {
|
||||
var isChainValid = true
|
||||
var corruptedBlock = Block()
|
||||
|
||||
for i in 1...chain.count-1 {
|
||||
if chain[i].previousHash != chain[i-1].hash {
|
||||
isChainValid = false
|
||||
corruptedBlock = chain[i]
|
||||
}
|
||||
}
|
||||
|
||||
print("Chain is valid : \(isChainValid)")
|
||||
|
||||
if !isChainValid {
|
||||
print("Corrupted block is : \(corruptedBlock.index)")
|
||||
}
|
||||
}
|
||||
}
|
||||
22
SwiftChain/Models/Transaction.swift
Normal file
22
SwiftChain/Models/Transaction.swift
Normal file
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Victor BODINAUD on 31/03/2021.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class Transaction {
|
||||
var sender: String
|
||||
var receiver: String
|
||||
var amount: Int
|
||||
var type: String
|
||||
|
||||
init(sender: String, receiver: String, amount: Int, type: String) {
|
||||
self.sender = sender
|
||||
self.receiver = receiver
|
||||
self.amount = amount
|
||||
self.type = type
|
||||
}
|
||||
}
|
||||
44
SwiftChain/main.swift
Normal file
44
SwiftChain/main.swift
Normal file
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// main.swift
|
||||
// Blockchain
|
||||
//
|
||||
// Created by Victor BODINAUD on 27/02/2020.
|
||||
// Copyright © 2020 Victor BODINAUD. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
let blockchain = Blockchain()
|
||||
var command: String?
|
||||
|
||||
blockchain.createGenesisBlock(data: "")
|
||||
|
||||
repeat {
|
||||
print("Enter command:")
|
||||
command = readLine()
|
||||
|
||||
// Create a new block
|
||||
if command == "create" {
|
||||
print("data for the new block:")
|
||||
let data = readLine()
|
||||
blockchain.createBlock(data: data ?? "")
|
||||
}
|
||||
|
||||
// Create a lot of blocks
|
||||
if command == "spam" {
|
||||
for _ in 1...1000 {
|
||||
blockchain.createBlock(data: "\((blockchain.chain.last?.index ?? 0)+1)")
|
||||
}
|
||||
}
|
||||
|
||||
// Check validity of the blockchain
|
||||
if command == "validity" {
|
||||
blockchain.chainValidity()
|
||||
}
|
||||
|
||||
// Insert a corrupted block
|
||||
if command == "corrupt" {
|
||||
blockchain.insertCorruptedBlock()
|
||||
}
|
||||
|
||||
} while command != "exit"
|
||||
Reference in New Issue
Block a user