Skip to content

Commit

Permalink
refactor: remove mint and wallet error mods
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Apr 12, 2024
1 parent 27f718d commit ce20795
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 182 deletions.
25 changes: 10 additions & 15 deletions crates/cdk/src/dhke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bitcoin::hashes::sha256::Hash as Sha256Hash;
use bitcoin::hashes::Hash;
use bitcoin::secp256k1::{Parity, PublicKey as NormalizedPublicKey, Scalar, XOnlyPublicKey};

use crate::error::{self, Error};
use crate::error::Error;
use crate::nuts::nut01::{PublicKey, SecretKey};
use crate::nuts::nut12::ProofDleq;
use crate::nuts::{BlindSignature, Keys, Proof, Proofs};
Expand Down Expand Up @@ -62,7 +62,7 @@ where
pub fn blind_message(
secret: &[u8],
blinding_factor: Option<SecretKey>,
) -> Result<(PublicKey, SecretKey), error::wallet::Error> {
) -> Result<(PublicKey, SecretKey), Error> {
let y: PublicKey = hash_to_curve(secret)?;
let r: SecretKey = blinding_factor.unwrap_or_else(SecretKey::generate);
Ok((y.combine(&r.public_key())?.into(), r))
Expand All @@ -77,7 +77,7 @@ pub fn unblind_message(
r: &SecretKey,
// K
mint_pubkey: &PublicKey,
) -> Result<PublicKey, error::wallet::Error> {
) -> Result<PublicKey, Error> {
let r: Scalar = Scalar::from(r.deref().to_owned());

// a = r * K
Expand All @@ -94,15 +94,13 @@ pub fn construct_proofs(
rs: Vec<SecretKey>,
secrets: Vec<Secret>,
keys: &Keys,
) -> Result<Proofs, error::wallet::Error> {
) -> Result<Proofs, Error> {
let mut proofs = vec![];
for ((blinded_signature, r), secret) in promises.into_iter().zip(rs).zip(secrets) {
let blinded_c: PublicKey = blinded_signature.c;
let a: PublicKey =
keys.amount_key(blinded_signature.amount)
.ok_or(error::wallet::Error::CustomError(
"Could not get proofs".to_string(),
))?;
let a: PublicKey = keys
.amount_key(blinded_signature.amount)
.ok_or(Error::CustomError("Could not get proofs".to_string()))?;

let unblinded_signature: PublicKey = unblind_message(&blinded_c, &r, &a)?;

Expand All @@ -129,10 +127,7 @@ pub fn construct_proofs(
/// * `k` is the private key of mint (one for each amount)
/// * `B_` is the blinded message
#[inline]
pub fn sign_message(
k: &SecretKey,
blinded_message: &PublicKey,
) -> Result<PublicKey, error::mint::Error> {
pub fn sign_message(k: &SecretKey, blinded_message: &PublicKey) -> Result<PublicKey, Error> {
let k: Scalar = Scalar::from(k.deref().to_owned());
Ok(blinded_message.mul_tweak(&SECP256K1, &k)?.into())
}
Expand All @@ -142,7 +137,7 @@ pub fn verify_message(
a: &SecretKey,
unblinded_message: PublicKey,
msg: &[u8],
) -> Result<(), error::mint::Error> {
) -> Result<(), Error> {
// Y
let y: PublicKey = hash_to_curve(msg)?;

Expand All @@ -154,7 +149,7 @@ pub fn verify_message(
return Ok(());
}

Err(error::mint::Error::TokenNotVerifed)
Err(Error::TokenNotVerifed)
}

#[cfg(test)]
Expand Down
38 changes: 0 additions & 38 deletions crates/cdk/src/error/mint.rs

This file was deleted.

80 changes: 40 additions & 40 deletions crates/cdk/src/error/mod.rs
Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@
//! Errors
use std::string::FromUtf8Error;

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::util::hex;

pub mod mint;
pub mod wallet;

#[derive(Debug, Error)]
pub enum Error {
/// Parse Url Error
#[error(transparent)]
UrlParseError(#[from] url::ParseError),
/// Utf8 parse error
#[error(transparent)]
Utf8ParseError(#[from] FromUtf8Error),
/// Serde Json error
#[error(transparent)]
SerdeJsonError(#[from] serde_json::Error),
/// Base64 error
#[error(transparent)]
Base64Error(#[from] base64::DecodeError),
/// From hex error
#[error(transparent)]
HexError(#[from] hex::Error),
/// Secp256k1 error
#[error(transparent)]
Secp256k1(#[from] bitcoin::secp256k1::Error),
#[error("No Key for Amoun")]
/// Mint does not have a key for amount
#[error("No Key for Amount")]
AmountKey,
/// Amount is not what expected
#[error("Amount miss match")]
Amount,
/// Token is already spent
#[error("Token already spent")]
TokenSpent,
/// Token could not be validated
#[error("Token not verified")]
TokenNotVerifed,
/// Bolt11 invoice does not have amount
#[error("Invoice Amount undefined")]
InvoiceAmountUndefined,
/// Proof is missing a required field
#[error("Proof missing required field")]
MissingProofField,
/// No valid point on curve
#[error("No valid point found")]
NoValidPoint,
#[error("Kind not found")]
KindNotFound,
#[error("Unknown Tag")]
UnknownTag,
#[error("Incorrect Secret Kind")]
IncorrectSecretKind,
#[error("Spending conditions not met")]
SpendConditionsNotMet,
#[error("Could not convert key")]
Key,
#[error("Invalid signature")]
InvalidSignature,
#[error("Locktime in past")]
LocktimeInPast,
#[error(transparent)]
Secret(#[from] super::secret::Error),
/// Secp256k1 error
#[error(transparent)]
NUT01(#[from] crate::nuts::nut01::Error),
Secp256k1(#[from] bitcoin::secp256k1::Error),
/// Secret error
#[error(transparent)]
NUT02(#[from] crate::nuts::nut02::Error),
Secret(#[from] super::secret::Error),
/// Bip32 error
#[cfg(feature = "nut13")]
#[error(transparent)]
Bip32(#[from] bitcoin::bip32::Error),
/// Parse int error
#[error(transparent)]
ParseInt(#[from] std::num::ParseIntError),
/// Parse Url Error
#[error(transparent)]
UrlParseError(#[from] url::ParseError),
/// Utf8 parse error
#[error(transparent)]
Utf8ParseError(#[from] FromUtf8Error),
/// Serde Json error
#[error(transparent)]
SerdeJsonError(#[from] serde_json::Error),
/// Base64 error
#[error(transparent)]
Base64Error(#[from] base64::DecodeError),
/// From hex error
#[error(transparent)]
HexError(#[from] hex::Error),
/// Nut01 error
#[error(transparent)]
NUT01(#[from] crate::nuts::nut01::Error),
/// NUT02 error
#[error(transparent)]
NUT02(#[from] crate::nuts::nut02::Error),
/// NUT11 Error
#[error(transparent)]
NUT11(#[from] crate::nuts::nut11::Error),
/// Custom error
#[error("`{0}`")]
CustomError(String),
Expand Down
49 changes: 0 additions & 49 deletions crates/cdk/src/error/wallet.rs

This file was deleted.

2 changes: 2 additions & 0 deletions crates/cdk/src/mint/localstore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub enum Error {
#[error(transparent)]
Cashu(#[from] crate::error::Error),
#[error(transparent)]
NUT00(#[from] crate::nuts::nut00::Error),
#[error(transparent)]
CashuNut02(#[from] crate::nuts::nut02::Error),
#[error(transparent)]
Secret(#[from] crate::secret::Error),
Expand Down
14 changes: 6 additions & 8 deletions crates/cdk/src/mint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ use tracing::{debug, error, info};

use crate::dhke::{hash_to_curve, sign_message, verify_message};
use crate::error::ErrorResponse;
use crate::nuts::nut07::{ProofState, State};
use crate::nuts::{
BlindSignature, BlindedMessage, CheckStateRequest, CheckStateResponse, MeltBolt11Request,
MeltBolt11Response, Proof, RestoreRequest, RestoreResponse, SwapRequest, SwapResponse, *,
};
use crate::nuts::*;
use crate::types::{MeltQuote, MintQuote};
use crate::Amount;

mod localstore;
pub mod localstore;
#[cfg(all(not(target_arch = "wasm32"), feature = "redb"))]
pub use localstore::RedbLocalStore;
pub use localstore::{LocalStore, MemoryLocalStore};
Expand Down Expand Up @@ -45,14 +41,16 @@ pub enum Error {
#[error("`{0}`")]
Custom(String),
#[error(transparent)]
CashuMint(#[from] crate::error::mint::Error),
#[error(transparent)]
Cashu(#[from] crate::error::Error),
#[error(transparent)]
Localstore(#[from] localstore::Error),
#[error(transparent)]
Secret(#[from] crate::secret::Error),
#[error(transparent)]
NUT00(#[from] crate::nuts::nut00::Error),
#[error(transparent)]
NUT11(#[from] crate::nuts::nut11::Error),
#[error(transparent)]
Nut12(#[from] crate::nuts::nut12::Error),
#[error("Unknown quote")]
UnknownQuote,
Expand Down
Loading

0 comments on commit ce20795

Please sign in to comment.