Take your Ethereum Dapp to Galaxy Devices Around the World

Shuvo Saha

Engineer, Samsung Developer Program

Whether you are making a cryptocurrency exchange or a game on the Ethereum Blockchain, if you want to get more users for your Web Dapp, this article is for you.

More web traffic now comes from smartphones than PCs. So, if you want to get more users for your Dapp, you need to take your application to the “Galaxies” (that is, Samsung Galaxy devices)! Thankfully, the Samsung Blockchain Platform (SBP) SDK has got you covered for all of your Blockchain needs.

This article explores how to connect to a hardware wallet using SBP SDK and how to leverage that connection to sign your cryptocurrency transaction. You will learn how to use Cucumber Webview provided by SBP SDK to display your Web Dapp and leverage SBP SDK features. You will also explore how to send the signed transaction to a Blockchain network and receive payment. Let’s get started!

Initialize the Samsung Blockchain Platform SDK

Before using SBP SDK, make sure to include the required supporting libraries. To learn more about the required libraries and how to add them to the project, you can review the SBP SDK Getting Started guide.

To begin, initialize the SBP SDK for our application. Running the following code segment initializes the SBlockchain object. You need to use this object for all further communication with the SBP SDK.

try {
    mSBlockchain = new SBlockchain();
    mSBlockchain.initialize(this);

} catch (SsdkUnsupportedException e) {
    handlePlatformSDKUnsupportedError(e);
}

Connect to a hardware wallet

Many Samsung Galaxy devices, such as the Galaxy Note20 and S20, already have a hardware wallet available: the Samsung Blockchain Keystore. You will connect to it in this demonstration. However, you can adapt the following code to connect to other supported hardware wallets, such as the Ledger Nano X, by simply changing the hardware wallet type.

private HardwareWallet connectedHardwareWallet;

ListenableFutureTask<HardwareWallet> connectionTask = mSBlockchain.getHardwareWalletManager().connect(HardwareWalletType.SAMSUNG, true);
connectionTask.setCallback(new ListenableFutureTask.Callback<HardwareWallet>() {
    @Override
    public void onSuccess(HardwareWallet hardwareWallet) {
        connectedHardwareWallet = hardwareWallet;
        setupAccounts(connectedHardwareWallet);
    }

    @Override
    public void onFailure(ExecutionException e) {
        Log.e(LOG_TAG, "Connecting to Hardware Wallet failed.");
        Log.e(LOG_TAG, e.getMessage());
    }

    @Override
    public void onCancelled(InterruptedException e) {
        Log.e(LOG_TAG, "Connecting to Hardware Wallet cancelled.");
        Log.e(LOG_TAG, e.getMessage());
    }
});

Once successfully connected to the hardware wallet, you can retrieve the accounts associated with it.

Retrieve Ethereum accounts

Using the SBP’s account manager, you can retrieve the list of accounts associated with a hardware wallet.

AccountManager accountManager = mSBlockchain.getAccountManager();
List<Account> accountList = accountManager.getAccounts(connectedWallet.getWalletId(), COIN_NETWORK_INFO.getCoinType(), COIN_NETWORK_INFO.getNetworkType());

In order to get a list of accounts using SBP SDK, you need to specify the wallet ID of the hardware wallet connected, the coin type (cryptocurrency) you want to use (as of writing this article, only Ethereum is supported), and the desired network type (such as MAINNET or ROPSTEN).

If the account list is empty, the account manager can generate a new account based on the connected hardware wallet, the specified cryptocurrency, and the network.

accountManager.generateNewAccount(connectedHardwareWallet, COIN_NETWORK_INFO).setCallback(
    new ListenableFutureTask.Callback<Account>() {
        @Override
        public void onSuccess(Account newAccount) {
            defaultAccount = newAccount;
            showAccountAddressOnUI(defaultAccount.getAddress());
            initializeWebView();
            Log.d(LOG_TAG, "Account fetched.");
        }

        @Override
        public void onFailure(ExecutionException e) {
            Log.e(LOG_TAG, "Account fetching failed.");
            Log.e(LOG_TAG, e.getMessage());
        }

        @Override
        public void onCancelled(InterruptedException e) {
            Log.e(LOG_TAG, "Account fetching cancelled.");
            Log.e(LOG_TAG, e.getMessage());
        }
    });

After the account is retrieved, you can proceed to loading our Web Dapp using Cucumber WebView.

Initialize Cucumber WebView

The next step is to prepare the Cucumber WebView and enable it to make transactions through the Dapp.

dAppWebView = findViewById(R.id.dapp_web_view);
dAppWebView.addCoinServiceConnector(COIN_NETWORK_INFO.getCoinType(),
        ethereumCoinService, defaultAccount, transactionListener);

To communicate with the blockchain network, use the CoinService interface of the SBP. The coin service enables us to retrieve the information needed for the transaction, such as nonce and gas prices. After all the information for the transaction has been retrieved, the coin service can help us upload the signed transaction to the blockchain network.

Browser-Based Web Dapps built using web3JS nowadays use Metamask as their web3 provider. When our Dapp is loaded on the dAppWebView, the SBP SDK works as the web3 provider and forwards the web3JS-prepared transaction to a BaseOnSendTransactionListener event handler, which handles the transaction created by our Web Dapp.

After preparing the transaction, a payment intent powered by the SBP SDK can be launched, which provides an interactive UI with the transaction details and a mechanism to select the preferred transaction speed.

CoinService ethereumCoinService = CoinServiceFactory.getCoinService(MainActivity.this, COIN_NETWORK_INFO);
BaseOnSendTransactionListener transactionListener = new OnSendEthereumTransactionListener() {
    @Override
    public void onSendTransaction(String requestID, EthereumAccount ethereumAccount, String toAddress, BigInteger value, String data, BigInteger nonce) {
        Intent paymentIntent = dAppWebView.createEthereumPaymentSheetActivityIntent(MainActivity.this, requestID, connectedHardwareWallet, toAddress, value, data, nonce);
        MainActivity.this.startActivityForResult(paymentIntent, 0);
    }
};
dAppWebView.addCoinServiceConnector(COIN_NETWORK_INFO.getCoinType(),ethereumCoinService, defaultAccount, transactionListener);
dAppWebView.loadUrl(MARKETPLACE_URL);

Our Cucumber WebView (dAppWebView) is now ready to load our Web Dapp using its URL.

dAppWebView.loadUrl("https://www.example.com");

When the marketplace has loaded, the SBP prompts the user to allow the Dapp to connect to our hardware wallet.

Sample App Screenshot 1 Sample App Screenshot 2

The SBP’s payment intent UI enables the user to easily purchase items from the marketplace.

Sample App Screenshot 3 Sample App Screenshot 4

Using the payment intent UI, the user can choose the transaction speed and send the transaction to their hardware wallet for signing. Once signing is done, the SBP SDK sends the transaction to the blockchain network for processing.

Bonus: display the transaction ID

In addition to enabling users to sign in and send a transaction with your Dapp, the SBP SDK can also retrieve the transaction ID.

Sample App Screenshot 5

The payment intent returns the transaction Information, which can be parsed using the onActivityResult method, if desired.

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0 && data != null) {
        dAppWebView.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case Activity.RESULT_OK:
                String transactionID = data.getStringExtra("txid");
                Log.d(LOG_TAG, "TransactionId : " + transactionID);
                showAlertDialog("Transaction Successful with Id : " + transactionID);
                break;
            case Activity.RESULT_CANCELED:
                Log.d(LOG_TAG, "Transaction canceled by user.");
                break;
            default:
                Log.d(LOG_TAG, "Unknown Activity Result Code. Result Code: " + resultCode);
        }
    }
}

Conclusion

With the SBP SDK, your Dapp is no longer confined to PCs. Both new and existing users can have your Dapp at their fingertips on Samsung Galaxy devices. It’s also easy to add support for new cryptocurrencies and hardware wallets to your Dapp; the SBP has you covered. Submit your application to the Samsung Galaxy Store and reach millions of new users today!

Additional resources