Skip to content

Commit

Permalink
fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Aug 2, 2024
1 parent cdb656f commit 36ea99b
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 50 deletions.
2 changes: 1 addition & 1 deletion ndk/src/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { NDKRelay } from "../relay/index.js";
import type { NDKFilter, NDKSubscription } from "../subscription/index.js";
import type { Hexpubkey, ProfilePointer } from "../user/index.js";
import type { NDKUserProfile } from "../user/profile.js";
import { NDKLnUrlData } from "../zapper/ln.js";
import type { NDKLnUrlData } from "../zapper/ln.js";

export interface NDKCacheAdapter {
/**
Expand Down
1 change: 0 additions & 1 deletion ndk/src/events/encode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ describe("event.encode", () => {
it("encodes all relays the event is known to be on", async () => {
const event = new NDKEvent(undefined);
event.kind = 1;
event.onRelays = [new NDKRelay("wss://relay1/"), new NDKRelay("wss://relay2/")];
await event.sign(NDKPrivateKeySigner.generate());

const encoded = event.encode();
Expand Down
10 changes: 6 additions & 4 deletions ndk/src/events/kinds/nutzap/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import debug from "debug";
import NDK, { Hexpubkey, NDKEvent, NDKKind, NDKUser, NostrEvent } from "../../../index.js";
import { Proof } from "./proof.js";
import type { Hexpubkey, NostrEvent } from "../../../index.js";
import type NDK from "../../../index.js";
import { NDKEvent, NDKKind, NDKUser } from "../../../index.js";
import type { Proof } from "./proof.js";

/**
* Represents a NIP-61 nutzap
Expand Down Expand Up @@ -116,7 +118,7 @@ export class NDKNutzap extends NDKEvent {
return this.tagValue("unit") ?? "msat";
}

set unit(value: string | undefined) {
set unit(value: string | undefined) {
this.removeTag("unit");
if (value) this.tag(["unit", value]);
}
Expand Down Expand Up @@ -177,4 +179,4 @@ export class NDKNutzap extends NDKEvent {
this.proofs.length > 0
);
}
}
}
20 changes: 16 additions & 4 deletions ndk/src/ndk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@ import { Queue } from "./queue/index.js";
import { signatureVerificationInit } from "../events/signature.js";
import { NDKSubscriptionManager } from "../subscription/manager.js";
import { setActiveUser } from "./active-user.js";
import { CashuPayCb, LnPayCb, NDKPaymentConfirmation, NDKZapConfirmation, NDKZapper, NDKZapSplit } from "../zapper/index.js";
import type {
CashuPayCb,
LnPayCb,
NDKPaymentConfirmation,
NDKZapSplit} from "../zapper/index.js";
import {
NDKZapConfirmation,
NDKZapper
} from "../zapper/index.js";
import type { NostrEvent } from "nostr-tools";
import { NDKLnUrlData } from "../zapper/ln.js";
import type { NDKLnUrlData } from "../zapper/ln.js";

export type NDKValidationRatioFn = (
relay: NDKRelay,
Expand All @@ -38,7 +46,9 @@ export type NDKValidationRatioFn = (
export interface NDKWalletConfig {
onLnPay?: LnPayCb;
onCashuPay?: CashuPayCb;
onPaymentComplete: (results: Map<NDKZapSplit, NDKPaymentConfirmation | Error | undefined>) => void;
onPaymentComplete: (
results: Map<NDKZapSplit, NDKPaymentConfirmation | Error | undefined>
) => void;
}

export interface NDKConstructorParams {
Expand Down Expand Up @@ -737,7 +747,9 @@ export class NDK extends EventEmitter<{
tags?: NDKTag[];
onLnPay?: LnPayCb | false;
onCashuPay?: CashuPayCb | false;
onComplete?: (results: Map<NDKZapSplit, NDKPaymentConfirmation | Error | undefined>) => void;
onComplete?: (
results: Map<NDKZapSplit, NDKPaymentConfirmation | Error | undefined>
) => void;
signer?: NDKSigner;
}
): NDKZapper {
Expand Down
10 changes: 8 additions & 2 deletions ndk/src/relay/connectivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ export class NDKRelayConnectivity {
case "OK": {
const ok: boolean = data[2];
const reason: string = data[3];
const ep = this.openEventPublishes.get(id) as EventPublishResolver[] | undefined;
const ep = this.openEventPublishes.get(id) as
| EventPublishResolver[]
| undefined;
const firstEp = ep?.pop();

if (!ep || !firstEp) {
Expand Down Expand Up @@ -506,7 +508,11 @@ export class NDKRelayConnectivity {
const ret = new Promise<string>((resolve, reject) => {
const val = this.openEventPublishes.get(event.id!) ?? [];
if (val.length > 0) {
console.warn("Duplicate event publishing detected, you are publishing event "+event.id!+" twice");
console.warn(
"Duplicate event publishing detected, you are publishing event " +
event.id! +
" twice"
);
}

val.push({ resolve, reject });
Expand Down
1 change: 0 additions & 1 deletion ndk/src/relay/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ describe("NDKRelay", () => {
it("creates a new NDKRelay", () => {
expect(relay).toBeInstanceOf(NDKRelay);
expect(relay.url).toBe("ws://localhost/");
expect(relay.activeSubscriptions.length).toBe(0);
expect(relay.status).toEqual(NDKRelayStatus.DISCONNECTED);
});
});
Expand Down
4 changes: 0 additions & 4 deletions ndk/src/relay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,6 @@ export class NDKRelay extends EventEmitter<{
return [["r", this.url]];
}

// public activeSubscriptions(): Map<NDKFilter[], NDKSubscription[]> {
// // return this.subs.executedFilters();
// }

public addValidatedEvent(): void {
this.validatedEventCount++;
}
Expand Down
7 changes: 4 additions & 3 deletions ndk/src/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import type {
NDKZapMethodInfo,
} from "../index.js";
import { NDKCashuMintList } from "../events/kinds/nutzap/mint-list.js";
import type {
LNPaymentRequest,
NDKLnUrlData} from "../zapper/ln.js";
import {
getNip57ZapSpecFromLud,
LnPaymentInfo,
LNPaymentRequest,
NDKLnUrlData,
LnPaymentInfo
} from "../zapper/ln.js";

export type Hexpubkey = string;
Expand Down
24 changes: 14 additions & 10 deletions ndk/src/zapper/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { NostrEvent } from "nostr-tools";
import type { NDKZapDetails} from ".";
import { NDKZapper } from ".";
import { NDKEvent } from "../events";
import { NDKCashuMintList } from "../events/kinds/nutzap/mint-list";
import { NDK } from "../ndk";
import { NDKPrivateKeySigner } from "../signers/private-key";
import type { NDKUser } from "../user";
import type { CashuPaymentInfo } from "./nip61";
import type { LnPaymentInfo } from "./ln";

jest.mock("./ln.js", () => ({
getNip57ZapSpecFromLud: jest.fn(async () => {
Expand Down Expand Up @@ -91,10 +94,10 @@ describe("getZapMethod", () => {
await mintList.sign(signer);
ndk.fetchEvents = jest.fn().mockResolvedValue(new Set([mintList]));
const zapper = new NDKZapper(user, 1000);
zapper.onNutPay = async (payment: NDKZapPaymentDetails): Promise<false> => false;
const zapMethod = await zapper.getZapMethod(ndk, user.pubkey);
zapper.onCashuPay = async (payment: NDKZapDetails<CashuPaymentInfo>) => undefined;
const zapMethod = (await zapper.getZapMethods(ndk, user.pubkey))[0];
expect(zapMethod.type).toBe("nip61");
expect((zapMethod.data as NutPaymentInfo).mints).toEqual([
expect((zapMethod.data as CashuPaymentInfo).mints).toEqual([
"https://mint1",
"https://mint2",
]);
Expand All @@ -107,13 +110,14 @@ describe("getZapMethod", () => {
} as NostrEvent);
ndk.fetchEvents = jest.fn().mockResolvedValue(new Set<NDKEvent>([profile]));
const zapper = new NDKZapper(user, 1000);
zapper.onNutPay = async (payment: NDKZapPaymentDetails): Promise<false> => false;
zapper.onLnPay = async (payment: NDKZapPaymentDetails): Promise<NDKZapConfirmation> =>
false;
const zapMethod = await zapper.getZapMethod(
ndk,
"fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52"
);
zapper.onCashuPay = async (payment: NDKZapDetails<CashuPaymentInfo>) => undefined;
zapper.onLnPay = async (payment: NDKZapDetails<LnPaymentInfo>) => undefined;
const zapMethod = (
await zapper.getZapMethods(
ndk,
"fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52"
)
)[0];

expect(zapMethod.type).toBe("nip57");
});
Expand Down
31 changes: 20 additions & 11 deletions ndk/src/zapper/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { NDK } from "../ndk";
import type { NDKTag } from "../events";
import { NDKEvent } from "../events";
import type { NDKEvent } from "../events";
import type { Hexpubkey } from "../user";
import { NDKUser } from "../user";
import type { NDKSigner } from "../signers";
Expand All @@ -10,8 +10,8 @@ import { getRelayListForUsers } from "../utils/get-users-relay-list";
import { EventEmitter } from "tseep";
import { generateZapRequest } from "./nip57";
import { NDKNutzap } from "../events/kinds/nutzap";
import { LnPaymentInfo, NDKLnUrlData, NDKPaymentConfirmationLN, NDKZapConfirmationLN } from "./ln";
import { NDKZapConfirmationCashu, CashuPaymentInfo, NDKPaymentConfirmationCashu } from "./nip61";
import type { LnPaymentInfo, NDKLnUrlData, NDKPaymentConfirmationLN, NDKZapConfirmationLN } from "./ln";
import type { NDKZapConfirmationCashu, CashuPaymentInfo, NDKPaymentConfirmationCashu } from "./nip61";
import { NDKRelaySet } from "../relay/sets";

const d = createDebug("ndk:zapper");
Expand Down Expand Up @@ -75,7 +75,7 @@ export type LnPayCb = (
) => Promise<NDKPaymentConfirmationLN | undefined>;
export type CashuPayCb = (
payment: NDKZapDetails<CashuPaymentInfo>
) => Promise<NDKPaymentConfirmationCashu | Error>;
) => Promise<NDKPaymentConfirmationCashu | undefined>;

/**
*
Expand Down Expand Up @@ -108,7 +108,9 @@ class NDKZapper extends EventEmitter<{
* in any of the provided mints and return the proofs and mint used.
*/
public onCashuPay?: CashuPayCb;
public onComplete?: (results: Map<NDKZapSplit, NDKPaymentConfirmation | Error | undefined>) => void;
public onComplete?: (
results: Map<NDKZapSplit, NDKPaymentConfirmation | Error | undefined>
) => void;

public maxRelays = 3;

Expand Down Expand Up @@ -165,7 +167,7 @@ class NDKZapper extends EventEmitter<{
}

async zapSplit(split: NDKZapSplit): Promise<NDKPaymentConfirmation | undefined> {
let zapped = false;
const zapped = false;
let zapMethods = await this.getZapMethods(this.ndk, split.pubkey);
let retVal: NDKPaymentConfirmation | Error | undefined;

Expand Down Expand Up @@ -195,7 +197,7 @@ class NDKZapper extends EventEmitter<{
switch (zapMethod.type) {
case "nip61": {
const data = zapMethod.data as CashuPaymentInfo;
let ret: NDKPaymentConfirmationCashu | Error;
let ret: NDKPaymentConfirmationCashu | undefined;
ret = await this.onCashuPay!({
target: this.target,
comment: this.comment,
Expand Down Expand Up @@ -224,7 +226,7 @@ class NDKZapper extends EventEmitter<{

// we have a confirmation, generate the nutzap
const nutzap = new NDKNutzap(this.ndk);
nutzap.tags = [ ...nutzap.tags, ...(this.tags || []) ];
nutzap.tags = [...nutzap.tags, ...(this.tags || [])];
nutzap.proofs = proofs;
nutzap.mint = mint;
nutzap.comment = this.comment;
Expand Down Expand Up @@ -279,9 +281,16 @@ class NDKZapper extends EventEmitter<{
}
}
} catch (e: any) {
if (e instanceof Error) retVal = e
else retVal = new Error(e)
d("Error zapping to %s with %d %s using %s: %o", split.pubkey, split.amount, this.unit, zapMethod.type, e);
if (e instanceof Error) retVal = e;
else retVal = new Error(e);
d(
"Error zapping to %s with %d %s using %s: %o",
split.pubkey,
split.amount,
this.unit,
zapMethod.type,
e
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion ndk/src/zapper/ln.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { bech32 } from "@scure/base";
import type { NDK } from "../ndk";
import createDebug from "debug";
import { Hexpubkey } from "../user";
import type { Hexpubkey } from "../user";

const d = createDebug("ndk:zapper:ln");

Expand Down
14 changes: 8 additions & 6 deletions ndk/src/zapper/nip57.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { nip57, NostrEvent } from "nostr-tools";
import { NDKTag, NDKEvent } from "../events";
import { NDKSigner } from "../signers";
import { NDKUser } from "../user";
import { NDK } from "../ndk";
import { NDKLnUrlData } from "./ln.js";
import type { NostrEvent } from "nostr-tools";
import { nip57 } from "nostr-tools";
import type { NDKTag} from "../events";
import { NDKEvent } from "../events";
import type { NDKSigner } from "../signers";
import type { NDKUser } from "../user";
import type { NDK } from "../ndk";
import type { NDKLnUrlData } from "./ln.js";

export async function generateZapRequest(
target: NDKEvent | NDKUser,
Expand Down
4 changes: 2 additions & 2 deletions ndk/src/zapper/nip61.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NDKNutzap } from "../events/kinds/nutzap";
import { Proof } from "../events/kinds/nutzap/proof";
import type { NDKNutzap } from "../events/kinds/nutzap";
import type { Proof } from "../events/kinds/nutzap/proof";

/**
* Provides information that should be used to send a NIP-61 nutzap.
Expand Down

0 comments on commit 36ea99b

Please sign in to comment.