update to the latest version of cashu-ts

This commit is contained in:
Danny Morabito 2024-12-04 12:36:39 +01:00
parent c1d309ba09
commit d5e91a356d
Signed by: dannym
GPG key ID: 7CC8056A5A04557E
3 changed files with 20 additions and 22 deletions

BIN
bun.lockb

Binary file not shown.

View file

@ -17,7 +17,7 @@
"typescript": "^5.3.2" "typescript": "^5.3.2"
}, },
"dependencies": { "dependencies": {
"@cashu/cashu-ts": "^1.2.1", "@cashu/cashu-ts": "^2.0.0",
"@nostr-dev-kit/ndk": "^2.10.7" "@nostr-dev-kit/ndk": "^2.10.7"
} }
} }

View file

@ -1,5 +1,5 @@
import {CashuMint, CashuWallet, getDecodedToken, getEncodedToken, type Proof} from "@cashu/cashu-ts"; import { CashuMint, CashuWallet, getDecodedToken, getEncodedToken } from "@cashu/cashu-ts";
import type {TokenEntry} from "@cashu/cashu-ts"; import type { Token } from "@cashu/cashu-ts";
import { getMailSubscriptionDurationForSats } from "./general.ts"; import { getMailSubscriptionDurationForSats } from "./general.ts";
class InvalidTokenException extends Error { class InvalidTokenException extends Error {
@ -20,29 +20,29 @@ export class TokenInfo {
] as const; ] as const;
private static readonly MIN_AMOUNT = 21 as const; private static readonly MIN_AMOUNT = 21 as const;
public readonly tokens: TokenEntry[]; public readonly token: Token;
public readonly amount: number; public readonly amount: number;
public readonly mint: string;
public readonly proofs: Proof[]; get mint() {
return this.token.mint;
}
get proofs() {
return this.token.proofs;
}
constructor(protected readonly tokenString: string) { constructor(protected readonly tokenString: string) {
const decodedTokenData = getDecodedToken(tokenString); const token = getDecodedToken(tokenString);
const tokens = decodedTokenData.token;
const amount = tokens.flatMap(t => t.proofs).reduce((c, x) => c + x.amount, 0);
if(decodedTokenData.token.length === 0) const amount = token.proofs.reduce((c, x) => c + x.amount, 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)) if (!TokenInfo.ALLOWED_MINTS.includes(token.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'); throw new InvalidTokenException('Unsupported mint');
if (amount < TokenInfo.MIN_AMOUNT) if (amount < TokenInfo.MIN_AMOUNT)
throw new InvalidTokenException(`Invalid amount. Minimum required: ${TokenInfo.MIN_AMOUNT} sats`); throw new InvalidTokenException(`Invalid amount. Minimum required: ${TokenInfo.MIN_AMOUNT} sats`);
this.tokens = tokens; this.token = token;
this.amount = amount; this.amount = amount;
this.mint = tokens[0].mint;
this.proofs = tokens.flatMap(t => t.proofs);
} }
async receive(): Promise<string> { async receive(): Promise<string> {
@ -50,10 +50,8 @@ export class TokenInfo {
const wallet = new CashuWallet(mint); const wallet = new CashuWallet(mint);
const newToken = await wallet.receive(this.tokenString); const newToken = await wallet.receive(this.tokenString);
return getEncodedToken({ return getEncodedToken({
token: [{
mint: this.mint, mint: this.mint,
proofs: newToken proofs: newToken
}]
}); });
} }
} }