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:
Luke Parker
2025-11-04 19:02:37 -05:00
parent 1b499edfe1
commit 973287d0a1
40 changed files with 232 additions and 79 deletions

View File

@@ -0,0 +1,27 @@
[package]
name = "serai-client-bitcoin"
version = "0.1.0"
description = "Bitcoin client library for the Serai network"
license = "MIT"
repository = "https://github.com/serai-dex/serai/tree/develop/substrate/client/bitcoin"
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
keywords = ["serai"]
edition = "2021"
rust-version = "1.85"
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
[lints]
workspace = true
[dependencies]
borsh = { version = "1", default-features = false, features = ["derive"] }
serai-primitives = { path = "../../primitives", version = "0.1", default-features = false }
bitcoin = { version = "0.32", default-features = false }
[features]
std = ["borsh/std", "serai-primitives/std", "bitcoin/std"]

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022-2025 Luke Parker
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,172 @@
#![no_std]
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 serai_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
}
}