mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-11-22 01:27:39 +00:00
42 lines
1.6 KiB
TypeScript
42 lines
1.6 KiB
TypeScript
import {bytesToHex} from "@noble/curves/utils"
|
|
import {formatNameCode} from "../src/code"
|
|
import {makeCommitment, registerIntentDigest, type Registration} from "../src/registration"
|
|
import {signDigest, walletFromEntropy} from "../src/wallet"
|
|
import {chainId, duration, resolver, sponsor, tld} from "./config"
|
|
|
|
const wallet = walletFromEntropy(crypto.getRandomValues(new Uint8Array(16)))
|
|
element("code").textContent = formatNameCode(wallet.entropy)
|
|
|
|
element("register").addEventListener("click", () => {
|
|
const registration: Registration = {
|
|
label: (element("label") as HTMLInputElement).value,
|
|
tld,
|
|
owner: wallet.address,
|
|
duration,
|
|
secret: crypto.getRandomValues(new Uint8Array(32)),
|
|
resolver,
|
|
contactLinks: lines("contact"),
|
|
channelLinks: lines("channel"),
|
|
}
|
|
const nonce = 0n
|
|
const deadline = BigInt(Math.floor(Date.now() / 1000) + 3600)
|
|
const signature = signDigest(wallet.privateKey, registerIntentDigest(chainId, sponsor, registration, nonce, deadline))
|
|
element("commit-request").textContent = json({commitment: makeCommitment(registration)})
|
|
element("register-request").textContent = json({...registration, nonce, deadline, signature})
|
|
})
|
|
|
|
function element(id: string): HTMLElement {
|
|
return document.getElementById(id)!
|
|
}
|
|
|
|
function lines(id: string): string[] {
|
|
return (element(id) as HTMLTextAreaElement).value
|
|
.split("\n")
|
|
.map(line => line.trim())
|
|
.filter(line => line !== "")
|
|
}
|
|
|
|
function json(request: object): string {
|
|
return JSON.stringify(request, (_, v) => (v instanceof Uint8Array ? "0x" + bytesToHex(v) : typeof v === "bigint" ? v.toString() : v), 2)
|
|
}
|