Skip to content

Instantly share code, notes, and snippets.

/// @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)
@dominiek
dominiek / secp256k1_ecdsa_sig_recover.c
Created February 16, 2018 03:05
secp256k1_ecdsa_sig_recover.c
static int secp256k1_ecdsa_sig_recover(const secp256k1_ecmult_context *ctx, const secp256k1_scalar *sigr, const secp256k1_scalar* sigs, secp256k1_ge *pubkey, const secp256k1_scalar *message, int recid) {
unsigned char brx[32];
secp256k1_fe fx;
secp256k1_ge x;
secp256k1_gej xj;
secp256k1_scalar rn, u1, u2;
secp256k1_gej qj;
int r;
if (secp256k1_scalar_is_zero(sigr) || secp256k1_scalar_is_zero(sigs)) {
@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)
}
@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 / 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 / EtherDelta Trade Logic 2.sol
Created February 8, 2018 04:54
EtherDelta Trade Logic 2
function tradeBalances(address tokenGet, uint amountGet, address tokenGive, uint amountGive, address user, uint amount) private {
uint feeMakeXfer = safeMul(amount, feeMake) / (1 ether);
uint feeTakeXfer = safeMul(amount, feeTake) / (1 ether);
uint feeRebateXfer = 0;
if (accountLevelsAddr != 0x0) {
uint accountLevel = AccountLevels(accountLevelsAddr).accountLevel(user);
if (accountLevel==1) feeRebateXfer = safeMul(amount, feeRebate) / (1 ether);
if (accountLevel==2) feeRebateXfer = feeTakeXfer;
}
tokens[tokenGet][msg.sender] = safeSub(tokens[tokenGet][msg.sender], safeAdd(amount, feeTakeXfer));