Skip to content

Commit

Permalink
feat(mint): restore function
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Mar 11, 2024
1 parent 94c94fb commit f4c34e7
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 7 deletions.
26 changes: 22 additions & 4 deletions crates/cashu-sdk/src/mint/localstore/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct MemoryLocalStore {
melt_quotes: Arc<Mutex<HashMap<String, MeltQuote>>>,
pending_proofs: Arc<Mutex<HashMap<Vec<u8>, Proof>>>,
spent_proofs: Arc<Mutex<HashMap<Vec<u8>, Proof>>>,
blinded_signatures: Arc<Mutex<HashMap<String, BlindedSignature>>>,
blinded_signatures: Arc<Mutex<HashMap<Box<[u8]>, BlindedSignature>>>,
}

impl MemoryLocalStore {
Expand All @@ -34,7 +34,7 @@ impl MemoryLocalStore {
melt_quotes: Vec<MeltQuote>,
pending_proofs: Proofs,
spent_proofs: Proofs,
blinded_signatures: HashMap<String, BlindedSignature>,
blinded_signatures: HashMap<Box<[u8]>, BlindedSignature>,
) -> Result<Self, Error> {
Ok(Self {
mint_info: Arc::new(Mutex::new(mint_info)),
Expand Down Expand Up @@ -238,9 +238,10 @@ impl LocalStore for MemoryLocalStore {
self.blinded_signatures
.lock()
.await
.insert(blinded_message.to_string(), blinded_signature);
.insert(blinded_message.to_bytes(), blinded_signature);
Ok(())
}

async fn get_blinded_signature(
&self,
blinded_message: &PublicKey,
Expand All @@ -249,7 +250,24 @@ impl LocalStore for MemoryLocalStore {
.blinded_signatures
.lock()
.await
.get(&blinded_message.to_string())
.get(&blinded_message.to_bytes())
.cloned())
}

async fn get_blinded_signatures(
&self,
blinded_messages: Vec<PublicKey>,
) -> Result<Vec<Option<BlindedSignature>>, Error> {
let mut signatures = Vec::with_capacity(blinded_messages.len());

let blinded_signatures = self.blinded_signatures.lock().await;

for blinded_message in blinded_messages {
let signature = blinded_signatures.get(&blinded_message.to_bytes()).cloned();

signatures.push(signature)
}

Ok(signatures)
}
}
4 changes: 4 additions & 0 deletions crates/cashu-sdk/src/mint/localstore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,8 @@ pub trait LocalStore {
&self,
blinded_message: &PublicKey,
) -> Result<Option<BlindedSignature>, Error>;
async fn get_blinded_signatures(
&self,
blinded_messages: Vec<PublicKey>,
) -> Result<Vec<Option<BlindedSignature>>, Error>;
}
25 changes: 22 additions & 3 deletions crates/cashu-sdk/src/mint/localstore/redb_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const PENDING_PROOFS_TABLE: TableDefinition<&[u8], &str> = TableDefinition::new(
const SPENT_PROOFS_TABLE: TableDefinition<&[u8], &str> = TableDefinition::new("spent_proofs");
const CONFIG_TABLE: TableDefinition<&str, &str> = TableDefinition::new("config");
// Key is hex blinded_message B_ value is blinded_signature
const BLINDED_SIGNATURES: TableDefinition<&str, &str> = TableDefinition::new("blinded_signatures");
const BLINDED_SIGNATURES: TableDefinition<&[u8], &str> = TableDefinition::new("blinded_signatures");

#[derive(Debug, Clone)]
pub struct RedbLocalStore {
Expand Down Expand Up @@ -415,7 +415,7 @@ impl LocalStore for RedbLocalStore {
{
let mut table = write_txn.open_table(BLINDED_SIGNATURES)?;
table.insert(
blinded_message.to_string().as_str(),
blinded_message.to_bytes().as_ref(),
serde_json::to_string(&blinded_signature)?.as_str(),
)?;
}
Expand All @@ -433,10 +433,29 @@ impl LocalStore for RedbLocalStore {
let read_txn = db.begin_read()?;
let table = read_txn.open_table(BLINDED_SIGNATURES)?;

if let Some(blinded_signature) = table.get(blinded_message.to_string().as_str())? {
if let Some(blinded_signature) = table.get(blinded_message.to_bytes().as_ref())? {
return Ok(serde_json::from_str(blinded_signature.value())?);
}

Ok(None)
}

async fn get_blinded_signatures(
&self,
blinded_messages: Vec<PublicKey>,
) -> Result<Vec<Option<BlindedSignature>>, Error> {
let db = self.db.lock().await;
let read_txn = db.begin_read()?;
let table = read_txn.open_table(BLINDED_SIGNATURES)?;

let mut signatures = Vec::with_capacity(blinded_messages.len());

for blinded_message in blinded_messages {
if let Some(blinded_signature) = table.get(blinded_message.to_bytes().as_ref())? {
signatures.push(Some(serde_json::from_str(blinded_signature.value())?))
}
}

Ok(signatures)
}
}
29 changes: 29 additions & 0 deletions crates/cashu-sdk/src/mint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,35 @@ impl Mint {
pub async fn mint_info(&self) -> Result<MintInfo, Error> {
Ok(self.localstore.get_mint_info().await?)
}

#[cfg(feature = "nut09")]
pub async fn restore(&self, request: RestoreRequest) -> Result<RestoreResponse, Error> {
let output_len = request.outputs.len();

let mut outputs = Vec::with_capacity(output_len);
let mut signatures = Vec::with_capacity(output_len);

let blinded_message = request.outputs.iter().map(|b| b.b.clone()).collect();

let blinded_signatures = self
.localstore
.get_blinded_signatures(blinded_message)
.await?;

for (blinded_message, blinded_signature) in
request.outputs.into_iter().zip(blinded_signatures)
{
if let Some(blinded_signature) = blinded_signature {
outputs.push(blinded_message);
signatures.push(blinded_signature);
}
}

Ok(RestoreResponse {
outputs,
signatures,
})
}
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down
2 changes: 2 additions & 0 deletions crates/cashu/src/nuts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub use nut06::{MintInfo, MintVersion, Nuts};
pub use nut07::{CheckStateRequest, CheckStateResponse};
#[cfg(feature = "nut08")]
pub use nut08::{MeltBolt11Request, MeltBolt11Response};
#[cfg(feature = "nut09")]
pub use nut09::{RestoreRequest, RestoreResponse};
#[cfg(feature = "nut10")]
pub use nut10::{Kind, Secret as Nut10Secret, SecretData};
#[cfg(feature = "nut11")]
Expand Down
6 changes: 6 additions & 0 deletions crates/cashu/src/nuts/nut01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ impl PublicKey {
}
}

impl From<PublicKey> for Box<[u8]> {
fn from(pubkey: PublicKey) -> Box<[u8]> {
pubkey.to_bytes()
}
}

impl FromStr for PublicKey {
type Err = Error;

Expand Down
1 change: 1 addition & 0 deletions crates/cashu/src/nuts/nut09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct RestoreResponse {
/// Outputs
pub outputs: Vec<BlindedMessage>,
/// Signatures
// TODO: remove rename just for temp compatanlite with nutshell
#[serde(rename = "promises")]
pub signatures: Vec<BlindedSignature>,
}
Expand Down

0 comments on commit f4c34e7

Please sign in to comment.