Normally, the number after the URL should match the version number of the SDK you installed. For example, “0.3.30” in this case.
If encountering issues with the web page failing to load for the corresponding version number, please try using a different version of the SDK or submit an issue on GitHub for feedback. We will work quickly to resolve the problem.
import { HardwareWebSdk as HardwareSDK } from'@onekeyfe/hd-web-sdk';HardwareSDK.init({ debug:true, connectSrc:'https://jssdk.onekey.so/0.3.30/',// Set to true if you need to use API related to firmware version checking, // otherwise keep as false. fetchConfig:false,})
Implement the low-level transport plugin to communicate with the native platform via JSBridge. Utilize native platform capabilities to manage device connections and send/receive data.
For example, the code snippet below registers some events to communicate with native via WebViewJavascriptBridge library.
import HardwareSDK from'@onekeyfe/hd-common-connect-sdk'import { createDeferred, isHeaderChunk, COMMON_HEADER_SIZE } from'./utils'functioncreateLowlevelPlugin() {constplugin= {enumerate: () => {returnnewPromise((resolve) => {// Invoke the ‘enumerate’ method registered on the native side to retrieve the result of the device search.bridge.callHandler('enumerate', {}, (response) => {resolve(response) }) }) },send: (uuid, data) => {returnnewPromise((resolve) => {// Invoke the ‘send’ method registered on the native side to send data to the devicebridge.callHandler('send', {uuid, data}, (response) => {resolve(response) }) }) },receive: () => {returnnewPromise((resolve) => {// Receive data from the device, // verify and concatenate the data packets,// and only return the complete packets to the SDK.// You can refer to OneKey Message Protocol for this.// For more implementation details, please refer to the code // https://github.com/originalix/Hardware-Lowlevel-Communicate/blob/main/web/index.js#L171 runPromise =createDeferred()constresponse=runPromise.promiseresolve(response) }) },connect: (uuid) => {returnnewPromise((resolve) => {// Call the native connect method and pass in the UUID of the Bluetooth device.bridge.callHandler('connect', {uuid})// Since the connect method in the native end may be asynchronous, // we also register a ‘connectFinished’ event to wait for the result returned by the native endbridge.registerHandler('connectFinished', () => {resolve() }) }) },disconnect: (uuid) => {returnnewPromise((resolve) => {// Disconnect the device.bridge.callHandler('disconnect', {uuid}, (response) => {resolve(response) }) }) },init: () => {// Do something you want to handle during SDK initialization.returnPromise.resolve() }, version:'OneKey-1.0' }return plugin}// This plugin object will be passed as a parameter during SDK initialization.constplugin=createLowlevelPlugin()
Initialize the SDK.
import HardwareSDK from'@onekeyfe/hd-common-connect-sdk';constsettings= { env:'lowlevel', debug:true}/** * Pass the previously written low-level plugin as the third parameter. */HardwareSDK.init(settings,undefined, plugin)
To improve the user experience when calling some methods such as getAddress and signTransaction, it is recommended to provide the user with some prompts at the UI layer for device unlocking and confirmation. This can be achieved through event passing.
It is recommended to register some common UI events before using the SDK.
import { UI_EVENT, UI_RESPONSE, CoreMessage } from'@onekeyfe/hd-core';HardwareSDK.on(UI_EVENT, (message:CoreMessage) => {// Handle the PIN code input eventif (message.type ===UI_REQUEST.REQUEST_PIN) {// Enter the PIN code on the deviceHardwareSDK.uiResponse({ type:UI_RESPONSE.RECEIVE_PIN, payload:'@@ONEKEY_INPUT_PIN_IN_DEVICE', }); }// Handle the passphrase eventif (message.type ===UI_REQUEST.REQUEST_PASSPHRASE) {// Enter the passphrase on the deviceHardwareSDK.uiResponse({ type:UI_RESPONSE.RECEIVE_PASSPHRASE, payload: { value:'', passphraseOnDevice:true, save:false, }, }); }if (message.type ===UI_REQUEST.REQUEST_BUTTON) {// Confirmation is required on the device, a UI prompt can be displayed }});
Get your Bitcoin address
After completing the steps of initializing the SDK and subscribe UI event listeners, we will now try to get the first Native Segwit Bitcoin address from the hardware wallet.
First, we need to search for the device and get its information.
constsearchDeviceResponse=awaitHardwareSDK.searchDevices()let deviceif (searchDeviceResponse.success &&searchDeviceResponse.payload.length>0) {// Use the first device in the search results device =searchDeviceResponse.payload[0]}
Next, we will get the first Native Segwit Bitcoin address.
During the execution of this method, the UI_REQUEST.REQUEST_PIN and UI_REQUEST.REQUEST_BUTTON events will be notified.
Now your Bitcoin address will be printed on the console.
Congratulations! You have mastered the usage of the SDK, as the calling method for all APIs is the same.
Advanced: Passphrase
Our wallet also supports hidden wallets. Once you have learned the basic API calling methods, it is easy for you to use the hidden wallet. You just need to add a parameter in Common Params. Please refer to the documentation related to Passphrase for more information.