Cocos2d clientside integration for Zilliqa Platform


As a game developer, today I’ll show how I did to connect cocos creator with zilliqa chain.

Structure diagram & sample app flow

Bundled zilliqa-js
Cocos creator app was run in browserify mode so we need bundle zilliqa-js with some adapt to make it work.

  • By default when we target webpack to ‘web’. The modules in @zilliqa will used end point ‘dist/index.umd.js’. But it was a bundled file, the sub modules as ‘bn.js’ be spam in many places. To solve this I make some alias to set end point link to ‘dist/index.js’.
  • Cocos creator don’t have util.inquire(“long”) so I removed it by a plugin.
  • Cocos creator don’t have window.crypto.getRandomValues so I added it in ‘A-zilliqa-polyfill-for-cocos.js’. That file must be load before zilliqa-js, so I placed it at root folder and the name begin with ‘A’.
cd deps
# install @zilliqa-js and devDependencies
yarn install

# bundle the sdk
yarn build

# copy the bundled ZilliqaJS (dist/zilliqa.cocos.js) to your project's asset/script directory inside zilliqa-sdk folder, 
# copy the A-zilliqa-polyfill-for-cocos.js to your project's asset/script. Make sure it will be loaded first.

Swapper layer
This layer was designed to support connect to zilliqa chain more easy. Like: create new account, store multiple accounts, deploy contract,… And when the zilliqa-js was be upgraded, we just need changed in this place.

  • getAllAccounts
  • getUserAddress
  • wasAuthenticated
  • connect
  • getNetworkId
  • loadKeyStore
  • saveKeyStore
  • generateNewAccount
  • authorizeAccount
  • logOut
  • sendFaucetRequest
  • getBalance
  • deployContract
  • loadContractFromAddress
  • getSmartContractInit
  • getSmartContractState
  • getSmartContractCode

Some questions

  • What happen if I trick user send zils to me? Was we need a trusted third party keep user account?



The wallet instance could handle the account/keyStore things.
And the transactions and contract could be handled by Core/Blockchain/Contract instance
You may split the dependencies for different use cases.


There are 2 class will interact with block chain:

ZilliqaNetwork have main role as a wallet, keep everything ready to work with block chain. (currently it include getSmartContractState, getSmartContractCode, getSmartContractInit because it wasn’t implement inside contract class)

Each contract will be bind with a contract binding. It will support to working with contract more easy.