Revert "nip19: remove note1."

This reverts commit a8a805fb71.
This commit is contained in:
Asai Toshiya
2024-11-21 23:51:38 +09:00
committed by fiatjaf_
parent b22e2465cc
commit 2e85f7a5fe
6 changed files with 57 additions and 29 deletions

View File

@@ -8,6 +8,7 @@ export type NEvent = `nevent1${string}`
export type NAddr = `naddr1${string}`
export type NSec = `nsec1${string}`
export type NPub = `npub1${string}`
export type Note = `note1${string}`
export type Ncryptsec = `ncryptsec1${string}`
export const NostrTypeGuard = {
@@ -16,6 +17,7 @@ export const NostrTypeGuard = {
isNAddr: (value?: string | null): value is NAddr => /^naddr1[a-z\d]+$/.test(value || ''),
isNSec: (value?: string | null): value is NSec => /^nsec1[a-z\d]{58}$/.test(value || ''),
isNPub: (value?: string | null): value is NPub => /^npub1[a-z\d]{58}$/.test(value || ''),
isNote: (value?: string | null): value is Note => /^note1[a-z\d]+$/.test(value || ''),
isNcryptsec: (value?: string | null): value is Ncryptsec => /^ncryptsec1[a-z\d]+$/.test(value || ''),
}
@@ -65,6 +67,7 @@ type Prefixes = {
naddr: AddressPointer
nsec: Uint8Array
npub: string
note: string
}
type DecodeValue<Prefix extends keyof Prefixes> = {
@@ -137,6 +140,7 @@ export function decode(nip19: string): DecodeResult {
return { type: prefix, data }
case 'npub':
case 'note':
return { type: prefix, data: bytesToHex(data) }
default:
@@ -169,6 +173,10 @@ export function npubEncode(hex: string): NPub {
return encodeBytes('npub', hexToBytes(hex))
}
export function noteEncode(hex: string): Note {
return encodeBytes('note', hexToBytes(hex))
}
function encodeBech32<Prefix extends string>(prefix: Prefix, data: Uint8Array): `${Prefix}1${string}` {
let words = bech32.toWords(data)
return bech32.encode(prefix, words, Bech32MaxSize) as `${Prefix}1${string}`