diff --git a/src/util/Address.ts b/src/util/Address.ts index 9fec50c..d4b3f5c 100644 --- a/src/util/Address.ts +++ b/src/util/Address.ts @@ -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 diff --git a/src/util/Events.ts b/src/util/Events.ts index 8395006..2c33cbb 100644 --- a/src/util/Events.ts +++ b/src/util/Events.ts @@ -43,7 +43,7 @@ export const hasValidSignature = cached({ }, }) -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 diff --git a/src/util/Router.ts b/src/util/Router.ts index 77b2bd3..8a46559 100644 --- a/src/util/Router.ts +++ b/src/util/Router.ts @@ -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 diff --git a/src/util/Tags.ts b/src/util/Tags.ts index 5af839f..bad513c 100644 --- a/src/util/Tags.ts +++ b/src/util/Tags.ts @@ -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 as OmitStatics, ' 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]