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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// Copyright 2023 Centrifuge Foundation (centrifuge.io).
//
// This file is part of the Centrifuge chain project.
// Centrifuge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version (see http://www.gnu.org/licenses).
// Centrifuge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

use cfg_primitives::{AccountId, Balance};
use cfg_traits::{PreConditions, TransferAllowance};
use cfg_types::{
	domain_address::DomainAddress,
	locations::RestrictedTransferLocation,
	tokens::{CurrencyId, FilterCurrency},
};
use frame_support::{traits::IsSubType, RuntimeDebugNoBound};
use pallet_restricted_tokens::TransferDetails;
use pallet_restricted_xtokens::TransferEffects;
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_runtime::{
	traits::{Convert, DispatchInfoOf, SignedExtension, StaticLookup},
	transaction_validity::{InvalidTransaction, TransactionValidityError},
	DispatchError, DispatchResult, TokenError,
};
use sp_std::{boxed::Box, vec::Vec};
use staging_xcm::{
	v4::{Asset, Location},
	VersionedLocation,
};

pub struct PreXcmTransfer<T, C>(sp_std::marker::PhantomData<(T, C)>);

impl<
		T: TransferAllowance<
			AccountId,
			CurrencyId = FilterCurrency,
			Location = RestrictedTransferLocation,
		>,
		C: Convert<Location, Option<CurrencyId>>,
	> PreConditions<TransferEffects<AccountId, CurrencyId, Balance>> for PreXcmTransfer<T, C>
{
	type Result = DispatchResult;

	fn check(t: TransferEffects<AccountId, CurrencyId, Balance>) -> Self::Result {
		let currency_based_check = |sender: AccountId, destination: VersionedLocation, currency| {
			amalgamate_allowance(
				T::allowance(
					sender.clone(),
					RestrictedTransferLocation::Xcm(Box::new(destination.clone())),
					FilterCurrency::Specific(currency),
				),
				T::allowance(
					sender,
					RestrictedTransferLocation::Xcm(Box::new(destination)),
					FilterCurrency::All,
				),
			)
		};

		let asset_based_check = |sender, destination, asset: Asset| {
			let currency =
				C::convert(asset.id.0).ok_or(DispatchError::Token(TokenError::UnknownAsset))?;

			currency_based_check(sender, destination, currency)
		};

		match t {
			TransferEffects::Transfer {
				sender,
				destination,
				currency_id,
				..
			} => currency_based_check(sender, destination, currency_id),
			TransferEffects::TransferMultiAsset {
				sender,
				destination,
				asset,
			} => asset_based_check(sender, destination, asset),
			TransferEffects::TransferWithFee {
				sender,
				destination,
				currency_id,
				..
			} => currency_based_check(sender, destination, currency_id),
			TransferEffects::TransferMultiAssetWithFee {
				sender,
				destination,
				asset,
				fee_asset,
			} => {
				asset_based_check(sender.clone(), destination.clone(), asset)?;

				// NOTE: We do check the fee asset and assume that the destination
				//       is the same as for the actual assets. This is a pure subjective
				//       security assumption to not allow randomly burning fees of
				//       protected assets.
				asset_based_check(sender, destination, fee_asset)
			}
			TransferEffects::TransferMultiCurrencies {
				sender,
				destination,
				currencies,
				fee,
			} => {
				for (currency, ..) in currencies {
					currency_based_check(sender.clone(), destination.clone(), currency)?;
				}

				// NOTE: We do check the fee asset and assume that the destination
				//       is the same as for the actual assets. This is a pure subjective
				//       security assumption to not allow randomly burning fees of
				//       protected assets.
				currency_based_check(sender, destination, fee.0)
			}
			TransferEffects::TransferMultiAssets {
				sender,
				destination,
				assets,
				fee_asset,
			} => {
				// NOTE: We do not check the fee, as we assume, that this is not a transfer
				//       but rather a burn of tokens. Furthermore, we do not know the
				//       destination where those fees will go.
				for asset in assets.into_inner() {
					asset_based_check(sender.clone(), destination.clone().clone(), asset)?;
				}

				// NOTE: We do check the fee asset and assume that the destination
				//       is the same as for the actual assets. This is a pure subjective
				//       security assumption to not allow randomly burning fees of
				//       protected assets.
				asset_based_check(sender, destination, fee_asset)
			}
		}
	}
}

pub struct PreNativeTransfer<T>(sp_std::marker::PhantomData<T>);

impl<
		T: TransferAllowance<
			AccountId,
			CurrencyId = FilterCurrency,
			Location = RestrictedTransferLocation,
		>,
	> PreConditions<TransferDetails<AccountId, CurrencyId, Balance>> for PreNativeTransfer<T>
{
	type Result = bool;

	fn check(t: TransferDetails<AccountId, CurrencyId, Balance>) -> Self::Result {
		amalgamate_allowance(
			T::allowance(
				t.send.clone(),
				RestrictedTransferLocation::Local(t.recv.clone()),
				FilterCurrency::Specific(t.id),
			),
			T::allowance(
				t.send.clone(),
				RestrictedTransferLocation::Local(t.recv.clone()),
				FilterCurrency::All,
			),
		)
		.is_ok()
	}
}
pub struct PreLpTransfer<T>(sp_std::marker::PhantomData<T>);

impl<
		T: TransferAllowance<
			AccountId,
			CurrencyId = FilterCurrency,
			Location = RestrictedTransferLocation,
		>,
	> PreConditions<(AccountId, DomainAddress, CurrencyId)> for PreLpTransfer<T>
{
	type Result = DispatchResult;

	fn check(t: (AccountId, DomainAddress, CurrencyId)) -> Self::Result {
		let (sender, receiver, currency) = t;
		// NOTE: The order of the allowance check here is
		amalgamate_allowance(
			T::allowance(
				sender.clone(),
				RestrictedTransferLocation::Address(receiver.clone()),
				FilterCurrency::Specific(currency),
			),
			T::allowance(
				sender,
				RestrictedTransferLocation::Address(receiver),
				FilterCurrency::All,
			),
		)
	}
}

// NOTE: This code here is really critical. The test are resided in the
// integration tests section for this reason. The importance is, that
// nobody is able to create a call that can possibly bypass this filtering.
#[derive(
	Clone, Copy, PartialOrd, Ord, PartialEq, Eq, RuntimeDebugNoBound, Encode, Decode, TypeInfo,
)]
#[scale_info(skip_type_params(T))]
pub struct PreBalanceTransferExtension<T: frame_system::Config>(sp_std::marker::PhantomData<T>);

#[allow(clippy::new_without_default)]
impl<T> PreBalanceTransferExtension<T>
where
	T: frame_system::Config<AccountId = AccountId>
		+ pallet_balances::Config
		+ pallet_utility::Config<RuntimeCall = <T as frame_system::Config>::RuntimeCall>
		+ pallet_proxy::Config<RuntimeCall = <T as frame_system::Config>::RuntimeCall>
		+ pallet_remarks::Config<RuntimeCall = <T as frame_system::Config>::RuntimeCall>
		+ Sync
		+ Send,
	<T as frame_system::Config>::RuntimeCall: IsSubType<pallet_balances::Call<T>>
		+ IsSubType<pallet_utility::Call<T>>
		+ IsSubType<pallet_proxy::Call<T>>
		+ IsSubType<pallet_remarks::Call<T>>,
{
	pub fn new() -> Self {
		Self(sp_std::marker::PhantomData)
	}

	#[allow(clippy::type_complexity)]
	fn retrieve(
		caller: &T::AccountId,
		call: &<T as frame_system::Config>::RuntimeCall,
	) -> Result<Vec<(T::AccountId, T::AccountId)>, TransactionValidityError> {
		Self::recursive_search(caller.clone(), call, |who, balance_call, checks| {
			match balance_call {
				pallet_balances::Call::transfer_all { dest, .. }
				| pallet_balances::Call::transfer_allow_death { dest, .. }
				| pallet_balances::Call::transfer_keep_alive { dest, .. } => {
					let recv: T::AccountId = <T as frame_system::Config>::Lookup::lookup(
						dest.clone(),
					)
					.map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Call))?;

					checks.push((who, recv));
					Ok(())
				}

				// If the call is not a transfer we are fine with it to go through without
				// further checks
				_ => Ok(()),
			}
		})
	}

	#[allow(clippy::type_complexity)]
	#[allow(clippy::single_match)]
	#[allow(clippy::collapsible_match)]
	fn recursive_search<F>(
		caller: T::AccountId,
		call: &<T as frame_system::Config>::RuntimeCall,
		check: F,
	) -> Result<Vec<(T::AccountId, T::AccountId)>, TransactionValidityError>
	where
		F: Fn(
				T::AccountId,
				pallet_balances::Call<T>,
				&mut Vec<(T::AccountId, T::AccountId)>,
			) -> Result<(), TransactionValidityError>
			+ Clone,
	{
		let mut checks = Vec::new();

		if let Some(balance_call) = IsSubType::<pallet_balances::Call<T>>::is_sub_type(call) {
			check(caller, balance_call.clone(), &mut checks)?;
		} else if let Some(call) = IsSubType::<pallet_proxy::Call<T>>::is_sub_type(call) {
			match call {
				pallet_proxy::Call::<T>::proxy { real, call, .. }
				| pallet_proxy::Call::<T>::proxy_announced { real, call, .. } => {
					let caller = T::Lookup::lookup(real.clone())
						.map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Call))?;

					checks.extend(Self::recursive_search(caller, call, check)?);
				}
				_ => {}
			}
		} else if let Some(utility_call) = IsSubType::<pallet_utility::Call<T>>::is_sub_type(call) {
			match utility_call {
				pallet_utility::Call::<T>::batch { calls: batch_calls }
				| pallet_utility::Call::<T>::batch_all { calls: batch_calls } => {
					for batch_call in batch_calls {
						checks.extend(Self::recursive_search(
							caller.clone(),
							batch_call,
							check.clone(),
						)?);
					}
				}
				_ => {}
			}
		} else if let Some(remarks_call) = IsSubType::<pallet_remarks::Call<T>>::is_sub_type(call) {
			match remarks_call {
				pallet_remarks::Call::<T>::remark {
					call: remark_call, ..
				} => checks.extend(Self::recursive_search(caller, remark_call, check)?),
				_ => {}
			}
		}

		Ok(checks)
	}
}

impl<T> SignedExtension for PreBalanceTransferExtension<T>
where
	T: frame_system::Config<AccountId = AccountId>
		+ pallet_balances::Config
		+ pallet_utility::Config<RuntimeCall = <T as frame_system::Config>::RuntimeCall>
		+ pallet_proxy::Config<RuntimeCall = <T as frame_system::Config>::RuntimeCall>
		+ pallet_remarks::Config<RuntimeCall = <T as frame_system::Config>::RuntimeCall>
		+ pallet_transfer_allowlist::Config<
			CurrencyId = FilterCurrency,
			Location = RestrictedTransferLocation,
		> + Sync
		+ Send,
	<T as frame_system::Config>::RuntimeCall: IsSubType<pallet_balances::Call<T>>
		+ IsSubType<pallet_utility::Call<T>>
		+ IsSubType<pallet_proxy::Call<T>>
		+ IsSubType<pallet_remarks::Call<T>>,
{
	type AccountId = T::AccountId;
	type AdditionalSigned = ();
	type Call = <T as frame_system::Config>::RuntimeCall;
	type Pre = ();

	const IDENTIFIER: &'static str = "PreBalanceTransferExtension";

	fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> {
		Ok(())
	}

	fn pre_dispatch(
		self,
		who: &Self::AccountId,
		call: &Self::Call,
		_: &DispatchInfoOf<Self::Call>,
		_: usize,
	) -> Result<Self::Pre, TransactionValidityError> {
		Self::retrieve(who, call)?
			.iter()
			.try_for_each(|(who, recv)| {
				amalgamate_allowance(
					pallet_transfer_allowlist::pallet::Pallet::<T>::allowance(
						who.clone(),
						RestrictedTransferLocation::Local(recv.clone()),
						FilterCurrency::All,
					),
					pallet_transfer_allowlist::pallet::Pallet::<T>::allowance(
						who.clone(),
						RestrictedTransferLocation::Local(recv.clone()),
						FilterCurrency::Specific(CurrencyId::Native),
					),
				)
				.map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Custom(255)))
			})
	}
}

fn amalgamate_allowance(
	first: Result<Option<RestrictedTransferLocation>, DispatchError>,
	second: Result<Option<RestrictedTransferLocation>, DispatchError>,
) -> DispatchResult {
	match (first, second) {
		// There is an allowance set for `Specific(id)`, but NOT for the given recv
		// There is an allowance set for `All`, but NOT for the given recv
		(Err(e), Err(_)) => Err(e),
		// There is an allowance set for `Specific(id)`, but NOT for the given recv
		// There is an allowance set for `All`, for the given recv
		(Err(_), Ok(Some(_))) => Ok(()),
		// There is an allowance set for `Specific(id)`, for the given recv
		// There is an allowance set for `All`, but NOT for the given recv
		(Ok(Some(_)), Err(_)) => Ok(()),
		// There is NO allowance set for `Specific(id)`
		// There is an allowance set for `All`, but NOT for the given recv
		(Ok(None), Err(e)) => Err(e),
		// There is an allowance set for `Specific(id)`, but NOT for the given recv
		// There is NO allowance set for `All`
		(Err(e), Ok(None)) => Err(e),
		// There is an allowance set for `Specific(id)`, for the given recv
		// There is an allowance set for `All`, for the given recv
		(Ok(Some(_)), Ok(Some(_))) => Ok(()),
		// There is NO allowance set for `Specific(id)`
		// There is NO allowance set for `All`
		(Ok(None), Ok(None)) => Ok(()),
		// There is an allowance set for `Specific(id)`, for the given recv
		// There is NO allowance set for `All`
		(Ok(Some(_)), Ok(None)) => Ok(()),
		// There is NO allowance set for `Specific(id)`
		// There is an allowance set for `All`, for the given recv
		(Ok(None), Ok(Some(_))) => Ok(()),
	}
}