Skip to content

fix(tlock): harden hex helpers and add seal/open input validation - #200

Open
fadesany wants to merge 1 commit into
Sub-Rosa-Issue:mainfrom
fadesany:harden-hex-helpers-seal-open-validation
Open

fix(tlock): harden hex helpers and add seal/open input validation#200
fadesany wants to merge 1 commit into
Sub-Rosa-Issue:mainfrom
fadesany:harden-hex-helpers-seal-open-validation

Conversation

@fadesany

Copy link
Copy Markdown

Summary

Closes #191.

fromHex in packages/tlock/src/commitment.ts already validated hex strictly (even length, hex-only characters), but there was no exported predicate a caller could use to check a string before calling fromHex. This PR adds that helper and adds the guard clauses called for in the issue for sealBid/openBid.

  • isValidHex(hex: string): boolean — exported from commitment.ts and re-exported from the package root (index.ts). Returns true/false using the exact same rule fromHex enforces (optional 0x/0X prefix stripped, even length, hex-digits only), so isValidHex(s) and "fromHex(s) does not throw" always agree.
  • sealBid now throws round must be a positive integer, got <round> for a non-positive or non-integer Drand round, instead of letting an invalid round fail deep inside tlock-js's timelockEncrypt.
  • openBid now throws ciphertext must not be empty for an empty ciphertext, instead of an obscure downstream decode/decrypt failure.

Testing

  • packages/tlock/src/commitment.test.ts — new test asserting isValidHex agrees with fromHex's accept/reject behavior across the existing valid/invalid hex fixtures.
  • packages/tlock/src/seal.test.ts — new tests: sealBid rejects 0, -1, -100 rounds; openBid rejects an empty ciphertext.
  • Ran the full packages/tlock unit suite plus the live-quicknet seal.test.ts suite (node --import tsx --test ...) — all passing.
  • tsc --noEmit -p packages/tlock/tsconfig.json — clean.

🤖 Generated with Claude Code

- Export isValidHex from commitment.ts, a boolean predicate matching
  fromHex's own accept/reject decisions (even length, optional 0x
  prefix, hex-only characters), and re-export it from index.ts.
- sealBid now rejects a non-positive/non-integer Drand round with a
  clear error instead of letting it fail deep inside tlock-js.
- openBid now rejects an empty ciphertext with a clear error instead
  of an obscure downstream decode failure.
- Add unit tests for isValidHex and the new sealBid/openBid guard
  clauses.

Fixes Sub-Rosa-Issue#191
@fadesany
fadesany force-pushed the harden-hex-helpers-seal-open-validation branch from 93e8f25 to 64e3458 Compare August 25, 2026 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tlock: harden hex helpers and add seal/open input validation

1 participant