Add pubkey file

This commit is contained in:
Jon Staab
2026-02-04 10:31:09 -08:00
parent 69faa1d6a8
commit c7211e3dec
+33
View File
@@ -0,0 +1,33 @@
import {decode, npubEncode, nprofileEncode} from "nostr-tools/nip19"
export class Pubkey {
constructor(
readonly pubkey: string,
readonly relays: string[] = [],
) {}
static from(entity: string, relays: string[] = []) {
let pubkey: string
if (entity.match(/^[0-9a-f]{64}$/)) {
pubkey = entity
} else {
const {type, data} = decode(entity) as any
if (type === "npub") {
pubkey = data
} else if (type === "nprofile") {
pubkey = data.pubkey
} else {
throw new Error(`Invalid pubkey: ${entity}`)
}
}
return new Pubkey(pubkey, relays)
}
toString = () => this.pubkey
toNpub = () => npubEncode(this.pubkey)
toNprofile = () => nprofileEncode(this)
}