Improve Address utility

This commit is contained in:
Jon Staab
2024-03-01 15:55:32 -08:00
parent d5324627df
commit 42245183b6
4 changed files with 19 additions and 28 deletions
+15 -2
View File
@@ -22,15 +22,28 @@ export class Address {
this.identifier = identifier || ""
}
static fromEvent = (e: UnsignedEvent, relays: string[] = []) =>
static getKind = (a: string) => Address.fromRaw(a, []).kind
static getPubkey = (a: string) => Address.fromRaw(a, []).pubkey
static getIdentifier = (a: string) => Address.fromRaw(a, []).identifier
static fromEvent = (e: UnsignedEvent, relays: string[]) =>
new Address(e.kind, e.pubkey, Tags.fromEvent(e).get("d")?.value() || "", relays)
static fromRaw = (a: string, relays: string[] = []) => {
static fromRaw = (a: string, relays: string[]) => {
const [kind, pubkey, identifier] = a.split(":")
return new Address(kind, pubkey, identifier, relays)
}
static fromTag = (tag: string[]) => {
const [a, hint] = tag.slice(1)
const relays = hint ? [hint] : []
return this.fromRaw(a, relays)
}
static fromNaddr = (naddr: string) => {
let type
let data = {} as any
+1 -1
View File
@@ -43,7 +43,7 @@ export const hasValidSignature = cached<string, boolean, [Event]>({
},
})
export const getAddress = (e: UnsignedEvent) => Address.fromEvent(e).asRaw()
export const getAddress = (e: UnsignedEvent) => Address.fromEvent(e, []).asRaw()
export const getIdOrAddress = (e: Rumor) => isReplaceable(e) ? getAddress(e) : e.id
+3 -9
View File
@@ -1,10 +1,9 @@
import type {EventTemplate, UnsignedEvent} from 'nostr-tools'
import type {Rumor} from './Events'
import {nip19} from 'nostr-tools'
import {getAddress, isReplaceable} from './Events'
import {Tag, Tags} from './Tags'
import {first, uniq, shuffle} from './Tools'
import {isGroupAddress, isCommunityAddress} from './Address'
import {Address, isGroupAddress, isCommunityAddress} from './Address'
export enum RelayMode {
Inbox = "inbox",
@@ -208,13 +207,8 @@ export class Router {
return new Tags(tags)
}
getNaddr = (event: UnsignedEvent) =>
nip19.naddrEncode({
kind: event.kind,
pubkey: event.pubkey,
identifier: Tags.fromEvent(event).get("d")?.value() || "",
relays: this.Event(event).limit(3).getUrls(),
})
address = (event: UnsignedEvent) =>
Address.fromEvent(event, this.Event(event).limit(3).getUrls())
}
// Router Scenario
-16
View File
@@ -1,5 +1,4 @@
import {EventTemplate} from 'nostr-tools'
import {nip19} from 'nostr-tools'
import {Fluent} from './Fluent'
import type {OmitStatics} from './Tools'
import {last} from './Tools'
@@ -11,21 +10,6 @@ export class Tag extends (Fluent<string> as OmitStatics<typeof Fluent<string>, '
return new Tag(Array.from(xs))
}
static fromNaddr(naddr: string) {
const {type, data} = nip19.decode(naddr) as {
type: "naddr"
data: nip19.AddressPointer
}
if (type !== "naddr") {
throw new Error(`Invalid naddr ${naddr}`)
}
const {kind, pubkey, identifier, relays = []} = data
return Tag.from(["a", [kind, pubkey, identifier].join(':'), ...relays.slice(0, 1)])
}
valueOf = () => this.xs
key = () => this.xs[0]