From 9bc88d9910728905badf4e70a74f991d20dbeb84 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Tue, 13 Oct 2026 18:47:09 +0200 Subject: [PATCH] round 7 concluded --- names-web/src/code.ts | 3 +-- names-web/src/registration.ts | 8 ++++---- names-web/test/page.spec.ts | 3 +-- names-web/tsconfig.json | 3 +-- tests/NamesWebTests.hs | 17 ++++++++--------- 5 files changed, 15 insertions(+), 19 deletions(-) diff --git a/names-web/src/code.ts b/names-web/src/code.ts index 96e693f0f..9b0ebb097 100644 --- a/names-web/src/code.ts +++ b/names-web/src/code.ts @@ -5,7 +5,6 @@ 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) @@ -26,7 +25,7 @@ export function parseNameCode(code: string): Uint8Array | null { if (v === undefined) return null n = n * 32n + BigInt(v) } - const entropy = numberToBytesBE(n >> CHECKSUM_BITS, ENTROPY_BYTES) + const entropy = numberToBytesBE(n >> CHECKSUM_BITS, 16) return n % (1n << CHECKSUM_BITS) === checksum(entropy) ? entropy : null } diff --git a/names-web/src/registration.ts b/names-web/src/registration.ts index 7e5f9f88e..5b0f557d1 100644 --- a/names-web/src/registration.ts +++ b/names-web/src/registration.ts @@ -83,7 +83,7 @@ function abiTuple(values: AbiValue[]): Uint8Array { let offset = 32 * values.length const heads = values.map(v => { if (!v.dynamic) return v.bytes - const head = uint256(BigInt(offset)) + const head = uint256(offset) offset += v.bytes.length return head }) @@ -91,14 +91,14 @@ function abiTuple(values: AbiValue[]): Uint8Array { } function abiBytes(bytes: Uint8Array): Uint8Array { - return concatBytes(uint256(BigInt(bytes.length)), bytes, new Uint8Array((32 - (bytes.length % 32)) % 32)) + return concatBytes(uint256(bytes.length), bytes, new Uint8Array((32 - (bytes.length % 32)) % 32)) } function abiArray(items: Uint8Array[]): Uint8Array { - return concatBytes(uint256(BigInt(items.length)), abiTuple(items.map(dynamic))) + return concatBytes(uint256(items.length), abiTuple(items.map(dynamic))) } -function uint256(n: bigint): Uint8Array { +function uint256(n: number | bigint): Uint8Array { return numberToBytesBE(n, 32) } diff --git a/names-web/test/page.spec.ts b/names-web/test/page.spec.ts index 09d6dd47c..509f6fe84 100644 --- a/names-web/test/page.spec.ts +++ b/names-web/test/page.spec.ts @@ -16,8 +16,7 @@ test("prepares a commitment and a registration signed by the code's wallet", asy const request = JSON.parse(await page.locator("#register-request").innerText()) const wallet = walletFromEntropy(parseNameCode(code)!) - expect(request.owner).toBe(bytesToHex(wallet.address)) - expect(request).toMatchObject({label: "bakery1", contactLinks: ["https://smp.simplex.im/a#contact"], channelLinks: ["https://smp1.simplex.im/c#one", "https://smp2.simplex.im/c#two"]}) + expect(request).toMatchObject({label: "bakery1", owner: bytesToHex(wallet.address), contactLinks: ["https://smp.simplex.im/a#contact"], channelLinks: ["https://smp1.simplex.im/c#one", "https://smp2.simplex.im/c#two"]}) const registration: Registration = { ...request, diff --git a/names-web/tsconfig.json b/names-web/tsconfig.json index da6976637..496831b6a 100644 --- a/names-web/tsconfig.json +++ b/names-web/tsconfig.json @@ -6,8 +6,7 @@ "lib": ["ES2022"], "outDir": "dist", "rootDir": "src", - "strict": true, - "skipLibCheck": true + "strict": true }, "include": ["src/**/*.ts"] } diff --git a/tests/NamesWebTests.hs b/tests/NamesWebTests.hs index f844d823b..b0210a0fb 100644 --- a/tests/NamesWebTests.hs +++ b/tests/NamesWebTests.hs @@ -63,8 +63,8 @@ testNameCodes = do ("9e885d952ad362caeb4efe34a8e91bd2", "SN-KT45V-59ATD-HCNTT-EZRTA-HT8VT-8A0BT") ] $ \(entropy, code) -> do - callNode ("formatNameCode(" <> jsHex entropy <> ")") `shouldReturn` BC.pack code - callNode ("parseNameCode('" <> map toLower code <> "')") `shouldReturn` BC.pack entropy + callNode ("formatNameCode(" <> jsHex entropy <> ")") `shouldReturn` code + callNode ("parseNameCode('" <> map toLower code <> "')") `shouldReturn` entropy callNode "parseNameCode('SN-OOOOO-OOOOO-OOOOO-OOOOO-OOOOO-OVME2')" `shouldReturn` "00000000000000000000000000000000" callNode "String(parseNameCode('SN-KT45V-59ATD-HCNTT-EZRTA-HT8VT-8A0BV'))" `shouldReturn` "null" @@ -84,12 +84,11 @@ testChatVectors = do <> jsHex "2279b7a0a67db372996a5fab50d91eaa73d2ebe6" <> ", contactLinks: ['https://smp.simplex.im/a#contact'], channelLinks: ['https://smp1.simplex.im/c#one', 'https://smp2.simplex.im/c#two']}" -callNode :: String -> IO ByteString +callNode :: String -> IO String callNode expr = - BC.pack - <$> readCreateProcess - (proc "node" ["--input-type=module", "-e", imports <> "const v = " <> expr <> "; process.stdout.write(typeof v === 'string' ? v : Buffer.from(v).toString('hex'));"]) {cwd = Just namesWebDir} - "" + readCreateProcess + (proc "node" ["--input-type=module", "-e", imports <> "const v = " <> expr <> "; process.stdout.write(typeof v === 'string' ? v : Buffer.from(v).toString('hex'));"]) {cwd = Just namesWebDir} + "" where imports = "import {formatNameCode, parseNameCode} from './dist/code.js';" @@ -102,5 +101,5 @@ jsUint8 bs = "new Uint8Array([" <> intercalate "," (map show $ B.unpack bs) <> " jsHex :: String -> String jsHex = jsUint8 . either error id . BAE.convertFromBase BAE.Base16 . BC.pack -hex :: ByteString -> ByteString -hex = BAE.convertToBase BAE.Base16 +hex :: ByteString -> String +hex = BC.unpack . BAE.convertToBase BAE.Base16