@@ -15,26 +15,18 @@ use bdk::{
1515} ;
1616use jsonrpsee:: { core:: async_trait, proc_macros:: rpc, server:: Server , types:: ErrorObjectOwned } ;
1717use log:: info;
18- use protocol:: {
19- bitcoin,
20- bitcoin:: {
21- bip32:: Xpriv ,
22- Network :: { Regtest , Testnet } ,
23- OutPoint ,
24- } ,
25- constants:: ChainAnchor ,
26- hasher:: { BaseHash , KeyHasher , SpaceKey } ,
27- prepare:: DataSource ,
28- slabel:: SLabel ,
29- validate:: TxChangeSet ,
30- FullSpaceOut , SpaceOut ,
31- } ;
18+ use protocol:: { bitcoin, bitcoin:: {
19+ bip32:: Xpriv ,
20+ Network :: { Regtest , Testnet } ,
21+ OutPoint ,
22+ } , constants:: ChainAnchor , hasher:: { BaseHash , KeyHasher , SpaceKey } , prepare:: DataSource , slabel:: SLabel , validate:: TxChangeSet , Bytes , FullSpaceOut , SpaceOut } ;
3223use serde:: { Deserialize , Serialize } ;
3324use tokio:: {
3425 select,
3526 sync:: { broadcast, mpsc, oneshot, RwLock } ,
3627 task:: JoinSet ,
3728} ;
29+ use protocol:: bitcoin:: secp256k1;
3830use wallet:: { bdk_wallet as bdk, bdk_wallet:: template:: Bip86 , bitcoin:: hashes:: Hash , export:: WalletExport , Balance , DoubleUtxo , Listing , SpacesWallet , WalletConfig , WalletDescriptors , WalletInfo , WalletOutput } ;
3931
4032use crate :: {
@@ -58,6 +50,13 @@ pub struct ServerInfo {
5850 pub tip : ChainAnchor ,
5951}
6052
53+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
54+ pub struct SignedMessage {
55+ pub space : String ,
56+ pub message : protocol:: Bytes ,
57+ pub signature : secp256k1:: schnorr:: Signature ,
58+ }
59+
6160pub enum ChainStateCommand {
6261 CheckPackage {
6362 txs : Vec < String > ,
@@ -99,6 +98,10 @@ pub enum ChainStateCommand {
9998 listing : Listing ,
10099 resp : Responder < anyhow:: Result < ( ) > > ,
101100 } ,
101+ VerifyMessage {
102+ msg : SignedMessage ,
103+ resp : Responder < anyhow:: Result < ( ) > > ,
104+ } ,
102105}
103106
104107#[ derive( Clone ) ]
@@ -153,6 +156,12 @@ pub trait Rpc {
153156 #[ method( name = "walletimport" ) ]
154157 async fn wallet_import ( & self , wallet : WalletExport ) -> Result < ( ) , ErrorObjectOwned > ;
155158
159+ #[ method( name = "verifymessage" ) ]
160+ async fn verify_message ( & self , msg : SignedMessage ) -> Result < ( ) , ErrorObjectOwned > ;
161+
162+ #[ method( name = "walletsignmessage" ) ]
163+ async fn wallet_sign_message ( & self , wallet : & str , space : & str , msg : protocol:: Bytes ) -> Result < SignedMessage , ErrorObjectOwned > ;
164+
156165 #[ method( name = "walletgetinfo" ) ]
157166 async fn wallet_get_info ( & self , name : & str ) -> Result < WalletInfo , ErrorObjectOwned > ;
158167
@@ -797,13 +806,28 @@ impl RpcServer for RpcServerImpl {
797806 . map_err ( |error| ErrorObjectOwned :: owned ( -1 , error. to_string ( ) , None :: < String > ) )
798807 }
799808
809+ async fn wallet_sign_message ( & self , wallet : & str , space : & str , msg : Bytes ) -> Result < SignedMessage , ErrorObjectOwned > {
810+ self . wallet ( & wallet)
811+ . await ?
812+ . send_sign_message ( space, msg)
813+ . await
814+ . map_err ( |error| ErrorObjectOwned :: owned ( -1 , error. to_string ( ) , None :: < String > ) )
815+ }
816+
800817 async fn verify_listing ( & self , listing : Listing ) -> Result < ( ) , ErrorObjectOwned > {
801818 self . store
802819 . verify_listing ( listing)
803820 . await
804821 . map_err ( |error| ErrorObjectOwned :: owned ( -1 , error. to_string ( ) , None :: < String > ) )
805822 }
806823
824+ async fn verify_message ( & self , msg : SignedMessage ) -> Result < ( ) , ErrorObjectOwned > {
825+ self . store
826+ . verify_message ( msg)
827+ . await
828+ . map_err ( |error| ErrorObjectOwned :: owned ( -1 , error. to_string ( ) , None :: < String > ) )
829+ }
830+
807831 async fn wallet_list_transactions (
808832 & self ,
809833 wallet : & str ,
@@ -1006,6 +1030,11 @@ impl AsyncChainState {
10061030 ChainStateCommand :: VerifyListing { listing, resp } => {
10071031 _ = resp. send ( SpacesWallet :: verify_listing :: < Sha256 > ( chain_state, & listing) . map ( |_| ( ) ) ) ;
10081032 }
1033+ ChainStateCommand :: VerifyMessage { msg, resp } => {
1034+ _ = resp. send ( SpacesWallet :: verify_message :: < Sha256 > (
1035+ chain_state, & msg. space , msg. message . as_slice ( ) , & msg. signature
1036+ ) . map ( |_| ( ) ) ) ;
1037+ }
10091038 }
10101039 }
10111040
@@ -1047,6 +1076,14 @@ impl AsyncChainState {
10471076 resp_rx. await ?
10481077 }
10491078
1079+ pub async fn verify_message ( & self , msg : SignedMessage ) -> anyhow:: Result < ( ) > {
1080+ let ( resp, resp_rx) = oneshot:: channel ( ) ;
1081+ self . sender
1082+ . send ( ChainStateCommand :: VerifyMessage { msg, resp } )
1083+ . await ?;
1084+ resp_rx. await ?
1085+ }
1086+
10501087 pub async fn get_rollout ( & self , target : usize ) -> anyhow:: Result < Vec < RolloutEntry > > {
10511088 let ( resp, resp_rx) = oneshot:: channel ( ) ;
10521089 self . sender
0 commit comments