This file contains 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
{ | |
"timestamp": 568583169.33294904, | |
"proof": 33575, | |
"transactions": [ | |
{ | |
"amount": 5, | |
"recipient": "9h34147fe1f5426f9dd645de4b27ee21", | |
"sender": "d4ee26eee15148ee92c6cd394edd974e" | |
}, | |
{ |
This file contains 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
/** | |
Creates a new transaction to go into the next mined Block | |
- Parameter sender: Address of the Sender | |
- Parameter recipient: Address of the Recipient | |
- Parameter amount: Amount | |
- returns: The index of the Block that will hold this transaction | |
*/ | |
func newTransaction(sender: String, recipient: String, amount: Int64) -> Int64 { | |
let transaction = Transaction(sender: sender, recipient: recipient, amount: amount) |
This file contains 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
// MARK: - Initializers | |
init() { | |
chain = [] | |
current_transactions = [] | |
nodes = Set() | |
// Create the genesis block | |
self.newBlock(previous_hash: "1", proof: 100) | |
} |
This file contains 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
/** | |
Simple Proof of Work Algorithm: | |
- Find a number p' such that hash(pp') contains leading 4 zeroes, where p is the previous p' | |
- p is the previous proof, and p' is the new proof | |
- Parameter: last_proof: Int64 | |
- returns: Int64 | |
*/ | |
func proofOfWork(last_proof: Int64) -> Int64 { | |
var proof: Int64 = 0 |
This file contains 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
// Instance of blockchain | |
let blockchain = Blockchain() | |
// Create a new router | |
let router = Router() | |
router.all(middleware: BodyParser()) | |
// Handle HTTP GET requests to / | |
router.get("/") { | |
request, response, next in |
This file contains 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 Blockchain: Chain { | |
// MARK: - Properties | |
... | |
var nodes: Set<String> | |
// MARK: - Initializers | |
init() { | |
This file contains 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
{ | |
"info": { | |
"_postman_id": "4378e49e-75dc-4772-be83-27806ad15b2c", | |
"name": "Swift Blockchain", | |
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" | |
}, | |
"item": [ | |
{ | |
"name": "Get Chain", | |
"protocolProfileBehavior": { |
This file contains 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
curl -X GET \ | |
http://localhost:5000/nodes/resolve \ |
This file contains 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
curl -X POST \ | |
http://localhost:5000/nodes/register \ | |
-H 'content-type: application/json' \ | |
-d '{ | |
"nodes": ["http://127.0.0.104:5000"] | |
}' |
This file contains 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
curl -X GET \ | |
http://localhost:5000/mine \ |