Skip to content

Commit 74702ca

Browse files
authored
Merge pull request #16 from buffrr/sync-updates-tests
Sync updates & tests
2 parents cdcdd2a + ad52849 commit 74702ca

File tree

11 files changed

+227
-133
lines changed

11 files changed

+227
-133
lines changed

node/src/bin/space-cli.rs

Lines changed: 39 additions & 20 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 {
@@ -264,7 +273,7 @@ impl SpaceCli {
264273
let result = self
265274
.client
266275
.wallet_send_request(
267-
self.wallet.clone(),
276+
&self.wallet,
268277
RpcWalletTxBuilder {
269278
auction_outputs,
270279
requests: match req {
@@ -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,
@@ -373,7 +389,7 @@ async fn handle_commands(
373389
for (priority, spacehash) in hashes {
374390
let outpoint = cli
375391
.client
376-
.get_space_owner(hex::encode(spacehash.as_slice()))
392+
.get_space_owner(&hex::encode(spacehash.as_slice()))
377393
.await?;
378394

379395
if let Some(outpoint) = outpoint {
@@ -404,31 +420,31 @@ 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 } => {
412428
let response = cli.client.get_spaceout(outpoint).await?;
413429
println!("{}", serde_json::to_string_pretty(&response)?);
414430
}
415431
Commands::CreateWallet { name } => {
416-
cli.client.wallet_create(name).await?;
432+
cli.client.wallet_create(&name).await?;
417433
}
418434
Commands::LoadWallet { name } => {
419-
cli.client.wallet_load(name).await?;
435+
cli.client.wallet_load(&name).await?;
420436
}
421437
Commands::ImportWallet { path } => {
422438
let content =
423439
fs::read_to_string(path).map_err(|e| ClientError::Custom(e.to_string()))?;
424-
cli.client.wallet_import(content).await?;
440+
cli.client.wallet_import(&content).await?;
425441
}
426442
Commands::ExportWallet { name } => {
427-
let result = cli.client.wallet_export(name).await?;
443+
let result = cli.client.wallet_export(&name).await?;
428444
println!("{}", result);
429445
}
430446
Commands::GetWalletInfo { name } => {
431-
let result = cli.client.wallet_get_info(name).await?;
447+
let result = cli.client.wallet_get_info(&name).await?;
432448
println!("{}", serde_json::to_string_pretty(&result).expect("result"));
433449
}
434450
Commands::GetServerInfo => {
@@ -543,46 +559,49 @@ async fn handle_commands(
543559
.await?;
544560
}
545561
Commands::ListUnspent => {
546-
let spaces = cli.client.wallet_list_unspent(cli.wallet.clone()).await?;
562+
let spaces = cli.client.wallet_list_unspent(&cli.wallet).await?;
547563
println!("{}", serde_json::to_string_pretty(&spaces)?);
548564
}
549565
Commands::ListAuctionOutputs => {
550-
let spaces = cli
551-
.client
552-
.wallet_list_auction_outputs(cli.wallet.clone())
553-
.await?;
566+
let spaces = cli.client.wallet_list_auction_outputs(&cli.wallet).await?;
554567
println!("{}", serde_json::to_string_pretty(&spaces)?);
555568
}
556569
Commands::ListSpaces => {
557-
let spaces = cli.client.wallet_list_spaces(cli.wallet.clone()).await?;
570+
let spaces = cli.client.wallet_list_spaces(&cli.wallet).await?;
558571
println!("{}", serde_json::to_string_pretty(&spaces)?);
559572
}
560573
Commands::Balance => {
561-
let balance = cli.client.wallet_get_balance(cli.wallet.clone()).await?;
574+
let balance = cli.client.wallet_get_balance(&cli.wallet).await?;
562575
println!("{}", serde_json::to_string_pretty(&balance)?);
563576
}
564577
Commands::GetCoinAddress => {
565578
let response = cli
566579
.client
567-
.wallet_get_new_address(cli.wallet.clone(), AddressKind::Coin)
580+
.wallet_get_new_address(&cli.wallet, AddressKind::Coin)
568581
.await?;
569582
println!("{}", response);
570583
}
571584
Commands::GetSpaceAddress => {
572585
let response = cli
573586
.client
574-
.wallet_get_new_address(cli.wallet.clone(), AddressKind::Space)
587+
.wallet_get_new_address(&cli.wallet, AddressKind::Space)
575588
.await?;
576589
println!("{}", response);
577590
}
578591
Commands::BumpFee { txid, fee_rate } => {
579592
let fee_rate = FeeRate::from_sat_per_vb(fee_rate).expect("valid fee rate");
580593
let response = cli
581594
.client
582-
.wallet_bump_fee(cli.wallet.clone(), txid, fee_rate)
595+
.wallet_bump_fee(&cli.wallet, txid, fee_rate)
583596
.await?;
584597
println!("{}", serde_json::to_string_pretty(&response)?);
585598
}
599+
Commands::SpaceHash { space } => {
600+
println!(
601+
"{}",
602+
space_hash(&space).map_err(|e| ClientError::Custom(e.to_string()))?
603+
);
604+
}
586605
}
587606

588607
Ok(())

node/src/node.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub trait BlockSource {
2626
fn get_block(&self, hash: &BlockHash) -> Result<Block, BitcoinRpcError>;
2727
fn get_median_time(&self) -> Result<u64, BitcoinRpcError>;
2828
fn get_block_count(&self) -> Result<u64, BitcoinRpcError>;
29+
fn get_best_chain(&self) -> Result<ChainAnchor, BitcoinRpcError>;
2930
}
3031

3132
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)