Skip to content

Instantly share code, notes, and snippets.

View aunyks's full-sized avatar
Increasing potential

Gerald Nash aunyks

Increasing potential
View GitHub Profile
# ...blockchain
# ...Block class definition
miner_address = "q3nf394hjg-random-miner-address-34nf3i4nflkn3oi"
def proof_of_work(last_proof):
# Create a variable that we will use to find
# our next proof of work
incrementor = last_proof + 1
# Keep incrementing the incrementor until
@node.route('/blocks', methods=['GET'])
def get_blocks():
chain_to_send = blockchain
# Convert our blocks into dictionaries
# so we can send them as json objects later
for block in chain_to_send:
block_index = str(block.index)
block_timestamp = str(block.timestamp)
block_data = str(block.data)
block_hash = block.hash
@aunyks
aunyks / snakecoin-server-full-code.py
Last active March 8, 2024 19:22
The code in this gist isn't as succinct as I'd like it to be. Please bare with me and ask plenty of questions that you may have about it.
from flask import Flask
from flask import request
import json
import requests
import hashlib as hasher
import datetime as date
node = Flask(__name__)
# Define what a Snakecoin block is
class Block:
var crypto = require('crypto');
// Create a new random 32-byte private key.
function createPrivateKey(){
return crypto.randomBytes(32);
}
var eccrypto = require('eccrypto');
// Uncompressed (65-byte) public key
// that corresponds to the given private key
function getPublicKey(privateKey){
return eccrypto.getPublic(privateKey);
}
var crypto = require('crypto');
function createAddress(publicKey){
let sha256 = crypto.createHash("sha256");
sha256.update(publicKey);
return sha256.digest('hex');
}
function createWallet(){
let privateKey = createPrivateKey();
let publicKey = getPublicKey(privateKey);
let address = createAddress(publicKey);
return ({
private: privateKey.toString('hex'),
public: publicKey.toString('hex'),
address: address
});
}
var request = require('request');
function createTransaction(fromAddress, toAddress, amount, peerUrl){
// Send a post request
request.post(
// ...to the provided url
peerUrl,
{
// ...in JSON
json: {
// Grabbed from: https://github.com/ethereum/EIPs/issues/20
contract ERC20 {
function totalSupply() constant returns (uint theTotalSupply);
function balanceOf(address _owner) constant returns (uint balance);
function transfer(address _to, uint _value) returns (bool success);
function transferFrom(address _from, address _to, uint _value) returns (bool success);
function approve(address _spender, uint _value) returns (bool success);
function allowance(address _owner, address _spender) constant returns (uint remaining);
event Transfer(address indexed _from, address indexed _to, uint _value);
event Approval(address indexed _owner, address indexed _spender, uint _value);
contract MyERCToken {
// In this case, the total supply
// of MyERCToken is fixed, but
// it can very much be changed
uint256 _totalSupply = 1000000;
function totalSupply() constant returns (uint256 theTotalSupply) {
// Because our function signature
// states that the returning variable
// is "theTotalSupply", we'll just set that variable