fix(sdk): Allow calculateAtomId to accept string | Hex for better DX#1194
fix(sdk): Allow calculateAtomId to accept string | Hex for better DX#1194jeremie-olivier wants to merge 2 commits into
Conversation
## Problem
The calculateAtomId function currently has a type signature that only accepts
Hex, but the runtime behavior and documentation suggest it should accept strings
as well. This creates a developer experience issue where:
1. Documentation shows: calculateAtomId("completed")
2. TypeScript errors: Argument of type 'string' is not assignable to parameter of type 'Hex'
3. Developers must manually convert: calculateAtomId(stringToHex("completed"))
## Evidence
- Documentation examples use strings directly: https://docs.intuition.systems/docs/intuition-sdk/atoms-guide#calculateatomid
- Developer report of DX issue: https://discord.com/channels/909531430881746974/1416097676772249753/1494751047909118155
- Demonstration repository: https://github.com/jeremie-olivier/intuition-calculateAtomId-demo
## Solution
Update type signature from:
```typescript
function calculateAtomId(atomData: Hex): string
```
To:
```typescript
function calculateAtomId(atomData: string | Hex): string
```
Implementation uses explicit conversion to ensure type safety:
- If string input → convert with stringToHex()
- If hex input → use directly
## Changes
- packages/sdk/src/utils/calculate-atom-id.ts: Updated type signature and implementation
- packages/protocol/tests/helpers/calculate.ts: Updated for consistency
## Benefits
- ✅ Types match documentation examples
- ✅ Better developer experience (no forced stringToHex calls)
- ✅ Backward compatible (existing hex usage still works)
- ✅ Type safe (explicit conversion rather than assertions)
- ✅ Performance optimized (only converts when needed)
Code Review —
|
| Status | |
|---|---|
| Motivation / DX intent | ✅ Valid and well-argued |
| Type signature change | ✅ Correct direction |
| Runtime implementation | ❌ Always invokes stringToHex, including for existing Hex inputs |
| Backward compatibility | ❌ Silently changes hashes for existing hex callers |
| New tests | ❌ Missing |
The fix is one line away from being correct — please update the typeof guard to a startsWith('0x') check (or decide on the "strings only" approach) and add a regression test before merging.
Address critical bug identified in code review where typeof check was always
true for both strings and Hex values, causing double-encoding of hex inputs.
## Problem Fixed
- typeof atomData === 'string' was always true since Hex is `0x${string}` (still a JS string)
- This caused stringToHex() to always run, double-encoding hex inputs
- Result: calculateAtomId("0xdeadbeef") produced wrong hash vs old implementation
## Solution
- Use viem's isHex() utility to properly detect hex-encoded values
- isHex(atomData) ? atomData : stringToHex(atomData)
- Preserves backward compatibility for existing hex users
- Only converts plain strings to hex when needed
## Benefits
- ✅ Correct runtime behavior for both input types
- ✅ Maintains backward compatibility
- ✅ Uses proper viem utilities instead of fragile typeof checks
- ✅ Same atom IDs as before for existing hex inputs
Code ReviewOverviewThis PR fixes a real DX pain point: What's Good
Issues / Suggestions1. PR description contains a subtly buggy code snippetThe description shows: const hexData = typeof atomData === 'string' ? stringToHex(atomData) : atomDataSince 2. No new tests for plain-string inputThe PR description says "all existing tests pass" but doesn't add tests for the new behaviour — i.e. that it('produces the same id for a string and its hex equivalent', () => {
expect(calculateAtomId('completed')).toBe(
calculateAtomId(stringToHex('completed')),
)
})3. Missing changesetThis is a user-visible behaviour change in 4. Minor:
|
Problem
The
calculateAtomIdfunction currently has a restrictive type signature that only acceptsHex, but this creates a poor developer experience where documentation and expected usage patterns don't match the actual types.Evidence of the Issue
Documentation mismatch: Official docs show examples using strings directly:
Developer pain point: Real user experiencing this DX issue in Discord:
Discord thread
Comprehensive testing: Demo repository proving that both string and hex inputs work identically at runtime but TypeScript types are restrictive.
Solution
Update the type signature from:
To:
Implementation Details
stringToHex()rather than type assertionstypeofcheckBenefits
Changes
packages/sdk/src/utils/calculate-atom-id.ts: Updated type signature and implementationpackages/protocol/tests/helpers/calculate.ts: Updated for consistencyTesting
This change aligns the SDK with its documentation and significantly improves developer experience while maintaining full backward compatibility and type safety.