From 0e47158f190222eff378b84e7a3389104219c71d Mon Sep 17 00:00:00 2001 From: Doge Date: Tue, 30 Jul 2024 00:19:24 +0800 Subject: [PATCH 1/3] Introduced TON Standard Interface Detection A new TON Enhancement Proposal (TEP) has been added, proposing a standard interface detection mechanism for the TON blockchain. This is inspired by Ethereum's EIP-165 and aims to standardize methods for detecting and confirming the implementation of interfaces in smart contracts using FIFT, FUNC, and TACT languages. The proposal includes detailed specifications, example implementations, rationale behind the proposal, implementation steps, backward compatibility considerations, test cases and security considerations. --- text/0180-ton-standard-interface-detection.md | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 text/0180-ton-standard-interface-detection.md diff --git a/text/0180-ton-standard-interface-detection.md b/text/0180-ton-standard-interface-detection.md new file mode 100644 index 00000000..1308284f --- /dev/null +++ b/text/0180-ton-standard-interface-detection.md @@ -0,0 +1,93 @@ +- **TEP**: [0180](https://github.com/ton-blockchain/TEPs/pull/0180) +- **title**: *TON Standard Interface Detection* +- **status**: Draft +- **type**: Core +- **authors**: [Dr. Awesome Doge](https://github.com/hacker-volodya), [Pei](p@tonx.tg), [CC, Wang](https://github.com/a2468834), [Wei Yi](https://github.com/WeiYiChiuAtTonfura) +- **created**: 30.07.2024 + +# Summary +This TEP proposes the introduction of a Standard Interface Detection mechanism for the TON blockchain, inspired by Ethereum's EIP-165. The proposed standard aims to standardize the methods for detecting and confirming the implementation of interfaces in smart contracts on the TON blockchain using FIFT, FUNC, and TACT languages. This will enhance interoperability and simplify the integration of various smart contracts within the TON ecosystem. + +# Motivation +In the TON blockchain ecosystem, there is a need for a standardized approach to detect and confirm the implementation of interfaces by smart contracts. This standardization will: +- Simplify the process of interface detection for developers. +- Improve interoperability between different TON-based smart contracts. +- Provide a clear and consistent method for confirming interface implementations. + +# Specification + +## 1. Interface Identifier + +For this TEP, an *interface* is composed of a set of get-methods. Every interface will have a unique 64-bit integer identifier (ID) that represents itself. The ID will be calculated by the XOR of all get-methods' `method-id` in that interface. + +## 2. Interface Detection +Smart contracts that are compliant with this TEP should implement the following get-method: + +```func +int supports_interface(int interface_id) method_id; +``` + +- `interface_id`: The 64-bit integer of the specific interface ID +- `supports_interface` will return `1` along with TVM `ExitCode=0` only if the contract implements the certain `interface_id`. + +## 3. Implementation Requirements +- Smart contracts must implement the `supports_interface` get-method to support interface detection. +- The `supports_interface` method must use a reasonable amount of gas to ensure efficiency. + +## 4. Example Implementations + +### Example in FUNC +```func +;; Constant +int SUPPORTS_INTERFACE_ID() asm "82022 PUSHINT"; ;; 0x14066 +int NFT_COLLECTION_INTERFACE_ID() asm "45682 PUSHINT"; ;; 0xB272 + +;; Get method + +(int, cell, slice) get_collection_data() method_id; +slice get_nft_address_by_index(int index) method_id; +(int, int, slice) royalty_params() method_id; +cell get_nft_content(int index, cell individual_nft_content) method_id; + +int supports_interface(int interface_id) method_id { + if ((interface_id == SUPPORTS_INTERFACE_ID()) | (interface_id == NFT_COLLECTION_INTERFACE_ID())) { + return 1; + } + else { + return 0; + } +} +``` + +```mermaid +sequenceDiagram + participant Caller + participant Contract + Caller->>Contract: supports_interface(interface_id) + Contract-->>Caller: 1 / 0 +``` + +# Rationale +The rationale behind this proposal is to provide a robust and standardized way to handle interface detection within the TON ecosystem. By adopting and adapting the proven methods from Ethereum (EIP-165), we can ensure a high level of interoperability and simplicity for smart contract developers. + +# Implementation +The implementation will involve: +- Developing libraries and tools to support the new standard. +- Updating relevant documentation and guides for developers. +- Conducting security audits and tests to ensure the robustness of the implementation. + +# Backward Compatibility +For the contracts that deployed before this TEP, we could get TVM `ExitCode=11` along with the arbitrary stack return values. Only when calling `supports_interface` and getting `1` without any TVM exit error is viewed as the successful case indecates the contract supports the certain interface. Therefore, this TEP shall be fully backward compatible. + +# Test Cases +Include detailed test cases to validate the `supports_interface` method for various smart contracts and interfaces. + +# Security Considerations +Discuss potential security risks and mitigation strategies, including: +- Ensuring the accuracy of the `supports_interface` method. +- Protecting against gas limit attacks. +- Verifying the authenticity of the interface IDs. + +--- + +By adopting this TEP, the TON blockchain will benefit from enhanced interoperability and simplified interface detection processes, facilitating more sophisticated and integrated smart contract interactions. From 589a43e056f5db4a38b12e872ceb47b1bab6c386 Mon Sep 17 00:00:00 2001 From: a2468834 Date: Fri, 2 Aug 2024 14:29:44 +0800 Subject: [PATCH 2/3] Fix typos. --- text/0180-ton-standard-interface-detection.md | 69 +++++++++++-------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/text/0180-ton-standard-interface-detection.md b/text/0180-ton-standard-interface-detection.md index 1308284f..6838e247 100644 --- a/text/0180-ton-standard-interface-detection.md +++ b/text/0180-ton-standard-interface-detection.md @@ -1,70 +1,88 @@ - **TEP**: [0180](https://github.com/ton-blockchain/TEPs/pull/0180) - **title**: *TON Standard Interface Detection* - **status**: Draft -- **type**: Core +- **type**: Contract Interface - **authors**: [Dr. Awesome Doge](https://github.com/hacker-volodya), [Pei](p@tonx.tg), [CC, Wang](https://github.com/a2468834), [Wei Yi](https://github.com/WeiYiChiuAtTonfura) - **created**: 30.07.2024 # Summary -This TEP proposes the introduction of a Standard Interface Detection mechanism for the TON blockchain, inspired by Ethereum's EIP-165. The proposed standard aims to standardize the methods for detecting and confirming the implementation of interfaces in smart contracts on the TON blockchain using FIFT, FUNC, and TACT languages. This will enhance interoperability and simplify the integration of various smart contracts within the TON ecosystem. +This TEP proposes introducing a Standard Interface Detection mechanism for the TON blockchain, inspired by Ethereum's EIP-165. The proposed standard aims to standardize the methods for detecting and confirming the implementation of interfaces in smart contracts on the TON blockchain using FunC, Fift, and Tact languages. This will enhance interoperability and simplify the integration of various smart contracts within the TON ecosystem. # Motivation -In the TON blockchain ecosystem, there is a need for a standardized approach to detect and confirm the implementation of interfaces by smart contracts. This standardization will: +In the TON blockchain ecosystem, there is a need for a standardized approach to detect and confirm the implementation of interfaces by smart contracts. This TEP will: - Simplify the process of interface detection for developers. - Improve interoperability between different TON-based smart contracts. - Provide a clear and consistent method for confirming interface implementations. # Specification -## 1. Interface Identifier +> The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “NOT RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in [RFC 2119](https://www.ietf.org/rfc/rfc2119.html) and [RFC 8174](https://www.ietf.org/rfc/rfc8174.html). -For this TEP, an *interface* is composed of a set of get-methods. Every interface will have a unique 64-bit integer identifier (ID) that represents itself. The ID will be calculated by the XOR of all get-methods' `method-id` in that interface. +## Interface Identifier -## 2. Interface Detection -Smart contracts that are compliant with this TEP should implement the following get-method: +For this TEP, an *interface* is composed of a set of get-methods. Every interface has a unique 64-bit integer identifier (ID) that represents itself. This ID SHALL be calculated by doing XOR of all get-methods' `method-id` in that interface. The detailed procedure of `method-id` can be found at [here](https://github.com/ton-blockchain/ton/blob/db505f45b2f3a4f6469e0cbb55a3fc3ffcef59c5/lite-client/lite-client.cpp#L1228-L1234). + +## Interface Detection +Smart contracts that are compliant with this TEP SHOULD implement the following get-method: ```func int supports_interface(int interface_id) method_id; ``` -- `interface_id`: The 64-bit integer of the specific interface ID -- `supports_interface` will return `1` along with TVM `ExitCode=0` only if the contract implements the certain `interface_id`. +- `interface_id` is the integer indicates the specific interface ID. +- `supports_interface` MUST return `TRUE` (e.g., an integer `-1` in FunC) along with TVM `ExitCode=0` only if the smart contract implements the certain `interface_id`. + +If the smart contract supports multiple interfaces (e.g., making a wallet v4r2 that is also a NFT collection), it is REQUIRED to return `TRUE` with respect to all the supported `interface_id`s. This can be done by store a dictionary variable in the contract storage. + +## Implementation Requirements +- Smart contracts MUST implement the `supports_interface` get-method to support interface detection. +- The `supports_interface` method SHOULD use a reasonable amount of gas to ensure efficiency. -## 3. Implementation Requirements -- Smart contracts must implement the `supports_interface` get-method to support interface detection. -- The `supports_interface` method must use a reasonable amount of gas to ensure efficiency. +## Example Implementation -## 4. Example Implementations +Here is an example NFT collection in FunC language that supports this TEP. For the sake of simplicity, we have omitted part of codes that is not related to this TEP (e.g., `recv_internal()`). -### Example in FUNC ```func ;; Constant + int SUPPORTS_INTERFACE_ID() asm "82022 PUSHINT"; ;; 0x14066 int NFT_COLLECTION_INTERFACE_ID() asm "45682 PUSHINT"; ;; 0xB272 ;; Get method -(int, cell, slice) get_collection_data() method_id; -slice get_nft_address_by_index(int index) method_id; -(int, int, slice) royalty_params() method_id; -cell get_nft_content(int index, cell individual_nft_content) method_id; +(int, cell, slice) get_collection_data() method_id {} +slice get_nft_address_by_index(int index) method_id {} +(int, int, slice) royalty_params() method_id {} +cell get_nft_content(int index, cell individual_nft_content) method_id {} int supports_interface(int interface_id) method_id { - if ((interface_id == SUPPORTS_INTERFACE_ID()) | (interface_id == NFT_COLLECTION_INTERFACE_ID())) { - return 1; + if (interface_id == SUPPORTS_INTERFACE_ID()) { + return -1; } - else { - return 0; + + if (interface_id == NFT_COLLECTION_INTERFACE_ID()) { + return -1; } + + return 0; } ``` +According to the content mentioned above, here is the list of `method_id`s. +- `get_collection_data`=`0x1905B` +- `get_nft_address_by_index`=`0x167A3` +- `royalty_params`=`0x14ED7` +- `get_nft_content`=`0x10B5D` + +Next, do the bitwise XOR procedure on all of them. Therefore, the desired `NFT_COLLECTION_INTERFACE_ID` constant value will be `0xB272`. + + ```mermaid sequenceDiagram participant Caller participant Contract Caller->>Contract: supports_interface(interface_id) - Contract-->>Caller: 1 / 0 + Contract-->>Caller: -1 / 0 ``` # Rationale @@ -77,10 +95,7 @@ The implementation will involve: - Conducting security audits and tests to ensure the robustness of the implementation. # Backward Compatibility -For the contracts that deployed before this TEP, we could get TVM `ExitCode=11` along with the arbitrary stack return values. Only when calling `supports_interface` and getting `1` without any TVM exit error is viewed as the successful case indecates the contract supports the certain interface. Therefore, this TEP shall be fully backward compatible. - -# Test Cases -Include detailed test cases to validate the `supports_interface` method for various smart contracts and interfaces. +For the contracts deployed before this TEP, we could get TVM `ExitCode=11` along with the arbitrary stack return values. Only when calling `supports_interface` and getting `TRUE` without any TVM exit error is viewed as the successful case indicates the contract supports the specific interface. Therefore, this TEP shall be fully backward compatible. # Security Considerations Discuss potential security risks and mitigation strategies, including: From dafb2c5a162add8fe3b34cb16e54706cead2566e Mon Sep 17 00:00:00 2001 From: a2468834 Date: Fri, 2 Aug 2024 17:23:15 +0800 Subject: [PATCH 3/3] Update contact information. --- text/0180-ton-standard-interface-detection.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/0180-ton-standard-interface-detection.md b/text/0180-ton-standard-interface-detection.md index 6838e247..26cd8a6e 100644 --- a/text/0180-ton-standard-interface-detection.md +++ b/text/0180-ton-standard-interface-detection.md @@ -2,7 +2,7 @@ - **title**: *TON Standard Interface Detection* - **status**: Draft - **type**: Contract Interface -- **authors**: [Dr. Awesome Doge](https://github.com/hacker-volodya), [Pei](p@tonx.tg), [CC, Wang](https://github.com/a2468834), [Wei Yi](https://github.com/WeiYiChiuAtTonfura) +- **authors**: [Dr. Awesome Doge](https://github.com/hacker-volodya), [Pei](p@tonx.tg), [CC Wang](https://github.com/ccwang-at-TONX), [Wei Yi](https://github.com/WeiYiChiuAtTonfura) - **created**: 30.07.2024 # Summary