Connect With Hardware Wallet

Samsung Blockchain Platform SDK supports various cold wallets. It can be managed using HardwareWalletManager, and can get an instance from SBlockchain. Currently, there are two types of HardwareWallet, Samsung Blockchain Keystore and the Ledger Nano.

Get Instance

getHardwareWalletManager is a callable instance of SBlockchain. If the SBlockchain instance is not initialized, an IllegalStateException will be thrown.

try {
    HardwareWalletManager hardwareWalletManager = sblockchain.getHardwareWalletManager();
} catch (IllegalStateException e) {
    // handling exception
}

Connect

The HardwareWallet should be connected to sign, create, or restore an account. The connect API requires two mandatory parameters, HardwareWalletType and whether to clear the saved accounts. The HardwareWalletType is used to target its connection. There are two connection types: USB and Bluetooth, and is specific for the Ledger Nano X.

For USB connections, the device must be connected through OTG. A bonding state is required for Bluetooth connections. To connect a ledger device, the latest Ethereum application is installed on the ledger device and can be connected while running. If ready to connect, the connect API can be called.

ListenableFutureTask<HardwareWallet> connectionTask =
    hardwareWalletManager.connect(hardwareWalletType, true);

connectionTask.setCallback(
    new ListenableFutureTask.Callback<HardwareWallet>() {
        @Override
        public void onSuccess(HardwareWallet hardwareWallet) {

        }

        @Override
        public void onFailure(ExecutionException e) {
               Throwable cause = e.getCause();

               if (cause instanceof HardwareWalletException) {
                       // handling hardware wallet error
               } else if (cause instanceof RootSeedChangedException) {
                      // handling root seed changed exception
               }
        }

        @Override
        public void onCancelled(InterruptedException e) {

        }
    });

The parameters to take note of are as follows:

  • hardwareWalletType : target HardwareWallet to connect.

  • reset : whether to clear accounts. Samsung Blockchain Platform SDK only supports one root seed for each HardwareWallet for account management. If the root seed is changed by resetting the HardwareWallet, when the connect API is called, onFailure is called and the RootSeedChangedException is returned.

Disconnect

disconnect API is related to the target connection of the HardwareWallet. It is recommended to call it before closing your application or even before connecting to another HardwareWallet. If the disconnect API is not called, the broadcast receiver may continue to work.

hardwareWalletManager.disconnect(hardwareWallet);

Get Connected HardwareWallet

getConnectedHardwareWallet API returns an instance of the connected HardwareWallet. APIs that use the root seed requires an instance of connected HardwareWallet, so they can get the HardwareWallet connected and call it before calling them. If no HardwareWallet is connected, a null is returned.

HardwareWallet connectedHardwareWallet = hardwareWalletManager.getConnectedHardwareWallet();

Get Supported Coins

HardwareWallet instance is returned through the HardwareWalletManager that has APIs that support its feature. getSupportedCoins API returns a list of CoinType provided by the HardwareWallet and Samsung Blockchain Platform SDK in common.

List supportedCoinTypes = hardwareWalletManager.getConnectedHardwareWallet().getSupportedCoins();

StringBuilder sb = new StringBuilder();
sb.append(“Supported coins : ”).append(‘\n’);

for (CoinType coinType : supportedCoinTypes) {
    sb.append(‘[’).append(coinType.code).append(‘)’).append(‘\n’);
}

String s = sb.toString();

Get Version

getVersion API returns its version. For Samsung Blockchain Keystore, it returns the versionName of the Keystore; and for the Ledger it returns the version of the Ledger Application running. If you need a different behavior depending on the version, you can call this API to branch.

HardwareWallet connectedHardwareWallet = hardwareWalletManager.getConnectedHardwareWallet();
String version = connectedHardwareWallet.getVersion();

Get Connection State

isConnected API returns the state that the HardwareWallet instance is connected. If connected, it returns as true. Otherwise, it returns as false.

if (hardwareWallet.isConnected()) {
    hardwareWalletManager.disconnect(hardwareWallet);
}