just format

This commit is contained in:
Alex Gleason
2023-08-31 13:42:15 -05:00
parent 2a70bb18ff
commit 718032022c
55 changed files with 930 additions and 1488 deletions

View File

@@ -1,7 +1,7 @@
import {bytesToHex, concatBytes, hexToBytes} from '@noble/hashes/utils'
import {bech32} from '@scure/base'
import { bytesToHex, concatBytes, hexToBytes } from '@noble/hashes/utils'
import { bech32 } from '@scure/base'
import {utf8Decoder, utf8Encoder} from './utils.ts'
import { utf8Decoder, utf8Encoder } from './utils.ts'
const Bech32MaxSize = 5000
@@ -9,8 +9,7 @@ const Bech32MaxSize = 5000
* Bech32 regex.
* @see https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#bech32
*/
export const BECH32_REGEX =
/[\x21-\x7E]{1,83}1[023456789acdefghjklmnpqrstuvwxyz]{6,}/
export const BECH32_REGEX = /[\x21-\x7E]{1,83}1[023456789acdefghjklmnpqrstuvwxyz]{6,}/
export type ProfilePointer = {
pubkey: string // hex
@@ -52,7 +51,7 @@ export type DecodeResult = {
export function decode<Prefix extends keyof Prefixes>(nip19: `${Prefix}1${string}`): DecodeValue<Prefix>
export function decode(nip19: string): DecodeResult
export function decode(nip19: string): DecodeResult {
let {prefix, words} = bech32.decode(nip19, Bech32MaxSize)
let { prefix, words } = bech32.decode(nip19, Bech32MaxSize)
let data = new Uint8Array(bech32.fromWords(words))
switch (prefix) {
@@ -65,24 +64,23 @@ export function decode(nip19: string): DecodeResult {
type: 'nprofile',
data: {
pubkey: bytesToHex(tlv[0][0]),
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
}
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : [],
},
}
}
case 'nevent': {
let tlv = parseTLV(data)
if (!tlv[0]?.[0]) throw new Error('missing TLV 0 for nevent')
if (tlv[0][0].length !== 32) throw new Error('TLV 0 should be 32 bytes')
if (tlv[2] && tlv[2][0].length !== 32)
throw new Error('TLV 2 should be 32 bytes')
if (tlv[2] && tlv[2][0].length !== 32) throw new Error('TLV 2 should be 32 bytes')
return {
type: 'nevent',
data: {
id: bytesToHex(tlv[0][0]),
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : [],
author: tlv[2]?.[0] ? bytesToHex(tlv[2][0]) : undefined
}
author: tlv[2]?.[0] ? bytesToHex(tlv[2][0]) : undefined,
},
}
}
@@ -100,8 +98,8 @@ export function decode(nip19: string): DecodeResult {
identifier: utf8Decoder.decode(tlv[0][0]),
pubkey: bytesToHex(tlv[2][0]),
kind: parseInt(bytesToHex(tlv[3][0]), 16),
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
}
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : [],
},
}
}
@@ -111,21 +109,21 @@ export function decode(nip19: string): DecodeResult {
return {
type: 'nrelay',
data: utf8Decoder.decode(tlv[0][0])
data: utf8Decoder.decode(tlv[0][0]),
}
}
case 'nsec':
case 'npub':
case 'note':
return {type: prefix, data: bytesToHex(data)}
return { type: prefix, data: bytesToHex(data) }
default:
throw new Error(`unknown prefix ${prefix}`)
}
}
type TLV = {[t: number]: Uint8Array[]}
type TLV = { [t: number]: Uint8Array[] }
function parseTLV(data: Uint8Array): TLV {
let result: TLV = {}
@@ -168,7 +166,7 @@ function encodeBytes<Prefix extends string>(prefix: Prefix, hex: string): `${Pre
export function nprofileEncode(profile: ProfilePointer): `nprofile1${string}` {
let data = encodeTLV({
0: [hexToBytes(profile.pubkey)],
1: (profile.relays || []).map(url => utf8Encoder.encode(url))
1: (profile.relays || []).map(url => utf8Encoder.encode(url)),
})
return encodeBech32('nprofile', data)
}
@@ -177,7 +175,7 @@ export function neventEncode(event: EventPointer): `nevent1${string}` {
let data = encodeTLV({
0: [hexToBytes(event.id)],
1: (event.relays || []).map(url => utf8Encoder.encode(url)),
2: event.author ? [hexToBytes(event.author)] : []
2: event.author ? [hexToBytes(event.author)] : [],
})
return encodeBech32('nevent', data)
}
@@ -190,14 +188,14 @@ export function naddrEncode(addr: AddressPointer): `naddr1${string}` {
0: [utf8Encoder.encode(addr.identifier)],
1: (addr.relays || []).map(url => utf8Encoder.encode(url)),
2: [hexToBytes(addr.pubkey)],
3: [new Uint8Array(kind)]
3: [new Uint8Array(kind)],
})
return encodeBech32('naddr', data)
}
export function nrelayEncode(url: string): `nrelay1${string}` {
let data = encodeTLV({
0: [utf8Encoder.encode(url)]
0: [utf8Encoder.encode(url)],
})
return encodeBech32('nrelay', data)
}