Skip to content

Commit

Permalink
Merge branch 'main' into slabel
Browse files Browse the repository at this point in the history
  • Loading branch information
Ximik committed Jan 27, 2025
2 parents 7ac8443 + 361ca77 commit 72b6c9a
Show file tree
Hide file tree
Showing 7 changed files with 357 additions and 54 deletions.
67 changes: 66 additions & 1 deletion node/src/bin/space-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use spaced::{
store::Sha256,
wallets::AddressKind,
};
use spaced::rpc::SignedMessage;
use wallet::bitcoin::secp256k1::schnorr::Signature;
use wallet::export::WalletExport;
use wallet::Listing;
Expand Down Expand Up @@ -132,6 +133,16 @@ enum Commands {
#[arg(long, short)]
fee_rate: Option<u64>,
},
/// Renew ownership of a space
#[command(name = "renew", )]
Renew {
/// Spaces to renew
#[arg(display_order = 0)]
spaces: Vec<String>,
/// Fee rate to use in sat/vB
#[arg(long, short)]
fee_rate: Option<u64>,
},
/// Estimates the minimum bid needed for a rollout within the given target blocks
#[command(name = "estimatebid")]
EstimateBid {
Expand Down Expand Up @@ -193,6 +204,27 @@ enum Commands {
#[arg(long, short)]
fee_rate: Option<u64>,
},
/// Sign a message using the owner address of the specified space
#[command(name = "signmessage")]
SignMessage {
/// The space to use
space: String,
/// The message to sign
message: String,
},
/// Verify a message using the owner address of the specified space
#[command(name = "verifymessage")]
VerifyMessage {
/// The space to verify
space: String,

/// The message to verify
message: String,

/// The signature to verify
#[arg(long)]
signature: String,
},
/// List a space you own for sale
#[command(name = "sell")]
Sell {
Expand Down Expand Up @@ -532,6 +564,19 @@ async fn handle_commands(
)
.await?
}
Commands::Renew { spaces, fee_rate } => {
let spaces: Vec<_> = spaces.into_iter().map(|s| normalize_space(&s)).collect();
cli.send_request(
Some(RpcWalletRequest::Transfer(TransferSpacesParams {
spaces,
to: None,
})),
None,
fee_rate,
false,
)
.await?
}
Commands::Transfer {
spaces,
to,
Expand All @@ -541,7 +586,7 @@ async fn handle_commands(
cli.send_request(
Some(RpcWalletRequest::Transfer(TransferSpacesParams {
spaces,
to,
to: Some(to),
})),
None,
fee_rate,
Expand Down Expand Up @@ -700,6 +745,26 @@ async fn handle_commands(
.verify_listing(listing).await?;
println!("{}", serde_json::to_string_pretty(&result).expect("result"));
}
Commands::SignMessage { mut space, message } => {
space = normalize_space(&space);
let result = cli.client
.wallet_sign_message(&cli.wallet, &space, protocol::Bytes::new(message.as_bytes().to_vec())).await?;
println!("{}", result.signature);
}
Commands::VerifyMessage { mut space, message, signature } => {
space = normalize_space(&space);
let raw = hex::decode(signature)
.map_err(|_| ClientError::Custom("Invalid signature".to_string()))?;
let signature = Signature::from_slice(raw.as_slice())
.map_err(|_| ClientError::Custom("Invalid signature".to_string()))?;
let result = cli.client.verify_message(SignedMessage {
space,
message: protocol::Bytes::new(message.as_bytes().to_vec()),
signature,
}).await?;
println!("{}", serde_json::to_string_pretty(&result).expect("result"));
}

}

Ok(())
Expand Down
73 changes: 56 additions & 17 deletions node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,18 @@ use bdk::{
};
use jsonrpsee::{core::async_trait, proc_macros::rpc, server::Server, types::ErrorObjectOwned};
use log::info;
use protocol::{
bitcoin,
bitcoin::{
bip32::Xpriv,
Network::{Regtest, Testnet},
OutPoint,
},
constants::ChainAnchor,
hasher::{BaseHash, KeyHasher, SpaceKey},
prepare::DataSource,
slabel::SLabel,
validate::TxChangeSet,
FullSpaceOut, SpaceOut,
};
use protocol::{bitcoin, bitcoin::{
bip32::Xpriv,
Network::{Regtest, Testnet},
OutPoint,
}, constants::ChainAnchor, hasher::{BaseHash, KeyHasher, SpaceKey}, prepare::DataSource, slabel::SLabel, validate::TxChangeSet, Bytes, FullSpaceOut, SpaceOut};
use serde::{Deserialize, Serialize};
use tokio::{
select,
sync::{broadcast, mpsc, oneshot, RwLock},
task::JoinSet,
};
use protocol::bitcoin::secp256k1;
use wallet::{bdk_wallet as bdk, bdk_wallet::template::Bip86, bitcoin::hashes::Hash, export::WalletExport, Balance, DoubleUtxo, Listing, SpacesWallet, WalletConfig, WalletDescriptors, WalletInfo, WalletOutput};

use crate::{
Expand All @@ -58,6 +50,13 @@ pub struct ServerInfo {
pub tip: ChainAnchor,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignedMessage {
pub space: String,
pub message: protocol::Bytes,
pub signature: secp256k1::schnorr::Signature,
}

pub enum ChainStateCommand {
CheckPackage {
txs: Vec<String>,
Expand Down Expand Up @@ -99,6 +98,10 @@ pub enum ChainStateCommand {
listing: Listing,
resp: Responder<anyhow::Result<()>>,
},
VerifyMessage {
msg: SignedMessage,
resp: Responder<anyhow::Result<()>>,
},
}

#[derive(Clone)]
Expand Down Expand Up @@ -153,6 +156,12 @@ pub trait Rpc {
#[method(name = "walletimport")]
async fn wallet_import(&self, wallet: WalletExport) -> Result<(), ErrorObjectOwned>;

#[method(name = "verifymessage")]
async fn verify_message(&self, msg: SignedMessage) -> Result<(), ErrorObjectOwned>;

#[method(name = "walletsignmessage")]
async fn wallet_sign_message(&self, wallet: &str, space: &str, msg: protocol::Bytes) -> Result<SignedMessage, ErrorObjectOwned>;

#[method(name = "walletgetinfo")]
async fn wallet_get_info(&self, name: &str) -> Result<WalletInfo, ErrorObjectOwned>;

Expand Down Expand Up @@ -264,16 +273,18 @@ pub enum RpcWalletRequest {
Register(RegisterParams),
#[serde(rename = "execute")]
Execute(ExecuteParams),
#[serde(rename = "sendspaces")]
#[serde(rename = "transfer")]
Transfer(TransferSpacesParams),
#[serde(rename = "sendcoins")]
#[serde(rename = "send")]
SendCoins(SendCoinsParams),
}

#[derive(Clone, Serialize, Deserialize)]
pub struct TransferSpacesParams {
pub spaces: Vec<String>,
pub to: String,

#[serde(skip_serializing_if = "Option::is_none")]
pub to: Option<String>,
}

#[derive(Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -797,13 +808,28 @@ impl RpcServer for RpcServerImpl {
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))
}

async fn wallet_sign_message(&self, wallet: &str, space: &str, msg: Bytes) -> Result<SignedMessage, ErrorObjectOwned> {
self.wallet(&wallet)
.await?
.send_sign_message(space, msg)
.await
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))
}

async fn verify_listing(&self, listing: Listing) -> Result<(), ErrorObjectOwned> {
self.store
.verify_listing(listing)
.await
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))
}

async fn verify_message(&self, msg: SignedMessage) -> Result<(), ErrorObjectOwned> {
self.store
.verify_message(msg)
.await
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))
}

async fn wallet_list_transactions(
&self,
wallet: &str,
Expand Down Expand Up @@ -1006,6 +1032,11 @@ impl AsyncChainState {
ChainStateCommand::VerifyListing { listing, resp } => {
_ = resp.send(SpacesWallet::verify_listing::<Sha256>(chain_state, &listing).map(|_| ()));
}
ChainStateCommand::VerifyMessage { msg, resp } => {
_ = resp.send(SpacesWallet::verify_message::<Sha256>(
chain_state, &msg.space, msg.message.as_slice(), &msg.signature
).map(|_| ()));
}
}
}

Expand Down Expand Up @@ -1047,6 +1078,14 @@ impl AsyncChainState {
resp_rx.await?
}

pub async fn verify_message(&self, msg: SignedMessage) -> anyhow::Result<()> {
let (resp, resp_rx) = oneshot::channel();
self.sender
.send(ChainStateCommand::VerifyMessage { msg, resp })
.await?;
resp_rx.await?
}

pub async fn get_rollout(&self, target: usize) -> anyhow::Result<Vec<RolloutEntry>> {
let (resp, resp_rx) = oneshot::channel();
self.sender
Expand Down
Loading

0 comments on commit 72b6c9a

Please sign in to comment.