mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 20:59:23 +00:00
Smash serai-client so the processors don't need the entire lib to access their specific code
We prior controlled this with feature flags. It's just better to define their own crates.
This commit is contained in:
8
substrate/client/src/networks.rs
Normal file
8
substrate/client/src/networks.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
#[cfg(feature = "bitcoin")]
|
||||
pub use serai_client_bitcoin;
|
||||
|
||||
#[cfg(feature = "ethereum")]
|
||||
pub mod serai_client_ethereum;
|
||||
|
||||
#[cfg(feature = "monero")]
|
||||
pub mod serai_client_monero;
|
||||
@@ -1,170 +0,0 @@
|
||||
use core::{str::FromStr, fmt};
|
||||
|
||||
use borsh::{BorshSerialize, BorshDeserialize};
|
||||
|
||||
use bitcoin::{
|
||||
hashes::{Hash as HashTrait, hash160::Hash},
|
||||
PubkeyHash, ScriptHash,
|
||||
network::Network,
|
||||
WitnessVersion, WitnessProgram, ScriptBuf,
|
||||
address::{AddressType, NetworkChecked, Address as BAddress},
|
||||
};
|
||||
|
||||
use crate::primitives::address::ExternalAddress;
|
||||
|
||||
// SCALE-encodable representation of Bitcoin addresses, used internally.
|
||||
#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize)]
|
||||
enum EncodedAddress {
|
||||
P2PKH([u8; 20]),
|
||||
P2SH([u8; 20]),
|
||||
P2WPKH([u8; 20]),
|
||||
P2WSH([u8; 32]),
|
||||
P2TR([u8; 32]),
|
||||
}
|
||||
|
||||
impl TryFrom<&ScriptBuf> for EncodedAddress {
|
||||
type Error = ();
|
||||
fn try_from(script_buf: &ScriptBuf) -> Result<Self, ()> {
|
||||
// This uses mainnet as our encodings don't specify a network.
|
||||
let parsed_addr =
|
||||
BAddress::<NetworkChecked>::from_script(script_buf, Network::Bitcoin).map_err(|_| ())?;
|
||||
Ok(match parsed_addr.address_type() {
|
||||
Some(AddressType::P2pkh) => {
|
||||
EncodedAddress::P2PKH(*parsed_addr.pubkey_hash().unwrap().as_raw_hash().as_byte_array())
|
||||
}
|
||||
Some(AddressType::P2sh) => {
|
||||
EncodedAddress::P2SH(*parsed_addr.script_hash().unwrap().as_raw_hash().as_byte_array())
|
||||
}
|
||||
Some(AddressType::P2wpkh) => {
|
||||
let program = parsed_addr.witness_program().ok_or(())?;
|
||||
let program = program.program().as_bytes();
|
||||
EncodedAddress::P2WPKH(program.try_into().map_err(|_| ())?)
|
||||
}
|
||||
Some(AddressType::P2wsh) => {
|
||||
let program = parsed_addr.witness_program().ok_or(())?;
|
||||
let program = program.program().as_bytes();
|
||||
EncodedAddress::P2WSH(program.try_into().map_err(|_| ())?)
|
||||
}
|
||||
Some(AddressType::P2tr) => {
|
||||
let program = parsed_addr.witness_program().ok_or(())?;
|
||||
let program = program.program().as_bytes();
|
||||
EncodedAddress::P2TR(program.try_into().map_err(|_| ())?)
|
||||
}
|
||||
_ => Err(())?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EncodedAddress> for ScriptBuf {
|
||||
fn from(encoded: EncodedAddress) -> Self {
|
||||
match encoded {
|
||||
EncodedAddress::P2PKH(hash) => {
|
||||
ScriptBuf::new_p2pkh(&PubkeyHash::from_raw_hash(Hash::from_byte_array(hash)))
|
||||
}
|
||||
EncodedAddress::P2SH(hash) => {
|
||||
ScriptBuf::new_p2sh(&ScriptHash::from_raw_hash(Hash::from_byte_array(hash)))
|
||||
}
|
||||
EncodedAddress::P2WPKH(hash) => {
|
||||
ScriptBuf::new_witness_program(&WitnessProgram::new(WitnessVersion::V0, &hash).unwrap())
|
||||
}
|
||||
EncodedAddress::P2WSH(hash) => {
|
||||
ScriptBuf::new_witness_program(&WitnessProgram::new(WitnessVersion::V0, &hash).unwrap())
|
||||
}
|
||||
EncodedAddress::P2TR(key) => {
|
||||
ScriptBuf::new_witness_program(&WitnessProgram::new(WitnessVersion::V1, &key).unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A Bitcoin address usable with Serai.
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
pub struct Address(ScriptBuf);
|
||||
|
||||
// Support consuming into the underlying ScriptBuf.
|
||||
impl From<Address> for ScriptBuf {
|
||||
fn from(addr: Address) -> ScriptBuf {
|
||||
addr.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Address> for BAddress {
|
||||
fn from(addr: &Address) -> BAddress {
|
||||
// This fails if the script doesn't have an address representation, yet all our representable
|
||||
// addresses' scripts do
|
||||
BAddress::<NetworkChecked>::from_script(&addr.0, Network::Bitcoin).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
// Support converting a string into an address.
|
||||
impl FromStr for Address {
|
||||
type Err = ();
|
||||
fn from_str(str: &str) -> Result<Address, ()> {
|
||||
Address::new(
|
||||
BAddress::from_str(str)
|
||||
.map_err(|_| ())?
|
||||
.require_network(Network::Bitcoin)
|
||||
.map_err(|_| ())?
|
||||
.script_pubkey(),
|
||||
)
|
||||
.ok_or(())
|
||||
}
|
||||
}
|
||||
|
||||
// Support converting an address into a string.
|
||||
impl fmt::Display for Address {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
BAddress::from(self).fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<ExternalAddress> for Address {
|
||||
type Error = ();
|
||||
fn try_from(data: ExternalAddress) -> Result<Address, ()> {
|
||||
// Decode as an EncodedAddress, then map to a ScriptBuf
|
||||
let mut data = data.as_ref();
|
||||
let encoded = EncodedAddress::deserialize_reader(&mut data).map_err(|_| ())?;
|
||||
if !data.is_empty() {
|
||||
Err(())?
|
||||
}
|
||||
Ok(Address(ScriptBuf::from(encoded)))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Address> for EncodedAddress {
|
||||
fn from(addr: Address) -> EncodedAddress {
|
||||
// Safe since only encodable addresses can be created
|
||||
EncodedAddress::try_from(&addr.0).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Address> for ExternalAddress {
|
||||
fn from(addr: Address) -> ExternalAddress {
|
||||
// Safe since all variants are fixed-length and fit into `MAX_ADDRESS_LEN`
|
||||
ExternalAddress::try_from(borsh::to_vec(&EncodedAddress::from(addr)).unwrap()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl BorshSerialize for Address {
|
||||
fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
|
||||
EncodedAddress::from(self.clone()).serialize(writer)
|
||||
}
|
||||
}
|
||||
|
||||
impl BorshDeserialize for Address {
|
||||
fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
|
||||
Ok(Self(ScriptBuf::from(EncodedAddress::deserialize_reader(reader)?)))
|
||||
}
|
||||
}
|
||||
|
||||
impl Address {
|
||||
/// Create a new Address from a ScriptBuf.
|
||||
pub fn new(script_buf: ScriptBuf) -> Option<Self> {
|
||||
// If we can represent this Script, it's an acceptable address
|
||||
if EncodedAddress::try_from(&script_buf).is_ok() {
|
||||
return Some(Self(script_buf));
|
||||
}
|
||||
// Else, it isn't acceptable
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
use core::str::FromStr;
|
||||
use std::io::Read;
|
||||
|
||||
use borsh::{BorshSerialize, BorshDeserialize};
|
||||
|
||||
use crate::primitives::address::ExternalAddress;
|
||||
|
||||
/// THe maximum amount of gas an address is allowed to specify as its gas limit.
|
||||
///
|
||||
/// Payments to an address with a gas limit which exceed this value will be dropped entirely.
|
||||
pub const ADDRESS_GAS_LIMIT: u32 = 950_000;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize)]
|
||||
pub struct ContractDeployment {
|
||||
/// The gas limit to use for this contract's execution.
|
||||
///
|
||||
/// This MUST be less than the Serai gas limit. The cost of it, and the associated costs with
|
||||
/// making this transaction, will be deducted from the amount transferred.
|
||||
gas_limit: u32,
|
||||
/// The initialization code of the contract to deploy.
|
||||
///
|
||||
/// This contract will be deployed (executing the initialization code). No further calls will
|
||||
/// be made.
|
||||
code: Vec<u8>,
|
||||
}
|
||||
|
||||
/// A contract to deploy, enabling executing arbitrary code.
|
||||
impl ContractDeployment {
|
||||
pub fn new(gas_limit: u32, code: Vec<u8>) -> Option<Self> {
|
||||
// Check the gas limit is less the address gas limit
|
||||
if gas_limit > ADDRESS_GAS_LIMIT {
|
||||
None?;
|
||||
}
|
||||
|
||||
// The max address length, minus the type byte, minus the size of the gas
|
||||
const MAX_CODE_LEN: usize =
|
||||
(ExternalAddress::MAX_LEN as usize) - (1 + core::mem::size_of::<u32>());
|
||||
if code.len() > MAX_CODE_LEN {
|
||||
None?;
|
||||
}
|
||||
|
||||
Some(Self { gas_limit, code })
|
||||
}
|
||||
|
||||
pub fn gas_limit(&self) -> u32 {
|
||||
self.gas_limit
|
||||
}
|
||||
pub fn code(&self) -> &[u8] {
|
||||
&self.code
|
||||
}
|
||||
}
|
||||
|
||||
/// A representation of an Ethereum address.
|
||||
#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize)]
|
||||
pub enum Address {
|
||||
/// A traditional address.
|
||||
Address([u8; 20]),
|
||||
/// A contract to deploy, enabling executing arbitrary code.
|
||||
Contract(ContractDeployment),
|
||||
}
|
||||
|
||||
impl From<[u8; 20]> for Address {
|
||||
fn from(address: [u8; 20]) -> Self {
|
||||
Address::Address(address)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<ExternalAddress> for Address {
|
||||
type Error = ();
|
||||
fn try_from(data: ExternalAddress) -> Result<Address, ()> {
|
||||
let mut kind = [0xff];
|
||||
let mut reader: &[u8] = data.as_ref();
|
||||
reader.read_exact(&mut kind).map_err(|_| ())?;
|
||||
Ok(match kind[0] {
|
||||
0 => {
|
||||
let mut address = [0xff; 20];
|
||||
reader.read_exact(&mut address).map_err(|_| ())?;
|
||||
Address::Address(address)
|
||||
}
|
||||
1 => {
|
||||
let mut gas_limit = [0xff; 4];
|
||||
reader.read_exact(&mut gas_limit).map_err(|_| ())?;
|
||||
Address::Contract(ContractDeployment {
|
||||
gas_limit: {
|
||||
let gas_limit = u32::from_le_bytes(gas_limit);
|
||||
if gas_limit > ADDRESS_GAS_LIMIT {
|
||||
Err(())?;
|
||||
}
|
||||
gas_limit
|
||||
},
|
||||
// The code is whatever's left since the ExternalAddress is a delimited container of
|
||||
// appropriately bounded length
|
||||
code: reader.to_vec(),
|
||||
})
|
||||
}
|
||||
_ => Err(())?,
|
||||
})
|
||||
}
|
||||
}
|
||||
impl From<Address> for ExternalAddress {
|
||||
fn from(address: Address) -> ExternalAddress {
|
||||
let mut res = Vec::with_capacity(1 + 20);
|
||||
match address {
|
||||
Address::Address(address) => {
|
||||
res.push(0);
|
||||
res.extend(&address);
|
||||
}
|
||||
Address::Contract(ContractDeployment { gas_limit, code }) => {
|
||||
res.push(1);
|
||||
res.extend(&gas_limit.to_le_bytes());
|
||||
res.extend(&code);
|
||||
}
|
||||
}
|
||||
// We only construct addresses whose code is small enough this can safely be constructed
|
||||
ExternalAddress::try_from(res).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Address {
|
||||
type Err = ();
|
||||
fn from_str(str: &str) -> Result<Address, ()> {
|
||||
let Some(address) = str.strip_prefix("0x") else { Err(())? };
|
||||
if address.len() != 40 {
|
||||
Err(())?
|
||||
};
|
||||
Ok(Address::Address(
|
||||
hex::decode(address.to_lowercase()).map_err(|_| ())?.try_into().map_err(|_| ())?,
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
#[cfg(feature = "bitcoin")]
|
||||
pub mod bitcoin;
|
||||
|
||||
#[cfg(feature = "ethereum")]
|
||||
pub mod ethereum;
|
||||
|
||||
#[cfg(feature = "monero")]
|
||||
pub mod monero;
|
||||
@@ -1,142 +0,0 @@
|
||||
use core::{str::FromStr, fmt};
|
||||
|
||||
use dalek_ff_group::{EdwardsPoint, Ed25519};
|
||||
use ciphersuite::GroupIo;
|
||||
|
||||
use monero_address::{Network, AddressType as MoneroAddressType, MoneroAddress};
|
||||
|
||||
use crate::primitives::address::ExternalAddress;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
enum AddressType {
|
||||
Legacy,
|
||||
Subaddress,
|
||||
Featured(u8),
|
||||
}
|
||||
|
||||
/// A representation of a Monero address.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub struct Address {
|
||||
kind: AddressType,
|
||||
spend: EdwardsPoint,
|
||||
view: EdwardsPoint,
|
||||
}
|
||||
|
||||
fn byte_for_kind(kind: AddressType) -> u8 {
|
||||
// We use the second and third highest bits for the type
|
||||
// This leaves the top bit open for interpretation as a VarInt later
|
||||
match kind {
|
||||
AddressType::Legacy => 0,
|
||||
AddressType::Subaddress => 1 << 5,
|
||||
AddressType::Featured(flags) => {
|
||||
// The flags only take up the low three bits
|
||||
debug_assert!(flags <= 0b111);
|
||||
(2 << 5) | flags
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl borsh::BorshSerialize for Address {
|
||||
fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
|
||||
writer.write_all(&[byte_for_kind(self.kind)])?;
|
||||
writer.write_all(&self.spend.compress().to_bytes())?;
|
||||
writer.write_all(&self.view.compress().to_bytes())
|
||||
}
|
||||
}
|
||||
impl borsh::BorshDeserialize for Address {
|
||||
fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
|
||||
let mut kind_byte = [0xff];
|
||||
reader.read_exact(&mut kind_byte)?;
|
||||
let kind_byte = kind_byte[0];
|
||||
let kind = match kind_byte >> 5 {
|
||||
0 => AddressType::Legacy,
|
||||
1 => AddressType::Subaddress,
|
||||
2 => AddressType::Featured(kind_byte & 0b111),
|
||||
_ => Err(borsh::io::Error::other("unrecognized type"))?,
|
||||
};
|
||||
// Check this wasn't malleated
|
||||
if byte_for_kind(kind) != kind_byte {
|
||||
Err(borsh::io::Error::other("malleated type byte"))?;
|
||||
}
|
||||
let spend = Ed25519::read_G(reader)?;
|
||||
let view = Ed25519::read_G(reader)?;
|
||||
Ok(Self { kind, spend, view })
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<MoneroAddress> for Address {
|
||||
type Error = ();
|
||||
fn try_from(address: MoneroAddress) -> Result<Self, ()> {
|
||||
let spend = address.spend().compress().to_bytes();
|
||||
let view = address.view().compress().to_bytes();
|
||||
let kind = match address.kind() {
|
||||
MoneroAddressType::Legacy => AddressType::Legacy,
|
||||
MoneroAddressType::LegacyIntegrated(_) => Err(())?,
|
||||
MoneroAddressType::Subaddress => AddressType::Subaddress,
|
||||
MoneroAddressType::Featured { subaddress, payment_id, guaranteed } => {
|
||||
if payment_id.is_some() {
|
||||
Err(())?
|
||||
}
|
||||
// This maintains the same bit layout as featured addresses use
|
||||
AddressType::Featured(u8::from(*subaddress) + (u8::from(*guaranteed) << 2))
|
||||
}
|
||||
};
|
||||
Ok(Address {
|
||||
kind,
|
||||
spend: Ed25519::read_G(&mut spend.as_slice()).map_err(|_| ())?,
|
||||
view: Ed25519::read_G(&mut view.as_slice()).map_err(|_| ())?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Address> for MoneroAddress {
|
||||
fn from(address: Address) -> MoneroAddress {
|
||||
let kind = match address.kind {
|
||||
AddressType::Legacy => MoneroAddressType::Legacy,
|
||||
AddressType::Subaddress => MoneroAddressType::Subaddress,
|
||||
AddressType::Featured(features) => {
|
||||
debug_assert!(features <= 0b111);
|
||||
let subaddress = (features & 1) != 0;
|
||||
let integrated = (features & (1 << 1)) != 0;
|
||||
debug_assert!(!integrated);
|
||||
let guaranteed = (features & (1 << 2)) != 0;
|
||||
MoneroAddressType::Featured { subaddress, payment_id: None, guaranteed }
|
||||
}
|
||||
};
|
||||
MoneroAddress::new(Network::Mainnet, kind, address.spend.0, address.view.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<ExternalAddress> for Address {
|
||||
type Error = ();
|
||||
fn try_from(data: ExternalAddress) -> Result<Address, ()> {
|
||||
// Decode as an Address
|
||||
let mut data = data.as_ref();
|
||||
let address =
|
||||
<Address as borsh::BorshDeserialize>::deserialize_reader(&mut data).map_err(|_| ())?;
|
||||
if !data.is_empty() {
|
||||
Err(())?
|
||||
}
|
||||
Ok(address)
|
||||
}
|
||||
}
|
||||
impl From<Address> for ExternalAddress {
|
||||
fn from(address: Address) -> ExternalAddress {
|
||||
// This is 65 bytes which is less than MAX_ADDRESS_LEN
|
||||
ExternalAddress::try_from(borsh::to_vec(&address).unwrap()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Address {
|
||||
type Err = ();
|
||||
fn from_str(str: &str) -> Result<Address, ()> {
|
||||
let Ok(address) = MoneroAddress::from_str(Network::Mainnet, str) else { Err(())? };
|
||||
Address::try_from(address)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Address {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
MoneroAddress::from(*self).fmt(f)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user