Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/protocol/tests/helpers/calculate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { Hex } from 'viem'
import { encodePacked, keccak256, toHex } from 'viem'
import { encodePacked, isHex, keccak256, stringToHex, toHex } from 'viem'

export function calculateAtomId(atomData: Hex) {
export function calculateAtomId(atomData: string | Hex) {
const salt = keccak256(toHex('ATOM_SALT'))
// Convert to hex if it's not already hex-encoded, ensuring safe input to keccak256
const hexData = isHex(atomData) ? atomData : stringToHex(atomData)
return keccak256(
encodePacked(['bytes32', 'bytes'], [salt, keccak256(atomData)]),
encodePacked(['bytes32', 'bytes'], [salt, keccak256(hexData)]),
)
}

Expand Down
10 changes: 6 additions & 4 deletions packages/sdk/src/utils/calculate-atom-id.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { Hex } from 'viem'
import { encodePacked, keccak256, toHex } from 'viem'
import { encodePacked, isHex, keccak256, stringToHex, toHex } from 'viem'

/**
* Computes an atom ID by hashing the atom data with the ATOM_SALT.
* @param atomData Raw atom data as hex.
* @param atomData Raw atom data as string or hex.
* @returns Keccak256 hash representing the atom ID.
*/
export function calculateAtomId(atomData: Hex) {
export function calculateAtomId(atomData: string | Hex) {
const salt = keccak256(toHex('ATOM_SALT'))
// Convert to hex if it's not already hex-encoded, ensuring safe input to keccak256
const hexData = isHex(atomData) ? atomData : stringToHex(atomData)
return keccak256(
encodePacked(['bytes32', 'bytes'], [salt, keccak256(atomData)]),
encodePacked(['bytes32', 'bytes'], [salt, keccak256(hexData)]),
)
}
Loading