Skip to content

Commit b2aed62

Browse files
authored
Merge pull request #76 from Ximik/wallet2
Expose some methods and data for the wallet
2 parents b4ce419 + e61cfe1 commit b2aed62

File tree

4 files changed

+57
-27
lines changed

4 files changed

+57
-27
lines changed

client/src/wallets.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use spaces_protocol::{
1717
use spaces_wallet::{
1818
address::SpaceAddress,
1919
bdk_wallet::{
20-
chain::{local_chain::CheckPoint, BlockId},
20+
chain::{local_chain::CheckPoint, BlockId, ChainPosition},
2121
KeychainKind,
2222
},
2323
bitcoin,
@@ -69,8 +69,9 @@ pub struct ListSpacesResponse {
6969
#[derive(Tabled, Debug, Clone, Serialize, Deserialize)]
7070
#[tabled(rename_all = "UPPERCASE")]
7171
pub struct TxInfo {
72+
#[tabled(display_with = "display_block_height")]
73+
pub block_height: Option<u32>,
7274
pub txid: Txid,
73-
pub confirmed: bool,
7475
pub sent: Amount,
7576
pub received: Amount,
7677
#[tabled(display_with = "display_fee")]
@@ -79,6 +80,13 @@ pub struct TxInfo {
7980
pub events: Vec<TxEvent>,
8081
}
8182

83+
fn display_block_height(block_height: &Option<u32>) -> String {
84+
match block_height {
85+
None => "Unconfirmed".to_string(),
86+
Some(block_height) => block_height.to_string(),
87+
}
88+
}
89+
8290
fn display_fee(fee: &Option<Amount>) -> String {
8391
match fee {
8492
None => "--".to_string(),
@@ -743,14 +751,17 @@ impl RpcWallet {
743751
.skip(skip)
744752
.take(count)
745753
.map(|ctx| {
754+
let block_height = match ctx.chain_position {
755+
ChainPosition::Confirmed { anchor, .. } => Some(anchor.block_id.height),
756+
ChainPosition::Unconfirmed { .. } => None,
757+
};
746758
let tx = ctx.tx_node.tx.clone();
747759
let txid = ctx.tx_node.txid.clone();
748-
let confirmed = ctx.chain_position.is_confirmed();
749760
let (sent, received) = wallet.sent_and_received(&tx);
750761
let fee = wallet.calculate_fee(&tx).ok();
751762
TxInfo {
763+
block_height,
752764
txid,
753-
confirmed,
754765
sent,
755766
received,
756767
fee,

client/tests/integration_tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ async fn it_should_replace_mempool_bids(rig: &TestRig) -> anyhow::Result<()> {
689689
.wallet_list_transactions(ALICE, 1000, 0)
690690
.await
691691
.expect("list transactions");
692-
let unconfirmed: Vec<_> = txs.iter().filter(|tx| !tx.confirmed).collect();
692+
let unconfirmed: Vec<_> = txs.iter().filter(|tx| tx.block_height.is_none()).collect();
693693
for tx in &unconfirmed {
694694
println!("Alice's unconfiremd: {}", tx.txid);
695695
}
@@ -726,7 +726,7 @@ async fn it_should_replace_mempool_bids(rig: &TestRig) -> anyhow::Result<()> {
726726
assert!(
727727
eve_txs
728728
.iter()
729-
.any(|tx| tx.txid == eve_replacement_txid && tx.confirmed),
729+
.any(|tx| tx.txid == eve_replacement_txid && tx.block_height.is_some()),
730730
"Eve's tx should be confirmed"
731731
);
732732

protocol/src/script.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use bitcoin::{
1212
use serde::{Deserialize, Serialize};
1313

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

143-
if RESERVED_SPACES
144-
.iter()
145-
.any(|reserved| *reserved == name.as_ref())
146-
{
142+
if name.is_reserved() {
147143
return Ok(Err(ScriptError::ReservedName));
148144
}
149145

protocol/src/slabel.rs

+39-16
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use core::{
99
#[cfg(feature = "serde")]
1010
use serde::{de::Error as ErrorUtil, Deserialize, Deserializer, Serialize, Serializer};
1111

12-
use crate::errors::Error;
12+
use crate::{constants::RESERVED_SPACES, errors::Error};
1313

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

191+
impl SLabel {
192+
pub fn as_str_unprefixed(&self) -> Result<&str, core::str::Utf8Error> {
193+
let label_len = self.0[0] as usize;
194+
let label = &self.0[1..=label_len];
195+
core::str::from_utf8(label)
196+
}
197+
198+
pub fn to_string_unprefixed(&self) -> Result<String, core::str::Utf8Error> {
199+
self.as_str_unprefixed().map(|s| s.to_string())
200+
}
201+
202+
pub fn from_str_unprefixed(label: &str) -> Result<Self, Error> {
203+
if label.is_empty() {
204+
return Err(Error::Name(NameErrorKind::ZeroLength));
205+
}
206+
if label.len() > MAX_LABEL_LEN {
207+
return Err(Error::Name(NameErrorKind::TooLong));
208+
}
209+
let mut label_bytes = [0; MAX_LABEL_LEN + 1];
210+
label_bytes[0] = label.len() as u8;
211+
label_bytes[1..=label.len()].copy_from_slice(label.as_bytes());
212+
213+
SLabel::try_from(label_bytes.as_slice())
214+
}
215+
}
216+
191217
impl TryFrom<String> for SLabel {
192218
type Error = Error;
193219

@@ -204,26 +230,13 @@ impl TryFrom<&str> for SLabel {
204230
return Err(Error::Name(NameErrorKind::NotCanonical));
205231
}
206232
let label = &value[1..];
207-
if label.is_empty() {
208-
return Err(Error::Name(NameErrorKind::ZeroLength));
209-
}
210-
if label.len() > MAX_LABEL_LEN {
211-
return Err(Error::Name(NameErrorKind::TooLong));
212-
}
213-
let mut label_bytes = [0; MAX_LABEL_LEN + 1];
214-
label_bytes[0] = label.len() as u8;
215-
label_bytes[1..=label.len()].copy_from_slice(label.as_bytes());
216-
217-
SLabel::try_from(label_bytes.as_slice())
233+
Self::from_str_unprefixed(label)
218234
}
219235
}
220236

221237
impl Display for SLabel {
222238
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
223-
let label_len = self.0[0] as usize;
224-
let label = &self.0[1..=label_len];
225-
226-
let label_str = core::str::from_utf8(label).map_err(|_| core::fmt::Error)?;
239+
let label_str = self.as_str_unprefixed().map_err(|_| core::fmt::Error)?;
227240
write!(f, "@{}", label_str)
228241
}
229242
}
@@ -238,6 +251,10 @@ impl SLabel {
238251
pub fn as_name_ref(&self) -> SLabelRef {
239252
SLabelRef(&self.0)
240253
}
254+
255+
pub fn is_reserved(&self) -> bool {
256+
self.as_name_ref().is_reserved()
257+
}
241258
}
242259

243260
impl SLabelRef<'_> {
@@ -246,6 +263,12 @@ impl SLabelRef<'_> {
246263
owned.0[..self.0.len()].copy_from_slice(self.0);
247264
owned
248265
}
266+
267+
pub fn is_reserved(&self) -> bool {
268+
RESERVED_SPACES
269+
.iter()
270+
.any(|reserved| *reserved == self.as_ref())
271+
}
249272
}
250273

251274
#[cfg(test)]

0 commit comments

Comments
 (0)