Skip to content

Instantly share code, notes, and snippets.

View bajcmartinez's full-sized avatar

Juan Cruz Martinez bajcmartinez

View GitHub Profile
@bajcmartinez
bajcmartinez / slack-dark-theme.sh
Created June 4, 2019 19:23
Enable Slack Dark Mode for MAC OS
#!/bin/bash
# Updates your slack installation on MAC to use dark mode
# adjust this URL for your chosen darkmode.
theme_url="https://cdn.rawgit.com/laCour/slack-night-mode/master/css/raw/black.css"
ssbj_dir="/Applications/Slack.app/Contents/Resources/app.asar.unpacked/src/static/"
# ensure we are root
if [[ "$EUID" -ne 0 ]]; then
@bajcmartinez
bajcmartinez / Configuration for development with a corporate proxy
Last active July 2, 2019 19:40
Configuration for development with a corporate proxy
Just for title
@bajcmartinez
bajcmartinez / block.py
Created May 18, 2020 18:55
From Zero to Blockchain in Python - Part 1 - Block
import hashlib
from api.schema.block import BlockSchema
from time import time
class Block:
def __init__(self, index, transactions, nonce, previous_hash):
"""
Constructs a new block
@bajcmartinez
bajcmartinez / create_transaction.py
Created May 18, 2020 19:07
From Zero to Blockchain in Python - Part 1 - Create Transaction
def create_transaction(self, sender, recipient, amount):
"""
Creates a new transaction to go into the next block
:param sender: <str> sender address
:param recipient: <str> recipient address
:param amount: <float> amount
:return: <Transaction> generated transaction
"""
transaction = Transaction(sender, recipient, amount)
@bajcmartinez
bajcmartinez / constructor.py
Created May 18, 2020 19:08
From Zero to Blockchain in Python - Part 1 - Constructor
def __init__(self):
self.__current_transactions = []
self.__chain = []
# Create genesis block
self.create_genesis()
def create_genesis(self):
"""
Creates the Genesis block and passes it to the chain
@bajcmartinez
bajcmartinez / validate_proof_of_work.py
Created May 18, 2020 19:13
From Zero to Blockchain in Python - Part 1 - PoW
@staticmethod
def validate_proof_of_work(last_nonce, last_hash, nonce):
"""
Validates the nonce
:param last_nonce: <int> Nonce of the last block
:param nonce: <int> Current nonce to be validated
:param last_hash: <str> Hash of the last block
:return: <bool> True if correct, False if not.
"""
@bajcmartinez
bajcmartinez / add_blocks.py
Created May 18, 2020 19:19
From Zero to Blockchain in Python - Part 1 - Add Blocks
def add_block(self, block):
"""
Creates a new block and passes it to the chain
:param block: <Block> Block to add to the chain
:return: <bool> True if the block is added to the chain, False if not.
"""
if self.validate_block(block, self.last_block):
self.__chain.append(block)
@bajcmartinez
bajcmartinez / replace_blockchain.py
Created May 18, 2020 19:22
From Zero to Blockchain in Python - Part 1 - Replace Blockchain
def validate_chain(self, chain_to_validate):
"""
Verifies if a given chain is valid
:param chain_to_validate: <[Block]>
:return: <bool> True if the chain is valid
"""
# First validate both genesis blocks
if chain_to_validate[0].hash_block() != self.__chain[0].hash_block():
return False
@bajcmartinez
bajcmartinez / transaction.py
Created May 18, 2020 18:49
From Zero to Blockchain in Python - Part 1 - Transaction
import time
class Transaction:
def __init__(self, sender, recipient, amount):
"""
Creates a new transaction
:param sender: <str> sender account
:param recipient: <str> recipient account
@bajcmartinez
bajcmartinez / mine.py
Created May 18, 2020 19:17
From Zero to Blockchain in Python - Part 1 - Mine
def mine(self, reward_address):
"""
Mines a new block into the chain
:param reward_address: <str> address where the reward coin will be transferred to
:return: result of the mining attempt and the new block
"""
last_block = self.last_block
index = last_block.index + 1
previous_hash = last_block.hash