Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differentiate close and update signature checks #1150

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Documents changes that result in:

## Unreleased

- [#1150](https://github.com/raiden-network/raiden-contracts/pull/1150) Use different message ID for chnanelClose() and updateNonClosingBalanceProof()
- [#1148](https://github.com/raiden-network/raiden-contracts/pull/1148) Add TokenNetworkRegistry's address to MonitoringService's constructor arguments
- [#PR ID] Revert the upgrade web3.py and other dependencies
- [#1151](https://github.com/raiden-network/raiden-contracts/pull/1151) OneToN doesn't work for service providers not registered in ServiceRegistry
Expand Down
130 changes: 67 additions & 63 deletions raiden_contracts/data/contracts.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion raiden_contracts/data/source/raiden/TokenNetwork.sol
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ contract TokenNetwork is Utils {

// The closing participant must have signed the balance proof.
address recovered_closing_participant_address = recoverAddressFromBalanceProofCounterSignature(
MessageTypeId.BalanceProof,
channel_identifier,
balance_hash,
nonce,
Expand Down Expand Up @@ -621,6 +622,7 @@ contract TokenNetwork is Utils {
// We need the signature from the non-closing participant to allow
// anyone to make this transaction. E.g. a monitoring service.
recovered_non_closing_participant = recoverAddressFromBalanceProofCounterSignature(
MessageTypeId.BalanceProofUpdate,
channel_identifier,
balance_hash,
nonce,
Expand Down Expand Up @@ -1508,6 +1510,7 @@ contract TokenNetwork is Utils {
}

function recoverAddressFromBalanceProofCounterSignature(
MessageTypeId message_type_id,
uint256 channel_identifier,
bytes32 balance_hash,
uint256 nonce,
Expand All @@ -1527,7 +1530,7 @@ contract TokenNetwork is Utils {
message_length,
address(this),
chain_id,
uint256(MessageTypeId.BalanceProofUpdate),
uint256(message_type_id),
channel_identifier,
balance_hash,
nonce,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ contract TokenNetworkSignatureTest is TokenNetwork {
}

function recoverAddressFromBalanceProofCounterSignaturePublic(
MessageTypeId message_type_id,
uint256 channel_identifier,
bytes32 balance_hash,
uint256 nonce,
Expand All @@ -196,6 +197,7 @@ contract TokenNetworkSignatureTest is TokenNetwork {
returns (address signature_address)
{
return recoverAddressFromBalanceProofCounterSignature(
message_type_id,
channel_identifier,
balance_hash,
nonce,
Expand Down
16 changes: 10 additions & 6 deletions raiden_contracts/tests/fixtures/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from web3 import Web3
from web3.contract import Contract

from raiden_contracts.constants import TEST_SETTLE_TIMEOUT_MIN, ChannelState
from raiden_contracts.constants import TEST_SETTLE_TIMEOUT_MIN, ChannelState, MessageTypeId
from raiden_contracts.tests.utils import (
EMPTY_ADDITIONAL_HASH,
EMPTY_BALANCE_HASH,
Expand All @@ -24,7 +24,7 @@
from raiden_contracts.utils.proofs import (
hash_balance_data,
sign_balance_proof,
sign_balance_proof_update_message,
sign_balance_proof_message,
sign_cooperative_settle_message,
sign_withdraw_message,
)
Expand Down Expand Up @@ -203,10 +203,10 @@ def get(
additional_hash2,
)
balance_proof_close_signature_1 = create_balance_proof_countersignature(
participant1, channel_identifier, *balance_proof_2
participant1, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof_2
)
balance_proof_update_signature_2 = create_balance_proof_countersignature(
participant2, channel_identifier, *balance_proof_1
participant2, channel_identifier, MessageTypeId.BALANCE_PROOF_UPDATE, *balance_proof_1
)

token_network.functions.closeChannel(
Expand Down Expand Up @@ -647,6 +647,7 @@ def get(
_token_network.address,
int(_token_network.functions.chain_id().call()),
channel_identifier,
MessageTypeId.BALANCE_PROOF,
balance_hash,
nonce,
additional_hash,
Expand All @@ -664,6 +665,7 @@ def create_balance_proof_countersignature(
def get(
participant: HexAddress,
channel_identifier: int,
msg_type: MessageTypeId,
balance_hash: bytes,
nonce: int,
additional_hash: bytes,
Expand All @@ -674,11 +676,12 @@ def get(
_token_network = other_token_network or token_network
private_key = get_private_key(participant)

non_closing_signature = sign_balance_proof_update_message(
non_closing_signature = sign_balance_proof_message(
private_key,
_token_network.address,
int(_token_network.functions.chain_id().call()),
channel_identifier,
msg_type,
balance_hash,
nonce,
additional_hash,
Expand All @@ -703,11 +706,12 @@ def get(
_token_network = other_token_network or token_network
private_key = get_private_key(participant)

non_closing_signature = sign_balance_proof_update_message(
non_closing_signature = sign_balance_proof_message(
private_key,
_token_network.address,
int(_token_network.functions.chain_id().call()),
channel_identifier,
MessageTypeId.BALANCE_PROOF,
EMPTY_BALANCE_HASH,
0,
EMPTY_ADDITIONAL_HASH,
Expand Down
35 changes: 26 additions & 9 deletions raiden_contracts/tests/test_channel_close.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
from web3 import Web3
from web3.contract import Contract

from raiden_contracts.constants import TEST_SETTLE_TIMEOUT_MIN, ChannelEvent, ChannelState
from raiden_contracts.constants import (
TEST_SETTLE_TIMEOUT_MIN,
ChannelEvent,
ChannelState,
MessageTypeId,
)
from raiden_contracts.tests.utils import (
EMPTY_ADDITIONAL_HASH,
EMPTY_BALANCE_HASH,
Expand Down Expand Up @@ -124,7 +129,7 @@ def test_close_wrong_signature(
channel_identifier, C, transferred_amount, 0, nonce, locksroot
)
closing_signature_A = create_balance_proof_countersignature(
A, channel_identifier, *balance_proof
A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof
)

with pytest.raises(TransactionFailed):
Expand Down Expand Up @@ -225,7 +230,9 @@ def test_close_nonce_zero(
vals_B.nonce,
vals_B.locksroot,
)
close_sig_A = create_balance_proof_countersignature(A, channel_identifier, *balance_proof_B)
close_sig_A = create_balance_proof_countersignature(
A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof_B
)

(
_,
Expand Down Expand Up @@ -285,8 +292,12 @@ def test_close_first_argument_is_for_partner_transfer(

# Create balance proofs
balance_proof = create_balance_proof(channel_identifier, B)
closing_sig_A = create_balance_proof_countersignature(A, channel_identifier, *balance_proof)
closing_sig_B = create_balance_proof_countersignature(B, channel_identifier, *balance_proof)
closing_sig_A = create_balance_proof_countersignature(
A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof
)
closing_sig_B = create_balance_proof_countersignature(
B, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof
)

# closeChannel fails, if the provided balance proof is from the same participant who closes
with pytest.raises(TransactionFailed):
Expand Down Expand Up @@ -460,7 +471,9 @@ def test_close_channel_state(
vals_B.nonce,
vals_B.locksroot,
)
closing_sig_A = create_balance_proof_countersignature(A, channel_identifier, *balance_proof_B)
closing_sig_A = create_balance_proof_countersignature(
A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof_B
)

txn_hash = token_network.functions.closeChannel(
channel_identifier, B, A, *balance_proof_B, closing_sig_A
Expand Down Expand Up @@ -568,7 +581,9 @@ def test_close_replay_reopened_channel(
nonce,
values_B.locksroot,
)
closing_sig_A = create_balance_proof_countersignature(A, channel_identifier1, *balance_proof_B)
closing_sig_A = create_balance_proof_countersignature(
A, channel_identifier1, MessageTypeId.BALANCE_PROOF, *balance_proof_B
)
token_network.functions.closeChannel(
channel_identifier1, B, A, *balance_proof_B, closing_sig_A
).call_and_transact({"from": A})
Expand Down Expand Up @@ -605,7 +620,7 @@ def test_close_replay_reopened_channel(
values_B.locksroot,
)
closing_sig_A2 = create_balance_proof_countersignature(
A, channel_identifier2, *balance_proof_B2
A, channel_identifier2, MessageTypeId.BALANCE_PROOF, *balance_proof_B2
)
token_network.functions.closeChannel(
channel_identifier2, B, A, *balance_proof_B2, closing_sig_A2
Expand All @@ -631,7 +646,9 @@ def test_close_channel_event(
balance_proof = create_balance_proof(
channel_identifier, B, transferred_amount=5, locked_amount=0, nonce=3
)
close_sig = create_balance_proof_countersignature(A, channel_identifier, *balance_proof)
close_sig = create_balance_proof_countersignature(
A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof
)

txn_hash = token_network.functions.closeChannel(
channel_identifier, B, A, *balance_proof, close_sig
Expand Down
19 changes: 14 additions & 5 deletions raiden_contracts/tests/test_channel_settle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
from web3 import Web3
from web3.contract import Contract

from raiden_contracts.constants import TEST_SETTLE_TIMEOUT_MIN, ChannelEvent, ChannelState
from raiden_contracts.constants import (
TEST_SETTLE_TIMEOUT_MIN,
ChannelEvent,
ChannelState,
MessageTypeId,
)
from raiden_contracts.tests.fixtures.channel import call_settle
from raiden_contracts.tests.utils import (
EMPTY_ADDITIONAL_HASH,
Expand Down Expand Up @@ -184,7 +189,9 @@ def test_settle_single_direct_transfer_for_closing_party(
1,
LOCKSROOT_OF_NO_LOCKS,
)
closing_sig_A = create_balance_proof_countersignature(A, channel_identifier, *balance_proof_B)
closing_sig_A = create_balance_proof_countersignature(
A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof_B
)
token_network.functions.closeChannel(
channel_identifier, B, A, *balance_proof_B, closing_sig_A
).call_and_transact({"from": A})
Expand Down Expand Up @@ -266,7 +273,7 @@ def test_settle_single_direct_transfer_for_counterparty(
)

balance_proof_update_signature_B = create_balance_proof_countersignature(
B, channel_identifier, *balance_proof_A
B, channel_identifier, MessageTypeId.BALANCE_PROOF_UPDATE, *balance_proof_A
)
token_network.functions.updateNonClosingBalanceProof(
channel_identifier, A, B, *balance_proof_A, balance_proof_update_signature_B
Expand Down Expand Up @@ -584,9 +591,11 @@ def test_settle_channel_event(
balance_proof_A = create_balance_proof(channel_identifier, A, 10, 0, 1, LOCKSROOT_OF_NO_LOCKS)
balance_proof_B = create_balance_proof(channel_identifier, B, 5, 0, 3, LOCKSROOT_OF_NO_LOCKS)
balance_proof_update_signature_B = create_balance_proof_countersignature(
B, channel_identifier, *balance_proof_A
B, channel_identifier, MessageTypeId.BALANCE_PROOF_UPDATE, *balance_proof_A
)
close_sig_A = create_balance_proof_countersignature(
A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof_B
)
close_sig_A = create_balance_proof_countersignature(A, channel_identifier, *balance_proof_B)

token_network.functions.closeChannel(
channel_identifier, B, A, *balance_proof_B, close_sig_A
Expand Down
Loading