From d5e91a356d48016753f526930d4a50863641e8ce Mon Sep 17 00:00:00 2001 From: Danny Morabito Date: Wed, 4 Dec 2024 12:36:39 +0100 Subject: [PATCH] update to the latest version of cashu-ts --- bun.lockb | Bin 21694 -> 21670 bytes package.json | 2 +- src/cashu.ts | 40 +++++++++++++++++++--------------------- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/bun.lockb b/bun.lockb index 4efdcff19ba4380ac5039e6b1d47059fdd1277ba..9e97eb213ca55bf068745608163bd7eda95ede7c 100755 GIT binary patch delta 965 zcmcJN{Y#Tk6vv;tx#^snF49ucmFRrQZI3$hWxni9b1Al$)ESj%PFi7@WoTlW7zDX= zosa@8vJxc{;`u?5l7b?z1S6@e$ciF=3BeN7Y<4aw_z!g8!+E~vo^#K0&%I-Vq<@ff zsdm&VYZqTFTbH|U4te2Drgk*I&DxJ%>T5o7@^*JXwj|{KUJp@L)>l_M>lnu6bUIU0 zG|Y^cVMGkm>hNWl0QA>UOVC$Q%}IDb*r#SwN9>u$o$bZ-UYp#sSZeEGymQOr_2UN? z!v`AtS<}MYNQ=mFc6K`7`s{Sbtx3L^@IWR^72U<%G`O1(JU*#{Sya1V!e2`wz#Nn# zaA3o9a7(ok4}NJek3r*uOt}+uK7PCx0k9`9o4XJ+#XBg#6%)ck@(^kpJs-6hp?O6J z&9fi`v94+Rn%;+=8ufI&m|Kz^CjYrbWAIn0uJ>C>)v+(Sit_1DSBWR$-fFVG>*c4W zpI6hC=EY%64yke5yXx~ZxCEb^VnWr>_+H$(EMo@1ZwpD?E%YAW9G^ zO2H8=ff_{tQA1gDDflTQh1Y&BWM@aJ?QbUX|7iL|$6X)%xcG{tC(?nIv=v>sc|!Q{ zTt>Y)YUH`2%+lE5?AiUA+3dbN+UfpL^F1}G9mhc!Z=e@_B9Q1(ZbZ>4BRWV=n+vAn z%t3UB9x)(B(5J`5(E;0#0)$>tE|P^Pkz^zg2}11PPH!RYoFqd|^ehJAkXS^GBp@n; z7Olq>YjL`sxFJq!6mUW-C;P#pjiHwh!`gg7l8RNaoKI$$Fm(MPufI+nImD`xRT>85 znIRBvl*5!E3`PxMycsb(GA2QlZWFW_GaY77Al{k)eA^I6ibCMCH4;j!p?vHQ6-C=I<_|qq NHRRZZ+_24;_ZLI+?fC!z delta 991 zcmcJNUr1AN6vywq+nn3H{#VqjmAI*-n{%el%|C4u|D?_R$*EXrI@1TKMC}hXhe4Ss zXvh9QXqI4_AlOaxK|`phl)y}}@%fQeJLh~kzjJ^0^z@OB z`^Z+=o@t-X^9}Z){^^RJCCWPcy6NhT_A!2CQ)KdjRkG%LQZ{I4BcnP+1VIyoql`}w zZs_y3{IS~da$7Y)RN8E|_OnEC&p$fXmM8y^6?alG8bs$#`CUY z6bG+_YH|{;c%(3mo-@>87wnLR@Q^IF8d<3k8*tY_mq zX2YwL{LfdM_519e$~SbrDI28;P56#Ff9kTMT>hvgysi55;7y+w{PYLw-wYWG-9^t< zPA$&OwXaKq?FPTh;~P(>%y%cu_N2l%=m-tSp6$7KA>(84?2@!s({W<>lV!5tXx{wz zvSs#V?tQ*I+i{y(UR*hs88QF$;gc}s`&&a5#bds&hYP+soFeW)b%AqgR3a(NbXp9t4Ah9wdQim6*169ImAp5K-NGh@)(IPrT4@s&p3H!)q zg#8)0ND>l-C=n0D6R|*xs-A3Q=tMD@CdY&fVMUQh1QL#H&pUH$Ub;=go)t(mRH?;e z4Ya7kU__nHQ^;au3Wlo*a)A(?JV7bL+nqb@TvUoY!pdR@!nyU%RmMs*R^kZ|Yy2Tc zD~3zjAkgUicKy52UpZIneN7hvGunexvm4yg#=u)$gJ4{blcE(vsa^-Y`Yu5$yUA!$ v#+WuL`ZG0n8zUjq6v)8=;Dcc|6q{6t5Ps#j0c8q-Wa9t~m|d7VCO-8WiYoBY diff --git a/package.json b/package.json index 4b41963..83e53d3 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "typescript": "^5.3.2" }, "dependencies": { - "@cashu/cashu-ts": "^1.2.1", + "@cashu/cashu-ts": "^2.0.0", "@nostr-dev-kit/ndk": "^2.10.7" } } diff --git a/src/cashu.ts b/src/cashu.ts index fa303f7..a3080b8 100644 --- a/src/cashu.ts +++ b/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 } from "@cashu/cashu-ts"; +import type { Token } from "@cashu/cashu-ts"; +import { getMailSubscriptionDurationForSats } from "./general.ts"; class InvalidTokenException extends Error { constructor(message: string) { @@ -20,29 +20,29 @@ export class TokenInfo { ] as const; private static readonly MIN_AMOUNT = 21 as const; - public readonly tokens: TokenEntry[]; + public readonly token: Token; 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) { - const decodedTokenData = getDecodedToken(tokenString); - const tokens = decodedTokenData.token; - const amount = tokens.flatMap(t => t.proofs).reduce((c, x) => c + x.amount, 0); + const token = getDecodedToken(tokenString); - 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)) + const amount = token.proofs.reduce((c, x) => c + x.amount, 0); + + if (!TokenInfo.ALLOWED_MINTS.includes(token.mint)) throw new InvalidTokenException('Unsupported mint'); if (amount < TokenInfo.MIN_AMOUNT) throw new InvalidTokenException(`Invalid amount. Minimum required: ${TokenInfo.MIN_AMOUNT} sats`); - this.tokens = tokens; + this.token = token; this.amount = amount; - this.mint = tokens[0].mint; - this.proofs = tokens.flatMap(t => t.proofs); } async receive(): Promise { @@ -50,10 +50,8 @@ export class TokenInfo { const wallet = new CashuWallet(mint); const newToken = await wallet.receive(this.tokenString); return getEncodedToken({ - token: [{ - mint: this.mint, - proofs: newToken - }] + mint: this.mint, + proofs: newToken }); } }