Binance Cross Chain BC→BSC Tranfer

Gabrio Tognozzi
3 min readOct 30, 2022
Photo by Toby Yang on Unsplash

After reading at the $570 million Binance Hack, I was determined to get my head around Blockchain Bridges, how do they actually work, the various players on the market and the technical details behind them.

This post may be part of a serie about what happened during the BSC Hack if I find time to write and study.

Intro: BNB Smart & Beacon Chain

Binance is rebranding it’s blockchain products. It is shifting towards the BNB (Build aNd Build) naming. The blockchain as a wholeis now called the BNB Chain, but it is actually composed of two parts:

  • BNB Beacon Chain (BC): implements a vision of a decentralized exchange (DEX) for digital assets. At the heart of Beacon Chain is a highly performant matching engine built on distributed consensus that aims to replicate the <1 second trading efficiency of current centralized exchanges.
  • BNB Smart Chain (BSC): implements an EVM Compatible blockchain.

The BC Chain is a Tendermint-based blockchain that was developed with a CrossChainModule specifically to support Cross-Chain transfer of goods.

BNB Cross-Chain Transfer Flow Diagram

Cross Chain BC→BSC Transfer

In order to transfer goods from the BC to the BSC, the owner of the token can execute a CLI command as follows :

bnbcli bridge transfer-out --to 0xEe9546E92e6876EdF6a234eFFbD72d75360d91f0 --expire-time 1597543193 --chain-id Binance-Chain-Tigris --from owner --amount 100000000:BNB --node <http://dataseed4.binance.org:80>

In the above command you use the BNB CLI interface to call the CrossChainModule interface, implemented by the BNB Beacon Chain, to create a transfer toward a BSC address, defining a certain amount of BNB.

The code in charge of handling the transfer-out can be found in the bridge plugin of the BC source code. The function handleTransferOutMsg() calls the publishCrossChainEvent() that in turn creates a CrossTransferEvent .

Once the event gets included into the blockchain, then a BSC Relayer will process it, motivated by a BNB Chain specific incentive. If the event is a Transfer Out event, then the Relayer calls thehandlePackage() function of the BSC: Cross-Chain Bridge Smart Contract with the packed read from the BNB Beacon Chain.

The BSC Cross Chain Bridge Smart Contract gets called, and the handlePackage() function gets executed. This method validates the Merkle Proof and, if it is valid, parses the payload and relies to the correct handler the msgBytes.

A Transfer Out in the BC Chain, results in a Transfer In in the BSC Chain. The msgBytes are sent, in the case of TRANSFER_IN_CHANNELID(2), to the TokenHub SmartContract, calling the handleSynPackage()method.

The TokenHub Smart Contract acts as a Valut facilitating cross-chain transactions between BNB Beacon Chain (BEP2) and Binance Smart Chain (BEP20). When the TokenHub SC receives the msgBytes , it decodes the data, and if everything comes out to be correct, it sends the BEP20 amount specified into the payload to the specified recipient

# Snippet taken from the doTransferIn() function of the tokenHub smart contract code.IBEP20(transInSynPkg.contractAddr).transfer{gas: MAX_GAS_FOR_CALLING_BEP20}(transInSynPkg.recipient, transInSynPkg.amount);

Conclusion

This whole web3 thing is awesome. Thank you for spending some time with my writings, see you soon!😃👋

--

--