diff --git a/bun.lockb b/bun.lockb index 4efdcff..9e97eb2 100755 Binary files a/bun.lockb and b/bun.lockb differ 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 6321261..a3080b8 100644 --- a/src/cashu.ts +++ b/src/cashu.ts @@ -1,6 +1,6 @@ -import {getDecodedToken} from "@cashu/cashu-ts"; -import type {Proof, 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,23 +20,39 @@ export class TokenInfo { ] as const; private static readonly MIN_AMOUNT = 21 as const; - public readonly token: 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); - 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 token = getDecodedToken(tokenString); + + const amount = token.proofs.reduce((c, x) => c + x.amount, 0); + + if (!TokenInfo.ALLOWED_MINTS.includes(token.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.token = token; + this.amount = amount; + } + + async receive(): Promise { + const mint = new CashuMint(this.mint); + const wallet = new CashuWallet(mint); + const newToken = await wallet.receive(this.tokenString); + return getEncodedToken({ + mint: this.mint, + proofs: newToken + }); } } diff --git a/src/index.ts b/src/index.ts index 08492a2..6549f29 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -export * from "./cashu"; -export * from "./general"; +export * from "./cashu.ts"; +export * from "./general.ts"; export * from "./nostr.ts"; export * from "./email.ts"; \ No newline at end of file diff --git a/src/nostr.ts b/src/nostr.ts index 4b4cd80..af68454 100644 --- a/src/nostr.ts +++ b/src/nostr.ts @@ -1,6 +1,7 @@ import NDK, {NDKEvent, NDKPrivateKeySigner, NDKUser} from "@nostr-dev-kit/ndk"; import {generateSecretKey} from "nostr-tools"; import {randomTimeUpTo2DaysInThePast} from "./general.ts"; +import {decode as nip19Decode, npubEncode} from "nostr-tools/nip19"; export async function encryptEventForRecipient( ndk: NDK, @@ -25,3 +26,14 @@ export async function encryptEventForRecipient( giftWrap.ndk = ndk; return giftWrap; } + +export function npubToPubKeyString(npub: string) { + const decoded = nip19Decode(npub); + if (decoded.type !== 'npub') + throw new Error('Invalid npub'); + return decoded.data; +} + +export function pubKeyStringToNpub(pubKey: string) { + return npubEncode(pubKey); +}