1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use raiden_blockchain::keys::PrivateKey;
use raiden_primitives::{
	deserializers::u256_from_str,
	hashing::hash_balance_data,
	packing::{
		pack_balance_proof_message,
		pack_reward_proof,
	},
	serializers::u256_to_str,
	traits::ToBytes,
	types::{
		Address,
		BalanceHash,
		CanonicalIdentifier,
		ChainID,
		ChannelIdentifier,
		MessageHash,
		MessageTypeId,
		Nonce,
		Signature,
		TokenAmount,
		TokenNetworkAddress,
	},
};
use raiden_state_machine::types::BalanceProofState;
use serde::{
	Deserialize,
	Serialize,
};
use web3::signing::SigningError;

use super::SignedMessage;

/// Message sub-field `onchain_balance_proof` for `RequestMonitoring`.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct SignedBlindedBalanceProof {
	chain_id: ChainID,
	token_network_address: TokenNetworkAddress,
	#[serde(deserialize_with = "u256_from_str", serialize_with = "u256_to_str")]
	channel_identifier: ChannelIdentifier,
	#[serde(deserialize_with = "u256_from_str", serialize_with = "u256_to_str")]
	nonce: Nonce,
	additional_hash: MessageHash,
	balance_hash: BalanceHash,
	signature: Signature,
	non_closing_signature: Signature,
}

impl From<BalanceProofState> for SignedBlindedBalanceProof {
	fn from(balance_proof: BalanceProofState) -> Self {
		Self {
			chain_id: balance_proof.canonical_identifier.chain_identifier,
			token_network_address: balance_proof.canonical_identifier.token_network_address,
			channel_identifier: balance_proof.canonical_identifier.channel_identifier,
			nonce: balance_proof.nonce,
			additional_hash: balance_proof.message_hash.expect("BP message hash should be set"),
			balance_hash: hash_balance_data(
				balance_proof.transferred_amount,
				balance_proof.locked_amount,
				balance_proof.locksroot,
			)
			.expect("Balance hash should be generated"),
			signature: balance_proof.signature.expect("BP Signature should be set"),
			non_closing_signature: Signature::default(),
		}
	}
}

impl SignedMessage for SignedBlindedBalanceProof {
	fn bytes_to_sign(&self) -> Vec<u8> {
		pack_balance_proof_message(
			self.nonce,
			self.balance_hash,
			self.additional_hash,
			CanonicalIdentifier {
				chain_identifier: self.chain_id,
				token_network_address: self.token_network_address,
				channel_identifier: self.channel_identifier,
			},
			MessageTypeId::BalanceProofUpdate,
			self.signature.clone(),
		)
		.0
	}

	fn sign(&mut self, key: PrivateKey) -> Result<(), SigningError> {
		self.non_closing_signature = self.sign_message(key)?.to_bytes().into();
		Ok(())
	}
}

/// """Message to request channel watching from a monitoring service.
/// Spec:
///     <https://raiden-network-specification.readthedocs.io/en/latest/monitoring_service.html#monitor-request>
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub struct RequestMonitoring {
	pub balance_proof: SignedBlindedBalanceProof,
	#[serde(deserialize_with = "u256_from_str", serialize_with = "u256_to_str")]
	pub reward_amount: TokenAmount,
	pub monitoring_service_contract_address: Address,
	pub non_closing_participant: Address,
	pub non_closing_signature: Signature,
	pub signature: Signature,
}

impl RequestMonitoring {
	pub fn from_balance_proof(
		balance_proof: BalanceProofState,
		non_closing_participant: Address,
		reward_amount: TokenAmount,
		monitoring_service_contract_address: Address,
	) -> Self {
		let balance_proof: SignedBlindedBalanceProof = balance_proof.into();
		Self {
			reward_amount,
			non_closing_participant,
			monitoring_service_contract_address,
			balance_proof,
			non_closing_signature: Signature::default(),
			signature: Signature::default(),
		}
	}
}

impl SignedMessage for RequestMonitoring {
	fn bytes_to_sign(&self) -> Vec<u8> {
		pack_reward_proof(
			self.monitoring_service_contract_address,
			self.balance_proof.chain_id,
			self.balance_proof.token_network_address,
			self.non_closing_participant,
			self.non_closing_signature.clone(),
			self.reward_amount,
		)
		.0
	}

	fn sign(&mut self, key: PrivateKey) -> Result<(), SigningError> {
		self.balance_proof.sign(key.clone())?;
		self.non_closing_signature = self.balance_proof.non_closing_signature.clone();
		self.signature = self.sign_message(key)?.to_bytes().into();
		Ok(())
	}
}