Fix bugs, add timehash

This commit is contained in:
Jon Staab
2025-02-05 10:47:56 -08:00
parent f0b2b7c8b3
commit 5909b593ab
7 changed files with 72 additions and 41 deletions
+34
View File
@@ -1,5 +1,6 @@
import {hexToBytes, bytesToHex} from "@noble/hashes/utils"
import * as nip19 from "nostr-tools/nip19"
import {HOUR} from "@welshman/lib"
export const displayList = <T>(xs: T[], conj = "and", n = 6, locale = "en-US") => {
const stringItems = xs.map(String)
@@ -24,3 +25,36 @@ export const nsecDecode = (nsec: string) => {
return bytesToHex(data)
}
export const timeHash = (seconds: number, precision = 32) => {
const alphabet = "0123456789bcdefghjkmnpqrstuvwxyz"
const uint32 = Math.min(seconds >>> 0, 0xffffffff)
const binary = uint32.toString(2).padStart(32, "0")
const chunks = Math.min(Math.floor(precision / 5), 6)
let hash = ""
for (let i = 0; i < chunks * 5; i += 5) {
const chunk = binary.slice(i, i + 5)
const index = parseInt(chunk, 2)
hash += alphabet[index]
}
return hash
}
export const timeHashesBetween = (a: number, b: number) => {
const precisions = [10, 15, 20]
const start = Math.min(a, b) >>> 0
const end = Math.min(Math.max(a, b) >>> 0, 0xffffffff)
const hashes = new Set<string>()
for (let seconds = start; seconds <= end; seconds += HOUR) {
for (const precision of precisions) {
hashes.add(timeHash(seconds, precision))
}
}
return Array.from(hashes).sort()
}