Allow both multiple tokens encoded as one and a singular token, for cashu
This commit is contained in:
parent
c1d309ba09
commit
bc58250102
1 changed files with 5 additions and 5 deletions
10
src/cashu.ts
10
src/cashu.ts
|
@ -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');
|
||||
|
|
Loading…
Reference in a new issue