Compare commits
No commits in common. "master" and "v0.0.4" have entirely different histories.
4 changed files with 20 additions and 30 deletions
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
|
@ -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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
44
src/cashu.ts
44
src/cashu.ts
|
@ -1,6 +1,6 @@
|
||||||
import { CashuMint, CashuWallet, getDecodedToken, getEncodedToken } from "@cashu/cashu-ts";
|
import {getDecodedToken, type Proof} from "@cashu/cashu-ts";
|
||||||
import type { Token } from "@cashu/cashu-ts";
|
import type {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,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 token: Token;
|
public readonly tokens: 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);
|
||||||
|
const tokens = decodedTokenData.token;
|
||||||
|
const amount = tokens.flatMap(t => t.proofs).reduce((c, x) => c + x.amount, 0);
|
||||||
|
|
||||||
const amount = token.proofs.reduce((c, x) => c + x.amount, 0);
|
if(decodedTokenData.token.length === 0)
|
||||||
|
throw new InvalidTokenException('Invalid token format. We only accept tokens with at least one proof');
|
||||||
if (!TokenInfo.ALLOWED_MINTS.includes(token.mint))
|
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))
|
||||||
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.token = token;
|
this.tokens = tokens;
|
||||||
this.amount = amount;
|
this.amount = amount;
|
||||||
}
|
this.mint = tokens[0].mint;
|
||||||
|
this.proofs = tokens.flatMap(t => t.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
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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";
|
Loading…
Add table
Reference in a new issue