Skip to content

Commit

Permalink
feat: remove multimint support
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Oct 25, 2024
1 parent c91297a commit 4e6bf59
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 167 deletions.
2 changes: 1 addition & 1 deletion crates/cdk-cli/src/sub_commands/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ async fn receive_token(
) -> Result<Amount> {
let token: Token = Token::from_str(token_str)?;

let mint_url = token.proofs().into_keys().next().expect("Mint in token");
let mint_url = token.mint_url()?;

let wallet_key = WalletKey::new(mint_url.clone(), token.unit().unwrap_or_default());

Expand Down
113 changes: 62 additions & 51 deletions crates/cdk/src/nuts/nut00/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Token {
}

/// Proofs in [`Token`]
pub fn proofs(&self) -> HashMap<MintUrl, Proofs> {
pub fn proofs(&self) -> Proofs {
match self {
Self::TokenV3(token) => token.proofs(),
Self::TokenV4(token) => token.proofs(),
Expand All @@ -92,11 +92,27 @@ impl Token {
/// Unit
pub fn unit(&self) -> Option<CurrencyUnit> {
match self {
Self::TokenV3(token) => token.unit(),
Self::TokenV3(token) => *token.unit(),
Self::TokenV4(token) => Some(token.unit()),
}
}

/// Mint url
pub fn mint_url(&self) -> Result<MintUrl, Error> {
match self {
Self::TokenV3(token) => {
let mint_urls = token.mint_urls();

if mint_urls.len() != 1 {
return Err(Error::UnsupportedToken);
}

Ok(mint_urls.first().expect("Length is checked above").clone())
}
Self::TokenV4(token) => Ok(token.mint_url.clone()),
}
}

/// To v3 string
pub fn to_v3_string(&self) -> String {
let v3_token = match self {
Expand Down Expand Up @@ -187,24 +203,17 @@ impl TokenV3 {
})
}

fn proofs(&self) -> HashMap<MintUrl, Proofs> {
let mut proofs: HashMap<MintUrl, Proofs> = HashMap::new();

for token in self.token.clone() {
let mint_url = token.mint;
let mut mint_proofs = token.proofs;

proofs
.entry(mint_url)
.and_modify(|p| p.append(&mut mint_proofs))
.or_insert(mint_proofs);
}

proofs
/// Proofs
pub fn proofs(&self) -> Proofs {
self.token
.iter()
.flat_map(|token| token.proofs.clone())
.collect()
}

/// Value
#[inline]
fn value(&self) -> Result<Amount, Error> {
pub fn value(&self) -> Result<Amount, Error> {
Ok(Amount::try_sum(
self.token
.iter()
Expand All @@ -213,14 +222,27 @@ impl TokenV3 {
)?)
}

/// Memo
#[inline]
fn memo(&self) -> &Option<String> {
pub fn memo(&self) -> &Option<String> {
&self.memo
}

/// Unit
#[inline]
fn unit(&self) -> Option<CurrencyUnit> {
self.unit
pub fn unit(&self) -> &Option<CurrencyUnit> {
&self.unit
}

/// Mint Url
pub fn mint_urls(&self) -> Vec<MintUrl> {
let mut mint_urls = Vec::new();

for token in self.token.iter() {
mint_urls.push(token.mint.clone());
}

mint_urls
}
}

Expand Down Expand Up @@ -249,13 +271,10 @@ impl fmt::Display for TokenV3 {

impl From<TokenV4> for TokenV3 {
fn from(token: TokenV4) -> Self {
let (mint_url, proofs) = token
.proofs()
.into_iter()
.next()
.expect("Token has no proofs");
let proofs = token.proofs();

TokenV3 {
token: vec![TokenV3Token::new(mint_url, proofs)],
token: vec![TokenV3Token::new(token.mint_url, proofs)],
memo: token.memo,
unit: Some(token.unit),
}
Expand All @@ -281,28 +300,16 @@ pub struct TokenV4 {

impl TokenV4 {
/// Proofs from token
pub fn proofs(&self) -> HashMap<MintUrl, Proofs> {
let mint_url = &self.mint_url;
let mut proofs: HashMap<MintUrl, Proofs> = HashMap::new();

for token in self.token.clone() {
let mut mint_proofs = token
.proofs
.iter()
.map(|p| p.into_proof(&token.keyset_id))
.collect();

proofs
.entry(mint_url.clone())
.and_modify(|p| p.append(&mut mint_proofs))
.or_insert(mint_proofs);
}

proofs
pub fn proofs(&self) -> Proofs {
self.token
.iter()
.flat_map(|token| token.proofs.iter().map(|p| p.into_proof(&token.keyset_id)))
.collect()
}

/// Value
#[inline]
fn value(&self) -> Result<Amount, Error> {
pub fn value(&self) -> Result<Amount, Error> {
Ok(Amount::try_sum(
self.token
.iter()
Expand All @@ -311,13 +318,15 @@ impl TokenV4 {
)?)
}

/// Memo
#[inline]
fn memo(&self) -> &Option<String> {
pub fn memo(&self) -> &Option<String> {
&self.memo
}

/// Unit
#[inline]
fn unit(&self) -> CurrencyUnit {
pub fn unit(&self) -> CurrencyUnit {
self.unit
}
}
Expand Down Expand Up @@ -350,13 +359,15 @@ impl TryFrom<TokenV3> for TokenV4 {
type Error = Error;
fn try_from(token: TokenV3) -> Result<Self, Self::Error> {
let proofs = token.proofs();
if proofs.len() != 1 {
let mint_urls = token.mint_urls();

if mint_urls.len() != 1 {
return Err(Error::UnsupportedToken);
}

let (mint_url, mint_proofs) = proofs.iter().next().expect("No proofs");
let mint_url = mint_urls.first().expect("Len is checked");

let proofs = mint_proofs
let proofs = proofs
.iter()
.fold(HashMap::new(), |mut acc, val| {
acc.entry(val.keyset_id)
Expand All @@ -369,7 +380,7 @@ impl TryFrom<TokenV3> for TokenV4 {
.collect();

Ok(TokenV4 {
mint_url: mint_url.to_owned(),
mint_url: mint_url.clone(),
token: proofs,
memo: token.memo,
unit: token.unit.ok_or(Error::UnsupportedUnit)?,
Expand Down
Loading

0 comments on commit 4e6bf59

Please sign in to comment.