Take Your TRON Dapp to Galaxy Devices Around the World

Shuvo Saha

Engineer, Samsung Developer Program

Whether you are an enthusiast or veteran TRON developer, Samsung is here to help your decentralized application (Dapp) reach more people. By using the features in the Samsung Blockchain Platform SDK, you can enable millions of users to access your Dapp on their Samsung Galaxy devices. In a few easy steps, this article demonstrates how to make a simple Android application that can integrate with an existing Web Dapp.

Start by using the Samsung Blockchain Platform SDK to connect to a hardware wallet. Next, use the account manager to retrieve the user’s TRON account. Finally, display the Web Dapp using Cucumber WebView and enable it to send and receive payments.

Let’s get started!

Initialize Samsung Blockchain Platform SDK

Before using the Samsung Blockchain Platform SDK, include the required supporting libraries. To learn more about the prerequisite libraries and supported hardware wallets, refer to the Samsung Blockchain Platform SDK Getting Started guide.

To begin, initialize the Samsung Blockchain Platform SDK for your application. The SBlockchain object enables you to use the Samsung Blockchain Platform SDK features.

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 Note10 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, simply by 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 TRON accounts

The Samsung Blockchain Platform account manager can retrieve the list of accounts associated with the hardware wallet.

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

To retrieve the account list, specify the wallet ID from the connected hardware wallet, the coin type (cryptocurrency), and the desired network type, such as MAINNET or SHASTA_TESTNET. In this case, the cryptocurrency is TRON.

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

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

After the account is retrieved, you can proceed to loading the 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(),
        			tronCoinService, defaultAccount, transactionListener);

To communicate with the TRON blockchain network, use the CoinService interface of the Samsung Blockchain Platform. The coin service enables you to retrieve the information needed for the transaction, and also uploads the signed transaction to the blockchain network.

Browser-based Web Dapps built using the TronWeb library typically use TronLink as their TRON Web provider. In your mobile application, Cucumber WebView acts as the TRON Web provider and forwards the TRON Web Javascript-prepared transaction to the BaseOnSendTransactionListener event handler, which handles the transaction created by the Web Dapp.

After preparing the transaction, a payment intent powered by the Samsung Blockchain Platform SDK can be launched, which provides an interactive UI with the transaction details.

CoinService tronCoinService = CoinServiceFactory.getCoinService(MainActivity.this, 
						COIN_NETWORK_INFO);
BaseOnSendTransactionListener transactionListener = 
	new OnSendTronTransactionListener() {
		@Override
		public void onSendTransaction(@NotNull String requestID, 
		@NotNull TronPaymentType tronPaymentType, 
		@NotNull TronAccount fromAccount, 
		@NotNull String toAddress,
		@org.jetbrains.annotations.Nullable BigInteger productPrice,
		@org.jetbrains.annotations.Nullable String tokenID,
		@org.jetbrains.annotations.Nullable BigInteger tokenValue,
		@org.jetbrains.annotations.Nullable String data,
		@org.jetbrains.annotations.Nullable BigInteger feeLimit) {
	//Write code for what happens when transaction is sent
	Intent paymentIntent = dAppWebView.createTronPaymentSheetActivityIntent(
			MainActivity.this, requestID, connectedHardwareWallet, 
			fromAccount, toAddress, productPrice);
	MainActivity.this.startActivityForResult(paymentIntent, 0);
    }
};
dAppWebView.addCoinServiceConnector(COIN_NETWORK_INFO.getCoinType(),
        tronCoinService, defaultAccount, transactionListener);

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

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

When the marketplace has loaded, the Samsung Blockchain Platform prompts the user to allow the Dapp to connect to their hardware wallet.

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

Bonus: display transaction ID

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

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 Samsung Blockchain Platform 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 crypto-currencies and hardware wallets to your Dapp; the Samsung Blockchain Platform has you covered. Submit your application to the Samsung Galaxy Store and reach millions of new users today!

Additional Resources

Preferences Submitted

You have successfully updated your cookie preferences.