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
// This file is part of Darwinia.
//
// Copyright (C) 2018-2022 Darwinia Network
// SPDX-License-Identifier: GPL-3.0
//
// Darwinia 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.
//
// Darwinia 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.
//
// You should have received a copy of the GNU General Public License
// along with Darwinia. If not, see <https://www.gnu.org/licenses/>.

// --- crates.io ---
use ethereum::TransactionV2 as Transaction;
use sha3::{Digest, Keccak256};
// --- darwinia-network ---
use ethereum_primitives::{H160, H256, U256};
// --- paritytech ---
use frame_support::PalletId;
use sp_runtime::{traits::AccountIdConversion, AccountId32};
use sp_std::marker::PhantomData;

pub const POW_9: u32 = 1_000_000_000;
/// The address prefix for dvm address
const ADDR_PREFIX: &[u8] = b"dvm:";

/// A trait for converting from Substrate account_id to Ethereum address.
pub trait DeriveEthereumAddress {
	fn derive_ethereum_address(&self) -> H160;
}

/// A trait for converting from Ethereum address to Substrate account_id.
pub trait DeriveSubstrateAddress<AccountId> {
	fn derive_substrate_address(address: &H160) -> AccountId;
}

pub fn is_derived_from_eth(account_id: impl AsRef<[u8; 32]>) -> bool {
	let account_id: &[u8; 32] = account_id.as_ref();

	let mut account_id_prefix = [0u8; 11];
	account_id_prefix[0..4].copy_from_slice(ADDR_PREFIX);

	// Return true if prefix and checksum valid
	account_id[0..11] == account_id_prefix && account_id[31] == checksum_of(account_id)
}

impl DeriveEthereumAddress for PalletId {
	fn derive_ethereum_address(&self) -> H160 {
		let account_id: AccountId32 = self.into_account();
		account_id.derive_ethereum_address()
	}
}

// If AccountId32 is derived from an Ethereum address before, this should return the orginal
// Ethereum address, otherwise a new Ethereum address should be generated.
impl DeriveEthereumAddress for AccountId32 {
	fn derive_ethereum_address(&self) -> H160 {
		let bytes: &[u8; 32] = self.as_ref();
		let is_derived_from_eth = is_derived_from_eth(&self);

		if is_derived_from_eth {
			H160::from_slice(&bytes[11..31])
		} else {
			H160::from_slice(&bytes[0..20])
		}
	}
}

fn checksum_of(account_id: &[u8; 32]) -> u8 {
	account_id[1..31].iter().fold(account_id[0], |sum, &byte| sum ^ byte)
}

/// Darwinia network address mapping.
pub struct ConcatConverter<AccountId>(PhantomData<AccountId>);
/// The ConcatConverter used to convert Ethereum address to Substrate account_id
/// The concat rule included three parts:
/// 1. AccountId Prefix: concat("dvm:", "0x00000000000000"), length: 11 bytes
/// 2. EVM address: the original evm address, length: 20 bytes
/// 3. CheckSum:  byte_xor(AccountId Prefix + EVM address), length: 1 bytes
impl<AccountId> DeriveSubstrateAddress<AccountId> for ConcatConverter<AccountId>
where
	AccountId: From<[u8; 32]>,
{
	fn derive_substrate_address(address: &H160) -> AccountId {
		let mut raw_account = [0u8; 32];

		raw_account[0..4].copy_from_slice(ADDR_PREFIX);
		raw_account[11..31].copy_from_slice(&address[..]);
		raw_account[31] = checksum_of(&raw_account);

		raw_account.into()
	}
}

pub fn recover_signer(transaction: &Transaction) -> Option<H160> {
	let mut sig = [0u8; 65];
	let mut msg = [0u8; 32];
	match transaction {
		Transaction::Legacy(t) => {
			sig[0..32].copy_from_slice(&t.signature.r()[..]);
			sig[32..64].copy_from_slice(&t.signature.s()[..]);
			sig[64] = t.signature.standard_v();
			msg.copy_from_slice(&ethereum::LegacyTransactionMessage::from(t.clone()).hash()[..]);
		},
		Transaction::EIP2930(t) => {
			sig[0..32].copy_from_slice(&t.r[..]);
			sig[32..64].copy_from_slice(&t.s[..]);
			sig[64] = t.odd_y_parity as u8;
			msg.copy_from_slice(&ethereum::EIP2930TransactionMessage::from(t.clone()).hash()[..]);
		},
		Transaction::EIP1559(t) => {
			sig[0..32].copy_from_slice(&t.r[..]);
			sig[32..64].copy_from_slice(&t.s[..]);
			sig[64] = t.odd_y_parity as u8;
			msg.copy_from_slice(&ethereum::EIP1559TransactionMessage::from(t.clone()).hash()[..]);
		},
	}

	let pubkey = sp_io::crypto::secp256k1_ecdsa_recover(&sig, &msg).ok()?;
	Some(H160::from(H256::from_slice(Keccak256::digest(&pubkey).as_slice())))
}

/// Decimal conversion from RING/KTON to Ethereum decimal format.
pub fn decimal_convert(main_balance: u128, remaining_balance: Option<u128>) -> U256 {
	if let Some(rb) = remaining_balance {
		return U256::from(main_balance)
			.saturating_mul(U256::from(POW_9))
			.saturating_add(U256::from(rb));
	}
	U256::from(main_balance).saturating_mul(U256::from(POW_9))
}

#[cfg(test)]
mod tests {
	use super::*;
	use ethereum_primitives::H160;
	use std::str::FromStr;

	#[test]
	fn const_pow_9_should_work() {
		assert_eq!(U256::from(10).checked_pow(9.into()).unwrap(), POW_9.into())
	}

	#[test]
	fn test_derive_eth_address_from_subaccount_id() {
		let account_id_1 = AccountId32::from_str(
			"0x64766d3a000000000000006be02d1d3665660d22ff9624b7be0551ee1ac91bd2",
		)
		.unwrap();
		let eth_addr1 = account_id_1.derive_ethereum_address();
		assert_eq!(H160::from_str("6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b").unwrap(), eth_addr1);
		assert_eq!(
			ConcatConverter::<AccountId32>::derive_substrate_address(&eth_addr1),
			account_id_1
		);

		let account_id_2 = AccountId32::from_str(
			"0x02497755176da60a69586af4c5ea5f5de218eb84011677722646b602eb2d240e",
		)
		.unwrap();
		let eth_addr2 = account_id_2.derive_ethereum_address();
		assert_eq!(H160::from_str("02497755176da60a69586af4c5ea5f5de218eb84").unwrap(), eth_addr2);
		assert_ne!(
			ConcatConverter::<AccountId32>::derive_substrate_address(&eth_addr2),
			account_id_2
		);
	}

	#[test]
	fn test_is_derived_from_eth_works() {
		let account_id_1 = AccountId32::from_str(
			"0x64766d3a000000000000006be02d1d3665660d22ff9624b7be0551ee1ac91bd2",
		)
		.unwrap();
		assert!(is_derived_from_eth(account_id_1));
		let account_id_1 = AccountId32::from_str(
			"0x02497755176da60a69586af4c5ea5f5de218eb84011677722646b602eb2d240e",
		)
		.unwrap();
		assert!(!is_derived_from_eth(account_id_1));
	}

	#[test]
	fn test_eth_address_derive() {
		let eth_addr1 = H160::from_str("1234500000000000000000000000000000000000").unwrap();
		let derive_account_id_1 =
			ConcatConverter::<AccountId32>::derive_substrate_address(&eth_addr1);

		assert_eq!(derive_account_id_1.derive_ethereum_address(), eth_addr1);
	}
}