On this page
Migrate to web-sdk + WebUSB
Replace Bridge with WebUSB while keeping @onekeyfe/hd-web-sdk as your SDK entry.
Requirements
- Chromium browsers (Chrome/Edge desktop)
- Node.js 18+ (LTS recommended)
What Changed (At a Glance)
- Initialize the SDK with
env: 'webusb'- (Bridge (env:
web) → WebUSB (env:webusb).)
- (Bridge (env:
- Prefer omitting
connectSrcso@onekeyfe/hd-web-sdkfillshttps://jssdk.onekey.so/<installed-version>/. Override only if you host the iframe yourself. - Ask for user authorization via
navigator.usb.requestDevice()with official OneKey filters — must be triggered by a user gesture (e.g., a button click) due to Chrome security restrictions; a new authorization needs a user gesture; previously authorized devices can be enumerated without another chooser - Bridge-specific checks are deprecated (e.g.,
checkBridgeStatus()) - All other SDK usage remains the same
Initialize (hd-web-sdk + WebUSB)
import HdWebSdk from '@onekeyfe/hd-web-sdk';
// Same pattern as expo-example: default export → .HardwareWebSdk
const HardwareSDK = HdWebSdk.HardwareWebSdk;
const initialized = await HardwareSDK.init({
env: 'webusb',
// omit connectSrc — SDK fills https://jssdk.onekey.so/<installed-hd-web-sdk-version>/
debug: false,
fetchConfig: true,
});
if (!initialized) throw new Error('SDK initialization failed');Authorization (WebUSB Picker + Official Filter)
import { ONEKEY_WEBUSB_FILTER } from '@onekeyfe/hd-shared';
// Important: must be triggered by a user gesture (e.g., button click)
// Previously authorized devices can be enumerated without opening the chooser again.
await navigator.usb.requestDevice({ filters: ONEKEY_WEBUSB_FILTER });Enumerate Devices and First Call
// 1) Enumerate
const list = await HardwareSDK.searchDevices();
if (!list.success) throw new Error(list.payload.error);
// Select explicitly in the application when more than one device is available.
if (list.payload.length !== 1) throw new Error('Select exactly one device before continuing');
const { connectId } = list.payload[0] ?? {};
if (!connectId) throw new Error("No device connection");
// 2) Confirm deviceId
const state = await HardwareSDK.getDeviceState(connectId);
if (!state.success) throw new Error(state.payload.error);
const resolvedDeviceId = state.payload.identity.deviceId;
if (!resolvedDeviceId) throw new Error("Device has no wallet identity");
// 3) Example: BTC address
const r = await HardwareSDK.btcGetAddress(connectId, resolvedDeviceId, {
path: "m/44'/0'/0'/0/0",
coin: 'btc',
showOnOneKey: true,
useEmptyPassphrase: true,
});
if (r.success) {
console.log('BTC address:', r.payload.address);
} else {
console.error('Error:', r.payload.error, r.payload.code);
}Cleanup and Checklist
- Remove Bridge-specific checks/CTAs (e.g.,
checkBridgeStatus()) - Omit
connectSrcunless you host the iframe; it must match the installed@onekeyfe/hd-web-sdkversion - Feature-detect
navigator.usband show a friendly notice if unsupported - All other SDK methods and UI flows are unchanged
Last updated on