mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-11-22 17:48:24 +00:00
round 7 concluded
This commit is contained in:
1 parent
1a0d2300f4
commit
9bc88d9910
5 files changed
+15
-19
No files matched your search
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -6,8 +6,7 @@
|
||||
"lib": ["ES2022"],
|
||||
"outDir": "dist",
|
||||
"rootDir": "src",
|
||||
"strict": true,
|
||||
"skipLibCheck": true
|
||||
"strict": true
|
||||
},
|
||||
"include": ["src/**/*.ts"]
|
||||
}
|
||||
@@ -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
|
||||
Reference in new issue
Block a user