As a game developer, today I’ll show how I did to connect cocos creator with zilliqa chain.
Structure diagram & sample app flow
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.
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.
- What happen if I trick user send zils to me? Was we need a trusted third party keep user account?