Move parseBunkerUrl to static method, add getBunkerUrl
This commit is contained in:
@@ -179,7 +179,11 @@ export class Router {
|
|||||||
|
|
||||||
Quote = (event: TrustedEvent, value: string, relays: string[] = []) => {
|
Quote = (event: TrustedEvent, value: string, relays: string[] = []) => {
|
||||||
const tag = event.tags.find(t => t[1] === value)
|
const tag = event.tags.find(t => t[1] === value)
|
||||||
const scenarios = [this.ForPubkey(event.pubkey), this.FromPubkey(event.pubkey)]
|
const scenarios = [
|
||||||
|
this.FromRelays(relays),
|
||||||
|
this.ForPubkey(event.pubkey),
|
||||||
|
this.FromPubkey(event.pubkey),
|
||||||
|
]
|
||||||
|
|
||||||
if (tag?.[2] && isShareableRelayUrl(tag[2])) {
|
if (tag?.[2] && isShareableRelayUrl(tag[2])) {
|
||||||
scenarios.push(this.FromRelays([tag[2]]))
|
scenarios.push(this.FromRelays([tag[2]]))
|
||||||
|
|||||||
@@ -132,4 +132,5 @@ export type FeedOptions = {
|
|||||||
onEvent?: (event: TrustedEvent) => void
|
onEvent?: (event: TrustedEvent) => void
|
||||||
onExhausted?: () => void
|
onExhausted?: () => void
|
||||||
useWindowing?: boolean
|
useWindowing?: boolean
|
||||||
|
abortController?: AbortController
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ export enum Nip46Event {
|
|||||||
export type Nip46BrokerParams = {
|
export type Nip46BrokerParams = {
|
||||||
relays: string[]
|
relays: string[]
|
||||||
clientSecret: string
|
clientSecret: string
|
||||||
|
connectSecret?: string
|
||||||
signerPubkey?: string
|
signerPubkey?: string
|
||||||
algorithm?: Nip46Algorithm
|
algorithm?: Nip46Algorithm
|
||||||
}
|
}
|
||||||
@@ -261,6 +262,26 @@ export class Nip46Broker extends Emitter {
|
|||||||
return singleton
|
return singleton
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Static utility methods
|
||||||
|
|
||||||
|
static parseBunkerUrl = (url: string) => {
|
||||||
|
let connectSecret = ""
|
||||||
|
let signerPubkey = ""
|
||||||
|
let relays: string[] = []
|
||||||
|
|
||||||
|
try {
|
||||||
|
const _url = new URL(url)
|
||||||
|
|
||||||
|
relays = _url.searchParams.getAll("relay") || []
|
||||||
|
signerPubkey = _url.hostname || _url.pathname.replace(/\//g, "")
|
||||||
|
connectSecret = _url.searchParams.get("secret") || ""
|
||||||
|
} catch {
|
||||||
|
// pass
|
||||||
|
}
|
||||||
|
|
||||||
|
return {signerPubkey, connectSecret, relays: relays.map(normalizeRelayUrl)}
|
||||||
|
}
|
||||||
|
|
||||||
// Expose params without exposing params
|
// Expose params without exposing params
|
||||||
|
|
||||||
hasParams(params: Nip46BrokerParams) {
|
hasParams(params: Nip46BrokerParams) {
|
||||||
@@ -329,24 +350,6 @@ export class Nip46Broker extends Emitter {
|
|||||||
|
|
||||||
// Methods for initiating a connection
|
// Methods for initiating a connection
|
||||||
|
|
||||||
parseBunkerUrl = (url: string) => {
|
|
||||||
let connectSecret = ""
|
|
||||||
let signerPubkey = ""
|
|
||||||
let relays: string[] = []
|
|
||||||
|
|
||||||
try {
|
|
||||||
const _url = new URL(url)
|
|
||||||
|
|
||||||
relays = _url.searchParams.getAll("relay") || []
|
|
||||||
signerPubkey = _url.hostname || _url.pathname.replace(/\//g, "")
|
|
||||||
connectSecret = _url.searchParams.get("secret") || ""
|
|
||||||
} catch {
|
|
||||||
// pass
|
|
||||||
}
|
|
||||||
|
|
||||||
return {signerPubkey, connectSecret, relays: relays.map(normalizeRelayUrl)}
|
|
||||||
}
|
|
||||||
|
|
||||||
makeNostrconnectUrl = async (meta: Record<string, string> = {}) => {
|
makeNostrconnectUrl = async (meta: Record<string, string> = {}) => {
|
||||||
const clientPubkey = await this.signer.getPubkey()
|
const clientPubkey = await this.signer.getPubkey()
|
||||||
const secret = Math.random().toString(36).substring(7)
|
const secret = Math.random().toString(36).substring(7)
|
||||||
@@ -395,6 +398,26 @@ export class Nip46Broker extends Emitter {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Methods for serializing a connection
|
||||||
|
|
||||||
|
getBunkerUrl = () => {
|
||||||
|
if (!this.params.signerPubkey) {
|
||||||
|
throw new Error("Attempted to get a bunker url with no signerPubkey")
|
||||||
|
}
|
||||||
|
|
||||||
|
const params = new URLSearchParams()
|
||||||
|
|
||||||
|
for (const relay of this.params.relays) {
|
||||||
|
params.append("relay", relay)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.params.connectSecret) {
|
||||||
|
params.set("secret", this.params.connectSecret)
|
||||||
|
}
|
||||||
|
|
||||||
|
return "bunker://" + this.params.signerPubkey + "?" + params.toString()
|
||||||
|
}
|
||||||
|
|
||||||
// Normal NIP 46 methods
|
// Normal NIP 46 methods
|
||||||
|
|
||||||
ping = () => this.send("ping", [])
|
ping = () => this.send("ping", [])
|
||||||
|
|||||||
Reference in New Issue
Block a user