Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dominiek
dominiek / EtherDelta Trade Logic.sol
Created February 8, 2018 04:52
EtherDelta Trade Logic
mapping (address => mapping (bytes32 => bool)) public orders;
mapping (address => mapping (bytes32 => uint)) public orderFills;
function trade(address tokenGet, uint amountGet, address tokenGive, uint amountGive, uint expires, uint nonce, address user, uint8 v, bytes32 r, bytes32 s, uint amount) {
bytes32 hash = sha256(this, tokenGet, amountGet, tokenGive, amountGive, expires, nonce);
if (!(
(orders[user][hash] || ecrecover(sha3("\x19Ethereum Signed Message:\n32", hash),v,r,s) == user) &&
block.number <= expires &&
safeAdd(orderFills[user][hash], amount) <= amountGet
)) throw;
@dominiek
dominiek / EtherDelta Fund Management.sol
Last active June 17, 2021 19:52
EtherDelta Fund Management
mapping (address => mapping (address => uint)) public tokens;
function deposit() payable {
tokens[0][msg.sender] = safeAdd(tokens[0][msg.sender], msg.value);
Deposit(0, msg.sender, msg.value, tokens[0][msg.sender]);
}
function withdraw(uint amount) {
if (tokens[0][msg.sender] < amount) throw;
tokens[0][msg.sender] = safeSub(tokens[0][msg.sender], amount);
@dominiek
dominiek / IDEX.sol
Created February 8, 2018 07:36
IDEX Smart Contract
pragma solidity ^0.4.16;
contract Token {
bytes32 public standard;
bytes32 public name;
bytes32 public symbol;
uint256 public totalSupply;
uint8 public decimals;
bool public allowTransactions;
mapping (address => uint256) public balanceOf;
@dominiek
dominiek / ethereum_smart_contract_ecrecover.sol
Created February 16, 2018 02:53
ethereum_smart_contract_ecrecover.sol
function getOriginAddress(bytes32 signedMessage, uint8 v, bytes32 r, bytes32 s) constant returns(address) {
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 prefixedHash = keccak256(prefix, signedMessage);
return ecrecover(prefixedHash, v, r, s);
}
@dominiek
dominiek / ethereum_web3_signing.js
Created February 16, 2018 02:51
Ethereum Web3 Signing
const message = web3.sha3('Hello World');
const signature = await web3.eth.sign(account, message);
const { v, r, s } = ethUtil.fromRpcSig(signature);
@dominiek
dominiek / ethereum_ecrecover_native_contract.go
Created February 16, 2018 03:03
ethereum_ecrecover_native_contract.go
// ECRECOVER implemented as a native contract.
type ecrecover struct{}
func (c *ecrecover) RequiredGas(input []byte) uint64 {
return params.EcrecoverGas
}
func (c *ecrecover) Run(input []byte) ([]byte, error) {
const ecRecoverInputLength = 128
@dominiek
dominiek / elliptic_signing.js
Created February 16, 2018 03:02
elliptic_signing.js
EC.prototype.sign = function sign(msg, key, enc, options) {
if (typeof enc === 'object') {
options = enc;
enc = null;
}
if (!options)
options = {};
key = this.keyFromPrivate(key, enc);
msg = this._truncateToN(new BN(msg, 16));
@dominiek
dominiek / metamask_ethereum_signing.js
Created February 16, 2018 02:59
metamask_ethereum_signing.js
// For eth_sign, we need to sign arbitrary data:
signMessage (withAccount, data) {
const wallet = this._getWalletForAccount(withAccount)
const message = ethUtil.stripHexPrefix(data)
var privKey = wallet.getPrivateKey()
var msgSig = ethUtil.ecsign(new Buffer(message, 'hex'), privKey)
var rawMsgSig = ethUtil.bufferToHex(sigUtil.concatSig(msgSig.v, msgSig.r, msgSig.s))
return Promise.resolve(rawMsgSig)
}
/// @notice The minimum payment required to use breedWithAuto(). This fee goes towards
/// the gas cost paid by whatever calls giveBirth(), and can be dynamically updated by
/// the COO role as the gas price changes.
uint256 public autoBirthFee = 2 finney;
/// @notice Have a pregnant Kitty give birth!
/// @param _matronId A Kitty ready to give birth.
/// @return The Kitty ID of the new kitten.
@dominiek
dominiek / ethereum_smart_contract_ecrecover_evm_asm.sol
Created February 16, 2018 03:27
ethereum_smart_contract_ecrecover_evm_asm.sol
bool ret;
address addr;
assembly {
let size := mload(0x40)
mstore(size, msgHash)
mstore(add(size, 32), v)
mstore(add(size, 64), r)
mstore(add(size, 96), s)
ret := call(3000, 1, 0, size, 128, size, 32)
addr := mload(size)