Development
Step 1: Import project
Open Android Studio to import a sample shopping application project.
- Go to File > Open to open the Open File or Project window.
Every GUI resources are already included in the provided project. So what you need to do is fill out the code. Follow the steps below and the corresponding sample code. Going through the SDK document will be helpful as well.
Step 2: Initialize instance
Create Sblockchain
instance. This is the first step since Sblockchain
is the initial base class of SDK.
Along with that are:
HardwareWalletManager
: responsible for wallet operationsAccountManager
: responsible for account operations
mSBlockchain = new SBlockchain();
mSBlockchain.initialize(mContext);
mAccountManager = mSBlockchain.getAccountManager();
mHardwareWalletManager = mSBlockchain.getHardwareWalletManager();
mCoinNetworkInfo =
new CoinNetworkInfo(
CoinType.ETH,
EthereumNetworkType.ROPSTEN,
rpcUrl
);
connectToKeyStore();
Learn more by watching the video
Step 3: Connect to Samsung Blockchain Keystore
You can get hardware wallet instance from HardwareWalletManager
. HardwareWalletType
refers to the type of cold wallet to be connected, for example: Samsung Blockchain Keystore, Ledger Nano S, etc.
Refer to the sample code:
mHardwareWalletManager
.connect(
HardwareWalletType.SAMSUNG,
false
).setCallback(new ListenableFutureTask.Callback<HardwareWallet>() {
@Override
public void onSuccess(HardwareWallet hardwareWallet) {
handler.post(new Runnable() {
@Override
public void run() {
Log.i(TAG,"HardwareWallet is connected Successfully");
mProgressDialog.dismiss();
setAccountStatus();
}
});
}
@Override
public void onFailure(@NotNull ExecutionException e) {
mProgressDialog.dismiss();
e.printStackTrace();
}
@Override
public void onCancelled(@NotNull InterruptedException e) {
mProgressDialog.dismiss();
e.printStackTrace();
}
});
Second parameter of connect API refers to reset. If you do not want to reset the wallet then keep it false, otherwise true.
Learn more by watching the video
Step 4: Manage account
Now your shopping DApp is connected to the Keystore. It means, if you have your own account and balance, you can now buy something using Ether. Let’s create an Etherium account to be used for payment.
For generating new account with Platform SDK you need an instance of the AccountManager
from SBlockchain
. AccountManager
which provides methods regarding fetching, creating, restoring accounts, etc. Once you have an instance of AccountManager
, you can call generateNewAccount
as shown below. It has methods regarding accounts such as get
, create
, restore
, and more. Refer to SDK documents for more details.
Let’s create our own account.
HardwareWallet connectedHardwareWallet =
mHardwareWalletManager.getConnectedHardwareWallet();
if (connectedHardwareWallet != null) {
try {
return mAccountManager.generateNewAccount(connectedHardwareWallet, mCoinNetworkInfo).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
} else {
Log.i(TAG, "connectedHardwareWallet is null");
}
Learn more by watching the video
If you already have an account, it will be shown in the TextView
at the top and the button will be disabled to prevent further creation.
List<Account> accounts =
mAccountManager
.getAccounts(
null,
CoinType.ETH,
EthereumNetworkType.ROPSTEN
);
if (!accounts.isEmpty()) {
mEthereumAccount = (EthereumAccount) accounts.get(accounts.size() - 1);
tvAccountStatus.setText(mEthereumAccount.getAddress());
btnCreateAccount.setEnabled(false);
}
Learn more by watching the video
Step 5: Get instance for Ledger Service
Now that you have created an account, you can use it for payment in your sample shopping app. Each coin (blockchain ledgers) has their own system to be stored in blocks. Samsung Platform SDK provides abstracted API sets to developers for signing and sending supported cryptocurrency transactions over the Blockchain network. Create CoinServiceFactory
and just use any ledger you want. In this scenario, we will use EthereumService
.
EthereumService service =
(EthereumService) CoinServiceFactory.getCoinService(mContext, mCoinNetworkInfo);
Now your shopping DApp is ready to provide payment methods working on Ethereum.
Before Step 6, you need at least one valid Ethereum account.
Step 6: Show payment sheet
When your customer selects an item, you will show a payment sheet to them. It must show “Who will pay HOW MUCH Amount of ETHER to Whom”. In the Blockchain world, Ethereum ledgers need more information (i.e transaction nonce) to record that transaction into ledgers. But do no worry, Samsung Blockchain Platform SDK will do everything needed.
To show the payment sheet, intent should be created which is to call payment activity on click event of product object. Fill the params
required and startActivityForResult()
to show a payment sheet.
Item item = itemList.get(position);
BigInteger price = EthereumUtils.convertEthToWei(item.getPrice());
EthereumService service =
(EthereumService) CoinServiceFactory.getCoinService(mContext, mCoinNetworkInfo);
HardwareWallet connectedHardwareWallet =
mHardwareWalletManager.getConnectedHardwareWallet();
Intent intent =
service
.createEthereumPaymentSheetActivityIntent(
mContext,
connectedHardwareWallet,
mEthereumAccount,
SAMPLE_TO_ADDERSS,
price,
null,
null
);
startActivityForResult(intent, 0);
Learn more by watching the video
Step 7: Get Ether from faucet
You need money when you buy something. You need Ether in this case. There’s a free faucet site already. If you have an Etherium account, just go to WebView fragment and press request 1 ether from faucet button.
Step 8: See result of transaction
If payment performed successfully from the payment screen, result of transaction can be received from onActivityResult
. If transaction is performed properly, you will find RESULT_OK
at resultCode
. From the intent, you will get txid
which is used to search your transaction’s status.
-
Call
getTransactionDetail(txid: String)
API (method inEthereumService
) and check the block#. If there’s any valid countable number, your transaction is stored in blocks successfully and the payment is done. If you can’t find it, just wait. It’s processing among Ethereum nodes. -
Or just simply find
txid
in the Etherscan.io.- Set your network to Ropsten Testnet
- Search with
txid
Run the project you have created so far and verify that it works properly. Run the app and select the product from the list, the payment screen will be shown, select the fee to pay to the Ethereum network, and confirm the transaction with the button. Then, check the result of the generated transaction. Sample images of the app screen can be seen below.
You're done!
Congratulations! You have successfully achieved the goal of this Code Lab activity. Now, you develop a decentralized shopping app using Samsung Blockchain Platform SDK by yourself! But, if you're having trouble, you may check out the link below.