Samsung Blockchain Platform SDK manages the address on Blockchain as an account. It contains the information required for signing and the Blockchain address.
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 }
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.
List<Account> ethAccounts = accountManager.getAccounts( connectedHardwareWallet.getWalletId(), CoinType.ETH, EthereumNetworkType.MAINNET); for (Account account : ethAccounts) { Log.i(TAG, "Account: " + account.toString()); }
List<Account> tronAccounts = accountManager.getAccounts( connectedHardwareWallet.getWalletId(), CoinType.TRX, TronNetworkType.MAINNET); for (Account account : tronAccounts) { Log.i(TAG, "Account: " + account.toString()); }
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.
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);
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.
This API was deprecated in 1.1.00.
Changed restoreTarget as a single CoinNetworkInfo because only one coin type can be processed at a time on the ledger device.
Use restoreAccounts(HardwareWallet, Boolean, CoinNetworkInfo) instead.
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.
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);
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);
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.