-
-
Save Dexaran/a5afe2ae058d1c7db973c0841cc3006f to your computer and use it in GitHub Desktop.
EIP101 Standard Account code in Solidity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Implementation of the standard account contract as per EIP101 (Cryptocurrency | |
// abstraction). See: https://github.com/ethereum/EIPs/issues/28 | |
// | |
// Written by Alex Beregszaszi, use under the MIT license. | |
// | |
contract StandardAccount { | |
uint256 nextSequence = 1; | |
function() { | |
// assign calldata variables | |
bytes memory data = msg.data; | |
uint len = data.length; | |
// read out calldata sections | |
bytes32 v; | |
bytes32 r; | |
bytes32 s; | |
uint256 sequence; | |
uint256 price; | |
address to; | |
assembly { | |
v := mload(add(data, 32)) | |
r := mload(add(data, 64)) | |
s := mload(add(data, 96)) | |
sequence := mload(add(data, 128)) | |
price := mload(add(data, 160)) | |
to := mload(add(data, 192)) | |
} | |
// hash the data | |
bytes32 hash; | |
assembly { | |
hash := sha3(add(data, 128), sub(len, 128)) | |
} | |
// verify signature (the signer address must be the contract address) | |
if (ecrecover(hash, uint8(v), r, s) != address(this)) { | |
throw; | |
} | |
// confirm sequence | |
if (sequence != nextSequence) { | |
throw; | |
} | |
// increment for the next sequence | |
nextSequence++; | |
// send transaction to the recipient | |
uint256 gas = msg.gas; | |
assembly { | |
call(sub(gas, 50000), to, 0, add(data, 224), sub(len, 192), 0, 0) | |
} | |
// pay the miner | |
// FIXME: calculate gas used | |
Token(0).transfer(block.coinbase, price * gas); | |
} | |
} | |
contract Token { | |
event Transfer(address indexed _from, address indexed _to, uint256 _value); | |
event Approval(address indexed _owner, address indexed _spender, uint256 _value); | |
function totalSupply() constant returns (uint256 supply) {} | |
function balanceOf(address _owner) constant returns (uint256 balance) {} | |
function transfer(address _to, uint256 _value) returns (bool success) {} | |
function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {} | |
function approve(address _spender, uint256 _value) returns (bool success) {} | |
function allowance(address _owner, address _spender) constant returns (uint256 remaining) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment