diff --git a/src/cashu.ts b/src/cashu.ts index 6321261..9a81472 100644 --- a/src/cashu.ts +++ b/src/cashu.ts @@ -1,5 +1,5 @@ -import {getDecodedToken} from "@cashu/cashu-ts"; -import type {Proof, TokenEntry} from "@cashu/cashu-ts"; +import {getDecodedToken, type Proof} from "@cashu/cashu-ts"; +import type {TokenEntry} from "@cashu/cashu-ts"; import {getMailSubscriptionDurationForSats} from "./general.ts"; class InvalidTokenException extends Error { @@ -20,23 +20,31 @@ export class TokenInfo { ] as const; private static readonly MIN_AMOUNT = 21 as const; - public readonly token: TokenEntry; + public readonly tokens: TokenEntry[]; public readonly amount: number; public readonly mint: string; public readonly proofs: Proof[]; constructor(protected readonly tokenString: string) { const decodedTokenData = getDecodedToken(tokenString); - if (decodedTokenData.unit !== 'sat' || decodedTokenData.token.length !== 1) - throw new InvalidTokenException('Invalid token format. We only accept a single token denominated in sats'); - this.token = decodedTokenData.token[0]; - this.amount = this.token.proofs.reduce((c, x) => c + x.amount, 0); - if (this.amount < TokenInfo.MIN_AMOUNT) - throw new InvalidTokenException(`Invalid amount. Minimum required: ${TokenInfo.MIN_AMOUNT} sats`); - if (!TokenInfo.ALLOWED_MINTS.includes(this.token.mint)) + const tokens = decodedTokenData.token; + const amount = tokens.flatMap(t => t.proofs).reduce((c, x) => c + x.amount, 0); + + if (decodedTokenData.unit !== 'sat') + throw new InvalidTokenException('Invalid token format. We only accept tokens denominated in sats'); + if(decodedTokenData.token.length === 0) + throw new InvalidTokenException('Invalid token format. We only accept tokens with at least one proof'); + if (tokens.slice(1).some(t => t.mint !== tokens[0].mint)) + throw new InvalidTokenException('Invalid token format. We only accept tokens with the same mint'); + if (!TokenInfo.ALLOWED_MINTS.includes(tokens[0].mint)) throw new InvalidTokenException('Unsupported mint'); - this.mint = this.token.mint; - this.proofs = this.token.proofs; + if (amount < TokenInfo.MIN_AMOUNT) + throw new InvalidTokenException(`Invalid amount. Minimum required: ${TokenInfo.MIN_AMOUNT} sats`); + + this.tokens = tokens; + this.amount = amount; + this.mint = tokens[0].mint; + this.proofs = tokens.flatMap(t => t.proofs); } }