Merge branch 'coracle-social:master' into master
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/app",
|
||||
"version": "0.3.9",
|
||||
"version": "0.3.10",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "A collection of svelte stores for use in building nostr client applications.",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {writable, derived} from "svelte/store"
|
||||
import {Zapper} from "@welshman/util"
|
||||
import {Zapper, TrustedEvent, Zap, getTagValues, getLnUrl, zapFromEvent} from "@welshman/util"
|
||||
import {
|
||||
identity,
|
||||
removeNil,
|
||||
fetchJson,
|
||||
uniq,
|
||||
bech32ToHex,
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
postJson,
|
||||
} from "@welshman/lib"
|
||||
import {collection} from "@welshman/store"
|
||||
import {deriveProfile} from "./profiles.js"
|
||||
import {deriveProfile, loadProfile} from "./profiles.js"
|
||||
import {appContext} from "./context.js"
|
||||
|
||||
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
|
||||
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) {
|
||||
const res: any = await tryCatch(
|
||||
@@ -90,3 +90,40 @@ export const deriveZapperForPubkey = (pubkey: string, relays: string[] = []) =>
|
||||
|
||||
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,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/content",
|
||||
"version": "0.3.9",
|
||||
"version": "0.3.10",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "A collection of utilities for parsing nostr note content.",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/editor",
|
||||
"version": "0.3.9",
|
||||
"version": "0.3.10",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "A batteries-included nostr editor.",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/feeds",
|
||||
"version": "0.3.9",
|
||||
"version": "0.3.10",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "Utilities for building dynamic nostr feeds.",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/lib",
|
||||
"version": "0.3.9",
|
||||
"version": "0.3.10",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "A collection of utilities.",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/net",
|
||||
"version": "0.3.9",
|
||||
"version": "0.3.10",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "Utilities for connecting with nostr relays.",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/relay",
|
||||
"version": "0.3.9",
|
||||
"version": "0.3.10",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "An in-memory nostr relay implementation.",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/router",
|
||||
"version": "0.3.9",
|
||||
"version": "0.3.10",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "A collection of utilities for nostr relay selection.",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/signer",
|
||||
"version": "0.3.9",
|
||||
"version": "0.3.10",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "A nostr signer implemenation supporting several login methods.",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/store",
|
||||
"version": "0.3.9",
|
||||
"version": "0.3.10",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "A collection of utilities based on svelte/store for use with welshman",
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
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 {matchFilters, getIdAndAddress, getIdFilters, Filter, TrustedEvent} from "@welshman/util"
|
||||
import {custom} from "./custom.js"
|
||||
|
||||
export type DeriveEventsMappedOptions<T> = {
|
||||
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
|
||||
throttle?: number
|
||||
includeDeleted?: boolean
|
||||
@@ -27,14 +27,14 @@ export const deriveEventsMapped = <T>(
|
||||
let data: T[] = []
|
||||
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)
|
||||
|
||||
void promise.then(items => {
|
||||
if (deferred.has(event.id)) {
|
||||
deferred.delete(event.id)
|
||||
|
||||
for (const item of ensurePlural(items)) {
|
||||
for (const item of removeNil(ensurePlural(items))) {
|
||||
data.push(item)
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ export const deriveEventsMapped = <T>(
|
||||
if (items instanceof Promise) {
|
||||
defer(event, items)
|
||||
} else {
|
||||
for (const item of ensurePlural(items)) {
|
||||
for (const item of removeNil(ensurePlural(items))) {
|
||||
data.push(item)
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@ export const deriveEventsMapped = <T>(
|
||||
} else if (items) {
|
||||
dirty = true
|
||||
|
||||
for (const item of ensurePlural(items)) {
|
||||
for (const item of removeNil(ensurePlural(items))) {
|
||||
data.push(item as T)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ describe("Zaps", () => {
|
||||
})
|
||||
|
||||
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)
|
||||
|
||||
expect(result).toBeNull()
|
||||
expect(result).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should reject amount mismatch", () => {
|
||||
@@ -150,7 +150,7 @@ describe("Zaps", () => {
|
||||
|
||||
const result = zapFromEvent(response, validZapper)
|
||||
|
||||
expect(result).toBeNull()
|
||||
expect(result).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should reject incorrect zapper pubkey", () => {
|
||||
@@ -160,7 +160,7 @@ describe("Zaps", () => {
|
||||
|
||||
const result = zapFromEvent(response, validZapper)
|
||||
|
||||
expect(result).toBeNull()
|
||||
expect(result).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should reject incorrect lnurl", () => {
|
||||
@@ -172,7 +172,7 @@ describe("Zaps", () => {
|
||||
|
||||
const result = zapFromEvent(response, validZapper)
|
||||
|
||||
expect(result).toBeNull()
|
||||
expect(result).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should handle invalid description JSON", () => {
|
||||
@@ -183,7 +183,7 @@ describe("Zaps", () => {
|
||||
|
||||
const result = zapFromEvent(response, validZapper)
|
||||
|
||||
expect(result).toBeNull()
|
||||
expect(result).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should accept zap when recipient is zapper", () => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@welshman/util",
|
||||
"version": "0.3.9",
|
||||
"version": "0.3.10",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "A collection of nostr-related utilities.",
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import {hexToBech32, fromPairs} from "@welshman/lib"
|
||||
import type {TrustedEvent} from "./Events.js"
|
||||
import {now, tryCatch, fetchJson, hexToBech32, fromPairs} from "@welshman/lib"
|
||||
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 = {
|
||||
m: BigInt(1e3),
|
||||
@@ -12,6 +16,10 @@ const MAX_MILLISATS = BigInt("2100000000000000000")
|
||||
|
||||
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) => {
|
||||
let divisor, value
|
||||
if (hrpString.slice(-1).match(/^[munp]$/)) {
|
||||
@@ -68,7 +76,7 @@ export const getLnUrl = (address: string) => {
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
return undefined
|
||||
}
|
||||
|
||||
export type Zapper = {
|
||||
@@ -98,19 +106,19 @@ export const zapFromEvent = (response: TrustedEvent, zapper: Zapper | undefined)
|
||||
request: JSON.parse(responseMeta.description),
|
||||
}
|
||||
} catch (e) {
|
||||
return null
|
||||
return undefined
|
||||
}
|
||||
|
||||
// Don't count zaps that the user requested for himself
|
||||
if (zap.request.pubkey === zapper?.pubkey) {
|
||||
return null
|
||||
return undefined
|
||||
}
|
||||
|
||||
const {amount, lnurl} = fromPairs(zap.request.tags)
|
||||
|
||||
// Verify that the zapper actually sent the requested amount (if it was supplied)
|
||||
if (amount && parseInt(amount) !== zap.invoiceAmount) {
|
||||
return null
|
||||
return undefined
|
||||
}
|
||||
|
||||
// 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 (lnurl && lnurl !== zapper?.lnurl) {
|
||||
return null
|
||||
return undefined
|
||||
}
|
||||
|
||||
// Verify that the request actually came from the recipient's zapper
|
||||
if (zap.response.pubkey !== zapper?.nostrPubkey) {
|
||||
return null
|
||||
return undefined
|
||||
}
|
||||
|
||||
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