Created
December 29, 2017 19:49
-
-
Save azamsharp/b5f0831823502aca17be65521027430d to your computer and use it in GitHub Desktop.
Block class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Block : Codable { | |
var index :Int = 0 | |
var dateCreated :String | |
var previousHash :String! | |
var hash :String! | |
var nonce :Int | |
var message :String = "" | |
private (set) var transactions :[Transaction] = [Transaction]() | |
var key :String { | |
get { | |
let transactionsData = try! JSONEncoder().encode(self.transactions) | |
let transactionsJSONString = String(data: transactionsData, encoding: .utf8) | |
return String(self.index) + self.dateCreated + self.previousHash + transactionsJSONString! + String(self.nonce) | |
} | |
} | |
func addTransaction(transaction :Transaction) { | |
self.transactions.append(transaction) | |
} | |
init() { | |
self.dateCreated = Date().toString() | |
self.nonce = 0 | |
self.message = "Mined a New Block" | |
} | |
init(transaction :Transaction) { | |
self.dateCreated = Date().toString() | |
self.nonce = 0 | |
self.addTransaction(transaction: transaction) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment