Add some more zap tools
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import {writable, derived} from "svelte/store"
|
import {writable, derived} from "svelte/store"
|
||||||
import {Zapper} from "@welshman/util"
|
import {Zapper, TrustedEvent, Zap, getTagValues, getLnUrl, zapFromEvent} from "@welshman/util"
|
||||||
import {
|
import {
|
||||||
identity,
|
removeNil,
|
||||||
fetchJson,
|
fetchJson,
|
||||||
uniq,
|
uniq,
|
||||||
bech32ToHex,
|
bech32ToHex,
|
||||||
@@ -11,7 +11,7 @@ import {
|
|||||||
postJson,
|
postJson,
|
||||||
} from "@welshman/lib"
|
} from "@welshman/lib"
|
||||||
import {collection} from "@welshman/store"
|
import {collection} from "@welshman/store"
|
||||||
import {deriveProfile} from "./profiles.js"
|
import {deriveProfile, loadProfile} from "./profiles.js"
|
||||||
import {appContext} from "./context.js"
|
import {appContext} from "./context.js"
|
||||||
|
|
||||||
export const zappers = writable<Zapper[]>([])
|
export const zappers = writable<Zapper[]>([])
|
||||||
@@ -22,7 +22,7 @@ export const fetchZappers = async (lnurls: string[]) => {
|
|||||||
|
|
||||||
// Use dufflepud if we it's set up to protect user privacy, otherwise fetch directly
|
// Use dufflepud if we it's set up to protect user privacy, otherwise fetch directly
|
||||||
if (base) {
|
if (base) {
|
||||||
const hexUrls = lnurls.map(lnurl => tryCatch(() => bech32ToHex(lnurl))).filter(identity)
|
const hexUrls = removeNil(lnurls.map(lnurl => tryCatch(() => bech32ToHex(lnurl))))
|
||||||
|
|
||||||
if (hexUrls.length > 0) {
|
if (hexUrls.length > 0) {
|
||||||
const res: any = await tryCatch(
|
const res: any = await tryCatch(
|
||||||
@@ -90,3 +90,40 @@ export const deriveZapperForPubkey = (pubkey: string, relays: string[] = []) =>
|
|||||||
|
|
||||||
return $zappersByLnurl.get($profile.lnurl)
|
return $zappersByLnurl.get($profile.lnurl)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export const getLnUrlsForEvent = async (event: TrustedEvent) => {
|
||||||
|
const lnurls = removeNil(getTagValues("zap", event.tags).map(getLnUrl))
|
||||||
|
|
||||||
|
if (lnurls.length > 0) {
|
||||||
|
return lnurls
|
||||||
|
}
|
||||||
|
|
||||||
|
const profile = await loadProfile(event.pubkey)
|
||||||
|
|
||||||
|
return removeNil([profile?.lnurl])
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getZapperForZap = async (zap: TrustedEvent, parent: TrustedEvent) => {
|
||||||
|
const lnurls = await getLnUrlsForEvent(parent)
|
||||||
|
|
||||||
|
return lnurls.length > 0 ? loadZapper(lnurls[0]) : undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getValidZap = async (zap: TrustedEvent, parent: TrustedEvent) => {
|
||||||
|
const zapper = await getZapperForZap(zap, parent)
|
||||||
|
|
||||||
|
return zapper ? zapFromEvent(zap, zapper) : undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getValidZaps = async (zaps: TrustedEvent[], parent: TrustedEvent) =>
|
||||||
|
removeNil(await Promise.all(zaps.map(zap => getValidZap(zap, parent))))
|
||||||
|
|
||||||
|
export const deriveValidZaps = (zaps: TrustedEvent[], parent: TrustedEvent) => {
|
||||||
|
const store = writable<Zap[]>([])
|
||||||
|
|
||||||
|
getValidZaps(zaps, parent).then(validZaps => {
|
||||||
|
store.set(validZaps)
|
||||||
|
})
|
||||||
|
|
||||||
|
return store
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import {derived} from "svelte/store"
|
import {derived} from "svelte/store"
|
||||||
import {sortBy, identity, ensurePlural, batch, partition, first} from "@welshman/lib"
|
import {sortBy, identity, ensurePlural, removeNil, batch, partition, first} from "@welshman/lib"
|
||||||
import {Repository} from "@welshman/relay"
|
import {Repository} from "@welshman/relay"
|
||||||
import {matchFilters, getIdAndAddress, getIdFilters, Filter, TrustedEvent} from "@welshman/util"
|
import {matchFilters, getIdAndAddress, getIdFilters, Filter, TrustedEvent} from "@welshman/util"
|
||||||
import {custom} from "./custom.js"
|
import {custom} from "./custom.js"
|
||||||
|
|
||||||
export type DeriveEventsMappedOptions<T> = {
|
export type DeriveEventsMappedOptions<T> = {
|
||||||
filters: Filter[]
|
filters: Filter[]
|
||||||
eventToItem: (event: TrustedEvent) => T | T[] | Promise<T | T[]> | undefined
|
eventToItem: (event: TrustedEvent) => undefined | T | T[] | Promise<undefined | T | T[]>
|
||||||
itemToEvent: (item: T) => TrustedEvent
|
itemToEvent: (item: T) => TrustedEvent
|
||||||
throttle?: number
|
throttle?: number
|
||||||
includeDeleted?: boolean
|
includeDeleted?: boolean
|
||||||
@@ -27,14 +27,14 @@ export const deriveEventsMapped = <T>(
|
|||||||
let data: T[] = []
|
let data: T[] = []
|
||||||
const deferred = new Set()
|
const deferred = new Set()
|
||||||
|
|
||||||
const defer = (event: TrustedEvent, promise: Promise<T | T[]>) => {
|
const defer = (event: TrustedEvent, promise: Promise<undefined | T | T[]>) => {
|
||||||
deferred.add(event.id)
|
deferred.add(event.id)
|
||||||
|
|
||||||
void promise.then(items => {
|
void promise.then(items => {
|
||||||
if (deferred.has(event.id)) {
|
if (deferred.has(event.id)) {
|
||||||
deferred.delete(event.id)
|
deferred.delete(event.id)
|
||||||
|
|
||||||
for (const item of ensurePlural(items)) {
|
for (const item of removeNil(ensurePlural(items))) {
|
||||||
data.push(item)
|
data.push(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ export const deriveEventsMapped = <T>(
|
|||||||
if (items instanceof Promise) {
|
if (items instanceof Promise) {
|
||||||
defer(event, items)
|
defer(event, items)
|
||||||
} else {
|
} else {
|
||||||
for (const item of ensurePlural(items)) {
|
for (const item of removeNil(ensurePlural(items))) {
|
||||||
data.push(item)
|
data.push(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,7 +89,7 @@ export const deriveEventsMapped = <T>(
|
|||||||
} else if (items) {
|
} else if (items) {
|
||||||
dirty = true
|
dirty = true
|
||||||
|
|
||||||
for (const item of ensurePlural(items)) {
|
for (const item of removeNil(ensurePlural(items))) {
|
||||||
data.push(item as T)
|
data.push(item as T)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ describe("Zaps", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should return null for invalid input", () => {
|
it("should return null for invalid input", () => {
|
||||||
expect(getLnUrl("invalid")).toBeNull()
|
expect(getLnUrl("invalid")).toBeUndefined()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -138,7 +138,7 @@ describe("Zaps", () => {
|
|||||||
|
|
||||||
const result = zapFromEvent(response, validZapper)
|
const result = zapFromEvent(response, validZapper)
|
||||||
|
|
||||||
expect(result).toBeNull()
|
expect(result).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should reject amount mismatch", () => {
|
it("should reject amount mismatch", () => {
|
||||||
@@ -150,7 +150,7 @@ describe("Zaps", () => {
|
|||||||
|
|
||||||
const result = zapFromEvent(response, validZapper)
|
const result = zapFromEvent(response, validZapper)
|
||||||
|
|
||||||
expect(result).toBeNull()
|
expect(result).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should reject incorrect zapper pubkey", () => {
|
it("should reject incorrect zapper pubkey", () => {
|
||||||
@@ -160,7 +160,7 @@ describe("Zaps", () => {
|
|||||||
|
|
||||||
const result = zapFromEvent(response, validZapper)
|
const result = zapFromEvent(response, validZapper)
|
||||||
|
|
||||||
expect(result).toBeNull()
|
expect(result).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should reject incorrect lnurl", () => {
|
it("should reject incorrect lnurl", () => {
|
||||||
@@ -172,7 +172,7 @@ describe("Zaps", () => {
|
|||||||
|
|
||||||
const result = zapFromEvent(response, validZapper)
|
const result = zapFromEvent(response, validZapper)
|
||||||
|
|
||||||
expect(result).toBeNull()
|
expect(result).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should handle invalid description JSON", () => {
|
it("should handle invalid description JSON", () => {
|
||||||
@@ -183,7 +183,7 @@ describe("Zaps", () => {
|
|||||||
|
|
||||||
const result = zapFromEvent(response, validZapper)
|
const result = zapFromEvent(response, validZapper)
|
||||||
|
|
||||||
expect(result).toBeNull()
|
expect(result).toBeUndefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should accept zap when recipient is zapper", () => {
|
it("should accept zap when recipient is zapper", () => {
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import {hexToBech32, fromPairs} from "@welshman/lib"
|
import {now, tryCatch, fetchJson, hexToBech32, fromPairs} from "@welshman/lib"
|
||||||
import type {TrustedEvent} from "./Events.js"
|
import {ZAP_RESPONSE, ZAP_REQUEST} from "./Kinds.js"
|
||||||
|
import {getTagValue} from "./Tags.js"
|
||||||
|
import type {Filter} from "./Filters.js"
|
||||||
|
import type {TrustedEvent, SignedEvent} from "./Events.js"
|
||||||
|
import {makeEvent} from "./Events.js"
|
||||||
|
|
||||||
const DIVISORS = {
|
const DIVISORS = {
|
||||||
m: BigInt(1e3),
|
m: BigInt(1e3),
|
||||||
@@ -12,6 +16,10 @@ const MAX_MILLISATS = BigInt("2100000000000000000")
|
|||||||
|
|
||||||
const MILLISATS_PER_BTC = BigInt(1e11)
|
const MILLISATS_PER_BTC = BigInt(1e11)
|
||||||
|
|
||||||
|
export const toMsats = (sats: number) => sats * 1000
|
||||||
|
|
||||||
|
export const fromMsats = (msats: number) => Math.floor(msats / 1000)
|
||||||
|
|
||||||
export const hrpToMillisat = (hrpString: string) => {
|
export const hrpToMillisat = (hrpString: string) => {
|
||||||
let divisor, value
|
let divisor, value
|
||||||
if (hrpString.slice(-1).match(/^[munp]$/)) {
|
if (hrpString.slice(-1).match(/^[munp]$/)) {
|
||||||
@@ -68,7 +76,7 @@ export const getLnUrl = (address: string) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Zapper = {
|
export type Zapper = {
|
||||||
@@ -98,19 +106,19 @@ export const zapFromEvent = (response: TrustedEvent, zapper: Zapper | undefined)
|
|||||||
request: JSON.parse(responseMeta.description),
|
request: JSON.parse(responseMeta.description),
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return null
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't count zaps that the user requested for himself
|
// Don't count zaps that the user requested for himself
|
||||||
if (zap.request.pubkey === zapper?.pubkey) {
|
if (zap.request.pubkey === zapper?.pubkey) {
|
||||||
return null
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
const {amount, lnurl} = fromPairs(zap.request.tags)
|
const {amount, lnurl} = fromPairs(zap.request.tags)
|
||||||
|
|
||||||
// Verify that the zapper actually sent the requested amount (if it was supplied)
|
// Verify that the zapper actually sent the requested amount (if it was supplied)
|
||||||
if (amount && parseInt(amount) !== zap.invoiceAmount) {
|
if (amount && parseInt(amount) !== zap.invoiceAmount) {
|
||||||
return null
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the recipient and the zapper are the same person, it's legit
|
// If the recipient and the zapper are the same person, it's legit
|
||||||
@@ -120,13 +128,89 @@ export const zapFromEvent = (response: TrustedEvent, zapper: Zapper | undefined)
|
|||||||
|
|
||||||
// If the sending client provided an lnurl tag, verify that too
|
// If the sending client provided an lnurl tag, verify that too
|
||||||
if (lnurl && lnurl !== zapper?.lnurl) {
|
if (lnurl && lnurl !== zapper?.lnurl) {
|
||||||
return null
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify that the request actually came from the recipient's zapper
|
// Verify that the request actually came from the recipient's zapper
|
||||||
if (zap.response.pubkey !== zapper?.nostrPubkey) {
|
if (zap.response.pubkey !== zapper?.nostrPubkey) {
|
||||||
return null
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
return zap
|
return zap
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ZapRequestParams = {
|
||||||
|
msats: number
|
||||||
|
zapper: Zapper
|
||||||
|
pubkey: string
|
||||||
|
relays: string[]
|
||||||
|
content?: string
|
||||||
|
eventId?: string
|
||||||
|
anonymous?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export const makeZapRequest = ({
|
||||||
|
msats,
|
||||||
|
zapper,
|
||||||
|
pubkey,
|
||||||
|
relays,
|
||||||
|
content = "",
|
||||||
|
eventId,
|
||||||
|
anonymous,
|
||||||
|
}: ZapRequestParams) => {
|
||||||
|
const tags = [
|
||||||
|
["relays", ...relays],
|
||||||
|
["amount", String(msats)],
|
||||||
|
["lnurl", zapper.lnurl],
|
||||||
|
["p", pubkey],
|
||||||
|
]
|
||||||
|
|
||||||
|
if (eventId) {
|
||||||
|
tags.push(["e", eventId])
|
||||||
|
}
|
||||||
|
|
||||||
|
if (anonymous) {
|
||||||
|
tags.push(["anon"])
|
||||||
|
}
|
||||||
|
|
||||||
|
return makeEvent(ZAP_REQUEST, {content, tags})
|
||||||
|
}
|
||||||
|
|
||||||
|
export type RequestInvoiceParams = {
|
||||||
|
zapper: Zapper
|
||||||
|
event: SignedEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
export const requestZap = async ({zapper, event}: RequestInvoiceParams) => {
|
||||||
|
const zapString = encodeURI(JSON.stringify(event))
|
||||||
|
const msats = parseInt(getTagValue("amount", event.tags)!)
|
||||||
|
const qs = `?amount=${msats}&nostr=${zapString}&lnurl=${zapper.lnurl}`
|
||||||
|
const res = await tryCatch(() => fetchJson(zapper.callback + qs))
|
||||||
|
|
||||||
|
return res?.pr ? {invoice: res.pr} : {error: res.reason || "Failed to request invoice"}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ZapResponseFilterParams = {
|
||||||
|
zapper: Zapper
|
||||||
|
pubkey: string
|
||||||
|
eventId?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getZapResponseFilter = ({zapper, pubkey, eventId}: ZapResponseFilterParams) => {
|
||||||
|
if (!zapper.nostrPubkey) {
|
||||||
|
throw new Error("Zapper did not have a nostr pubkey")
|
||||||
|
}
|
||||||
|
|
||||||
|
const filter: Filter = {
|
||||||
|
kinds: [ZAP_RESPONSE],
|
||||||
|
authors: [zapper.nostrPubkey],
|
||||||
|
since: now() - 30,
|
||||||
|
"#p": [pubkey],
|
||||||
|
}
|
||||||
|
|
||||||
|
if (eventId) {
|
||||||
|
filter["#e"] = [eventId]
|
||||||
|
}
|
||||||
|
|
||||||
|
return filter
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user