@@ -32,6 +32,8 @@ use tokio::{
32
32
sync:: { broadcast, mpsc, oneshot, RwLock } ,
33
33
task:: JoinSet ,
34
34
} ;
35
+ use protocol:: hasher:: KeyHasher ;
36
+ use protocol:: slabel:: SLabel ;
35
37
use wallet:: {
36
38
bdk_wallet as bdk, bdk_wallet:: template:: Bip86 , bitcoin:: hashes:: Hash , export:: WalletExport ,
37
39
DoubleUtxo , SpacesWallet , WalletConfig , WalletDescriptors , WalletInfo ,
@@ -46,6 +48,7 @@ use crate::{
46
48
AddressKind , Balance , RpcWallet , TxResponse , WalletCommand , WalletOutput , WalletResponse ,
47
49
} ,
48
50
} ;
51
+ use crate :: store:: Sha256 ;
49
52
50
53
pub ( crate ) type Responder < T > = oneshot:: Sender < T > ;
51
54
@@ -101,11 +104,11 @@ pub trait Rpc {
101
104
async fn get_server_info ( & self ) -> Result < ServerInfo , ErrorObjectOwned > ;
102
105
103
106
#[ 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 > ;
105
108
106
109
#[ 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 > ;
109
112
110
113
#[ method( name = "getspaceout" ) ]
111
114
async fn get_spaceout ( & self , outpoint : OutPoint ) -> Result < Option < SpaceOut > , ErrorObjectOwned > ;
@@ -172,7 +175,7 @@ pub trait Rpc {
172
175
173
176
#[ method( name = "walletlistspaces" ) ]
174
177
async fn wallet_list_spaces ( & self , wallet : & str )
175
- -> Result < Vec < WalletOutput > , ErrorObjectOwned > ;
178
+ -> Result < Vec < WalletOutput > , ErrorObjectOwned > ;
176
179
177
180
#[ method( name = "walletlistunspent" ) ]
178
181
async fn wallet_list_unspent (
@@ -567,8 +570,9 @@ impl RpcServer for RpcServerImpl {
567
570
Ok ( ServerInfo { chain, tip } )
568
571
}
569
572
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
+
572
576
let info = self
573
577
. store
574
578
. get_space ( space_hash)
@@ -579,9 +583,9 @@ impl RpcServer for RpcServerImpl {
579
583
580
584
async fn get_space_owner (
581
585
& self ,
582
- space_hash : & str ,
586
+ space_or_hash : & str ,
583
587
) -> Result < Option < OutPoint > , ErrorObjectOwned > {
584
- let space_hash = space_hash_from_string ( space_hash ) ?;
588
+ let space_hash = get_space_key ( space_or_hash ) ?;
585
589
let info = self
586
590
. store
587
591
. get_space_outpoint ( space_hash)
@@ -983,20 +987,29 @@ impl AsyncChainState {
983
987
}
984
988
}
985
989
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
+
987
1005
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 ( |_| {
989
1007
ErrorObjectOwned :: owned (
990
1008
-1 ,
991
- "expected a 32-byte hex encoded space hash" ,
1009
+ "expected a space name prefixed with @ or a hex encoded space hash" ,
992
1010
None :: < String > ,
993
1011
)
994
1012
} ) ?;
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) )
1002
1015
}
0 commit comments