-
Notifications
You must be signed in to change notification settings - Fork 3
Dapp 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.
Due to browser security restrictions, we can't communicate with dapps running on file://
. Please use a local server for development.
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();
});
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.
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 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)
})
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);