Quickstart
1. Prepare
- Install the CubWallet extension in desktop Chrome.
- Create or import an account that has a private key in the wallet (a watch-only account cannot sign).
- Open your page over https or http://localhost / http://127.0.0.1. Do not use
file://.
2. Detect and connect
Use this pattern: check whether window.bitshares is already there, and also listen for bitshares#initialized, so you do not conclude "no wallet" before the injection finishes.
function getProvider() {
if (window.bitshares) return Promise.resolve(window.bitshares);
return new Promise((resolve, reject) => {
const onInit = () => {
window.removeEventListener('bitshares#initialized', onInit);
window.bitshares ? resolve(window.bitshares) : reject(new Error('CubWallet was not detected'));
};
window.addEventListener('bitshares#initialized', onInit, { once: true });
setTimeout(() => {
window.removeEventListener('bitshares#initialized', onInit);
reject(new Error('CubWallet was not detected. Install the extension and open this page over https or http://localhost.'));
}, 3000);
});
}
async function connect() {
const bts = await getProvider();
if (!bts.isCubWallet) {
console.warn('window.bitshares is present, but it is not CubWallet');
}
const { chainId, network } = await bts.request({ method: 'bts_chainId' });
const accounts = await bts.request({ method: 'bts_requestAccounts' });
// accounts: [{ id: '1.2.xxx', name: 'your-name' }, ...]
return { bts, chainId, network, accounts };
}bts_requestAccounts opens the wallet's connect window. The promise resolves after the user selects accounts and approves. If this origin already has authorized accounts on the current network and the wallet is unlocked, the method returns those accounts immediately and does not open a window.
3. Sign a transfer without broadcasting
Amounts must be integer strings in the asset's smallest unit. Mainnet BTS has precision 5, so 0.00001 BTS is "1". Do not set the fee, the reference block or the transaction expiration; the wallet fills those in.
async function signTransfer(bts, from, toAccountId) {
const result = await bts.request({
method: 'bts_signTransaction', // sign only, do not broadcast
params: {
operations: [
['transfer', {
from,
to: toAccountId, // e.g. '1.2.17' — must be an account id
amount: { amount: '1', asset_id: '1.3.0' },
memo: 'hello from my dApp', // optional; plaintext, encrypted by the wallet
extensions: [],
}],
],
},
});
// result.broadcasted === false
// result.txid, result.signed_transaction
return result;
}This opens the approval window. What the user sees is decoded from the transaction bytes, not from the JSON you passed in. If the user rejects or closes the window, the promise rejects with 用户拒绝了请求 or 用户关闭了确认窗口.
Use bts_signAndBroadcast only when the user clearly intends to send the transaction to the chain. Do not sign or broadcast automatically on page load.
4. Listen for network changes and lock
bts.on('chainChanged', ({ chainId, network }) => {
// The user switches the network in the wallet; the dApp cannot change it.
// Permissions are stored per origin and network; call bts_accounts again afterwards.
console.log('network', network, chainId);
});
bts.on('lock', () => {
// After lock, bts_accounts returns []
});
bts.on('unlock', async () => {
const accounts = await bts.request({ method: 'bts_accounts' });
});See the API reference for parameters, return values and errors. You can also walk through each call on the test dApp.
CubWallet