service.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. use std::sync::Arc;
  2. use cashu::{BlindSignature, BlindedMessage, Proof};
  3. use cdk_common::error::Error;
  4. use cdk_common::mint::MintKeySetInfo;
  5. use tokio::sync::{mpsc, oneshot};
  6. use tokio::task::JoinHandle;
  7. use crate::signatory::{RotateKeyArguments, Signatory, SignatoryKeySet};
  8. enum Request {
  9. BlindSign(
  10. (
  11. BlindedMessage,
  12. oneshot::Sender<Result<BlindSignature, Error>>,
  13. ),
  14. ),
  15. VerifyProof((Proof, oneshot::Sender<Result<(), Error>>)),
  16. AuthKeysets(oneshot::Sender<Result<Option<Vec<SignatoryKeySet>>, Error>>),
  17. Keysets(oneshot::Sender<Result<Vec<SignatoryKeySet>, Error>>),
  18. RotateKeyset(
  19. (
  20. RotateKeyArguments,
  21. oneshot::Sender<Result<MintKeySetInfo, Error>>,
  22. ),
  23. ),
  24. }
  25. /// Creates a service-like to wrap an implementation of the Signatory
  26. ///
  27. /// This implements the actor model, ensuring the Signatory and their private key is moved from the
  28. /// main thread to their own tokio task, and communicates with the main program by passing messages,
  29. /// an extra layer of security to move the keys to another layer.
  30. pub struct Service {
  31. pipeline: mpsc::Sender<Request>,
  32. runner: Option<JoinHandle<()>>,
  33. }
  34. impl Drop for Service {
  35. fn drop(&mut self) {
  36. if let Some(runner) = self.runner.take() {
  37. runner.abort();
  38. }
  39. }
  40. }
  41. impl Service {
  42. pub fn new(handler: Arc<dyn Signatory + Send + Sync>) -> Self {
  43. let (tx, rx) = mpsc::channel(10_000);
  44. let runner = Some(tokio::spawn(Self::runner(rx, handler)));
  45. Self {
  46. pipeline: tx,
  47. runner,
  48. }
  49. }
  50. async fn runner(
  51. mut receiver: mpsc::Receiver<Request>,
  52. handler: Arc<dyn Signatory + Send + Sync>,
  53. ) {
  54. while let Some(request) = receiver.recv().await {
  55. match request {
  56. Request::BlindSign((blinded_message, response)) => {
  57. let output = handler.blind_sign(blinded_message).await;
  58. if let Err(err) = response.send(output) {
  59. tracing::error!("Error sending response: {:?}", err);
  60. }
  61. }
  62. Request::VerifyProof((proof, response)) => {
  63. let output = handler.verify_proof(proof).await;
  64. if let Err(err) = response.send(output) {
  65. tracing::error!("Error sending response: {:?}", err);
  66. }
  67. }
  68. Request::AuthKeysets(response) => {
  69. let output = handler.auth_keysets().await;
  70. if let Err(err) = response.send(output) {
  71. tracing::error!("Error sending response: {:?}", err);
  72. }
  73. }
  74. Request::Keysets(response) => {
  75. let output = handler.keysets().await;
  76. if let Err(err) = response.send(output) {
  77. tracing::error!("Error sending response: {:?}", err);
  78. }
  79. }
  80. Request::RotateKeyset((args, response)) => {
  81. let output = handler.rotate_keyset(args).await;
  82. if let Err(err) = response.send(output) {
  83. tracing::error!("Error sending response: {:?}", err);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. #[async_trait::async_trait]
  91. impl Signatory for Service {
  92. async fn blind_sign(&self, blinded_message: BlindedMessage) -> Result<BlindSignature, Error> {
  93. let (tx, rx) = oneshot::channel();
  94. self.pipeline
  95. .send(Request::BlindSign((blinded_message, tx)))
  96. .await
  97. .map_err(|e| Error::SendError(e.to_string()))?;
  98. rx.await.map_err(|e| Error::RecvError(e.to_string()))?
  99. }
  100. async fn verify_proof(&self, proof: Proof) -> Result<(), Error> {
  101. let (tx, rx) = oneshot::channel();
  102. self.pipeline
  103. .send(Request::VerifyProof((proof, tx)))
  104. .await
  105. .map_err(|e| Error::SendError(e.to_string()))?;
  106. rx.await.map_err(|e| Error::RecvError(e.to_string()))?
  107. }
  108. async fn auth_keysets(&self) -> Result<Option<Vec<SignatoryKeySet>>, Error> {
  109. let (tx, rx) = oneshot::channel();
  110. self.pipeline
  111. .send(Request::AuthKeysets(tx))
  112. .await
  113. .map_err(|e| Error::SendError(e.to_string()))?;
  114. rx.await.map_err(|e| Error::RecvError(e.to_string()))?
  115. }
  116. async fn keysets(&self) -> Result<Vec<SignatoryKeySet>, Error> {
  117. let (tx, rx) = oneshot::channel();
  118. self.pipeline
  119. .send(Request::Keysets(tx))
  120. .await
  121. .map_err(|e| Error::SendError(e.to_string()))?;
  122. rx.await.map_err(|e| Error::RecvError(e.to_string()))?
  123. }
  124. async fn rotate_keyset(&self, args: RotateKeyArguments) -> Result<MintKeySetInfo, Error> {
  125. let (tx, rx) = oneshot::channel();
  126. self.pipeline
  127. .send(Request::RotateKeyset((args, tx)))
  128. .await
  129. .map_err(|e| Error::SendError(e.to_string()))?;
  130. rx.await.map_err(|e| Error::RecvError(e.to_string()))?
  131. }
  132. }