Skip to content

Commit 9020ae7

Browse files
committed
Require space hashes directly for some rpc calls
1 parent 80acd5f commit 9020ae7

File tree

2 files changed

+55
-28
lines changed

2 files changed

+55
-28
lines changed

node/src/bin/space-cli.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extern crate core;
22

3-
use std::{fs, path::PathBuf};
3+
use std::{fs, path::PathBuf, str::FromStr};
44

55
use base64::{prelude::BASE64_STANDARD, Engine};
66
use clap::{Parser, Subcommand};
@@ -10,7 +10,9 @@ use jsonrpsee::{
1010
};
1111
use protocol::{
1212
bitcoin::{Amount, FeeRate, OutPoint, Txid},
13+
hasher::{KeyHasher, SpaceHash},
1314
opcodes::OP_SETALL,
15+
sname::{NameLike, SName},
1416
Covenant, FullSpaceOut,
1517
};
1618
use serde::{Deserialize, Serialize};
@@ -20,6 +22,7 @@ use spaced::{
2022
BidParams, ExecuteParams, OpenParams, RegisterParams, RpcClient, RpcWalletRequest,
2123
RpcWalletTxBuilder, SendCoinsParams, TransferSpacesParams,
2224
},
25+
store::Sha256,
2326
wallets::AddressKind,
2427
};
2528

@@ -222,6 +225,12 @@ enum Commands {
222225
/// compatible with most bitcoin wallets
223226
#[command(name = "getnewaddress")]
224227
GetCoinAddress,
228+
/// Calculate a spacehash from the specified space name
229+
#[command(name = "spacehash")]
230+
SpaceHash {
231+
/// The space name
232+
space: String,
233+
},
225234
}
226235

227236
struct SpaceCli {
@@ -360,6 +369,13 @@ async fn main() -> anyhow::Result<()> {
360369
Ok(())
361370
}
362371

372+
fn space_hash(spaceish: &str) -> anyhow::Result<String> {
373+
let space = normalize_space(&spaceish);
374+
let sname = SName::from_str(&space)?;
375+
let spacehash = SpaceHash::from(Sha256::hash(sname.to_bytes()));
376+
Ok(hex::encode(spacehash.as_slice()))
377+
}
378+
363379
async fn handle_commands(
364380
cli: &SpaceCli,
365381
command: Commands,
@@ -404,8 +420,8 @@ async fn handle_commands(
404420
println!("{} sat", Amount::from_sat(response).to_string());
405421
}
406422
Commands::GetSpace { space } => {
407-
let space = normalize_space(&space);
408-
let response = cli.client.get_space(&space).await?;
423+
let space_hash = space_hash(&space).map_err(|e| ClientError::Custom(e.to_string()))?;
424+
let response = cli.client.get_space(&space_hash).await?;
409425
println!("{}", serde_json::to_string_pretty(&response)?);
410426
}
411427
Commands::GetSpaceOut { outpoint } => {
@@ -580,6 +596,12 @@ async fn handle_commands(
580596
.await?;
581597
println!("{}", serde_json::to_string_pretty(&response)?);
582598
}
599+
Commands::SpaceHash { space } => {
600+
println!(
601+
"{}",
602+
space_hash(&space).map_err(|e| ClientError::Custom(e.to_string()))?
603+
);
604+
}
583605
}
584606

585607
Ok(())

node/src/rpc.rs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ use protocol::{
2323
OutPoint,
2424
},
2525
constants::ChainAnchor,
26-
hasher::{BaseHash, KeyHasher, SpaceHash},
26+
hasher::{BaseHash, SpaceHash},
2727
prepare::DataSource,
28-
sname::{NameLike, SName},
2928
FullSpaceOut, SpaceOut,
3029
};
3130
use serde::{Deserialize, Serialize};
@@ -43,7 +42,7 @@ use crate::{
4342
config::ExtendedNetwork,
4443
node::ValidatedBlock,
4544
source::BitcoinRpc,
46-
store::{ChainState, LiveSnapshot, Sha256},
45+
store::{ChainState, LiveSnapshot},
4746
wallets::{AddressKind, JointBalance, RpcWallet, TxResponse, WalletCommand, WalletResponse},
4847
};
4948

@@ -97,10 +96,11 @@ pub trait Rpc {
9796
async fn get_server_info(&self) -> Result<ServerInfo, ErrorObjectOwned>;
9897

9998
#[method(name = "getspace")]
100-
async fn get_space(&self, space: &str) -> Result<Option<FullSpaceOut>, ErrorObjectOwned>;
99+
async fn get_space(&self, space_hash: &str) -> Result<Option<FullSpaceOut>, ErrorObjectOwned>;
101100

102101
#[method(name = "getspaceowner")]
103-
async fn get_space_owner(&self, space: &str) -> Result<Option<OutPoint>, ErrorObjectOwned>;
102+
async fn get_space_owner(&self, space_hash: &str)
103+
-> Result<Option<OutPoint>, ErrorObjectOwned>;
104104

105105
#[method(name = "getspaceout")]
106106
async fn get_spaceout(&self, outpoint: OutPoint) -> Result<Option<SpaceOut>, ErrorObjectOwned>;
@@ -603,16 +603,8 @@ impl RpcServer for RpcServerImpl {
603603
Ok(ServerInfo { chain, tip })
604604
}
605605

606-
async fn get_space(&self, space: &str) -> Result<Option<FullSpaceOut>, ErrorObjectOwned> {
607-
let space = SName::from_str(&space).map_err(|_| {
608-
ErrorObjectOwned::owned(
609-
-1,
610-
"must be a valid canonical space name (e.g. @bitcoin)",
611-
None::<String>,
612-
)
613-
})?;
614-
615-
let space_hash = SpaceHash::from(Sha256::hash(space.to_bytes()));
606+
async fn get_space(&self, space_hash: &str) -> Result<Option<FullSpaceOut>, ErrorObjectOwned> {
607+
let space_hash = space_hash_from_string(space_hash)?;
616608
let info = self
617609
.store
618610
.get_space(space_hash)
@@ -621,16 +613,11 @@ impl RpcServer for RpcServerImpl {
621613
Ok(info)
622614
}
623615

624-
async fn get_space_owner(&self, space: &str) -> Result<Option<OutPoint>, ErrorObjectOwned> {
625-
let space = SName::from_str(space).map_err(|_| {
626-
ErrorObjectOwned::owned(
627-
-1,
628-
"must be a valid canonical space name (e.g. @bitcoin)",
629-
None::<String>,
630-
)
631-
})?;
632-
let space_hash = SpaceHash::from(Sha256::hash(space.to_bytes()));
633-
616+
async fn get_space_owner(
617+
&self,
618+
space_hash: &str,
619+
) -> Result<Option<OutPoint>, ErrorObjectOwned> {
620+
let space_hash = space_hash_from_string(space_hash)?;
634621
let info = self
635622
.store
636623
.get_space_outpoint(space_hash)
@@ -936,3 +923,21 @@ impl AsyncChainState {
936923
resp_rx.await?
937924
}
938925
}
926+
927+
fn space_hash_from_string(space_hash: &str) -> Result<SpaceHash, ErrorObjectOwned> {
928+
let mut hash = [0u8; 32];
929+
hex::decode_to_slice(space_hash, &mut hash).map_err(|_| {
930+
ErrorObjectOwned::owned(
931+
-1,
932+
"expected a 32-byte hex encoded space hash",
933+
None::<String>,
934+
)
935+
})?;
936+
SpaceHash::from_raw(hash).map_err(|_| {
937+
ErrorObjectOwned::owned(
938+
-1,
939+
"expected a 32-byte hex encoded space hash",
940+
None::<String>,
941+
)
942+
})
943+
}

0 commit comments

Comments
 (0)