sigit.io/src/utils/nostr.ts

80 lines
1.9 KiB
TypeScript
Raw Normal View History

import { nip05, nip19, verifyEvent } from 'nostr-tools'
import { SignedEvent } from '../types'
/**
* @param hexKey hex private or public key
* @returns whether or not is key valid
*/
const validateHex = (hexKey: string) => {
return hexKey.match(/^[a-f0-9]{64}$/)
}
/**
* NPUB provided - it will convert NPUB to HEX
* NIP-05 provided - it will query NIP-05 profile and return HEX key if found
* HEX provided - it will return HEX
*
* @param pubKey in NPUB, NIP-05 or HEX format
* @returns HEX format
*/
export const pubToHex = async (pubKey: string): Promise<string | null> => {
// If key is NIP-05
if (pubKey.indexOf('@') !== -1)
return Promise.resolve(
(await nip05.queryProfile(pubKey).then((res) => res?.pubkey)) || null
)
// If key is NPUB
if (pubKey.startsWith('npub')) {
try {
return nip19.decode(pubKey).data as string
} catch (error) {
return Promise.resolve(null)
}
}
// valid hex key
if (validateHex(pubKey)) return Promise.resolve(pubKey)
// Not a valid hex key
return Promise.resolve(null)
}
/**
* If NSEC key is provided function will convert it to HEX
* If HEX key Is provided function will validate the HEX format and return
*
* @param nsec or private key in HEX format
*/
export const nsecToHex = (nsec: string): string | null => {
// If key is NSEC
if (nsec.startsWith('nsec')) {
try {
return nip19.decode(nsec).data as string
} catch (error) {
return null
}
}
// since it's not NSEC key we check if it's a valid hex key
if (validateHex(nsec)) return nsec
return null
}
2024-03-01 10:16:35 +00:00
export const hexToNpub = (hexPubkey: string | undefined): string => {
if (!hexPubkey) return 'n/a'
return nip19.npubEncode(hexPubkey)
}
export const verifySignedEvent = (event: SignedEvent) => {
const isGood = verifyEvent(event)
if (!isGood) {
throw new Error(
'Signed event did not pass verification. Check sig, id and pubkey.'
)
}
}