Skip to content

Instantly share code, notes, and snippets.

View michielmulders's full-sized avatar

Michiel Mulders michielmulders

View GitHub Profile
@michielmulders
michielmulders / main.js
Created December 28, 2017 14:47
Build Your Own Blockchain with JavaScript
const SHA256 = require('crypto-js/sha256');
class Block {
constructor(index, timestamp, data, previousHash = '') {
this.index = index;
this.previousHash = previousHash;
this.timestamp = timestamp;
this.data = data;
this.hash = this.calculateHash();
this.nonce = 0;
@michielmulders
michielmulders / erc777-burn.sol
Created February 17, 2018 23:45
ERC777 Burn implementation interface Ethereum
/// @notice Burns `_amount` tokens from `_tokenHolder`
/// Sample burn function to showcase the use of the `Burned` event.
/// @param _tokenHolder The address that will lose the tokens
/// @param _amount The quantity of tokens to burn
function burn(address _tokenHolder, uint256 _amount, bytes _userData, bytes _operatorData) public onlyOwner {
requireMultiple(_amount);
require(balanceOf(_tokenHolder) >= _amount);
mBalances[_tokenHolder] = mBalances[_tokenHolder].sub(_amount);
mTotalSupply = mTotalSupply.sub(_amount);
@michielmulders
michielmulders / chaincode.go
Created March 5, 2018 14:23
Hyperledger not-complete chaincode example from Fabcar
// Define the Smart Contract structure
type SmartContract struct { }
// Define the car structure, with 4 properties.
type Car struct {
Make string `json:"make"`
Model string `json:"model"`
Colour string `json:"colour"`
Owner string `json:"owner"`
}
@michielmulders
michielmulders / nem-transfer.js
Created March 5, 2018 18:20
Nem Blockchain Transfer Transaction
// Include the library
var nem = require("../../build/index.js").default;
// Create an NIS endpoint object
var endpoint = nem.model.objects.create("endpoint")(nem.model.nodes.defaultTestnet, nem.model.nodes.defaultPort);
// Create a common object holding key
var common = nem.model.objects.create("common")("", "Private key");
// Create an un-prepared transfer transaction object
@michielmulders
michielmulders / erc223.sol
Created March 7, 2018 11:14
ERC223 New Transfer function
function transfer(address _to, uint _value, bytes _data) returns (bool success)
@michielmulders
michielmulders / MyFirstToken.sol
Created March 30, 2018 11:22
My First Solidity ERC20 ERC223 Token
pragma solidity ^0.4.0;
interface ERC20 {
function transferFrom(address _from, address _to, uint _value) public returns (bool);
function approve(address _spender, uint _value) public returns (bool);
function allowance(address _owner, address _spender) public constant returns (uint);
event Approval(address indexed _owner, address indexed _spender, uint _value);
}
interface ERC223 {
@michielmulders
michielmulders / facetoken.sol
Created March 30, 2018 13:19
FaceToken ERC20 Full Contract Solidity Code
library SafeMath {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a * b;
assert(a == 0 || c / a == b);
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint256 c = a / b;
@michielmulders
michielmulders / batchTransfer.js
Created April 27, 2018 20:04
BatchOverflow Bug ERC20 - Not using SafeMath Lib
function batchTransfer(address[] _receivers, uint256 _value) public whenNotPaused returns (bool) {
uint cnt = _receivers.length;
uint256 amount = uint256(cnt) * _value;
require(cnt > 0 && cnt <= 20);
require(_value > 0 && balances[msg.sender] >= amount);
balances[msg.sender] = balances[msg.sender].sub(amount);
for (uint i = 0; i < cnt; i++) {
balances[_receivers[i]] = balances[_receivers[i]].add(_value);
Transfer(msg.sender, _receivers[i], _value);
@michielmulders
michielmulders / cryptoconditions.js
Created March 28, 2018 07:49
BigchainDB crypto conditions
import { Router } from "express";
import logger from "logops";
import * as driver from "bigchaindb-driver";
import conn from "../helpers/bigchaindb";
const base58 = require("bs58");
const cryptoconditions = require("crypto-conditions");
const router = Router();
export default router;
@michielmulders
michielmulders / blockwatcher.sol
Created May 24, 2018 08:52
Blocknumber Watcher Solidity Ethereum
function f( blocknumber, to_address, value_) {
var filter = web3.eth.filter('latest').watch(
function(err, blockHash) {
var target=blocknumber;
if(web3.eth.blockNumber==target) {
filter.stopWatching(); // your function here
web3.eth.sendTransaction({to:to_address, from:web3.eth.coinbase, value: web3.toWei(value_,"ether")});
filter = null;