Compare commits

..

No commits in common. "master" and "v0.0.1" have entirely different histories.

5 changed files with 19 additions and 47 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": "^2.0.0", "@cashu/cashu-ts": "^1.2.1",
"@nostr-dev-kit/ndk": "^2.10.7" "@nostr-dev-kit/ndk": "^2.10.7"
} }
} }

View file

@ -1,6 +1,6 @@
import { CashuMint, CashuWallet, getDecodedToken, getEncodedToken } from "@cashu/cashu-ts"; import {getDecodedToken} from "@cashu/cashu-ts";
import type { Token } from "@cashu/cashu-ts"; import type {Proof, TokenEntry} from "@cashu/cashu-ts";
import { getMailSubscriptionDurationForSats } from "./general.ts"; import {getMailSubscriptionDurationForSats} from "./general.ts";
class InvalidTokenException extends Error { class InvalidTokenException extends Error {
constructor(message: string) { constructor(message: string) {
@ -20,39 +20,23 @@ 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 token: Token; public readonly token: TokenEntry;
public readonly amount: number; public readonly amount: number;
public readonly mint: string;
get mint() { public readonly proofs: Proof[];
return this.token.mint;
}
get proofs() {
return this.token.proofs;
}
constructor(protected readonly tokenString: string) { constructor(protected readonly tokenString: string) {
const token = getDecodedToken(tokenString); const decodedTokenData = getDecodedToken(tokenString);
if (decodedTokenData.unit !== 'sat' || decodedTokenData.token.length !== 1)
const amount = token.proofs.reduce((c, x) => c + x.amount, 0); throw new InvalidTokenException('Invalid token format. We only accept a single token denominated in sats');
this.token = decodedTokenData.token[0];
if (!TokenInfo.ALLOWED_MINTS.includes(token.mint)) this.amount = this.token.proofs.reduce((c, x) => c + x.amount, 0);
throw new InvalidTokenException('Unsupported mint'); if (this.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`);
if (!TokenInfo.ALLOWED_MINTS.includes(this.token.mint))
this.token = token; throw new InvalidTokenException('Unsupported mint');
this.amount = amount; this.mint = this.token.mint;
} this.proofs = this.token.proofs;
async receive(): Promise<string> {
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
});
} }
} }

View file

@ -1,4 +1,4 @@
export * from "./cashu.ts"; export * from "./cashu";
export * from "./general.ts"; export * from "./general";
export * from "./nostr.ts"; export * from "./nostr.ts";
export * from "./email.ts"; export * from "./email.ts";

View file

@ -1,7 +1,6 @@
import NDK, {NDKEvent, NDKPrivateKeySigner, NDKUser} from "@nostr-dev-kit/ndk"; import NDK, {NDKEvent, NDKPrivateKeySigner, NDKUser} from "@nostr-dev-kit/ndk";
import {generateSecretKey} from "nostr-tools"; import {generateSecretKey} from "nostr-tools";
import {randomTimeUpTo2DaysInThePast} from "./general.ts"; import {randomTimeUpTo2DaysInThePast} from "./general.ts";
import {decode as nip19Decode, npubEncode} from "nostr-tools/nip19";
export async function encryptEventForRecipient( export async function encryptEventForRecipient(
ndk: NDK, ndk: NDK,
@ -26,14 +25,3 @@ export async function encryptEventForRecipient(
giftWrap.ndk = ndk; giftWrap.ndk = ndk;
return giftWrap; 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);
}