2024-09-12 18:40:10 -04:00
|
|
|
use std::io;
|
|
|
|
|
|
|
|
|
|
use ciphersuite::{group::Group, Ciphersuite, Ed25519};
|
|
|
|
|
|
|
|
|
|
use monero_wallet::WalletOutput;
|
|
|
|
|
|
|
|
|
|
use scale::{Encode, Decode};
|
|
|
|
|
use borsh::{BorshSerialize, BorshDeserialize};
|
|
|
|
|
|
|
|
|
|
use serai_client::{
|
|
|
|
|
primitives::{Coin, Amount, Balance},
|
|
|
|
|
networks::monero::Address,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use primitives::{OutputType, ReceivedOutput};
|
|
|
|
|
|
2024-09-13 02:12:32 -04:00
|
|
|
use crate::{EXTERNAL_SUBADDRESS, BRANCH_SUBADDRESS, CHANGE_SUBADDRESS, FORWARDED_SUBADDRESS};
|
|
|
|
|
|
2024-09-12 18:40:10 -04:00
|
|
|
#[rustfmt::skip]
|
|
|
|
|
#[derive(
|
|
|
|
|
Clone, Copy, PartialEq, Eq, Default, Hash, Debug, Encode, Decode, BorshSerialize, BorshDeserialize,
|
|
|
|
|
)]
|
|
|
|
|
pub(crate) struct OutputId(pub(crate) [u8; 32]);
|
|
|
|
|
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-13 05:11:07 -04:00
|
|
|
pub(crate) struct Output(pub(crate) WalletOutput);
|
2024-09-12 18:40:10 -04:00
|
|
|
impl ReceivedOutput<<Ed25519 as Ciphersuite>::G, Address> for Output {
|
|
|
|
|
type Id = OutputId;
|
|
|
|
|
type TransactionId = [u8; 32];
|
|
|
|
|
|
|
|
|
|
fn kind(&self) -> OutputType {
|
2024-09-14 01:38:31 -04:00
|
|
|
let subaddress = self.0.subaddress().unwrap();
|
|
|
|
|
if subaddress == EXTERNAL_SUBADDRESS {
|
2024-09-13 02:12:32 -04:00
|
|
|
return OutputType::External;
|
|
|
|
|
}
|
2024-09-14 01:38:31 -04:00
|
|
|
if subaddress == BRANCH_SUBADDRESS {
|
2024-09-13 02:12:32 -04:00
|
|
|
return OutputType::Branch;
|
|
|
|
|
}
|
2024-09-14 01:38:31 -04:00
|
|
|
if subaddress == CHANGE_SUBADDRESS {
|
2024-09-13 02:12:32 -04:00
|
|
|
return OutputType::Change;
|
|
|
|
|
}
|
2024-09-14 01:38:31 -04:00
|
|
|
if subaddress == FORWARDED_SUBADDRESS {
|
2024-09-13 02:12:32 -04:00
|
|
|
return OutputType::Forwarded;
|
|
|
|
|
}
|
|
|
|
|
unreachable!("scanned output to unknown subaddress");
|
2024-09-12 18:40:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn id(&self) -> Self::Id {
|
|
|
|
|
OutputId(self.0.key().compress().to_bytes())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn transaction_id(&self) -> Self::TransactionId {
|
|
|
|
|
self.0.transaction()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn key(&self) -> <Ed25519 as Ciphersuite>::G {
|
|
|
|
|
// The spend key will be a key we generated, so it'll be in the prime-order subgroup
|
|
|
|
|
// The output's key is the spend key + (key_offset * G), so it's in the prime-order subgroup if
|
|
|
|
|
// the spend key is
|
|
|
|
|
dalek_ff_group::EdwardsPoint(
|
|
|
|
|
self.0.key() - (*<Ed25519 as Ciphersuite>::G::generator() * self.0.key_offset()),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn presumed_origin(&self) -> Option<Address> {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn balance(&self) -> Balance {
|
|
|
|
|
Balance { coin: Coin::Monero, amount: Amount(self.0.commitment().amount) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn data(&self) -> &[u8] {
|
|
|
|
|
self.0.arbitrary_data().first().map_or(&[], Vec::as_slice)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn write<W: io::Write>(&self, writer: &mut W) -> io::Result<()> {
|
|
|
|
|
self.0.write(writer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read<R: io::Read>(reader: &mut R) -> io::Result<Self> {
|
|
|
|
|
WalletOutput::read(reader).map(Self)
|
|
|
|
|
}
|
|
|
|
|
}
|