Detect the wallet
window.bitshares
The extension injects a Object.freezed object into the page's MAIN world and defines it as window.bitshares (non-writable, non-configurable, enumerable). If the page already has an object under that name, the extension does not replace it.
The object has only these members:
| Member | Type | Meaning |
|---|---|---|
isCubWallet | true | Identifies the provider as CubWallet |
isBtsWallet | true | Compatibility flag for dApps written against BTS Wallet 0.16 and earlier. New code should check isCubWallet first |
request({ method, params }) | Promise | Call an RPC method |
on(event, fn) | void | Subscribe to an event |
removeListener(event, fn) | void | Unsubscribe |
There are no synchronous fields such as selectedAddress or chainId, and no enable() method. Read the network and accounts with request.
Announcement event bitshares#initialized
After the injection, the extension dispatches a regular DOM event on window:
window.dispatchEvent(new Event('bitshares#initialized'));This is not a provider.on(...) event; listen with window.addEventListener('bitshares#initialized', ...). If the provider is already present when your script runs, the event will not fire again, so always do both:
function whenBitsharesReady(cb) {
if (window.bitshares) {
cb(window.bitshares);
return;
}
window.addEventListener('bitshares#initialized', () => {
if (window.bitshares) cb(window.bitshares);
}, { once: true });
}When the provider is injected
The background accepts a connection only when all of the following hold, and the page can use the provider only in those cases:
- The origin is
https://host(a standard https origin, no path in the origin itself), orhttp://localhost[:port], orhttp://127.0.0.1[:port]. - The page is the top-level frame (
frameId === 0). An iframe does not get the provider. - The connection comes from this extension's content script (
port.senderis set by the browser and cannot be forged by the page).
So:
- Opening a local HTML file with
file://fails. Serve the folder, for examplepython3 -m http.server 8080, and openhttp://localhost:8080/. - Public or LAN
http://origins other than localhost / 127.0.0.1 are not injected. - If the extension is not installed, is disabled, or is disabled on this site,
window.bitsharesisundefined.
The origin the wallet sees is port.sender.origin from the browser. A domain the page claims for itself is ignored. The same origin is shown in the approval window and used for permissions.
Recognizing CubWallet
const bts = window.bitshares;
if (bts?.isCubWallet) {
// CubWallet
} else if (bts?.isBtsWallet) {
// Older builds used this flag; CubWallet still sets it to true so existing dApps keep working
}isBtsWallet will stay available. Do not treat it as “this is not CubWallet”.
CubWallet