Add some utils, add support for nip44 encryption to nip46 signer

This commit is contained in:
Jon Staab
2024-08-16 08:58:55 -07:00
parent e38422c9ba
commit bd65aca99d
5 changed files with 74 additions and 69 deletions
+3 -3
View File
@@ -60,8 +60,8 @@ export class Address {
export const getAddress = (e: AddressableEvent) => Address.fromEvent(e).toString()
export const isGroupAddress = (a: string, ...args: unknown[]) => Address.from(a).kind === GROUP
export const isGroupAddress = (a: string, ...args: unknown[]) => Address.isAddress(a) && Address.from(a).kind === GROUP
export const isCommunityAddress = (a: string, ...args: unknown[]) => Address.from(a).kind === COMMUNITY
export const isCommunityAddress = (a: string, ...args: unknown[]) => Address.isAddress(a) && Address.from(a).kind === COMMUNITY
export const isContextAddress = (a: string, ...args: unknown[]) => [GROUP, COMMUNITY].includes(Address.from(a).kind)
export const isContextAddress = (a: string, ...args: unknown[]) => Address.isAddress(a) && [GROUP, COMMUNITY].includes(Address.from(a).kind)
+38 -32
View File
@@ -1,6 +1,6 @@
import {EventTemplate} from 'nostr-tools'
import type {OmitStatics} from '@welshman/lib'
import {Fluent, nth, ensurePlural} from '@welshman/lib'
import {Fluent, nth, nthEq, ensurePlural} from '@welshman/lib'
import {isRelayUrl, normalizeRelayUrl} from './Relay'
import {Address, isContextAddress} from './Address'
import {GROUP, COMMUNITY} from './Kinds'
@@ -76,37 +76,7 @@ export class Tags extends (Fluent<Tag> as OmitStatics<typeof Fluent<Tag>, 'from'
topics = () => this.whereKey("t").values().map((t: string) => t.replace(/^#/, ""))
ancestors = (x?: boolean) => {
const tags = this.filterByKey(["a", "e", "q"]).reject(t => t.isContext())
const mentionTags = tags.whereKey("q")
const roots: string[][] = []
const replies: string[][] = []
const mentions: string[][] = []
const dispatchTags = (thisTags: Tags) =>
thisTags.forEach((t: Tag, i: number) => {
if (t.nth(3) === 'root') {
if (tags.filter(t => t.nth(3) === "reply").count() === 0) {
replies.push(t.valueOf())
} else {
roots.push(t.valueOf())
}
} else if (t.nth(3) === 'reply') {
replies.push(t.valueOf())
} else if (t.nth(3) === 'mention') {
mentions.push(t.valueOf())
} else if (i === thisTags.count() - 1) {
replies.push(t.valueOf())
} else if (i === 0) {
roots.push(t.valueOf())
} else {
mentions.push(t.valueOf())
}
})
// Add different types separately so positional logic works
dispatchTags(tags.whereKey("e"))
dispatchTags(tags.whereKey("a").filter(t => Boolean(t.nth(3))))
mentionTags.forEach((t: Tag) => mentions.push(t.valueOf()))
const {roots, replies, mentions} = getAncestorTags(this.unwrap())
return {
roots: Tags.wrap(roots),
@@ -240,3 +210,39 @@ export const getKindTags = (tags: string[][]) =>
tags.filter(t => ["k"].includes(t[0]) && t[1].match(/^\d+$/))
export const getKindTagValues = (tags: string[][]) => getKindTags(tags).map(t => parseInt(t[1]))
export const getAncestorTags = (tags: string[][]) => {
const validTags = tags.filter(t => ["a", "e", "q"].includes(t[0]) && !isContextAddress(t[1]))
const mentionTags = validTags.filter(nthEq(0, "q"))
const roots: string[][] = []
const replies: string[][] = []
const mentions: string[][] = []
const dispatchTags = (thisTags: string[][]) =>
thisTags.forEach((t: string[], i: number) => {
if (t[3] === 'root') {
if (validTags.filter(nthEq(3, "reply")).length === 0) {
replies.push(t)
} else {
roots.push(t)
}
} else if (t[3] === 'reply') {
replies.push(t)
} else if (t[3] === 'mention') {
mentions.push(t)
} else if (i === thisTags.length - 1) {
replies.push(t)
} else if (i === 0) {
roots.push(t)
} else {
mentions.push(t)
}
})
// Add different types separately so positional logic works
dispatchTags(validTags.filter(nthEq(0, "e")))
dispatchTags(validTags.filter(nthEq(0, "a")).filter(t => Boolean(t[3])))
mentionTags.forEach((t: string[]) => mentions.push(t))
return {roots, replies, mentions}
}