mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-11-22 15:38:34 +00:00
names-web example code to register names from web UI
This commit is contained in:
1 parent
4e6f558614
commit
4eafc41c80
17 files changed
+642
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import {sha256} from "@noble/hashes/sha2"
|
||||
import {bytesToNumberBE, numberToBytesBE} from "@noble/curves/utils"
|
||||
|
||||
const ALPHABET = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
|
||||
const PREFIX = "SN"
|
||||
const CODE_LENGTH = 30
|
||||
const CHECKSUM_BITS = 22n
|
||||
const ENTROPY_BYTES = 16
|
||||
|
||||
export function formatNameCode(entropy: Uint8Array): string {
|
||||
let n = (bytesToNumberBE(entropy) << CHECKSUM_BITS) + checksum(entropy)
|
||||
let body = ""
|
||||
for (let i = 0; i < CODE_LENGTH; i++) {
|
||||
body = ALPHABET[Number(n % 32n)] + body
|
||||
n /= 32n
|
||||
}
|
||||
return [PREFIX, ...body.match(/.{5}/g)!].join("-")
|
||||
}
|
||||
|
||||
export function parseNameCode(code: string): Uint8Array | null {
|
||||
const cleaned = code.replace(/[^\p{L}\p{N}]/gu, "").toUpperCase()
|
||||
if (!cleaned.startsWith(PREFIX) || cleaned.length !== PREFIX.length + CODE_LENGTH) return null
|
||||
let n = 0n
|
||||
for (const c of cleaned.slice(PREFIX.length)) {
|
||||
const v = charValue(c)
|
||||
if (v === undefined) return null
|
||||
n = n * 32n + BigInt(v)
|
||||
}
|
||||
const entropy = numberToBytesBE(n >> CHECKSUM_BITS, ENTROPY_BYTES)
|
||||
return n % (1n << CHECKSUM_BITS) === checksum(entropy) ? entropy : null
|
||||
}
|
||||
|
||||
function charValue(c: string): number | undefined {
|
||||
if (c === "I" || c === "L") return 1
|
||||
if (c === "O") return 0
|
||||
const v = ALPHABET.indexOf(c)
|
||||
return v < 0 ? undefined : v
|
||||
}
|
||||
|
||||
function checksum(entropy: Uint8Array): bigint {
|
||||
return bytesToNumberBE(sha256(entropy)) >> (256n - CHECKSUM_BITS)
|
||||
}
|
||||
Reference in new issue
Block a user