Initial commit
This commit is contained in:
commit
04aa38bbe4
10 changed files with 354 additions and 0 deletions
50
src/cashu.ts
Normal file
50
src/cashu.ts
Normal file
|
@ -0,0 +1,50 @@
|
|||
import {getDecodedToken} from "@cashu/cashu-ts";
|
||||
import type {Proof, TokenEntry} from "@cashu/cashu-ts";
|
||||
import {getMailSubscriptionDurationForSats} from "./general.ts";
|
||||
|
||||
class InvalidTokenException extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'InvalidTokenException';
|
||||
}
|
||||
}
|
||||
|
||||
export class TokenInfo {
|
||||
private static readonly ALLOWED_MINTS: readonly string[] = [
|
||||
'https://mint.minibits.cash/Bitcoin',
|
||||
'https://stablenut.umint.cash',
|
||||
'https://mint.lnvoltz.com',
|
||||
'https://mint.coinos.io',
|
||||
'https://mint.lnwallet.app',
|
||||
'https://mint.0xchat.com'
|
||||
] as const;
|
||||
private static readonly MIN_AMOUNT = 21 as const;
|
||||
|
||||
public readonly token: TokenEntry;
|
||||
public readonly amount: number;
|
||||
public readonly mint: string;
|
||||
public readonly proofs: Proof[];
|
||||
|
||||
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))
|
||||
throw new InvalidTokenException('Unsupported mint');
|
||||
this.mint = this.token.mint;
|
||||
this.proofs = this.token.proofs;
|
||||
}
|
||||
}
|
||||
|
||||
export class TokenInfoWithMailSubscriptionDuration extends TokenInfo {
|
||||
public readonly duration: number;
|
||||
|
||||
constructor(tokenString: string) {
|
||||
super(tokenString);
|
||||
this.duration = getMailSubscriptionDurationForSats(this.amount);
|
||||
}
|
||||
}
|
34
src/email.ts
Normal file
34
src/email.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
/**
|
||||
* Parses a raw email string into its constituent parts, including subject, headers, and body.
|
||||
*
|
||||
* @param emailText The raw email text to parse.
|
||||
* @returns An object containing the extracted email components.
|
||||
*/
|
||||
export function parseEmail(emailText: string) {
|
||||
const lines = emailText.split('\n');
|
||||
const headers: { [key: string]: string } = {};
|
||||
let bodyLines = [];
|
||||
let isBody = false;
|
||||
|
||||
for (let line of lines) {
|
||||
if (!isBody) {
|
||||
if (line.trim() === '') {
|
||||
isBody = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
const colonIndex = line.indexOf(':');
|
||||
if (colonIndex !== -1) {
|
||||
const key = line.slice(0, colonIndex);
|
||||
headers[key] = line.slice(colonIndex + 1).trim();
|
||||
}
|
||||
} else
|
||||
bodyLines.push(line);
|
||||
}
|
||||
|
||||
return {
|
||||
subject: headers['Subject'] || 'No subject',
|
||||
headers,
|
||||
body: bodyLines.join('\n').trim()
|
||||
};
|
||||
}
|
10
src/general.ts
Normal file
10
src/general.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
export function randomTimeUpTo2DaysInThePast() {
|
||||
const now = Date.now();
|
||||
const twoDaysAgo = now - 2 * 24 * 60 * 60 * 1000 - 3600 * 1000; // 1 hour buffer in case of clock skew
|
||||
return Math.floor((Math.floor(Math.random() * (now - twoDaysAgo)) + twoDaysAgo) / 1000);
|
||||
}
|
||||
|
||||
export function getMailSubscriptionDurationForSats(n: number) {
|
||||
const twentyOneSatsToSeconds = 8640 / 21;
|
||||
return Math.floor(n * twentyOneSatsToSeconds);
|
||||
}
|
4
src/index.ts
Normal file
4
src/index.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
export * from "./cashu";
|
||||
export * from "./general";
|
||||
export * from "./nostr.ts";
|
||||
export * from "./email.ts";
|
27
src/nostr.ts
Normal file
27
src/nostr.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import NDK, {NDKEvent, NDKPrivateKeySigner, NDKUser} from "@nostr-dev-kit/ndk";
|
||||
import {generateSecretKey} from "nostr-tools";
|
||||
import {randomTimeUpTo2DaysInThePast} from "./general.ts";
|
||||
|
||||
export async function encryptEventForRecipient(
|
||||
ndk: NDK,
|
||||
event: NDKEvent,
|
||||
recipient: NDKUser
|
||||
): Promise<NDKEvent> {
|
||||
await ndk.connect();
|
||||
let randomKey = generateSecretKey();
|
||||
const randomKeySinger = new NDKPrivateKeySigner(randomKey);
|
||||
const seal = new NDKEvent();
|
||||
seal.pubkey = recipient.pubkey;
|
||||
seal.kind = 13;
|
||||
seal.content = await ndk.signer!.nip44Encrypt(recipient, JSON.stringify(event));
|
||||
seal.created_at = randomTimeUpTo2DaysInThePast();
|
||||
await seal.sign(ndk.signer);
|
||||
const giftWrap = new NDKEvent();
|
||||
giftWrap.kind = 1059;
|
||||
giftWrap.created_at = randomTimeUpTo2DaysInThePast();
|
||||
giftWrap.content = await randomKeySinger.nip44Encrypt(recipient, JSON.stringify(seal));
|
||||
giftWrap.tags.push(['p', recipient.pubkey]);
|
||||
await giftWrap.sign(randomKeySinger);
|
||||
giftWrap.ndk = ndk;
|
||||
return giftWrap;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue