BitBox02 JavaScript library
The API is currenly unstable. Expect frequent breaking changes until we start tagging versions.
The JavaScript library is compiled from
bitbox02-api-go using GopherJS.
Integration
To talk to the device from a browser using this API, you will need the BitBoxBridge: https://github.com/digitalbitbox/bitbox-bridge
To integrate the BitBox02 into your application, your domain will need to be whitelisted in the Bridge. To do so, please submit a Pull Request or an Issue in the bitbox-bridge repo.
Install:
$ npm install bitbox02-api
Import
import { BitBox02API, getDevicePath} from 'bitbox02-api';
Initialize
To get the device path, the BitBoxBridge needs to be running
const devicePath = await getDevicePath();
const BitBox02 = new BitBox02API(devicePath);
Connect
The BitBox02API.connect()
method takes 5 arguments:
connect (showPairingCb, userVerify, handleAttestationCb, onCloseCb, setStatusCb)
For more detailed example see the Sample integration below.
Methods and functions
ethGetRootPubKey: Get Ethereum Root Pub Key
Get eth xpub for a given coin and derivation path
const rootPub = await BitBox02.ethGetRootPubKey(keypath: string);
ethSignTransaction: Sign Ethereum transaction
To sign Etehreum transactions, we recommend using the Transaction
type provided by the ethereumjs
library https://github.com/ethereumjs/ethereumjs-tx/blob/master/src/transaction.ts and passing this transaction data through our sanitizeEthTransactionData(sigData)
function to prepare the tx for the API:
function sanitizeEthTransactionData(sigData)
Then to send the data to the device and get the signature bytes:
import { sanitizeEthTransactionData } from 'bitbox02-api';
const sanitizedData = sanitizeEthTransactionData(sigData);
const result = await BitBox02.ethSignTransaction(sanitizedData);
ethSignMessage: Sign Ethereum messages
const result = await BitBox02.ethSignMessage(msgData);
ethDisplayAddress: Display Ethereum address on BitBox02 screen for verification
await BitBox02.ethDisplayAddress(id)
Check if product is supported
To use with Ethereum, the user needs a BB02 Multi and not the Bitcoin Only edition. You can use for the following check:
import { api } from 'bitbox02-api';
BitBox02.fw.Product() === api.common.Product.BitBox02Multi
Sample integration
This is a sample BitBox02Wallet class integration for connecting to the BitBox02 device using the BitBoxBridge and this JS API
import {
BitBox02API,
getDevicePath,
api,
sanitizeEthTransactionData
} from 'bitbox02-api';
class BitBox02Wallet {
constructor(logout) {
this.logout = logout;
this.status = undefined;
this.pairingConfirmed = false;
}
async connect() {
const devicePath = await getDevicePath();
this.BitBox02 = new BitBox02API(devicePath);
}
async init(keypath) {
await this.BitBox02.connect(
pairingCode => {
this.pairingCode = pairingCode;
},
async () => {
return new Promise(resolve => {
this.pairingConfirmed = true;
this.pairingConfirmationResolve = resolve;
});
},
attestationResult => {
this.attestation = attestationResult;
},
() => {
this.logout();
},
status => {
this.status = status;
}
);
if (this.BitBox02.fw.Product() !== api.common.Product.BitBox02Multi) {
throw new Error('Unsupported device');
}
if (!this.attestation) {
errorHandler('Attestation failed');
}
const rootPub = await this.BitBox02.ethGetRootPubKey(keypath);
}
Develop locally
To compile:
$ make dockerinit
$ make dockercompile
To run a demo: $ make servedemo
and visit localhost:8000