60 lines
1.5 KiB
Swift
60 lines
1.5 KiB
Swift
//
|
|
// 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)
|
|
}
|
|
}
|