Improve Address utility
This commit is contained in:
+15
-2
@@ -22,15 +22,28 @@ export class Address {
|
|||||||
this.identifier = identifier || ""
|
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)
|
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(":")
|
const [kind, pubkey, identifier] = a.split(":")
|
||||||
|
|
||||||
return new Address(kind, pubkey, identifier, relays)
|
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) => {
|
static fromNaddr = (naddr: string) => {
|
||||||
let type
|
let type
|
||||||
let data = {} as any
|
let data = {} as any
|
||||||
|
|||||||
+1
-1
@@ -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
|
export const getIdOrAddress = (e: Rumor) => isReplaceable(e) ? getAddress(e) : e.id
|
||||||
|
|
||||||
|
|||||||
+3
-9
@@ -1,10 +1,9 @@
|
|||||||
import type {EventTemplate, UnsignedEvent} from 'nostr-tools'
|
import type {EventTemplate, UnsignedEvent} from 'nostr-tools'
|
||||||
import type {Rumor} from './Events'
|
import type {Rumor} from './Events'
|
||||||
import {nip19} from 'nostr-tools'
|
|
||||||
import {getAddress, isReplaceable} from './Events'
|
import {getAddress, isReplaceable} from './Events'
|
||||||
import {Tag, Tags} from './Tags'
|
import {Tag, Tags} from './Tags'
|
||||||
import {first, uniq, shuffle} from './Tools'
|
import {first, uniq, shuffle} from './Tools'
|
||||||
import {isGroupAddress, isCommunityAddress} from './Address'
|
import {Address, isGroupAddress, isCommunityAddress} from './Address'
|
||||||
|
|
||||||
export enum RelayMode {
|
export enum RelayMode {
|
||||||
Inbox = "inbox",
|
Inbox = "inbox",
|
||||||
@@ -208,13 +207,8 @@ export class Router {
|
|||||||
return new Tags(tags)
|
return new Tags(tags)
|
||||||
}
|
}
|
||||||
|
|
||||||
getNaddr = (event: UnsignedEvent) =>
|
address = (event: UnsignedEvent) =>
|
||||||
nip19.naddrEncode({
|
Address.fromEvent(event, this.Event(event).limit(3).getUrls())
|
||||||
kind: event.kind,
|
|
||||||
pubkey: event.pubkey,
|
|
||||||
identifier: Tags.fromEvent(event).get("d")?.value() || "",
|
|
||||||
relays: this.Event(event).limit(3).getUrls(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Router Scenario
|
// Router Scenario
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import {EventTemplate} from 'nostr-tools'
|
import {EventTemplate} from 'nostr-tools'
|
||||||
import {nip19} from 'nostr-tools'
|
|
||||||
import {Fluent} from './Fluent'
|
import {Fluent} from './Fluent'
|
||||||
import type {OmitStatics} from './Tools'
|
import type {OmitStatics} from './Tools'
|
||||||
import {last} 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))
|
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
|
valueOf = () => this.xs
|
||||||
|
|
||||||
key = () => this.xs[0]
|
key = () => this.xs[0]
|
||||||
|
|||||||
Reference in New Issue
Block a user