Account Management

Samsung Blockchain Platform SDK manages the address on Blockchain as an account. It contains the information required for signing and the Blockchain address.

Get Instance

getAccountManager API is callable from the SBlockchain instance. If the SBlockchain instance is not initialized, an IllegalStateException is thrown.

try {
    AccountManager accountManager = sblockchain.getAccountManager();
} catch (IllegalStateException e) {
    // handling exception
}

Get Accounts

getAccounts API returns the accounts stored in shared preference. It supports three parameters for filtering: walletId, CoinType, and NetworkType. Each parameter is optional and can be null to get all stored accounts.

Ethereum

List<Account> ethAccounts = accountManager.getAccounts(
    connectedHardwareWallet.getWalletId(),
    CoinType.ETH,
    EthereumNetworkType.MAINNET);

for (Account account : ethAccounts) {
    Log.i(TAG, "Account: " + account.toString());
}

Tron

List<Account> tronAccounts = accountManager.getAccounts(
    connectedHardwareWallet.getWalletId(),
    CoinType.TRX,
    TronNetworkType.MAINNET);

for (Account account : tronAccounts) {
    Log.i(TAG, "Account: " + account.toString());
}

Generate New Account

generateNewAccount API generates a new account that has the HD Path according to the BIP-32 standard and creates an address based on it. Before creation, the AccountManager retrieves previously used accounts derived from the root seed and place it in the next index. If an account with no history on Blockchain has already been created, an AccountException is thrown or RootSeedChangedException is thrown if the root seed has changed.

Ethereum

CoinNetworkInfo coinNetworkInfo = new CoinNetworkInfo(
        CoinType.ETH,
        EthereumNetworkType.MAINNET,
        RPC_ENDPOINT);

ListenableFutureTask.Callback<Account> generatingNewAccountCallback =
    new ListenableFutureTask.Callback<Account>() {
        @Override
        public void onSuccess(Account account) {
            Log.i(TAG, "Generated account address: " + account.getAddress());
        }

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

            if (cause instanceof AccountException) {
                // handling account error
            } else if (cause instanceof RootSeedChangedException) {
                // handling root seed changed exception
            } else if (cause instanceof RemoteClientException) {
                // handling network error
            }
        }

        @Override
        public void onCancelled(InterruptedException e) {

        }
    };

accountManager
    .generateNewAccount(
        connectedHardwareWallet,
        coinNetworkInfo
    ).setCallback(generatingNewAccountCallback);

Tron

CoinNetworkInfo coinNetworkInfo = new CoinNetworkInfo(
            CoinType.TRX,
            TronNetworkType.MAINNET,
            RPC_ENDPOINT);

ListenableFutureTask.Callback<Account> generatingNewAccountCallback =
    new ListenableFutureTask.Callback<Account>() {
        @Override
        public void onSuccess(Account account) {
            Log.i(TAG, "Generated account address: " + account.getAddress());
        }

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

            if (cause instanceof AccountException) {
                // handling account error
            } else if (cause instanceof RootSeedChangedException) {
                // handling root seed changed exception
            } else if (cause instanceof RemoteClientException) {
                // handling network error
            }
        }

        @Override
        public void onCancelled(InterruptedException e) {

        }
    };

accountManager
    .generateNewAccount(
        connectedHardwareWallet,
        coinNetworkInfo
    ).setCallback(generatingNewAccountCallback);

The parameters to take note of are as follows:

  • hardwareWallet : is currently connected to the HardwareWallet to derive the address from the root seed.

  • coinNetworkInfo : Blockchain information of the account to be created. It requires a CoinType, NetworkType, and RPC node address.

Restore Accounts

The restoreAccounts API retrieves previously used accounts from the root seed of the target HardwareWallet and stores them in a shared preference. It derives the address using the HD Path according to the BIP-32 standard and checks if the address has a Blockchain history. The HD Path rules are provided by the AccountManager that conform to the BIP-44 standard. Alternatively, you can specify HD Path rule as a parameter.

Ethereum

CoinNetworkInfo coinNetworkInfo = new CoinNetworkInfo(
        CoinType.ETH,
        EthereumNetworkType.MAINNET,
        RPC_ENDPOINT);

ListenableFutureTask.Callback<Boolean> restoreAccountsCallback =
    new ListenableFutureTask.Callback<Boolean>() {
        @Override
        public void onSuccess(Boolean result) {

        }

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

            if (cause instanceof AccountException) {
                // handling account error
            } else if (cause instanceof RootSeedChangedException) {
                // handling root seed changed exception
            } else if (cause instanceof RemoteClientException) {
                // handling network error
            }
        }

        @Override
        public void onCancelled(InterruptedException exception) {

        }
    });

accountManager
    .restoreAccounts(
        connectedWallet,
        false,
        coinNetworkInfo
    ).setCallback(restoreAccountsCallback);

Tron

CoinNetworkInfo coinNetworkInfo = new CoinNetworkInfo(
        CoinType.TRX,
        TronNetworkType.MAINNET,
        RPC_ENDPOINT);

ListenableFutureTask.Callback<Boolean> restoreAccountsCallback =
    new ListenableFutureTask.Callback<Boolean>() {
        @Override
        public void onSuccess(Boolean result) {

        }

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

            if (cause instanceof AccountException) {
                // handling account error
            } else if (cause instanceof RootSeedChangedException) {
                // handling root seed changed exception
            } else if (cause instanceof RemoteClientException) {
                // handling network error
            }
        }

        @Override
        public void onCancelled(InterruptedException exception) {

        }
    });

mAccountManager
    .restoreAccounts(
        connectedWallet,
        false,
        coinNetworkInfo
    ).setCallback(restoreAccountsCallback);

The parameters to take note of are as follows:

  • hardwareWallet : is currently connected to the HardwareWallet to derive address from root seed.

  • reset : whether to clear accounts. If true, remove all accounts in the shared preferences and write restored accounts.

  • restoreTargets : Blockchain information of the account to be created. It requires a CoinType, NetworkType, and RPC node address.

  • How to handle the returned value : The return value is a list of failed cases in restoreTarget, and an empty list is returned if all of the requested targets are successful.