Skip to content

Commit 0f03bb8

Browse files
committed
Accept either space name or its hash. Closes #23
1 parent 9726464 commit 0f03bb8

File tree

2 files changed

+46
-35
lines changed

2 files changed

+46
-35
lines changed

node/src/bin/space-cli.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use jsonrpsee::{
99
};
1010
use protocol::{
1111
bitcoin::{Amount, FeeRate, OutPoint, Txid},
12-
hasher::{KeyHasher, SpaceKey},
12+
hasher::{KeyHasher},
1313
slabel::SLabel,
1414
Covenant, FullSpaceOut,
1515
};
@@ -216,19 +216,18 @@ enum Commands {
216216
/// compatible with most bitcoin wallets
217217
#[command(name = "getnewaddress")]
218218
GetCoinAddress,
219-
/// Calculate a spacehash from the specified space name
220-
#[command(name = "spacehash")]
221-
SpaceHash {
222-
/// The space name
223-
space: String,
224-
},
225219
/// Force spend an output owned by wallet (for testing only)
226220
#[command(name = "forcespend")]
227221
ForceSpend {
228222
outpoint: OutPoint,
229223
#[arg(long, short)]
230224
fee_rate: u64,
231225
},
226+
/// DNS encodes the space and calculates the SHA-256 hash
227+
#[command(name = "hashspace")]
228+
HashSpace {
229+
space: String,
230+
},
232231
}
233232

234233
struct SpaceCli {
@@ -367,11 +366,10 @@ async fn main() -> anyhow::Result<()> {
367366
Ok(())
368367
}
369368

370-
fn space_hash(spaceish: &str) -> anyhow::Result<String> {
369+
fn hash_space(spaceish: &str) -> anyhow::Result<String> {
371370
let space = normalize_space(&spaceish);
372371
let sname = SLabel::from_str(&space)?;
373-
let spacehash = SpaceKey::from(Sha256::hash(sname.as_ref()));
374-
Ok(hex::encode(spacehash.as_slice()))
372+
Ok(hex::encode(Sha256::hash(sname.as_ref())))
375373
}
376374

377375
async fn handle_commands(
@@ -424,7 +422,7 @@ async fn handle_commands(
424422
println!("{} sat", Amount::from_sat(response).to_string());
425423
}
426424
Commands::GetSpace { space } => {
427-
let space_hash = space_hash(&space).map_err(|e| ClientError::Custom(e.to_string()))?;
425+
let space_hash = hash_space(&space).map_err(|e| ClientError::Custom(e.to_string()))?;
428426
let response = cli.client.get_space(&space_hash).await?;
429427
println!("{}", serde_json::to_string_pretty(&response)?);
430428
}
@@ -603,12 +601,6 @@ async fn handle_commands(
603601
.await?;
604602
println!("{}", serde_json::to_string_pretty(&response)?);
605603
}
606-
Commands::SpaceHash { space } => {
607-
println!(
608-
"{}",
609-
space_hash(&space).map_err(|e| ClientError::Custom(e.to_string()))?
610-
);
611-
}
612604
Commands::ForceSpend { outpoint, fee_rate } => {
613605
let result = cli
614606
.client
@@ -620,6 +612,12 @@ async fn handle_commands(
620612
.await?;
621613
println!("{}", serde_json::to_string_pretty(&result).expect("result"));
622614
}
615+
Commands::HashSpace { space } => {
616+
println!(
617+
"{}",
618+
hash_space(&space).map_err(|e| ClientError::Custom(e.to_string()))?
619+
);
620+
}
623621
}
624622

625623
Ok(())

node/src/rpc.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ use tokio::{
3232
sync::{broadcast, mpsc, oneshot, RwLock},
3333
task::JoinSet,
3434
};
35+
use protocol::hasher::KeyHasher;
36+
use protocol::slabel::SLabel;
3537
use wallet::{
3638
bdk_wallet as bdk, bdk_wallet::template::Bip86, bitcoin::hashes::Hash, export::WalletExport,
3739
DoubleUtxo, SpacesWallet, WalletConfig, WalletDescriptors, WalletInfo,
@@ -46,6 +48,7 @@ use crate::{
4648
AddressKind, Balance, RpcWallet, TxResponse, WalletCommand, WalletOutput, WalletResponse,
4749
},
4850
};
51+
use crate::store::Sha256;
4952

5053
pub(crate) type Responder<T> = oneshot::Sender<T>;
5154

@@ -101,11 +104,11 @@ pub trait Rpc {
101104
async fn get_server_info(&self) -> Result<ServerInfo, ErrorObjectOwned>;
102105

103106
#[method(name = "getspace")]
104-
async fn get_space(&self, space_hash: &str) -> Result<Option<FullSpaceOut>, ErrorObjectOwned>;
107+
async fn get_space(&self, space_or_hash: &str) -> Result<Option<FullSpaceOut>, ErrorObjectOwned>;
105108

106109
#[method(name = "getspaceowner")]
107-
async fn get_space_owner(&self, space_hash: &str)
108-
-> Result<Option<OutPoint>, ErrorObjectOwned>;
110+
async fn get_space_owner(&self, space_or_hash: &str)
111+
-> Result<Option<OutPoint>, ErrorObjectOwned>;
109112

110113
#[method(name = "getspaceout")]
111114
async fn get_spaceout(&self, outpoint: OutPoint) -> Result<Option<SpaceOut>, ErrorObjectOwned>;
@@ -172,7 +175,7 @@ pub trait Rpc {
172175

173176
#[method(name = "walletlistspaces")]
174177
async fn wallet_list_spaces(&self, wallet: &str)
175-
-> Result<Vec<WalletOutput>, ErrorObjectOwned>;
178+
-> Result<Vec<WalletOutput>, ErrorObjectOwned>;
176179

177180
#[method(name = "walletlistunspent")]
178181
async fn wallet_list_unspent(
@@ -567,8 +570,9 @@ impl RpcServer for RpcServerImpl {
567570
Ok(ServerInfo { chain, tip })
568571
}
569572

570-
async fn get_space(&self, space_hash: &str) -> Result<Option<FullSpaceOut>, ErrorObjectOwned> {
571-
let space_hash = space_hash_from_string(space_hash)?;
573+
async fn get_space(&self, space_or_hash: &str) -> Result<Option<FullSpaceOut>, ErrorObjectOwned> {
574+
let space_hash = get_space_key(space_or_hash)?;
575+
572576
let info = self
573577
.store
574578
.get_space(space_hash)
@@ -579,9 +583,9 @@ impl RpcServer for RpcServerImpl {
579583

580584
async fn get_space_owner(
581585
&self,
582-
space_hash: &str,
586+
space_or_hash: &str,
583587
) -> Result<Option<OutPoint>, ErrorObjectOwned> {
584-
let space_hash = space_hash_from_string(space_hash)?;
588+
let space_hash = get_space_key(space_or_hash)?;
585589
let info = self
586590
.store
587591
.get_space_outpoint(space_hash)
@@ -983,20 +987,29 @@ impl AsyncChainState {
983987
}
984988
}
985989

986-
fn space_hash_from_string(space_hash: &str) -> Result<SpaceKey, ErrorObjectOwned> {
990+
fn get_space_key(space_or_hash: &str) -> Result<SpaceKey, ErrorObjectOwned> {
991+
if space_or_hash.len() != 64 {
992+
return Ok(
993+
SpaceKey::from(
994+
Sha256::hash(SLabel::try_from(space_or_hash).map_err(|_| {
995+
ErrorObjectOwned::owned(
996+
-1,
997+
"expected a space name prefixed with @ or a hex encoded space hash",
998+
None::<String>,
999+
)
1000+
})?.as_ref())
1001+
)
1002+
);
1003+
}
1004+
9871005
let mut hash = [0u8; 32];
988-
hex::decode_to_slice(space_hash, &mut hash).map_err(|_| {
1006+
hex::decode_to_slice(space_or_hash, &mut hash).map_err(|_| {
9891007
ErrorObjectOwned::owned(
9901008
-1,
991-
"expected a 32-byte hex encoded space hash",
1009+
"expected a space name prefixed with @ or a hex encoded space hash",
9921010
None::<String>,
9931011
)
9941012
})?;
995-
SpaceKey::from_raw(hash).map_err(|_| {
996-
ErrorObjectOwned::owned(
997-
-1,
998-
"expected a 32-byte hex encoded space hash",
999-
None::<String>,
1000-
)
1001-
})
1013+
1014+
Ok(SpaceKey::from(hash))
10021015
}

0 commit comments

Comments
 (0)