This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from loredanacirstea/contract-sign-lib
Extract balance message hash logic for EIP712 in a separate contract
- Loading branch information
Showing
9 changed files
with
185 additions
and
14 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
pragma solidity ^0.4.17; | ||
|
||
contract MicroRaidenEIP712Helper { | ||
/// @dev Returns the message hash used for creating the balance proof. | ||
/// Used in the RaidenMicroTransferChannels contract. | ||
/// Should be kept up-to-date with eth_signTypedData https://github.com/ethereum/EIPs/pull/712. | ||
/// @param _receiver_address The address that receives tokens. | ||
/// @param _open_block_number The block number at which a channel between the | ||
/// sender and receiver was created. | ||
/// @param _balance The amount of tokens owed by the sender to the receiver. | ||
/// @param _contract RaidenMicroTransferChannels contract address | ||
/// @return Hash of the message composed from the above parameters. | ||
function getMessageHash( | ||
address _receiver_address, | ||
uint32 _open_block_number, | ||
uint192 _balance, | ||
address _contract) | ||
public | ||
pure | ||
returns (bytes32) | ||
{ | ||
// The variable names from below will be shown to the sender when signing | ||
// the balance proof, so they have to be kept in sync with the Dapp client. | ||
// The hashed strings should be kept in sync with this function's parameters | ||
// (variable names and types) | ||
bytes32 message_hash = keccak256( | ||
keccak256('address receiver', 'uint32 block_created', 'uint192 balance', 'address contract'), | ||
keccak256(_receiver_address, _open_block_number, _balance, _contract) | ||
); | ||
|
||
return message_hash; | ||
} | ||
} |
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import pytest | ||
from ethereum import tester | ||
from utils import sign | ||
from tests.utils import balance_proof_hash | ||
from eth_utils import force_bytes | ||
from tests.fixtures import ( | ||
owner_index, | ||
owner, | ||
contract_params, | ||
create_contract, | ||
get_token_contract, | ||
get_accounts, | ||
create_accounts, | ||
empty_address | ||
) | ||
from tests.fixtures_uraiden import ( | ||
token_contract, | ||
token_instance, | ||
get_uraiden_contract, | ||
uraiden_contract, | ||
uraiden_instance, | ||
eip712_contract, | ||
eip712_instance | ||
) | ||
|
||
|
||
def test_eip712_instance_fixture(uraiden_instance): | ||
assert uraiden_instance.call().microraiden_eip712_helper() | ||
|
||
|
||
def test_eip712_instance(owner, get_accounts, uraiden_contract, eip712_contract, eip712_instance): | ||
uraiden_instance = uraiden_contract() | ||
(A, B) = get_accounts(2) | ||
assert uraiden_instance.call().microraiden_eip712_helper() == empty_address | ||
uraiden_instance.transact({'from': owner}).setEip712HelperContract(eip712_instance.address) | ||
assert uraiden_instance.call().microraiden_eip712_helper() == eip712_instance.address | ||
|
||
# Test eip712_instance address change | ||
eip712_instance2 = eip712_contract() | ||
assert eip712_instance.address != eip712_instance2.address | ||
|
||
# Only owner can change this | ||
with pytest.raises(tester.TransactionFailed): | ||
uraiden_instance.transact({'from': A}).setEip712HelperContract(eip712_instance2.address) | ||
|
||
uraiden_instance.transact({'from': owner}).setEip712HelperContract(eip712_instance2.address) | ||
assert uraiden_instance.call().microraiden_eip712_helper() == eip712_instance2.address | ||
|
||
|
||
def test_get_message_hash(get_accounts, uraiden_instance, eip712_instance): | ||
(A, B) = get_accounts(2) | ||
receiver = '0x5601ea8445a5d96eeebf89a67c4199fbb7a43fbb' | ||
block = 4804175 | ||
balance = 22000000000000000000 | ||
|
||
message_hash = sign.eth_signed_typed_data_message( | ||
('address', ('uint', 32), ('uint', 192), 'address'), | ||
('receiver', 'block_created', 'balance', 'contract'), | ||
(receiver, block, balance, uraiden_instance.address) | ||
) | ||
|
||
eip712_message_hash = eip712_instance.call().getMessageHash( | ||
receiver, | ||
block, | ||
balance, | ||
uraiden_instance.address | ||
) | ||
assert force_bytes(eip712_message_hash) == message_hash | ||
|
||
eip712_message_hash = eip712_instance.call().getMessageHash( | ||
A, | ||
block, | ||
balance, | ||
uraiden_instance.address | ||
) | ||
assert force_bytes(eip712_message_hash) != message_hash | ||
|
||
eip712_message_hash = eip712_instance.call().getMessageHash( | ||
receiver, | ||
10, | ||
balance, | ||
uraiden_instance.address | ||
) | ||
assert force_bytes(eip712_message_hash) != message_hash | ||
|
||
eip712_message_hash = eip712_instance.call().getMessageHash( | ||
receiver, | ||
block, | ||
20, | ||
uraiden_instance.address | ||
) | ||
assert force_bytes(eip712_message_hash) != message_hash | ||
|
||
eip712_message_hash = eip712_instance.call().getMessageHash( | ||
receiver, | ||
block, | ||
balance, | ||
A | ||
) | ||
assert force_bytes(eip712_message_hash) != message_hash |
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