Skip to content

Instantly share code, notes, and snippets.

@Pipeliner
Created June 15, 2018 17:18
Show Gist options
  • Save Pipeliner/c28057dae1c6c0619f34f71c5e6ccba8 to your computer and use it in GitHub Desktop.
Save Pipeliner/c28057dae1c6c0619f34f71c5e6ccba8 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.16;
contract Token {
function balanceOf(address tokenOwner) public constant returns (uint balance);
function transfer(address to, uint tokens) public returns (bool success);
}
contract CoinGiver {
address public signerr;
// first token, then claimer
//mapping (address => mapping (address => bool)) rrr;
mapping (address => mapping (address => bool)) public claimed;
//mapping(uint => uint) mymap;
mapping (address => mapping (address => uint256)) allowed;
function CoinGiver() public {
signerr = msg.sender;
}
function giveCoins(bytes message, uint8 v, bytes32 r, bytes32 s) external {
// For HDWallet, we don't need the prefix: hash = keccak256(message);
bytes32 prefixedHash = keccak256(message);
address realSigner = ecrecover(prefixedHash, v, r, s);
require (realSigner == signerr);
address tokenAddress = bytesToAddress(message, 0);
address userAddress = bytesToAddress(message, 20); // sizeof(address)
uint256 amount = bytesToUint256(message, 40); // sizeof(address) * 2
require(claimed[tokenAddress][userAddress] == false);
Token(tokenAddress).transfer(userAddress, amount);
claimed[tokenAddress][userAddress] = true;
}
function bytesToAddress(bytes _address, uint8 _offset) pure private returns (address) {
uint160 m = 0;
uint160 b = 0;
for (uint8 i = 0; i < 20; i++) {
m *= 256;
b = uint160(_address[_offset + i]);
m += (b);
}
return address(m);
}
function bytesToUint256(bytes _address, uint8 _offset) pure private returns (uint256) {
uint256 m = 0;
uint256 b = 0;
for (uint8 i = 0; i < 32; i++) {
m *= 256;
b = uint160(_address[_offset + i]);
m += b;
}
return m;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment