Skip to content

Commit 9f2df39

Browse files
authored
Merge pull request #118 from buffrr/scripts-cleanup
Script cleanups
2 parents 9060210 + b02c6c4 commit 9f2df39

File tree

10 files changed

+509
-765
lines changed

10 files changed

+509
-765
lines changed

client/src/bin/space-cli.rs

Lines changed: 65 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ use spaces_client::{
2929
print_wallet_balance_response, print_wallet_info, print_wallet_response, Format,
3030
},
3131
rpc::{
32-
BidParams, ExecuteParams, OpenParams, RegisterParams, RpcClient, RpcWalletRequest,
32+
BidParams, OpenParams, RegisterParams, RpcClient, RpcWalletRequest,
3333
RpcWalletTxBuilder, SendCoinsParams, TransferSpacesParams,
3434
},
3535
serialize_base64,
3636
wallets::{AddressKind, WalletResponse},
3737
};
38-
use spaces_client::rpc::{CommitParams, CreatePtrParams, DelegateParams, SetPtrDataParams, TransferPtrParams};
38+
use spaces_client::rpc::{CommitParams, CreatePtrParams, DelegateParams, SetPtrDataParams};
3939
use spaces_client::store::Sha256;
4040
use spaces_protocol::bitcoin::{Amount, FeeRate, OutPoint, Txid};
4141
use spaces_protocol::slabel::SLabel;
@@ -166,34 +166,21 @@ enum Commands {
166166
/// The sha256 hash of the spk or the spk itself prefixed with hex:
167167
spk: String,
168168
},
169-
/// Transfer ownership of a set of spaces to the given name or address
169+
/// Transfer ownership of spaces and/or PTRs to the given name or address
170170
#[command(
171171
name = "transfer",
172-
override_usage = "space-cli transfer [SPACES]... --to <SPACE-OR-ADDRESS>"
172+
override_usage = "space-cli transfer [SPACES-OR-PTRS]... --to <SPACE-OR-ADDRESS> [--data <DATA>]"
173173
)]
174174
Transfer {
175-
/// Spaces to send
175+
/// Spaces (e.g., @bitcoin) and/or PTRs (e.g., sptr1...) to send
176176
#[arg(display_order = 0)]
177177
spaces: Vec<String>,
178-
/// Recipient space name or address (must be a space address)
179-
#[arg(long, display_order = 1)]
180-
to: String,
181-
/// Fee rate to use in sat/vB
182-
#[arg(long, short)]
183-
fee_rate: Option<u64>,
184-
},
185-
/// Transfer ownership of a set of ptrs to the given name or address
186-
#[command(
187-
name = "transferptr",
188-
override_usage = "space-cli transferptr [PTRS]... --to <SPACE-OR-ADDRESS>"
189-
)]
190-
TransferPtr {
191-
/// Ptrs to send
192-
#[arg(display_order = 0)]
193-
ptrs: Vec<String>,
194-
/// Recipient space name or address (must be a space address)
178+
/// Recipient space name or address
195179
#[arg(long, display_order = 1)]
196180
to: String,
181+
/// Optional data to set on all transferred spaces/PTRs (hex-encoded)
182+
#[arg(long, display_order = 2)]
183+
data: Option<String>,
197184
/// Fee rate to use in sat/vB
198185
#[arg(long, short)]
199186
fee_rate: Option<u64>,
@@ -800,11 +787,16 @@ async fn handle_commands(cli: &SpaceCli, command: Commands) -> Result<(), Client
800787
.await?
801788
}
802789
Commands::Renew { spaces, fee_rate } => {
803-
let spaces: Vec<_> = spaces.into_iter().map(|s| normalize_space(&s)).collect();
790+
use spaces_client::rpc::SpaceOrPtr;
791+
let spaces: Vec<_> = spaces.into_iter().map(|s| {
792+
let normalized = normalize_space(&s);
793+
SpaceOrPtr::Space(SLabel::from_str(&normalized).expect("valid space"))
794+
}).collect();
804795
cli.send_request(
805796
Some(RpcWalletRequest::Transfer(TransferSpacesParams {
806797
spaces,
807798
to: None,
799+
data: None,
808800
})),
809801
None,
810802
fee_rate,
@@ -815,13 +807,41 @@ async fn handle_commands(cli: &SpaceCli, command: Commands) -> Result<(), Client
815807
Commands::Transfer {
816808
spaces,
817809
to,
810+
data,
818811
fee_rate,
819812
} => {
820-
let spaces: Vec<_> = spaces.into_iter().map(|s| normalize_space(&s)).collect();
813+
use spaces_client::rpc::SpaceOrPtr;
814+
// Parse spaces and PTRs into SpaceOrPtr
815+
let spaces: Result<Vec<_>, _> = spaces.into_iter().map(|s| {
816+
if s.starts_with("sptr1") {
817+
// Parse as SPTR
818+
Sptr::from_str(&s).map(SpaceOrPtr::Ptr)
819+
.map_err(|e| ClientError::Custom(format!("Invalid SPTR '{}': {}", s, e)))
820+
} else {
821+
// Normalize and parse as space
822+
let normalized = normalize_space(&s);
823+
SLabel::from_str(&normalized).map(SpaceOrPtr::Space)
824+
.map_err(|e| ClientError::Custom(format!("Invalid space '{}': {}", s, e)))
825+
}
826+
}).collect();
827+
let spaces = spaces?;
828+
829+
// Parse hex data if present
830+
let data = match data {
831+
Some(hex_str) => {
832+
let data = hex::decode(hex_str).map_err(|e| {
833+
ClientError::Custom(format!("Invalid hex data: {}", e))
834+
})?;
835+
Some(data)
836+
}
837+
None => None,
838+
};
839+
821840
cli.send_request(
822841
Some(RpcWalletRequest::Transfer(TransferSpacesParams {
823842
spaces,
824843
to: Some(to),
844+
data,
825845
})),
826846
None,
827847
fee_rate,
@@ -873,21 +893,22 @@ async fn handle_commands(cli: &SpaceCli, command: Commands) -> Result<(), Client
873893
)
874894
.await?;
875895
} else {
876-
// Space fallback: use existing space script
877-
let space = normalize_space(&space_or_sptr);
878-
let space_script =
879-
spaces_protocol::script::SpaceScript::create_set_fallback(data.as_slice());
880-
881-
cli.send_request(
882-
Some(RpcWalletRequest::Execute(ExecuteParams {
883-
context: vec![space],
884-
space_script,
885-
})),
886-
None,
887-
fee_rate,
888-
false,
889-
)
890-
.await?;
896+
// TODO: support set data for spaces
897+
// // Space fallback: use existing space script
898+
// let space = normalize_space(&space_or_sptr);
899+
// let space_script =
900+
// spaces_protocol::script::create_set_data(data.as_slice());
901+
//
902+
// cli.send_request(
903+
// Some(RpcWalletRequest::Execute(ExecuteParams {
904+
// context: vec![space],
905+
// space_script,
906+
// })),
907+
// None,
908+
// fee_rate,
909+
// false,
910+
// )
911+
// .await?;
891912
}
892913
}
893914
Commands::ListUnspent => {
@@ -1095,24 +1116,6 @@ async fn handle_commands(cli: &SpaceCli, command: Commands) -> Result<(), Client
10951116
)
10961117
.await?
10971118
}
1098-
Commands::TransferPtr { ptrs, to, fee_rate } => {
1099-
let mut parsed = Vec::with_capacity(ptrs.len());
1100-
for ptr in ptrs {
1101-
parsed.push(Sptr::from_str(&ptr)
1102-
.map_err(|e| ClientError::Custom(format!("invalid sptr:{}: {}", ptr, e.to_string())))?);
1103-
}
1104-
1105-
cli.send_request(
1106-
Some(RpcWalletRequest::TransferPtr(TransferPtrParams {
1107-
ptrs: parsed,
1108-
to,
1109-
})),
1110-
None,
1111-
fee_rate,
1112-
false,
1113-
)
1114-
.await?
1115-
}
11161119
Commands::GetPtr { spk } => {
11171120
let sptr = Sptr::from_str(&spk)
11181121
.map_err(|e| ClientError::Custom(format!("input error: {}", e.to_string())))?;
@@ -1209,6 +1212,7 @@ async fn handle_commands(cli: &SpaceCli, command: Commands) -> Result<(), Client
12091212
None => return Err(ClientError::Custom("no such space".to_string()))
12101213
};
12111214

1215+
use spaces_client::rpc::SpaceOrPtr;
12121216
let label = space_info.spaceout.space.as_ref().expect("space").name.clone();
12131217
let delegation = cli.client.get_delegation(label.clone()).await?;
12141218
if delegation.is_none() {
@@ -1217,9 +1221,10 @@ async fn handle_commands(cli: &SpaceCli, command: Commands) -> Result<(), Client
12171221
let delegation = delegation.unwrap();
12181222

12191223
cli.send_request(
1220-
Some(RpcWalletRequest::TransferPtr(TransferPtrParams {
1221-
ptrs: vec![delegation],
1222-
to,
1224+
Some(RpcWalletRequest::Transfer(TransferSpacesParams {
1225+
spaces: vec![SpaceOrPtr::Ptr(delegation)],
1226+
to: Some(to),
1227+
data: None,
12231228
})),
12241229
None,
12251230
fee_rate,

client/src/rpc.rs

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,8 @@ pub enum RpcWalletRequest {
477477
Bid(BidParams),
478478
#[serde(rename = "register")]
479479
Register(RegisterParams),
480-
#[serde(rename = "execute")]
481-
Execute(ExecuteParams),
482480
#[serde(rename = "transfer")]
483481
Transfer(TransferSpacesParams),
484-
#[serde(rename = "transferptr")]
485-
TransferPtr(TransferPtrParams),
486482
#[serde(rename = "createptr")]
487483
CreatePtr(CreatePtrParams),
488484
#[serde(rename = "delegate")]
@@ -495,18 +491,56 @@ pub enum RpcWalletRequest {
495491
SendCoins(SendCoinsParams),
496492
}
497493

494+
/// Either a space name or a PTR
495+
#[derive(Clone, Debug)]
496+
pub enum SpaceOrPtr {
497+
Space(SLabel),
498+
Ptr(Sptr),
499+
}
500+
501+
impl Serialize for SpaceOrPtr {
502+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
503+
where
504+
S: Serializer,
505+
{
506+
match self {
507+
SpaceOrPtr::Space(label) => serializer.serialize_str(&label.to_string()),
508+
SpaceOrPtr::Ptr(sptr) => serializer.serialize_str(&sptr.to_string()),
509+
}
510+
}
511+
}
512+
513+
impl<'de> Deserialize<'de> for SpaceOrPtr {
514+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
515+
where
516+
D: Deserializer<'de>,
517+
{
518+
let s = String::deserialize(deserializer)?;
519+
520+
// Try to parse as SPTR first (starts with "sptr1")
521+
if s.starts_with("sptr1") {
522+
Sptr::from_str(&s)
523+
.map(SpaceOrPtr::Ptr)
524+
.map_err(serde::de::Error::custom)
525+
} else {
526+
// Otherwise parse as space name
527+
SLabel::from_str(&s)
528+
.map(SpaceOrPtr::Space)
529+
.map_err(serde::de::Error::custom)
530+
}
531+
}
532+
}
533+
498534
#[derive(Clone, Serialize, Deserialize)]
499535
pub struct TransferSpacesParams {
500-
pub spaces: Vec<String>,
536+
/// List of spaces and/or PTRs to transfer
537+
pub spaces: Vec<SpaceOrPtr>,
501538

502539
#[serde(skip_serializing_if = "Option::is_none")]
503540
pub to: Option<String>,
504-
}
505541

506-
#[derive(Clone, Serialize, Deserialize)]
507-
pub struct TransferPtrParams {
508-
pub ptrs: Vec<Sptr>,
509-
pub to: String,
542+
#[serde(skip_serializing_if = "Option::is_none")]
543+
pub data: Option<Vec<u8>>,
510544
}
511545

512546
#[derive(Clone, Serialize, Deserialize)]
@@ -537,12 +571,6 @@ pub struct SendCoinsParams {
537571
pub to: String,
538572
}
539573

540-
#[derive(Clone, Serialize, Deserialize)]
541-
pub struct ExecuteParams {
542-
pub context: Vec<String>,
543-
pub space_script: Vec<u8>,
544-
}
545-
546574
#[derive(Clone, Serialize, Deserialize)]
547575
pub struct OpenParams {
548576
pub name: String,

0 commit comments

Comments
 (0)