Allow both multiple tokens encoded as one and a singular token, for cashu

This commit is contained in:
Danny Morabito 2024-12-04 12:10:53 +01:00
parent c1d309ba09
commit bc58250102
Signed by: dannym
GPG key ID: 7CC8056A5A04557E

View file

@ -1,6 +1,6 @@
import {CashuMint, CashuWallet, getDecodedToken, getEncodedToken, type Proof} from "@cashu/cashu-ts";
import type {TokenEntry} from "@cashu/cashu-ts";
import {getMailSubscriptionDurationForSats} from "./general.ts";
import { CashuMint, CashuWallet, getDecodedToken, getEncodedToken, type Proof } from "@cashu/cashu-ts";
import type { TokenEntry } from "@cashu/cashu-ts";
import { getMailSubscriptionDurationForSats } from "./general.ts";
class InvalidTokenException extends Error {
constructor(message: string) {
@ -27,10 +27,10 @@ export class TokenInfo {
constructor(protected readonly tokenString: string) {
const decodedTokenData = getDecodedToken(tokenString);
const tokens = decodedTokenData.token;
const tokens = Array.isArray(decodedTokenData.token) ? decodedTokenData.token : [decodedTokenData.token];
const amount = tokens.flatMap(t => t.proofs).reduce((c, x) => c + x.amount, 0);
if(decodedTokenData.token.length === 0)
if (tokens.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');