Skip to content

Instantly share code, notes, and snippets.

View harshvishu's full-sized avatar
🍥
Learning

हर्ष • Harsh • ハラシャ harshvishu

🍥
Learning
View GitHub Profile
@harshvishu
harshvishu / Block.json
Last active January 7, 2019 19:47
Swift-Blockchain-Block.json
{
"timestamp": 568583169.33294904,
"proof": 33575,
"transactions": [
{
"amount": 5,
"recipient": "9h34147fe1f5426f9dd645de4b27ee21",
"sender": "d4ee26eee15148ee92c6cd394edd974e"
},
{
@harshvishu
harshvishu / NewTransaction.swift
Created January 8, 2019 12:25
Swit-Blockchain-NewTransaction.swift
/**
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)
@harshvishu
harshvishu / NewBlock.swift
Last active January 8, 2019 12:52
Swift-Blockchain-New-Block
// MARK: - Initializers
init() {
chain = []
current_transactions = []
nodes = Set()
// Create the genesis block
self.newBlock(previous_hash: "1", proof: 100)
}
@harshvishu
harshvishu / Proof.swift
Created January 8, 2019 12:53
Swift-Blockchain-Proof
/**
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
@harshvishu
harshvishu / Swift-Blockchain-API.swift
Last active January 8, 2019 18:10
Swift-Blockchain-Server
// 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
@harshvishu
harshvishu / Swift-Blockchain-Node.swift
Last active January 8, 2019 15:58
Swift-Blockchain-Node
class Blockchain: Chain {
// MARK: - Properties
...
var nodes: Set<String>
// MARK: - Initializers
init() {
{
"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": {
@harshvishu
harshvishu / Swift-Blockchain-Chain-Resolve.sh
Created January 8, 2019 17:36
Swift-Blockchain-Curl-Resolve
curl -X GET \
http://localhost:5000/nodes/resolve \
@harshvishu
harshvishu / Swift-Blockchain-NewNode.sh
Created January 8, 2019 17:37
Swift-Blockchain-New-Node
curl -X POST \
http://localhost:5000/nodes/register \
-H 'content-type: application/json' \
-d '{
"nodes": ["http://127.0.0.104:5000"]
}'
@harshvishu
harshvishu / Swift-Blockchain-Chain-Mine.sh
Created January 8, 2019 17:38
Swift-Blockchain-Mine
curl -X GET \
http://localhost:5000/mine \