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 = mSBlockchain.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, NetworkType. Each parameter is optional and can be null to get all stored accounts.

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

for (Account account : accountsList) {
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.

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

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(@NotNull 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(@NotNull 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.

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

ArrayList<CoinNetworkInfo> restoreTargets = new ArrayList<>();
restoreTargets.add(coinNetworkInfo);

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

        @Override
        public void onFailure(@NonNull final 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(@NotNull InterruptedException exception) {
            
        }
    });

mAccountManager
    .restoreAccounts(
            connectedWallet,
            false,
            restoreTargets
    ).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.