2024-09-14 07:54:18 -04:00
|
|
|
use std::io;
|
|
|
|
|
|
2024-09-18 15:50:21 -04:00
|
|
|
use ciphersuite::{group::GroupEncoding, Ciphersuite, Secp256k1};
|
2024-09-14 07:54:18 -04:00
|
|
|
|
2024-09-18 00:54:20 -04:00
|
|
|
use alloy_core::primitives::U256;
|
2024-09-14 07:54:18 -04:00
|
|
|
|
|
|
|
|
use scale::{Encode, Decode};
|
|
|
|
|
use borsh::{BorshSerialize, BorshDeserialize};
|
|
|
|
|
|
|
|
|
|
use serai_client::{
|
|
|
|
|
primitives::{NetworkId, Coin, Amount, Balance},
|
|
|
|
|
networks::ethereum::Address,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use primitives::{OutputType, ReceivedOutput};
|
2024-09-18 00:54:20 -04:00
|
|
|
use ethereum_router::{Coin as EthereumCoin, InInstruction as EthereumInInstruction};
|
2024-09-14 07:54:18 -04:00
|
|
|
|
2024-09-20 00:55:21 -04:00
|
|
|
use crate::{DAI, ETHER_DUST};
|
2024-09-14 07:54:18 -04:00
|
|
|
|
|
|
|
|
fn coin_to_serai_coin(coin: &EthereumCoin) -> Option<Coin> {
|
|
|
|
|
match coin {
|
|
|
|
|
EthereumCoin::Ether => Some(Coin::Ether),
|
|
|
|
|
EthereumCoin::Erc20(token) => {
|
|
|
|
|
if *token == DAI {
|
|
|
|
|
return Some(Coin::Dai);
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn amount_to_serai_amount(coin: Coin, amount: U256) -> Amount {
|
|
|
|
|
assert_eq!(coin.network(), NetworkId::Ethereum);
|
|
|
|
|
assert_eq!(coin.decimals(), 8);
|
|
|
|
|
// Remove 10 decimals so we go from 18 decimals to 8 decimals
|
|
|
|
|
let divisor = U256::from(10_000_000_000u64);
|
|
|
|
|
// This is valid up to 184b, which is assumed for the coins allowed
|
|
|
|
|
Amount(u64::try_from(amount / divisor).unwrap())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(
|
|
|
|
|
Clone, Copy, PartialEq, Eq, Hash, Debug, Encode, Decode, BorshSerialize, BorshDeserialize,
|
|
|
|
|
)]
|
|
|
|
|
pub(crate) struct OutputId(pub(crate) [u8; 40]);
|
|
|
|
|
impl Default for OutputId {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self([0; 40])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl AsRef<[u8]> for OutputId {
|
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
|
self.0.as_ref()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl AsMut<[u8]> for OutputId {
|
|
|
|
|
fn as_mut(&mut self) -> &mut [u8] {
|
|
|
|
|
self.0.as_mut()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
2024-09-20 00:55:21 -04:00
|
|
|
pub(crate) enum Output {
|
|
|
|
|
Output { key: <Secp256k1 as Ciphersuite>::G, instruction: EthereumInInstruction },
|
|
|
|
|
Eventuality { key: <Secp256k1 as Ciphersuite>::G, nonce: u64 },
|
2024-09-18 15:50:21 -04:00
|
|
|
}
|
2024-09-14 07:54:18 -04:00
|
|
|
impl ReceivedOutput<<Secp256k1 as Ciphersuite>::G, Address> for Output {
|
|
|
|
|
type Id = OutputId;
|
|
|
|
|
type TransactionId = [u8; 32];
|
|
|
|
|
|
|
|
|
|
fn kind(&self) -> OutputType {
|
2024-09-20 00:55:21 -04:00
|
|
|
match self {
|
|
|
|
|
// All outputs received are External
|
|
|
|
|
Output::Output { .. } => OutputType::External,
|
|
|
|
|
// Yet upon Eventuality completions, we report a Change output to ensure synchrony per the
|
|
|
|
|
// scanner's documented bounds
|
|
|
|
|
Output::Eventuality { .. } => OutputType::Change,
|
|
|
|
|
}
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn id(&self) -> Self::Id {
|
2024-09-20 00:55:21 -04:00
|
|
|
match self {
|
|
|
|
|
Output::Output { key: _, instruction } => {
|
|
|
|
|
let mut id = [0; 40];
|
2025-01-23 01:59:24 -05:00
|
|
|
id[.. 32].copy_from_slice(&instruction.id.block_hash);
|
|
|
|
|
id[32 ..].copy_from_slice(&instruction.id.index_within_block.to_le_bytes());
|
2024-09-20 00:55:21 -04:00
|
|
|
OutputId(id)
|
|
|
|
|
}
|
|
|
|
|
// Yet upon Eventuality completions, we report a Change output to ensure synchrony per the
|
|
|
|
|
// scanner's documented bounds
|
|
|
|
|
Output::Eventuality { key: _, nonce } => {
|
|
|
|
|
let mut id = [0; 40];
|
|
|
|
|
id[.. 8].copy_from_slice(&nonce.to_le_bytes());
|
|
|
|
|
OutputId(id)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn transaction_id(&self) -> Self::TransactionId {
|
2024-09-20 00:55:21 -04:00
|
|
|
match self {
|
2025-01-23 01:59:24 -05:00
|
|
|
Output::Output { key: _, instruction } => instruction.transaction_hash,
|
2024-09-20 00:55:21 -04:00
|
|
|
Output::Eventuality { key: _, nonce } => {
|
|
|
|
|
let mut id = [0; 32];
|
|
|
|
|
id[.. 8].copy_from_slice(&nonce.to_le_bytes());
|
|
|
|
|
id
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn key(&self) -> <Secp256k1 as Ciphersuite>::G {
|
2024-09-20 00:55:21 -04:00
|
|
|
match self {
|
|
|
|
|
Output::Output { key, .. } | Output::Eventuality { key, .. } => *key,
|
|
|
|
|
}
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn presumed_origin(&self) -> Option<Address> {
|
2024-09-20 00:55:21 -04:00
|
|
|
match self {
|
2025-01-23 01:59:24 -05:00
|
|
|
Output::Output { key: _, instruction } => Some(Address::Address(*instruction.from.0)),
|
2024-09-20 00:55:21 -04:00
|
|
|
Output::Eventuality { .. } => None,
|
|
|
|
|
}
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn balance(&self) -> Balance {
|
2024-09-20 00:55:21 -04:00
|
|
|
match self {
|
|
|
|
|
Output::Output { key: _, instruction } => {
|
|
|
|
|
let coin = coin_to_serai_coin(&instruction.coin).unwrap_or_else(|| {
|
|
|
|
|
panic!(
|
|
|
|
|
"mapping coin from an EthereumInInstruction with coin {}, which we don't handle.",
|
|
|
|
|
"this never should have been yielded"
|
|
|
|
|
)
|
|
|
|
|
});
|
|
|
|
|
Balance { coin, amount: amount_to_serai_amount(coin, instruction.amount) }
|
|
|
|
|
}
|
|
|
|
|
Output::Eventuality { .. } => Balance { coin: Coin::Ether, amount: ETHER_DUST },
|
|
|
|
|
}
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
fn data(&self) -> &[u8] {
|
2024-09-20 00:55:21 -04:00
|
|
|
match self {
|
|
|
|
|
Output::Output { key: _, instruction } => &instruction.data,
|
|
|
|
|
Output::Eventuality { .. } => &[],
|
|
|
|
|
}
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn write<W: io::Write>(&self, writer: &mut W) -> io::Result<()> {
|
2024-09-20 00:55:21 -04:00
|
|
|
match self {
|
|
|
|
|
Output::Output { key, instruction } => {
|
|
|
|
|
writer.write_all(&[0])?;
|
|
|
|
|
writer.write_all(key.to_bytes().as_ref())?;
|
2025-01-21 03:49:29 -05:00
|
|
|
instruction.serialize(writer)
|
2024-09-20 00:55:21 -04:00
|
|
|
}
|
|
|
|
|
Output::Eventuality { key, nonce } => {
|
|
|
|
|
writer.write_all(&[1])?;
|
|
|
|
|
writer.write_all(key.to_bytes().as_ref())?;
|
|
|
|
|
writer.write_all(&nonce.to_le_bytes())
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
fn read<R: io::Read>(reader: &mut R) -> io::Result<Self> {
|
2024-09-20 00:55:21 -04:00
|
|
|
let mut kind = [0xff];
|
|
|
|
|
reader.read_exact(&mut kind)?;
|
|
|
|
|
if kind[0] >= 2 {
|
|
|
|
|
Err(io::Error::other("unknown Output type"))?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(match kind[0] {
|
|
|
|
|
0 => {
|
|
|
|
|
let key = Secp256k1::read_G(reader)?;
|
2025-01-21 03:49:29 -05:00
|
|
|
let instruction = EthereumInInstruction::deserialize_reader(reader)?;
|
2024-09-20 00:55:21 -04:00
|
|
|
Self::Output { key, instruction }
|
|
|
|
|
}
|
|
|
|
|
1 => {
|
|
|
|
|
let key = Secp256k1::read_G(reader)?;
|
|
|
|
|
let mut nonce = [0; 8];
|
|
|
|
|
reader.read_exact(&mut nonce)?;
|
|
|
|
|
let nonce = u64::from_le_bytes(nonce);
|
|
|
|
|
Self::Eventuality { key, nonce }
|
|
|
|
|
}
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
})
|
2024-09-14 07:54:18 -04:00
|
|
|
}
|
|
|
|
|
}
|