-
Notifications
You must be signed in to change notification settings - Fork 58
feat(foundations): normalized hash #1881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
memearchivarius
wants to merge
14
commits into
main
Choose a base branch
from
1877-normalized-hash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c7729b0
draft
memearchivarius 143f787
Update normalized-hash.mdx
memearchivarius 908e80f
Merge branch 'main' into 1877-normalized-hash
novusnota deb2ec4
ai suggestion
novusnota db9cba2
Merge branch 'main' into 1877-normalized-hash
novusnota 4e907a5
rework
memearchivarius 5b9539f
TonAPI
memearchivarius c2fa653
Update normalized-hash.mdx
memearchivarius 193ceb9
Merge branch 'main' into 1877-normalized-hash
memearchivarius a9870ea
Update message-lookup.mdx
memearchivarius 1464a96
Merge branch '1877-normalized-hash' of https://github.com/ton-org/doc…
memearchivarius a37fd48
Apply suggestion from @github-actions[bot]
memearchivarius 5ebb1ff
Revised support section
memearchivarius 7f9c233
lower case link
memearchivarius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| --- | ||
| title: "Normalized message hash" | ||
| sidebarTitle: "Normalized hash" | ||
| --- | ||
|
|
||
| import { Aside } from "/snippets/aside.jsx"; | ||
|
|
||
| The normalized message hash is a consistent and reliable identifier for [external-in](/foundations/messages/external-in) messages. It remains constant regardless of variations in the `src`, `import_fee`, and `init` fields, which can change without affecting the validity of the message. This hash is particularly useful for tracking transactions. | ||
novusnota marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| <Aside type="note"> | ||
| For the canonical specification, refer to [TEP-467: Normalized Message Hash](https://github.com/ton-blockchain/TEPs/blob/master/text/0467-normalized-message-hash.md). | ||
| </Aside> | ||
memearchivarius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Why normalization is needed | ||
|
|
||
| In TON, an external-in message can be constructed and packed in different ways that are all logically identical. Fields such as the source address (`src`), the `import_fee`, and the state initialization (`init`) can vary depending on the sender's implementation or the API provider processing the message. | ||
memearchivarius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Because these non-essential fields are included when computing the standard cell hash, functionally identical messages can produce different message hashes. This variability complicates reliable transaction tracking and deduplication, as highlighted in the [TON Console transaction tracking guide](https://docs.tonconsole.com/academy/transaction-tracking). | ||
|
|
||
| By applying normalization rules before calculating the hash, the variable fields are removed or zeroed out, ensuring that the same logical message always produces the exact same hash. | ||
|
|
||
| ## Calculate the normalized hash | ||
|
|
||
| The normalized hash is computed by standardizing the `ext_in_msg_info$10` fields before serializing the message. This approach is independent of any specific SDK and can be implemented in any language. | ||
|
|
||
| According to TEP-467, apply the following rules: | ||
|
|
||
| 1. **Source Address (`src`)**: Set to `addr_none$00`. | ||
| 2. **Import Fee (`import_fee`)**: Set to `0`. | ||
| 3. **InitState (`init`)**: Set to `nothing$0`. | ||
| 4. **Body (`body:(Either X ^X)`)**: Always store as a reference (`^X`). The content of the body itself is included without modification. | ||
|
|
||
| After constructing the cell with these standardized values, calculate its standard [cell hash](/foundations/serialization/cells) (the SHA-256 of the root cell). | ||
memearchivarius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ### Low-level construction example | ||
|
|
||
| The following TypeScript example demonstrates the raw cell construction. This logic illustrates the underlying TL-B serialization and can be adapted to any programming language or SDK. | ||
|
|
||
| ```typescript | ||
| import { beginCell, Cell, Address } from "@ton/core"; | ||
|
|
||
| function normalizeExternalMessage(destAddress: Address, bodyCell: Cell): Cell { | ||
memearchivarius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return beginCell() | ||
| .storeUint(0b10, 2) // external-in message prefix | ||
| .storeUint(0b00, 2) // src: addr_none$00 | ||
| .storeAddress(destAddress) // dest: MsgAddressInt | ||
| .storeCoins(0) // import_fee: 0 | ||
| .storeBit(false) // init: nothing$0 | ||
| .storeBit(true) // body: stored as a reference (^X) | ||
| .storeRef(bodyCell) // The actual body cell | ||
| .endCell(); | ||
| } | ||
|
|
||
| // Compute the standard cell hash | ||
| const normalizedExtMessage = normalizeExternalMessage(destAddress, bodyCell); | ||
| const normalizedHash = normalizedExtMessage.hash().toString("hex"); | ||
| ``` | ||
|
|
||
| ## Tools and SDK support | ||
|
|
||
| Many tools and libraries in the TON ecosystem support the normalized message hash either natively or through API integration. | ||
|
|
||
| | Tool | Support | Details | | ||
| |------|---------|---------| | ||
| | **ton/ton** (`@ton/ton`) | Yes | Use `storeMessage(..., { forceRef: true })` and custom logic to clear fields. See [Message lookup](/ecosystem/ton-connect/message-lookup) for an example. | | ||
| | **TonCenter** | Yes | API methods such as `sendBocReturnHash` return the normalized hash; transaction lookups support it. | | ||
| | **TonAPI** | Yes | [Transaction tracking](https://docs.tonconsole.com/tonapi/cookbook/transaction-tracking) and API lookups natively use the normalized hash. | | ||
| | **ton-blockchain/ton** | Yes | Supported at the node level ([ton-blockchain/ton#1557](https://github.com/ton-blockchain/ton/pull/1557)). | | ||
|
|
||
| <Aside type="tip"> | ||
| For a practical guide on searching for transactions using TON Connect and the normalized hash, see the [Message lookup](/ecosystem/ton-connect/message-lookup) guide. | ||
| </Aside> | ||
|
|
||
| ## Uniqueness constraints | ||
|
|
||
| The normalized message hash is **not** a globally unique identifier across the entire blockchain. It is possible for custom smart contracts to process identical external messages that produce the same normalized hash, resulting in multiple distinct transaction chains. | ||
memearchivarius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| However, the normalized message hash serves as a unique identifier for a specific transaction trace when the following conditions are met: | ||
|
|
||
| - The message is sent to a [wallet contract](/standard/wallets/comparison). | ||
| - The message includes an instruction to send an internal message with the `IGNORE_ERRORS=+2` flag (this is standard behavior for [TON Connect](/ecosystem/ton-connect/overview) and for wallets starting from v5). | ||
| - The wallet contract is never deleted. | ||
|
|
||
| <Aside type="note"> | ||
| For dApps interacting with user wallets via TON Connect, or for exchanges controlling their outbound external messages, it is safe to reference traces using the normalized hash. This applies even before the transactions are finalized. | ||
memearchivarius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </Aside> | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.