Skip to content

Expose some methods and data for the wallet #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions client/src/wallets.rs
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ use spaces_protocol::{
use spaces_wallet::{
address::SpaceAddress,
bdk_wallet::{
chain::{local_chain::CheckPoint, BlockId},
chain::{local_chain::CheckPoint, BlockId, ChainPosition},
KeychainKind,
},
bitcoin,
@@ -69,8 +69,9 @@ pub struct ListSpacesResponse {
#[derive(Tabled, Debug, Clone, Serialize, Deserialize)]
#[tabled(rename_all = "UPPERCASE")]
pub struct TxInfo {
#[tabled(display_with = "display_block_height")]
pub block_height: Option<u32>,
pub txid: Txid,
pub confirmed: bool,
pub sent: Amount,
pub received: Amount,
#[tabled(display_with = "display_fee")]
@@ -79,6 +80,13 @@ pub struct TxInfo {
pub events: Vec<TxEvent>,
}

fn display_block_height(block_height: &Option<u32>) -> String {
match block_height {
None => "Unconfirmed".to_string(),
Some(block_height) => block_height.to_string(),
}
}

fn display_fee(fee: &Option<Amount>) -> String {
match fee {
None => "--".to_string(),
@@ -743,14 +751,17 @@ impl RpcWallet {
.skip(skip)
.take(count)
.map(|ctx| {
let block_height = match ctx.chain_position {
ChainPosition::Confirmed { anchor, .. } => Some(anchor.block_id.height),
ChainPosition::Unconfirmed { .. } => None,
};
let tx = ctx.tx_node.tx.clone();
let txid = ctx.tx_node.txid.clone();
let confirmed = ctx.chain_position.is_confirmed();
let (sent, received) = wallet.sent_and_received(&tx);
let fee = wallet.calculate_fee(&tx).ok();
TxInfo {
block_height,
txid,
confirmed,
sent,
received,
fee,
4 changes: 2 additions & 2 deletions client/tests/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -689,7 +689,7 @@ async fn it_should_replace_mempool_bids(rig: &TestRig) -> anyhow::Result<()> {
.wallet_list_transactions(ALICE, 1000, 0)
.await
.expect("list transactions");
let unconfirmed: Vec<_> = txs.iter().filter(|tx| !tx.confirmed).collect();
let unconfirmed: Vec<_> = txs.iter().filter(|tx| tx.block_height.is_none()).collect();
for tx in &unconfirmed {
println!("Alice's unconfiremd: {}", tx.txid);
}
@@ -726,7 +726,7 @@ async fn it_should_replace_mempool_bids(rig: &TestRig) -> anyhow::Result<()> {
assert!(
eve_txs
.iter()
.any(|tx| tx.txid == eve_replacement_txid && tx.confirmed),
.any(|tx| tx.txid == eve_replacement_txid && tx.block_height.is_some()),
"Eve's tx should be confirmed"
);

6 changes: 1 addition & 5 deletions protocol/src/script.rs
Original file line number Diff line number Diff line change
@@ -12,7 +12,6 @@ use bitcoin::{
use serde::{Deserialize, Serialize};

use crate::{
constants::RESERVED_SPACES,
hasher::{KeyHasher, SpaceKey},
prepare::DataSource,
slabel::{SLabel, SLabelRef},
@@ -140,10 +139,7 @@ impl SpaceScript {
}
let name = name.unwrap();

if RESERVED_SPACES
.iter()
.any(|reserved| *reserved == name.as_ref())
{
if name.is_reserved() {
return Ok(Err(ScriptError::ReservedName));
}

55 changes: 39 additions & 16 deletions protocol/src/slabel.rs
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ use core::{
#[cfg(feature = "serde")]
use serde::{de::Error as ErrorUtil, Deserialize, Deserializer, Serialize, Serializer};

use crate::errors::Error;
use crate::{constants::RESERVED_SPACES, errors::Error};

pub const MAX_LABEL_LEN: usize = 62;
pub const PUNYCODE_PREFIX: &[u8] = b"xn--";
@@ -188,6 +188,32 @@ impl<'a> TryFrom<&'a [u8]> for SLabelRef<'a> {
}
}

impl SLabel {
pub fn as_str_unprefixed(&self) -> Result<&str, core::str::Utf8Error> {
let label_len = self.0[0] as usize;
let label = &self.0[1..=label_len];
core::str::from_utf8(label)
}

pub fn to_string_unprefixed(&self) -> Result<String, core::str::Utf8Error> {
self.as_str_unprefixed().map(|s| s.to_string())
}

pub fn from_str_unprefixed(label: &str) -> Result<Self, Error> {
if label.is_empty() {
return Err(Error::Name(NameErrorKind::ZeroLength));
}
if label.len() > MAX_LABEL_LEN {
return Err(Error::Name(NameErrorKind::TooLong));
}
let mut label_bytes = [0; MAX_LABEL_LEN + 1];
label_bytes[0] = label.len() as u8;
label_bytes[1..=label.len()].copy_from_slice(label.as_bytes());

SLabel::try_from(label_bytes.as_slice())
}
}

impl TryFrom<String> for SLabel {
type Error = Error;

@@ -204,26 +230,13 @@ impl TryFrom<&str> for SLabel {
return Err(Error::Name(NameErrorKind::NotCanonical));
}
let label = &value[1..];
if label.is_empty() {
return Err(Error::Name(NameErrorKind::ZeroLength));
}
if label.len() > MAX_LABEL_LEN {
return Err(Error::Name(NameErrorKind::TooLong));
}
let mut label_bytes = [0; MAX_LABEL_LEN + 1];
label_bytes[0] = label.len() as u8;
label_bytes[1..=label.len()].copy_from_slice(label.as_bytes());

SLabel::try_from(label_bytes.as_slice())
Self::from_str_unprefixed(label)
}
}

impl Display for SLabel {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
let label_len = self.0[0] as usize;
let label = &self.0[1..=label_len];

let label_str = core::str::from_utf8(label).map_err(|_| core::fmt::Error)?;
let label_str = self.as_str_unprefixed().map_err(|_| core::fmt::Error)?;
write!(f, "@{}", label_str)
}
}
@@ -238,6 +251,10 @@ impl SLabel {
pub fn as_name_ref(&self) -> SLabelRef {
SLabelRef(&self.0)
}

pub fn is_reserved(&self) -> bool {
self.as_name_ref().is_reserved()
}
}

impl SLabelRef<'_> {
@@ -246,6 +263,12 @@ impl SLabelRef<'_> {
owned.0[..self.0.len()].copy_from_slice(self.0);
owned
}

pub fn is_reserved(&self) -> bool {
RESERVED_SPACES
.iter()
.any(|reserved| *reserved == self.as_ref())
}
}

#[cfg(test)]