Skip to content

Dapp Developer Guide

Zhiting Lin edited this page May 8, 2019 · 7 revisions

Bytom-Chrome-Extension Developer Guide

While Bytom Chrome Extension exposes the Bytom-JS-SDK, there are few things to keep in mind. Below are requirements for Bytom Chrome Extension support as well as some best practices to keep in mind.

Requirements

Http(s) - Web Server Required

Due to browser security restrictions, we can't communicate with dapps running on file://. Please use a local server for development.

Bytom-JS-SDK - Bytom Browser Environment Check

Extension injects Bytom object and convenience Bytom-JS-SDK library into the javascript context. Look for this before using your fallback strategy (local node / hosted node + in-dapp id mgmt / read-only / fail).

Note that the environmental Bytom check is wrapped in a window.addEventListener('load', ...) handler. This approach avoids race conditions with bytomJSSDK injection timing.

Environmental Bytom contains all the account and keys relevant functionality.

window.addEventListener('load', async function() {
  if (typeof window.bytom !== 'undefined') {

    let networks = {
       testnet: 'http://app.bycoin.io:3020/',
       mainnet: 'https://api.bycoin.im:8000/'
    };

    try {
      const bytom = new Bytom(networks, '')
      bytom.setNetType(window.bytom.net)
    } catch (err) {
      console.log(err);
    }

  }

 startApp();
});

All Async - Think of Bytom-Chrome-extension as a light client

The user does not have the full blockchain on their machine, so data lookups can be a little slow. Bytom-Chrome-Extension store doing the account and the key management in the indexedDB. Please DO NOT delete the Bytom-Chrome-Extension, as it store all the keys and acount information.

Best Practices

Network check

When a user interacts with a dapp via Bytom-JS-SDK, they may be on the mainnet or testnet.

For example:

 window.bytom.net

Account management and transaction signing is managed externally to the dapp

Account management and the sent transaction function are managed by the Chrome-Extension. Chrome-extension wrap up all the both the basic transaction and the advanced transaction functionality.

Normal transaction calls:

 window.bytom.send_transaction({
   from:"",
   to:"",
   asset:"",
   amount:"",
   confirmations:1
 }) 

Advanced transaction calls:

 window.bytom.send_advanced_transaction({
   input:"",
   output:"",
   gas:400000,
   args:{},
   confirmations:1
 }) 
   .then((resp) => {
     //transaction_hash
     console.log(resp)
 })

Listening for Selected Account Changes

Since these variables reflect user intention, but do not (currently) have events representing their values changing, we somewhat reluctantly recommend using an interval to check for account changes.

For example, if your application only cares about the value of Bytom-Chrome-Extension's current account, you might add some code like this somewhere in your application:

let accountInterval = setInterval(async function() {
const account = window.bytom.default_account
if ( account.guid !==currentAccount.guid) {
      updateInterface();
}
}, 10000);
Clone this wiki locally