diff --git a/crates/brk_computer/src/stores.rs b/crates/brk_computer/src/stores.rs index c7127d1b8..f23a08120 100644 --- a/crates/brk_computer/src/stores.rs +++ b/crates/brk_computer/src/stores.rs @@ -1,89 +1,464 @@ -use std::path::Path; +use std::{path::Path, thread}; use brk_core::{ - OutputIndex, P2AAddressIndex, P2MSOutputIndex, P2PK33AddressIndex, P2PK65AddressIndex, - P2PKHAddressIndex, P2SHAddressIndex, P2TRAddressIndex, P2WPKHAddressIndex, P2WSHAddressIndex, - Version, + AddressData, EmptyAddressData, P2AAddressIndex, P2AAddressIndexOutputindex, P2PK33AddressIndex, + P2PK33AddressIndexOutputindex, P2PK65AddressIndex, P2PK65AddressIndexOutputindex, + P2PKHAddressIndex, P2PKHAddressIndexOutputindex, P2SHAddressIndex, P2SHAddressIndexOutputindex, + P2TRAddressIndex, P2TRAddressIndexOutputindex, P2WPKHAddressIndex, + P2WPKHAddressIndexOutputindex, P2WSHAddressIndex, P2WSHAddressIndexOutputindex, Unit, Version, }; use brk_store::Store; use fjall::TransactionalKeyspace; -use jiff::Unit; -const _VERSION: Version = Version::ZERO; +const VERSION: Version = Version::ZERO; #[derive(Clone)] pub struct Stores { - pub p2aaddressindex_to_addressdata: Store<(P2AAddressIndex, OutputIndex), Unit>, - pub p2aaddressindex_to_emptyaddressdata: Store<(P2AAddressIndex, OutputIndex), Unit>, - pub p2aaddressindex_to_utxos_received: Store<(P2AAddressIndex, OutputIndex), Unit>, - pub p2aaddressindex_to_utxos_sent: Store, + pub p2aaddressindex_to_addressdata: Store, + pub p2aaddressindex_to_emptyaddressdata: Store, + pub p2aaddressindex_to_utxos_received: Store, + pub p2aaddressindex_to_utxos_sent: Store, - pub p2msoutputindex_to_addressdata: Store, - pub p2msoutputindex_to_emptyaddressdata: Store, - pub p2msoutputindex_to_utxos_received: Store, - pub p2msoutputindex_to_utxos_sent: Store, + pub p2pk33addressindex_to_addressdata: Store, + pub p2pk33addressindex_to_emptyaddressdata: Store, + pub p2pk33addressindex_to_utxos_received: Store, + pub p2pk33addressindex_to_utxos_sent: Store, - pub p2pk33addressindex_to_addressdata: Store, - pub p2pk33addressindex_to_emptyaddressdata: Store, - pub p2pk33addressindex_to_utxos_received: Store, - pub p2pk33addressindex_to_utxos_sent: Store, + pub p2pk65addressindex_to_addressdata: Store, + pub p2pk65addressindex_to_emptyaddressdata: Store, + pub p2pk65addressindex_to_utxos_received: Store, + pub p2pk65addressindex_to_utxos_sent: Store, - pub p2pk65addressindex_to_addressdata: Store, - pub p2pk65addressindex_to_emptyaddressdata: Store, - pub p2pk65addressindex_to_utxos_received: Store, - pub p2pk65addressindex_to_utxos_sent: Store, + pub p2pkhaddressindex_to_addressdata: Store, + pub p2pkhaddressindex_to_emptyaddressdata: Store, + pub p2pkhaddressindex_to_utxos_received: Store, + pub p2pkhaddressindex_to_utxos_sent: Store, - pub p2pkhaddressindex_to_addressdata: Store, - pub p2pkhaddressindex_to_emptyaddressdata: Store, - pub p2pkhaddressindex_to_utxos_received: Store, - pub p2pkhaddressindex_to_utxos_sent: Store, + pub p2shaddressindex_to_addressdata: Store, + pub p2shaddressindex_to_emptyaddressdata: Store, + pub p2shaddressindex_to_utxos_received: Store, + pub p2shaddressindex_to_utxos_sent: Store, - pub p2shaddressindex_to_addressdata: Store, - pub p2shaddressindex_to_emptyaddressdata: Store, - pub p2shaddressindex_to_utxos_received: Store, - pub p2shaddressindex_to_utxos_sent: Store, + pub p2traddressindex_to_addressdata: Store, + pub p2traddressindex_to_emptyaddressdata: Store, + pub p2traddressindex_to_utxos_received: Store, + pub p2traddressindex_to_utxos_sent: Store, - pub p2traddressindex_to_addressdata: Store, - pub p2traddressindex_to_emptyaddressdata: Store, - pub p2traddressindex_to_utxos_received: Store, - pub p2traddressindex_to_utxos_sent: Store, + pub p2wpkhaddressindex_to_addressdata: Store, + pub p2wpkhaddressindex_to_emptyaddressdata: Store, + pub p2wpkhaddressindex_to_utxos_received: Store, + pub p2wpkhaddressindex_to_utxos_sent: Store, - pub p2wpkhaddressindex_to_addressdata: Store, - pub p2wpkhaddressindex_to_emptyaddressdata: Store, - pub p2wpkhaddressindex_to_utxos_received: Store, - pub p2wpkhaddressindex_to_utxos_sent: Store, - - pub p2wshaddressindex_to_addressdata: Store, - pub p2wshaddressindex_to_emptyaddressdata: Store, - pub p2wshaddressindex_to_utxos_received: Store, - pub p2wshaddressindex_to_utxos_sent: Store, + pub p2wshaddressindex_to_addressdata: Store, + pub p2wshaddressindex_to_emptyaddressdata: Store, + pub p2wshaddressindex_to_utxos_received: Store, + pub p2wshaddressindex_to_utxos_sent: Store, } impl Stores { - pub fn import(_: &Path, _: Version, _: &TransactionalKeyspace) -> color_eyre::Result { - // let address_to_utxos_received = Store::import( - // keyspace.clone(), - // path, - // "address_to_utxos_received", - // version + VERSION + Version::ZERO, - // )?; - // let address_to_utxos_spent = Store::import( - // keyspace.clone(), - // path, - // "address_to_utxos_spent", - // version + VERSION + Version::ZERO, - // )?; + pub fn import( + path: &Path, + version: Version, + keyspace: &TransactionalKeyspace, + ) -> color_eyre::Result { + let ( + ( + p2aaddressindex_to_addressdata, + p2aaddressindex_to_emptyaddressdata, + p2aaddressindex_to_utxos_received, + p2aaddressindex_to_utxos_sent, + ), + ( + p2pk33addressindex_to_addressdata, + p2pk33addressindex_to_emptyaddressdata, + p2pk33addressindex_to_utxos_received, + p2pk33addressindex_to_utxos_sent, + ), + ( + p2pk65addressindex_to_addressdata, + p2pk65addressindex_to_emptyaddressdata, + p2pk65addressindex_to_utxos_received, + p2pk65addressindex_to_utxos_sent, + ), + ( + p2pkhaddressindex_to_addressdata, + p2pkhaddressindex_to_emptyaddressdata, + p2pkhaddressindex_to_utxos_received, + p2pkhaddressindex_to_utxos_sent, + ), + ( + p2shaddressindex_to_addressdata, + p2shaddressindex_to_emptyaddressdata, + p2shaddressindex_to_utxos_received, + p2shaddressindex_to_utxos_sent, + ), + ( + p2traddressindex_to_addressdata, + p2traddressindex_to_emptyaddressdata, + p2traddressindex_to_utxos_received, + p2traddressindex_to_utxos_sent, + ), + ( + p2wpkhaddressindex_to_addressdata, + p2wpkhaddressindex_to_emptyaddressdata, + p2wpkhaddressindex_to_utxos_received, + p2wpkhaddressindex_to_utxos_sent, + ), + ( + p2wshaddressindex_to_addressdata, + p2wshaddressindex_to_emptyaddressdata, + p2wshaddressindex_to_utxos_received, + p2wshaddressindex_to_utxos_sent, + ), + ) = thread::scope(|scope| { + let p2a = scope.spawn(|| { + ( + Store::import( + keyspace, + path, + "p2aaddressindex_to_addressdata", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2aaddressindex_to_emptyaddressdata", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2aaddressindex_to_utxos_received", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2aaddressindex_to_utxos_sent", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + ) + }); - todo!(); - // Ok(Self { - // p2aaddressindex_to_utxos_received: Store::import( - // keyspace, - // path, - // name, - // version, - // bloom_filter_bits, - // ), // address_to_utxos_received, - // // address_to_utxos_spent, - // }) + let p2pk33 = scope.spawn(|| { + ( + Store::import( + keyspace, + path, + "p2pk33addressindex_to_addressdata", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2pk33addressindex_to_emptyaddressdata", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2pk33addressindex_to_utxos_received", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2pk33addressindex_to_utxos_sent", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + ) + }); + + let p2pk65 = scope.spawn(|| { + ( + Store::import( + keyspace, + path, + "p2pk65addressindex_to_addressdata", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2pk65addressindex_to_emptyaddressdata", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2pk65addressindex_to_utxos_received", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2pk65addressindex_to_utxos_sent", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + ) + }); + + let p2pkh = scope.spawn(|| { + ( + Store::import( + keyspace, + path, + "p2pkhaddressindex_to_addressdata", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2pkhaddressindex_to_emptyaddressdata", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2pkhaddressindex_to_utxos_received", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2pkhaddressindex_to_utxos_sent", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + ) + }); + + let p2sh = scope.spawn(|| { + ( + Store::import( + keyspace, + path, + "p2shaddressindex_to_addressdata", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2shaddressindex_to_emptyaddressdata", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2shaddressindex_to_utxos_received", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2shaddressindex_to_utxos_sent", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + ) + }); + + let p2tr = scope.spawn(|| { + ( + Store::import( + keyspace, + path, + "p2traddressindex_to_addressdata", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2traddressindex_to_emptyaddressdata", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2traddressindex_to_utxos_received", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2traddressindex_to_utxos_sent", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + ) + }); + + let p2wpkh = scope.spawn(|| { + ( + Store::import( + keyspace, + path, + "p2wpkhaddressindex_to_addressdata", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2wpkhaddressindex_to_emptyaddressdata", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2wpkhaddressindex_to_utxos_received", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2wpkhaddressindex_to_utxos_sent", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + ) + }); + + let p2wsh = scope.spawn(|| { + ( + Store::import( + keyspace, + path, + "p2wshaddressindex_to_addressdata", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2wshaddressindex_to_emptyaddressdata", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2wshaddressindex_to_utxos_received", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + Store::import( + keyspace, + path, + "p2wshaddressindex_to_utxos_sent", + version + VERSION + Version::ZERO, + None, + ) + .unwrap(), + ) + }); + + ( + p2a.join().unwrap(), + p2pk33.join().unwrap(), + p2pk65.join().unwrap(), + p2pkh.join().unwrap(), + p2sh.join().unwrap(), + p2tr.join().unwrap(), + p2wpkh.join().unwrap(), + p2wsh.join().unwrap(), + ) + }); + + Ok(Self { + p2aaddressindex_to_addressdata, + p2aaddressindex_to_emptyaddressdata, + p2aaddressindex_to_utxos_received, + p2aaddressindex_to_utxos_sent, + + p2pk33addressindex_to_addressdata, + p2pk33addressindex_to_emptyaddressdata, + p2pk33addressindex_to_utxos_received, + p2pk33addressindex_to_utxos_sent, + + p2pk65addressindex_to_addressdata, + p2pk65addressindex_to_emptyaddressdata, + p2pk65addressindex_to_utxos_received, + p2pk65addressindex_to_utxos_sent, + + p2pkhaddressindex_to_addressdata, + p2pkhaddressindex_to_emptyaddressdata, + p2pkhaddressindex_to_utxos_received, + p2pkhaddressindex_to_utxos_sent, + + p2shaddressindex_to_addressdata, + p2shaddressindex_to_emptyaddressdata, + p2shaddressindex_to_utxos_received, + p2shaddressindex_to_utxos_sent, + + p2traddressindex_to_addressdata, + p2traddressindex_to_emptyaddressdata, + p2traddressindex_to_utxos_received, + p2traddressindex_to_utxos_sent, + + p2wpkhaddressindex_to_addressdata, + p2wpkhaddressindex_to_emptyaddressdata, + p2wpkhaddressindex_to_utxos_received, + p2wpkhaddressindex_to_utxos_sent, + + p2wshaddressindex_to_addressdata, + p2wshaddressindex_to_emptyaddressdata, + p2wshaddressindex_to_utxos_received, + p2wshaddressindex_to_utxos_sent, + }) } } diff --git a/crates/brk_core/src/structs/addressdata.rs b/crates/brk_core/src/structs/addressdata.rs index fdd07901d..936660c20 100644 --- a/crates/brk_core/src/structs/addressdata.rs +++ b/crates/brk_core/src/structs/addressdata.rs @@ -1,6 +1,11 @@ +use byteview::ByteView; +use zerocopy::{FromBytes, IntoBytes}; +use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout}; + use crate::{Dollars, Sats}; -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, FromBytes, Immutable, IntoBytes, KnownLayout)] +#[repr(C, packed)] pub struct AddressData { pub sent: Sats, pub received: Sats, @@ -13,3 +18,19 @@ impl AddressData { (u64::from(self.received) - u64::from(self.sent)).into() } } + +impl From for AddressData { + fn from(value: ByteView) -> Self { + Self::read_from_bytes(&value).unwrap() + } +} +impl From for ByteView { + fn from(value: AddressData) -> Self { + Self::from(&value) + } +} +impl From<&AddressData> for ByteView { + fn from(value: &AddressData) -> Self { + Self::new(value.as_bytes()) + } +} diff --git a/crates/brk_core/src/structs/emptyaddressdata.rs b/crates/brk_core/src/structs/emptyaddressdata.rs index b8926056e..34c9e0ce3 100644 --- a/crates/brk_core/src/structs/emptyaddressdata.rs +++ b/crates/brk_core/src/structs/emptyaddressdata.rs @@ -1,8 +1,28 @@ +use byteview::ByteView; +use zerocopy::{FromBytes, IntoBytes}; +use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout}; + use crate::Sats; -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone, FromBytes, Immutable, IntoBytes, KnownLayout)] pub struct EmptyAddressData { pub transfered: Sats, } impl EmptyAddressData {} + +impl From for EmptyAddressData { + fn from(value: ByteView) -> Self { + Self::read_from_bytes(&value).unwrap() + } +} +impl From for ByteView { + fn from(value: EmptyAddressData) -> Self { + Self::from(&value) + } +} +impl From<&EmptyAddressData> for ByteView { + fn from(value: &EmptyAddressData) -> Self { + Self::new(value.as_bytes()) + } +} diff --git a/crates/brk_core/src/structs/mod.rs b/crates/brk_core/src/structs/mod.rs index e1d5faa2b..d290e0c87 100644 --- a/crates/brk_core/src/structs/mod.rs +++ b/crates/brk_core/src/structs/mod.rs @@ -22,14 +22,22 @@ mod opreturnindex; mod outputindex; mod outputtype; mod p2aaddressindex; +mod p2aaddressindex_outputindex; mod p2msoutputindex; mod p2pk33addressindex; +mod p2pk33addressindex_outputindex; mod p2pk65addressindex; +mod p2pk65addressindex_outputindex; mod p2pkhaddressindex; +mod p2pkhaddressindex_outputindex; mod p2shaddressindex; +mod p2shaddressindex_outputindex; mod p2traddressindex; +mod p2traddressindex_outputindex; mod p2wpkhaddressindex; +mod p2wpkhaddressindex_outputindex; mod p2wshaddressindex; +mod p2wshaddressindex_outputindex; mod quarterindex; mod rawlocktime; mod sats; @@ -78,14 +86,22 @@ pub use opreturnindex::*; pub use outputindex::*; pub use outputtype::*; pub use p2aaddressindex::*; +pub use p2aaddressindex_outputindex::*; pub use p2msoutputindex::*; pub use p2pk33addressindex::*; +pub use p2pk33addressindex_outputindex::*; pub use p2pk65addressindex::*; +pub use p2pk65addressindex_outputindex::*; pub use p2pkhaddressindex::*; +pub use p2pkhaddressindex_outputindex::*; pub use p2shaddressindex::*; +pub use p2shaddressindex_outputindex::*; pub use p2traddressindex::*; +pub use p2traddressindex_outputindex::*; pub use p2wpkhaddressindex::*; +pub use p2wpkhaddressindex_outputindex::*; pub use p2wshaddressindex::*; +pub use p2wshaddressindex_outputindex::*; pub use quarterindex::*; pub use rawlocktime::*; pub use sats::*; diff --git a/crates/brk_core/src/structs/p2aaddressindex.rs b/crates/brk_core/src/structs/p2aaddressindex.rs index 9e268b9ef..6276d91bc 100644 --- a/crates/brk_core/src/structs/p2aaddressindex.rs +++ b/crates/brk_core/src/structs/p2aaddressindex.rs @@ -1,7 +1,9 @@ use std::ops::Add; +use byteview::ByteView; use derive_deref::{Deref, DerefMut}; use serde::Serialize; +use zerocopy::{FromBytes, IntoBytes}; use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout}; use crate::{CheckedSub, Printable, TypeIndex}; @@ -29,6 +31,16 @@ impl From for P2AAddressIndex { Self(value) } } +impl From for u32 { + fn from(value: P2AAddressIndex) -> Self { + Self::from(*value) + } +} +impl From for P2AAddressIndex { + fn from(value: u32) -> Self { + Self(TypeIndex::from(value)) + } +} impl From for usize { fn from(value: P2AAddressIndex) -> Self { Self::from(*value) @@ -59,3 +71,19 @@ impl Printable for P2AAddressIndex { &["aaddr", "p2aaddr", "p2aaddressindex"] } } + +impl From for P2AAddressIndex { + fn from(value: ByteView) -> Self { + Self::read_from_bytes(&value).unwrap() + } +} +impl From for ByteView { + fn from(value: P2AAddressIndex) -> Self { + Self::from(&value) + } +} +impl From<&P2AAddressIndex> for ByteView { + fn from(value: &P2AAddressIndex) -> Self { + Self::new(value.as_bytes()) + } +} diff --git a/crates/brk_core/src/structs/p2aaddressindex_outputindex.rs b/crates/brk_core/src/structs/p2aaddressindex_outputindex.rs new file mode 100644 index 000000000..f5e182bbc --- /dev/null +++ b/crates/brk_core/src/structs/p2aaddressindex_outputindex.rs @@ -0,0 +1,40 @@ +use byteview::ByteView; +use serde::Serialize; + +use crate::{copy_first_4bytes, copy_first_8bytes}; + +use super::{OutputIndex, P2AAddressIndex}; + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Serialize)] +pub struct P2AAddressIndexOutputindex { + addressindex: P2AAddressIndex, + outputindex: OutputIndex, +} + +impl From for P2AAddressIndexOutputindex { + fn from(value: ByteView) -> Self { + let addressindex = + P2AAddressIndex::from(u32::from_be_bytes(copy_first_4bytes(&value).unwrap())); + let outputindex = OutputIndex::from(u64::from_be_bytes(copy_first_8bytes(&value).unwrap())); + Self { + addressindex, + outputindex, + } + } +} +impl From for ByteView { + fn from(value: P2AAddressIndexOutputindex) -> Self { + ByteView::from(&value) + } +} +impl From<&P2AAddressIndexOutputindex> for ByteView { + fn from(value: &P2AAddressIndexOutputindex) -> Self { + ByteView::from( + [ + u32::from(value.addressindex).to_be_bytes().as_slice(), + u64::from(value.outputindex).to_be_bytes().as_slice(), + ] + .concat(), + ) + } +} diff --git a/crates/brk_core/src/structs/p2pk33addressindex.rs b/crates/brk_core/src/structs/p2pk33addressindex.rs index e925af645..c26426be6 100644 --- a/crates/brk_core/src/structs/p2pk33addressindex.rs +++ b/crates/brk_core/src/structs/p2pk33addressindex.rs @@ -1,7 +1,9 @@ use std::ops::Add; +use byteview::ByteView; use derive_deref::{Deref, DerefMut}; use serde::Serialize; +use zerocopy::{FromBytes, IntoBytes}; use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout}; use crate::{CheckedSub, Printable, TypeIndex}; @@ -29,6 +31,16 @@ impl From for P2PK33AddressIndex { Self(value) } } +impl From for u32 { + fn from(value: P2PK33AddressIndex) -> Self { + Self::from(*value) + } +} +impl From for P2PK33AddressIndex { + fn from(value: u32) -> Self { + Self(TypeIndex::from(value)) + } +} impl From for usize { fn from(value: P2PK33AddressIndex) -> Self { Self::from(*value) @@ -60,3 +72,19 @@ impl Printable for P2PK33AddressIndex { &["pk33addr", "p2pk33addr", "p2pk33addressindex"] } } + +impl From for P2PK33AddressIndex { + fn from(value: ByteView) -> Self { + Self::read_from_bytes(&value).unwrap() + } +} +impl From for ByteView { + fn from(value: P2PK33AddressIndex) -> Self { + Self::from(&value) + } +} +impl From<&P2PK33AddressIndex> for ByteView { + fn from(value: &P2PK33AddressIndex) -> Self { + Self::new(value.as_bytes()) + } +} diff --git a/crates/brk_core/src/structs/p2pk33addressindex_outputindex.rs b/crates/brk_core/src/structs/p2pk33addressindex_outputindex.rs new file mode 100644 index 000000000..f96989599 --- /dev/null +++ b/crates/brk_core/src/structs/p2pk33addressindex_outputindex.rs @@ -0,0 +1,40 @@ +use byteview::ByteView; +use serde::Serialize; + +use crate::{copy_first_4bytes, copy_first_8bytes}; + +use super::{OutputIndex, P2PK33AddressIndex}; + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Serialize)] +pub struct P2PK33AddressIndexOutputindex { + addressindex: P2PK33AddressIndex, + outputindex: OutputIndex, +} + +impl From for P2PK33AddressIndexOutputindex { + fn from(value: ByteView) -> Self { + let addressindex = + P2PK33AddressIndex::from(u32::from_be_bytes(copy_first_4bytes(&value).unwrap())); + let outputindex = OutputIndex::from(u64::from_be_bytes(copy_first_8bytes(&value).unwrap())); + Self { + addressindex, + outputindex, + } + } +} +impl From for ByteView { + fn from(value: P2PK33AddressIndexOutputindex) -> Self { + ByteView::from(&value) + } +} +impl From<&P2PK33AddressIndexOutputindex> for ByteView { + fn from(value: &P2PK33AddressIndexOutputindex) -> Self { + ByteView::from( + [ + u32::from(value.addressindex).to_be_bytes().as_slice(), + u64::from(value.outputindex).to_be_bytes().as_slice(), + ] + .concat(), + ) + } +} diff --git a/crates/brk_core/src/structs/p2pk65addressindex.rs b/crates/brk_core/src/structs/p2pk65addressindex.rs index a16b3a3e6..1b912859c 100644 --- a/crates/brk_core/src/structs/p2pk65addressindex.rs +++ b/crates/brk_core/src/structs/p2pk65addressindex.rs @@ -1,7 +1,9 @@ use std::ops::Add; +use byteview::ByteView; use derive_deref::{Deref, DerefMut}; use serde::Serialize; +use zerocopy::{FromBytes, IntoBytes}; use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout}; use crate::{CheckedSub, Printable, TypeIndex}; @@ -34,6 +36,16 @@ impl From for usize { Self::from(*value) } } +impl From for u32 { + fn from(value: P2PK65AddressIndex) -> Self { + Self::from(*value) + } +} +impl From for P2PK65AddressIndex { + fn from(value: u32) -> Self { + Self(TypeIndex::from(value)) + } +} impl From for P2PK65AddressIndex { fn from(value: usize) -> Self { Self(TypeIndex::from(value)) @@ -59,3 +71,19 @@ impl Printable for P2PK65AddressIndex { &["pk65addr", "p2pk65addr", "p2pk65addressindex"] } } + +impl From for P2PK65AddressIndex { + fn from(value: ByteView) -> Self { + Self::read_from_bytes(&value).unwrap() + } +} +impl From for ByteView { + fn from(value: P2PK65AddressIndex) -> Self { + Self::from(&value) + } +} +impl From<&P2PK65AddressIndex> for ByteView { + fn from(value: &P2PK65AddressIndex) -> Self { + Self::new(value.as_bytes()) + } +} diff --git a/crates/brk_core/src/structs/p2pk65addressindex_outputindex.rs b/crates/brk_core/src/structs/p2pk65addressindex_outputindex.rs new file mode 100644 index 000000000..6e17d6bdc --- /dev/null +++ b/crates/brk_core/src/structs/p2pk65addressindex_outputindex.rs @@ -0,0 +1,40 @@ +use byteview::ByteView; +use serde::Serialize; + +use crate::{copy_first_4bytes, copy_first_8bytes}; + +use super::{OutputIndex, P2PK65AddressIndex}; + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Serialize)] +pub struct P2PK65AddressIndexOutputindex { + addressindex: P2PK65AddressIndex, + outputindex: OutputIndex, +} + +impl From for P2PK65AddressIndexOutputindex { + fn from(value: ByteView) -> Self { + let addressindex = + P2PK65AddressIndex::from(u32::from_be_bytes(copy_first_4bytes(&value).unwrap())); + let outputindex = OutputIndex::from(u64::from_be_bytes(copy_first_8bytes(&value).unwrap())); + Self { + addressindex, + outputindex, + } + } +} +impl From for ByteView { + fn from(value: P2PK65AddressIndexOutputindex) -> Self { + ByteView::from(&value) + } +} +impl From<&P2PK65AddressIndexOutputindex> for ByteView { + fn from(value: &P2PK65AddressIndexOutputindex) -> Self { + ByteView::from( + [ + u32::from(value.addressindex).to_be_bytes().as_slice(), + u64::from(value.outputindex).to_be_bytes().as_slice(), + ] + .concat(), + ) + } +} diff --git a/crates/brk_core/src/structs/p2pkhaddressindex.rs b/crates/brk_core/src/structs/p2pkhaddressindex.rs index ca6e8c729..f275cc2ea 100644 --- a/crates/brk_core/src/structs/p2pkhaddressindex.rs +++ b/crates/brk_core/src/structs/p2pkhaddressindex.rs @@ -1,7 +1,9 @@ use std::ops::Add; +use byteview::ByteView; use derive_deref::{Deref, DerefMut}; use serde::Serialize; +use zerocopy::{FromBytes, IntoBytes}; use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout}; use crate::{CheckedSub, Printable, TypeIndex}; @@ -34,6 +36,16 @@ impl From for usize { Self::from(*value) } } +impl From for u32 { + fn from(value: P2PKHAddressIndex) -> Self { + Self::from(*value) + } +} +impl From for P2PKHAddressIndex { + fn from(value: u32) -> Self { + Self(TypeIndex::from(value)) + } +} impl From for P2PKHAddressIndex { fn from(value: usize) -> Self { Self(TypeIndex::from(value)) @@ -60,3 +72,19 @@ impl Printable for P2PKHAddressIndex { &["pkhaddr", "p2pkhaddr", "p2pkhaddressindex"] } } + +impl From for P2PKHAddressIndex { + fn from(value: ByteView) -> Self { + Self::read_from_bytes(&value).unwrap() + } +} +impl From for ByteView { + fn from(value: P2PKHAddressIndex) -> Self { + Self::from(&value) + } +} +impl From<&P2PKHAddressIndex> for ByteView { + fn from(value: &P2PKHAddressIndex) -> Self { + Self::new(value.as_bytes()) + } +} diff --git a/crates/brk_core/src/structs/p2pkhaddressindex_outputindex.rs b/crates/brk_core/src/structs/p2pkhaddressindex_outputindex.rs new file mode 100644 index 000000000..d2a23313f --- /dev/null +++ b/crates/brk_core/src/structs/p2pkhaddressindex_outputindex.rs @@ -0,0 +1,40 @@ +use byteview::ByteView; +use serde::Serialize; + +use crate::{copy_first_4bytes, copy_first_8bytes}; + +use super::{OutputIndex, P2PKHAddressIndex}; + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Serialize)] +pub struct P2PKHAddressIndexOutputindex { + addressindex: P2PKHAddressIndex, + outputindex: OutputIndex, +} + +impl From for P2PKHAddressIndexOutputindex { + fn from(value: ByteView) -> Self { + let addressindex = + P2PKHAddressIndex::from(u32::from_be_bytes(copy_first_4bytes(&value).unwrap())); + let outputindex = OutputIndex::from(u64::from_be_bytes(copy_first_8bytes(&value).unwrap())); + Self { + addressindex, + outputindex, + } + } +} +impl From for ByteView { + fn from(value: P2PKHAddressIndexOutputindex) -> Self { + ByteView::from(&value) + } +} +impl From<&P2PKHAddressIndexOutputindex> for ByteView { + fn from(value: &P2PKHAddressIndexOutputindex) -> Self { + ByteView::from( + [ + u32::from(value.addressindex).to_be_bytes().as_slice(), + u64::from(value.outputindex).to_be_bytes().as_slice(), + ] + .concat(), + ) + } +} diff --git a/crates/brk_core/src/structs/p2shaddressindex.rs b/crates/brk_core/src/structs/p2shaddressindex.rs index 90dff9c7a..cfe2ce217 100644 --- a/crates/brk_core/src/structs/p2shaddressindex.rs +++ b/crates/brk_core/src/structs/p2shaddressindex.rs @@ -1,7 +1,9 @@ use std::ops::Add; +use byteview::ByteView; use derive_deref::{Deref, DerefMut}; use serde::Serialize; +use zerocopy::{FromBytes, IntoBytes}; use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout}; use crate::{CheckedSub, Printable, TypeIndex}; @@ -29,6 +31,16 @@ impl From for P2SHAddressIndex { Self(value) } } +impl From for u32 { + fn from(value: P2SHAddressIndex) -> Self { + Self::from(*value) + } +} +impl From for P2SHAddressIndex { + fn from(value: u32) -> Self { + Self(TypeIndex::from(value)) + } +} impl From for usize { fn from(value: P2SHAddressIndex) -> Self { Self::from(*value) @@ -60,3 +72,19 @@ impl Printable for P2SHAddressIndex { &["shaddr", "p2shaddr", "p2shaddressindex"] } } + +impl From for P2SHAddressIndex { + fn from(value: ByteView) -> Self { + Self::read_from_bytes(&value).unwrap() + } +} +impl From for ByteView { + fn from(value: P2SHAddressIndex) -> Self { + Self::from(&value) + } +} +impl From<&P2SHAddressIndex> for ByteView { + fn from(value: &P2SHAddressIndex) -> Self { + Self::new(value.as_bytes()) + } +} diff --git a/crates/brk_core/src/structs/p2shaddressindex_outputindex.rs b/crates/brk_core/src/structs/p2shaddressindex_outputindex.rs new file mode 100644 index 000000000..5a0898729 --- /dev/null +++ b/crates/brk_core/src/structs/p2shaddressindex_outputindex.rs @@ -0,0 +1,40 @@ +use byteview::ByteView; +use serde::Serialize; + +use crate::{copy_first_4bytes, copy_first_8bytes}; + +use super::{OutputIndex, P2SHAddressIndex}; + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Serialize)] +pub struct P2SHAddressIndexOutputindex { + addressindex: P2SHAddressIndex, + outputindex: OutputIndex, +} + +impl From for P2SHAddressIndexOutputindex { + fn from(value: ByteView) -> Self { + let addressindex = + P2SHAddressIndex::from(u32::from_be_bytes(copy_first_4bytes(&value).unwrap())); + let outputindex = OutputIndex::from(u64::from_be_bytes(copy_first_8bytes(&value).unwrap())); + Self { + addressindex, + outputindex, + } + } +} +impl From for ByteView { + fn from(value: P2SHAddressIndexOutputindex) -> Self { + ByteView::from(&value) + } +} +impl From<&P2SHAddressIndexOutputindex> for ByteView { + fn from(value: &P2SHAddressIndexOutputindex) -> Self { + ByteView::from( + [ + u32::from(value.addressindex).to_be_bytes().as_slice(), + u64::from(value.outputindex).to_be_bytes().as_slice(), + ] + .concat(), + ) + } +} diff --git a/crates/brk_core/src/structs/p2traddressindex.rs b/crates/brk_core/src/structs/p2traddressindex.rs index ff6633516..568c4c7c1 100644 --- a/crates/brk_core/src/structs/p2traddressindex.rs +++ b/crates/brk_core/src/structs/p2traddressindex.rs @@ -1,7 +1,9 @@ use std::ops::Add; +use byteview::ByteView; use derive_deref::{Deref, DerefMut}; use serde::Serialize; +use zerocopy::{FromBytes, IntoBytes}; use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout}; use crate::{CheckedSub, Printable, TypeIndex}; @@ -29,6 +31,16 @@ impl From for P2TRAddressIndex { Self(value) } } +impl From for u32 { + fn from(value: P2TRAddressIndex) -> Self { + Self::from(*value) + } +} +impl From for P2TRAddressIndex { + fn from(value: u32) -> Self { + Self(TypeIndex::from(value)) + } +} impl From for usize { fn from(value: P2TRAddressIndex) -> Self { Self::from(*value) @@ -60,3 +72,19 @@ impl Printable for P2TRAddressIndex { &["traddr", "p2traddr", "p2traddressindex"] } } + +impl From for P2TRAddressIndex { + fn from(value: ByteView) -> Self { + Self::read_from_bytes(&value).unwrap() + } +} +impl From for ByteView { + fn from(value: P2TRAddressIndex) -> Self { + Self::from(&value) + } +} +impl From<&P2TRAddressIndex> for ByteView { + fn from(value: &P2TRAddressIndex) -> Self { + Self::new(value.as_bytes()) + } +} diff --git a/crates/brk_core/src/structs/p2traddressindex_outputindex.rs b/crates/brk_core/src/structs/p2traddressindex_outputindex.rs new file mode 100644 index 000000000..ae9d4cfd8 --- /dev/null +++ b/crates/brk_core/src/structs/p2traddressindex_outputindex.rs @@ -0,0 +1,40 @@ +use byteview::ByteView; +use serde::Serialize; + +use crate::{copy_first_4bytes, copy_first_8bytes}; + +use super::{OutputIndex, P2TRAddressIndex}; + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Serialize)] +pub struct P2TRAddressIndexOutputindex { + addressindex: P2TRAddressIndex, + outputindex: OutputIndex, +} + +impl From for P2TRAddressIndexOutputindex { + fn from(value: ByteView) -> Self { + let addressindex = + P2TRAddressIndex::from(u32::from_be_bytes(copy_first_4bytes(&value).unwrap())); + let outputindex = OutputIndex::from(u64::from_be_bytes(copy_first_8bytes(&value).unwrap())); + Self { + addressindex, + outputindex, + } + } +} +impl From for ByteView { + fn from(value: P2TRAddressIndexOutputindex) -> Self { + ByteView::from(&value) + } +} +impl From<&P2TRAddressIndexOutputindex> for ByteView { + fn from(value: &P2TRAddressIndexOutputindex) -> Self { + ByteView::from( + [ + u32::from(value.addressindex).to_be_bytes().as_slice(), + u64::from(value.outputindex).to_be_bytes().as_slice(), + ] + .concat(), + ) + } +} diff --git a/crates/brk_core/src/structs/p2wpkhaddressindex.rs b/crates/brk_core/src/structs/p2wpkhaddressindex.rs index fb6528b80..acaafed72 100644 --- a/crates/brk_core/src/structs/p2wpkhaddressindex.rs +++ b/crates/brk_core/src/structs/p2wpkhaddressindex.rs @@ -1,7 +1,9 @@ use std::ops::Add; +use byteview::ByteView; use derive_deref::{Deref, DerefMut}; use serde::Serialize; +use zerocopy::{FromBytes, IntoBytes}; use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout}; use crate::{CheckedSub, Printable, TypeIndex}; @@ -34,6 +36,16 @@ impl From for usize { Self::from(*value) } } +impl From for u32 { + fn from(value: P2WPKHAddressIndex) -> Self { + Self::from(*value) + } +} +impl From for P2WPKHAddressIndex { + fn from(value: u32) -> Self { + Self(TypeIndex::from(value)) + } +} impl From for P2WPKHAddressIndex { fn from(value: usize) -> Self { Self(TypeIndex::from(value)) @@ -60,3 +72,19 @@ impl Printable for P2WPKHAddressIndex { &["wpkhaddr", "p2wpkhaddr", "p2wpkhaddressindex"] } } + +impl From for P2WPKHAddressIndex { + fn from(value: ByteView) -> Self { + Self::read_from_bytes(&value).unwrap() + } +} +impl From for ByteView { + fn from(value: P2WPKHAddressIndex) -> Self { + Self::from(&value) + } +} +impl From<&P2WPKHAddressIndex> for ByteView { + fn from(value: &P2WPKHAddressIndex) -> Self { + Self::new(value.as_bytes()) + } +} diff --git a/crates/brk_core/src/structs/p2wpkhaddressindex_outputindex.rs b/crates/brk_core/src/structs/p2wpkhaddressindex_outputindex.rs new file mode 100644 index 000000000..abd91bc35 --- /dev/null +++ b/crates/brk_core/src/structs/p2wpkhaddressindex_outputindex.rs @@ -0,0 +1,40 @@ +use byteview::ByteView; +use serde::Serialize; + +use crate::{copy_first_4bytes, copy_first_8bytes}; + +use super::{OutputIndex, P2WPKHAddressIndex}; + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Serialize)] +pub struct P2WPKHAddressIndexOutputindex { + addressindex: P2WPKHAddressIndex, + outputindex: OutputIndex, +} + +impl From for P2WPKHAddressIndexOutputindex { + fn from(value: ByteView) -> Self { + let addressindex = + P2WPKHAddressIndex::from(u32::from_be_bytes(copy_first_4bytes(&value).unwrap())); + let outputindex = OutputIndex::from(u64::from_be_bytes(copy_first_8bytes(&value).unwrap())); + Self { + addressindex, + outputindex, + } + } +} +impl From for ByteView { + fn from(value: P2WPKHAddressIndexOutputindex) -> Self { + ByteView::from(&value) + } +} +impl From<&P2WPKHAddressIndexOutputindex> for ByteView { + fn from(value: &P2WPKHAddressIndexOutputindex) -> Self { + ByteView::from( + [ + u32::from(value.addressindex).to_be_bytes().as_slice(), + u64::from(value.outputindex).to_be_bytes().as_slice(), + ] + .concat(), + ) + } +} diff --git a/crates/brk_core/src/structs/p2wshaddressindex.rs b/crates/brk_core/src/structs/p2wshaddressindex.rs index 6a61485cb..389ad902f 100644 --- a/crates/brk_core/src/structs/p2wshaddressindex.rs +++ b/crates/brk_core/src/structs/p2wshaddressindex.rs @@ -1,7 +1,9 @@ use std::ops::Add; +use byteview::ByteView; use derive_deref::{Deref, DerefMut}; use serde::Serialize; +use zerocopy::{FromBytes, IntoBytes}; use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout}; use crate::{CheckedSub, Printable, TypeIndex}; @@ -29,6 +31,16 @@ impl From for P2WSHAddressIndex { Self(value) } } +impl From for u32 { + fn from(value: P2WSHAddressIndex) -> Self { + Self::from(*value) + } +} +impl From for P2WSHAddressIndex { + fn from(value: u32) -> Self { + Self(TypeIndex::from(value)) + } +} impl From for usize { fn from(value: P2WSHAddressIndex) -> Self { Self::from(*value) @@ -60,3 +72,19 @@ impl Printable for P2WSHAddressIndex { &["wshaddr", "p2wshaddr", "p2wshaddressindex"] } } + +impl From for P2WSHAddressIndex { + fn from(value: ByteView) -> Self { + Self::read_from_bytes(&value).unwrap() + } +} +impl From for ByteView { + fn from(value: P2WSHAddressIndex) -> Self { + Self::from(&value) + } +} +impl From<&P2WSHAddressIndex> for ByteView { + fn from(value: &P2WSHAddressIndex) -> Self { + Self::new(value.as_bytes()) + } +} diff --git a/crates/brk_core/src/structs/p2wshaddressindex_outputindex.rs b/crates/brk_core/src/structs/p2wshaddressindex_outputindex.rs new file mode 100644 index 000000000..2ef4742f3 --- /dev/null +++ b/crates/brk_core/src/structs/p2wshaddressindex_outputindex.rs @@ -0,0 +1,40 @@ +use byteview::ByteView; +use serde::Serialize; + +use crate::{copy_first_4bytes, copy_first_8bytes}; + +use super::{OutputIndex, P2WSHAddressIndex}; + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Serialize)] +pub struct P2WSHAddressIndexOutputindex { + addressindex: P2WSHAddressIndex, + outputindex: OutputIndex, +} + +impl From for P2WSHAddressIndexOutputindex { + fn from(value: ByteView) -> Self { + let addressindex = + P2WSHAddressIndex::from(u32::from_be_bytes(copy_first_4bytes(&value).unwrap())); + let outputindex = OutputIndex::from(u64::from_be_bytes(copy_first_8bytes(&value).unwrap())); + Self { + addressindex, + outputindex, + } + } +} +impl From for ByteView { + fn from(value: P2WSHAddressIndexOutputindex) -> Self { + ByteView::from(&value) + } +} +impl From<&P2WSHAddressIndexOutputindex> for ByteView { + fn from(value: &P2WSHAddressIndexOutputindex) -> Self { + ByteView::from( + [ + u32::from(value.addressindex).to_be_bytes().as_slice(), + u64::from(value.outputindex).to_be_bytes().as_slice(), + ] + .concat(), + ) + } +} diff --git a/crates/brk_core/src/structs/typeindex.rs b/crates/brk_core/src/structs/typeindex.rs index 995e91d0a..8ea03066a 100644 --- a/crates/brk_core/src/structs/typeindex.rs +++ b/crates/brk_core/src/structs/typeindex.rs @@ -45,6 +45,11 @@ impl From for TypeIndex { Self(value) } } +impl From for u32 { + fn from(value: TypeIndex) -> Self { + value.0 + } +} impl From for TypeIndex { fn from(value: u64) -> Self { diff --git a/crates/brk_core/src/utils/bytes.rs b/crates/brk_core/src/utils/bytes.rs index e1146adcc..52ea86924 100644 --- a/crates/brk_core/src/utils/bytes.rs +++ b/crates/brk_core/src/utils/bytes.rs @@ -1,5 +1,18 @@ use crate::{Error, Result}; +#[allow(clippy::result_unit_err)] +pub fn copy_first_4bytes(slice: &[u8]) -> Result<[u8; 4]> { + let mut buf: [u8; 4] = [0; 4]; + let buf_len = buf.len(); + if slice.len() < buf_len { + return Err(Error::String("Buffer is too small to convert to 8 bytes")); + } + slice.iter().take(buf_len).enumerate().for_each(|(i, r)| { + buf[i] = *r; + }); + Ok(buf) +} + #[allow(clippy::result_unit_err)] pub fn copy_first_8bytes(slice: &[u8]) -> Result<[u8; 8]> { let mut buf: [u8; 8] = [0; 8]; diff --git a/crates/brk_mcp/README.md b/crates/brk_mcp/README.md index 78344bd6f..e97a76617 100644 --- a/crates/brk_mcp/README.md +++ b/crates/brk_mcp/README.md @@ -9,7 +9,7 @@ A Model Context Protocol (MCP) which gives LLMs access to all available tools in ## Usage -To connect to the MCP you only need the correct URL, no token or auth is needed. +To connect to the MCP use any of the previous URL, no token or auth is needed. This implementation has only been tested with Claude and the [MCP inspector](https://modelcontextprotocol.io/docs/tools/inspector).