Skip to content

Commit

Permalink
feat: error codes and pubkey in check quote
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Nov 15, 2024
1 parent 9a159bd commit ad47123
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 14 deletions.
9 changes: 9 additions & 0 deletions crates/cdk/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ impl From<Error> for ErrorResponse {
error: Some(err.to_string()),
detail: None,
},
Error::NUT19(err) => ErrorResponse {
code: ErrorCode::WitnessMissingOrInvalid,
error: Some(err.to_string()),
detail: None,
},
_ => ErrorResponse {
code: ErrorCode::Unknown(9999),
error: Some(err.to_string()),
Expand Down Expand Up @@ -448,6 +453,8 @@ pub enum ErrorCode {
TransactionUnbalanced,
/// Amount outside of allowed range
AmountOutofLimitRange,
/// Witness missing or invalid
WitnessMissingOrInvalid,
/// Unknown error code
Unknown(u16),
}
Expand All @@ -472,6 +479,7 @@ impl ErrorCode {
20005 => Self::QuotePending,
20006 => Self::InvoiceAlreadyPaid,
20007 => Self::QuoteExpired,
20008 => Self::WitnessMissingOrInvalid,
_ => Self::Unknown(code),
}
}
Expand All @@ -495,6 +503,7 @@ impl ErrorCode {
Self::QuotePending => 20005,
Self::InvoiceAlreadyPaid => 20006,
Self::QuoteExpired => 20007,
Self::WitnessMissingOrInvalid => 20008,
Self::Unknown(code) => *code,
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/cdk/src/mint/mint_nut04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ impl Mint {
request: quote.request,
state,
expiry: Some(quote.expiry),
pubkey: quote.pubkey,
})
}

Expand Down Expand Up @@ -282,7 +283,7 @@ impl Mint {
// If the there is a public key provoided in mint quote request
// verify the signature is provided for the mint request
if let Some(pubkey) = mint_quote.pubkey {
mint_request.verify(pubkey)?;
mint_request.verify_witness(pubkey)?;
}

let blinded_messages: Vec<PublicKey> = mint_request
Expand Down
4 changes: 4 additions & 0 deletions crates/cdk/src/nuts/nut04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ pub struct MintQuoteBolt11Response {
pub state: MintQuoteState,
/// Unix timestamp until the quote is valid
pub expiry: Option<u64>,
/// NUT-19 Pubkey
#[serde(skip_serializing_if = "Option::is_none")]
pub pubkey: Option<PublicKey>,
}

#[cfg(feature = "mint")]
Expand All @@ -104,6 +107,7 @@ impl From<crate::mint::MintQuote> for MintQuoteBolt11Response {
request: mint_quote.request,
state: mint_quote.state,
expiry: Some(mint_quote.expiry),
pubkey: mint_quote.pubkey,
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions crates/cdk/src/nuts/nut19.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use super::{MintBolt11Request, PublicKey, SecretKey};
/// Nut19 Error
#[derive(Debug, Error)]
pub enum Error {
/// Quote witness not provided
#[error("Quote Witness not provided")]
QuoteWitnessMissing,
/// Witness not provided
#[error("Witness not provided")]
WitnessMissing,
/// Quote witness invalid signature
#[error("Quote witness invalid signature")]
InvalidWitness,
Expand Down Expand Up @@ -53,8 +53,8 @@ impl MintBolt11Request {
}

/// Verify signature on [`MintBolt11Request`]
pub fn verify(&self, pubkey: PublicKey) -> Result<(), Error> {
let witness = self.witness.as_ref().ok_or(Error::QuoteWitnessMissing)?;
pub fn verify_witness(&self, pubkey: PublicKey) -> Result<(), Error> {
let witness = self.witness.as_ref().ok_or(Error::WitnessMissing)?;

let signature = Signature::from_str(witness).map_err(|_| Error::InvalidWitness)?;

Expand Down Expand Up @@ -91,7 +91,7 @@ mod tests {

let request: MintBolt11Request = serde_json::from_str(r#"{"quote":"9d745270-1405-46de-b5c5-e2762b4f5e00","outputs":[{"amount":1,"id":"00456a94ab4e1c46","B_":"0342e5bcc77f5b2a3c2afb40bb591a1e27da83cddc968abdc0ec4904201a201834"},{"amount":1,"id":"00456a94ab4e1c46","B_":"032fd3c4dc49a2844a89998d5e9d5b0f0b00dde9310063acb8a92e2fdafa4126d4"},{"amount":1,"id":"00456a94ab4e1c46","B_":"033b6fde50b6a0dfe61ad148fff167ad9cf8308ded5f6f6b2fe000a036c464c311"},{"amount":1,"id":"00456a94ab4e1c46","B_":"02be5a55f03e5c0aaea77595d574bce92c6d57a2a0fb2b5955c0b87e4520e06b53"},{"amount":1,"id":"00456a94ab4e1c46","B_":"02209fc2873f28521cbdde7f7b3bb1521002463f5979686fd156f23fe6a8aa2b79"}], "witness": "d4b386f21f7aa7172f0994ee6e4dd966539484247ea71c99b81b8e09b1bb2acbc0026a43c221fd773471dc30d6a32b04692e6837ddaccf0830a63128308e4ee0"}"#).unwrap();

assert!(request.verify(pubkey).is_ok());
assert!(request.verify_witness(pubkey).is_ok());
}

#[test]
Expand All @@ -104,7 +104,7 @@ mod tests {

request.sign(secret.clone()).unwrap();

assert!(request.verify(secret.public_key()).is_ok());
assert!(request.verify_witness(secret.public_key()).is_ok());
}

#[test]
Expand All @@ -117,6 +117,6 @@ mod tests {
let request: MintBolt11Request = serde_json::from_str(r#"{"quote":"9d745270-1405-46de-b5c5-e2762b4f5e00","outputs":[{"amount":1,"id":"00456a94ab4e1c46","B_":"0342e5bcc77f5b2a3c2afb40bb591a1e27da83cddc968abdc0ec4904201a201834"},{"amount":1,"id":"00456a94ab4e1c46","B_":"032fd3c4dc49a2844a89998d5e9d5b0f0b00dde9310063acb8a92e2fdafa4126d4"},{"amount":1,"id":"00456a94ab4e1c46","B_":"033b6fde50b6a0dfe61ad148fff167ad9cf8308ded5f6f6b2fe000a036c464c311"},{"amount":1,"id":"00456a94ab4e1c46","B_":"02be5a55f03e5c0aaea77595d574bce92c6d57a2a0fb2b5955c0b87e4520e06b53"},{"amount":1,"id":"00456a94ab4e1c46","B_":"02209fc2873f28521cbdde7f7b3bb1521002463f5979686fd156f23fe6a8aa2b79"}],"witness":"cb2b8e7ea69362dfe2a07093f2bbc319226db33db2ef686c940b5ec976bcbfc78df0cd35b3e998adf437b09ee2c950bd66dfe9eb64abd706e43ebc7c669c36c3"}"#).unwrap();

// Signature is on a different quote id verification should fail
assert!(request.verify(pubkey).is_err());
assert!(request.verify_witness(pubkey).is_err());
}
}
8 changes: 4 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
description = "CDK Flake";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

rust-overlay = {
url = "github:oxalica/rust-overlay";
Expand Down

0 comments on commit ad47123

Please sign in to comment.