02 Architecture Audit Ternoa Bridge Relayer
02 Architecture Audit Ternoa Bridge Relayer
02 Architecture Audit Ternoa Bridge Relayer
Bridge Relayer
SMART CONTRACT AUDIT
24.05.2022
1. Disclaimer.................................................................................................................................................................................................................... 3
2. About the Project and Company ............................................................................................................................................................................. 4
2.1 Project Overview .................................................................................................................................................................................................. 5
3. Vulnerability & Risk Level ......................................................................................................................................................................................... 6
4. Auditing Strategy and Techniques Applied ............................................................................................................................................................ 7
4.1 Methodology ......................................................................................................................................................................................................... 7
5. Metrics ......................................................................................................................................................................................................................... 8
5.1 Tested Files .......................................................................................................................................................................................................... 8
5.2 Dependencies ...................................................................................................................................................................................................... 9
5.3 Codebase Overview ............................................................................................................................................................................................ 9
5.4 Architecture Overview ....................................................................................................................................................................................... 11
6. Scope of Work .......................................................................................................................................................................................................... 12
6.1 Findings Overview ............................................................................................................................................................................................. 13
6.2 Manual and Automated Vulnerability Test ..................................................................................................................................................... 14
6.2.1 Parameter Pending Is Missing ..................................................................................................................................................................... 14
6.2.2 No Error Handling For pollBlocks() .............................................................................................................................................................. 15
6.2.3 Incorrect Assertion Of GasPrice And GasLimit ......................................................................................................................................... 16
6.2.4 Incorrect Operator Used ................................................................................................................................................................................ 17
6.2.5 Feature Chain ID Problems > 255 ............................................................................................................................................................... 18
6.2.6 Variables Could Be Declared As Constant ................................................................................................................................................ 19
6.2.7 Dead Or Commented Out Code .................................................................................................................................................................. 19
6.2.8 Parameter Restriction .................................................................................................................................................................................... 20
1. Disclaimer
The audit makes no statements or warrantees about utility of the code, safety of the code, suitability of the business model, investment
advice, endorsement of the platform or its products, regulatory regime for the business model, or any other statements about fitness of
the contracts to purpose, or their bug free status. The audit documentation is for discussion purposes only.
The information presented in this report is confidential and privileged. If you are reading this report, you agree to keep it confidential,
not to copy, disclose or disseminate without the agreement of CAPSULE CORP. LABS SASU. If you are not the intended receptor of
this document, remember that any disclosure, copying or dissemination of it is forbidden.
Company address:
Website: https://www.ternoa.com
Twitter: https://twitter.com/ternoa_
GitHub: https://github.com/capsule-corp-ternoa
Telegram: https://t.me/ternoa
Discord: https://discord.gg/cNZTGtGJNR
Twitch: https://www.twitch.tv/ternoatv
Instagram: https://www.instagram.com/ternoa_
Medium: https://medium.com/ternoa
LinkedIn: https://www.linkedin.com/company/ternoa
YouTube: https://www.youtube.com/channel/UCUYvbtRE5HoWPz7z88V7Khw
Ternoa’s unique innovation include the ability for users to store private data within NFTs, user-specified conditions for automated
transfers of the secret NFTs to other wallets, lending or sharing NFTs with others while retaining full-rights and NFT ownership,
combining multiple NFTs into a new NFT, and sharing of NFT ownership through fractionalization.
Ternoa Bridge
A blockchain bridge provides a connection that allows for the transfer of tokens between two different blockchain ecosystems. A bridge
is required for Ternoa because as of the ICO date, CAPS were ERC-20 tokens. However, native CAPS are necessary to take part in
staking and rewards on Ternoa, as well as paying transaction fees.
The Ternoa bridge enables CAPS ERC-20 holders to transfer the token onto the Ternoa blockchain, on a 1:1 ratio. Unlike many other
bridges, the Ternoa Bridge will lock the ERC20 CAPS and mint the equivalent in native CAPS on the Ternoa Network.
In this manner, if you decide to sell your native CAPS, you will need to transfer them back to the Ethereum network by using the
Ternoa Bridge again. ERC20 contracts are built using ChainBridge, a modular Multi-Directional Blockchain Bridge to interact with
Ethereum, Substrate, based chains networks.
Documentation: https://docs.ternoa.network/v/bridge/
Frontend: https://bridge.ternoa.com
4.1 Methodology
The auditing process follows a routine series of steps:
3. Best practices review, which is a review of the codebase to improve efficiency, effectiveness, clarify, maintainability,
security, and control based on the established industry and academic practices, recommendations, and research.
4. Specific, itemized, actionable recommendations to help you take steps to secure your codebase.
The following are the MD5 hashes of the reviewed files. A file with a different MD5 hash has been modified, intentionally or otherwise,
after the security review. You are cautioned that a different MD5 hash could be (but is not necessarily) an indication of a changed
condition or potential vulnerability that was not within the scope of the review
@badrap/result https://www.npmjs.com/package/@badrap/result
@polkadot/api https://www.npmjs.com/package/@polkadot/api
ethers https://www.npmjs.com/package/ethers
Source: https://github.com/capsule-corp-ternoa/ternoa-bridge-core
src/chains/ethereum/connection.ts TypeScript 24 0 3 27
src/chains/ethereum/writer.ts TypeScript 26 4 7 37
src/chains/substrate/chain.ts TypeScript 64 7 16 87
src/chains/substrate/connection.ts TypeScript 34 0 7 41
src/utils/blockstore.ts TypeScript 25 0 3 28
src/utils/chain-interface.ts TypeScript 67 0 6 73
src/utils/core.ts TypeScript 54 0 12 66
src/utils/msg.ts TypeScript 40 0 6 46
src/utils/system.ts TypeScript 34 0 10 44
Ternoa Bridge is built on ChainBridge, a modular Multi-Directional Blockchain Bridge to interact with Ethereum, Substrate, based
chains networks. ChainBridge is an extensible cross-chain communication protocol. It currently supports bridging between EVM and
Substrate based chains.
A bridge contract (or pallet in Substrate) on each chain forms either side of a bridge. Handler contracts allow for customizable
behaviour upon receiving transactions to and from the bridge. For example locking up an asset on one side and minting a new one on
the other.
The main goal of this audit was to make sure the infrastructure is built according to newest standards and securely developed. The
auditors can provide additional feedback on the code upon the client’s request.
CRITICAL
9 HIGH
CRITICAL ISSUES
During the audit, Chainsulting‘s experts found 0 Critical issues in the code of the smart contract.
HIGH ISSUES
During the audit, Chainsulting’s experts found 0 High issues in the code of the smart contract.
MEDIUM ISSUES
During the audit, Chainsulting’s experts found 2 Medium issues in the code of the smart contract.
Attack / Description getTransactionCount() should have the second parameter set to “pending”. Because the default is
“latest” which could result in using a nonce which is in use by a pending transaction. Also keeping
an internal count of the current nonce and comparing it with getTransactionCount() can lead to
better results in situations where the RPC server might not be fully sync with the blockchain or
crashed.
Result/Recommendation We recommend to add a second parameter “pending” to getTransactionCount() and internal count
& compare of the current nonce.
Attack / Description If pollBlocks() throws or returns an error, it will just be displayed. There is no error handling to
restart pollBlocks() or to exit the process, to have it auto restarted externally. This can lead to
bridge errors and outages.
Line 41 - 50 (src/chains/substrate/listener.ts)
Result/Recommendation We recommend to add an error handling, to restart pollBlocks() or to exit the process.
LOW ISSUES
During the audit, Chainsulting’s experts found 5 Low issues in the code of the smart contract.
Attack / Description The types of gasPrice and gasLimit should not be asserted to BigNumber. gasPrice and gasLimit
should be checked for being undefined and an error should be thrown if they are. If they are
undefined and be used as BigNumber an error would be thrown anyway.
// Calculate fee
const voteFee = (options.gasPrice as BigNumber).mul(options.gasLimit as BigNumber);
Result/Recommendation We recommend to remove the assertion of gasPrice and gasLimit from BigNumber. gasPrice and
gasLimit should be checked for being undefined and an error should be thrown if they are.
Attack / Description An operator defines some function that will be performed on the data. The data on which operators
work are called operands. Wrong operators can lead to undefined results.
Attack / Description If originDomainID (chain ID) is greater than 255 the resulting value will be ambiguous for some
depositNonce values.
Result/Recommendation There's a bunch of other chains using large values for the chain id (https://github.com/ethereum-
lists/chains), better to increase depositNonce unit
Attack / Description Variables can be declared using const similar to var or let declarations. The const makes a variable
a constant where its value cannot be changed. Const variables have the same scoping rules as let
variables. Const variables must be declared and initialized in a single statement.
Attack / Description Programmers should not comment out code as it bloats programs and reduces readability.
Result/Recommendation The comment is false. There is no check. Unused code should be deleted and can be retrieved
from source control history if required.
INFORMATIONAL ISSUES
During the audit, Chainsulting’s experts found 2 Informational issues in the code of the smart contract.
Attack / Description The parameter “chainName” could be restricted to names of chains that are used to prevent typos
on calling the function.
Result/Recommendation We recommend to restrict the parameter to for example chainName: “substrate” | “ethereum”
Attack / Description Certain methods instead of cmp() will improve the code readability
The Ternoa Bridge is based on unchanged code from Chainbridge, which was audited by consensys and was not part of this audit.
Source: https://github.com/ChainSafe/ChainBridge
Documentation: https://chainbridge.chainsafe.io
7. Executive Summary
Two (2) independent Chainsulting experts performed an unbiased and isolated audit of the architecture and codebase. The
final debriefs took place on the May 19, 2022.
The main goal of this audit was to make sure the bridge relayer is built according to newest standards and securely developed. During
the audit, no critical, no high, 2 medium, 5 low and 3 informational issues were found, after the manual and automated security testing.
Chainsulting is a professional software development firm, founded in 20217 and based in Germany. They show ways, opportunities,
risks and offer comprehensive blockchain solutions. Some of their services include blockchain development, smart contract audits and
consulting.
Chainsulting conducts code audits on market-leading blockchains such as Hyperledger, Tezos, Ethereum, Binance Smart Chain, and
Solana to mitigate risk and instil trust and transparency into the vibrant crypto community. They have also reviewed and secure the
smart contracts of 1Inch, POA Network, Unicrypt, Amun, Furucombo among numerous other top DeFi projects.
Chainsulting currently secures $100 billion in user funds locked in multiple DeFi protocols. The team behind the leading audit firm relies
on their robust technical know-how in the blockchain sector to deliver top-notch smart contract audit solutions, tailored to the clients'
evolving business needs.
Check our website for further information: https://chainsulting.de