Thank you for the reply, @M_A_Hasan_Molla.
I am trying to follow what you said. But I’m still facing issues with the account creation. I tried generating and restoring account methods each but still unable to get an account.
Also, when I use the generateNewAccount method, I’m getting the following exception.
And following is the exception when I generateNewAccount after the restoreAccounts method(I’m not sure if I should really be doing this):
Please refer the following code:
package com.example.sdkpayment;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.samsung.android.sdk.blockchain.SBlockchain;
import com.samsung.android.sdk.blockchain.account.Account;
import com.samsung.android.sdk.blockchain.account.AccountManager;
import com.samsung.android.sdk.blockchain.coinservice.CoinNetworkInfo;
import com.samsung.android.sdk.blockchain.coinservice.CoinServiceFactory;
import com.samsung.android.sdk.blockchain.coinservice.ethereum.EthereumService;
import com.samsung.android.sdk.blockchain.exception.AccountException;
import com.samsung.android.sdk.blockchain.exception.HardwareWalletException;
import com.samsung.android.sdk.blockchain.exception.RemoteClientException;
import com.samsung.android.sdk.blockchain.exception.RootSeedChangedException;
import com.samsung.android.sdk.blockchain.network.EthereumNetworkType;
import com.samsung.android.sdk.blockchain.wallet.HardwareWallet;
import com.samsung.android.sdk.blockchain.wallet.HardwareWalletManager;
import com.samsung.android.sdk.blockchain.wallet.HardwareWalletType;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import com.samsung.android.sdk.blockchain.*;
import org.jetbrains.annotations.NotNull;
public class MainActivity extends AppCompatActivity {
TextView txt,txt2;
Button b1,b2,b3,b4;
Context context;
SBlockchain mSblockchain;
StringBuilder sb;
HardwareWalletManager hardwareWalletManager;
ArrayList suportedWallets;
ListenableFutureTask connectionTask;
HardwareWallet connectedHardwareWallet;
String supportedCoins;
CoinNetworkInfo coinNetworkInfo;
AccountManager accountManager;
ArrayList restoreTargets;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = findViewById(R.id.textView);
b1 = findViewById(R.id.button1);
b2 = findViewById(R.id.button2);
b3 = findViewById(R.id.button3);
b4 = findViewById(R.id.button4);
context = this;
txt2 = findViewById(R.id.textView2);
try {
mSblockchain = new SBlockchain();
mSblockchain.initialize(getApplicationContext());
hardwareWalletManager = mSblockchain.getHardwareWalletManager();
suportedWallets = mSblockchain.getSupportedHardwareWallet();
coinNetworkInfo = new CoinNetworkInfo(
CoinType.ETH,
EthereumNetworkType.MAINNET,
"https://mainnet.infura.io/v3/2e75dc3e014e412c97288f0b2169c5e0");
txt.setText(coinNetworkInfo.toString());
accountManager = mSblockchain.getAccountManager();
EthereumService etherService =
(EthereumService) CoinServiceFactory
.getCoinService(
context,
coinNetworkInfo
);
//txt.setText(etherService.toString());
//Toast.makeText(this,"Done",Toast.LENGTH_LONG).show();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
connectionTask = hardwareWalletManager.connect(suportedWallets.get(0), true);
connectionTask.setCallback(
new ListenableFutureTask.Callback<HardwareWallet>() {
@Override
public void onSuccess(HardwareWallet hardwareWallet) {
//txt.setText(hardwareWallet.getVersion());
//if(hWallet.isConnected())
//txt.setText("Success");
// accountManager = mSblockchain.getAccountManager();
// List<Account> accountsList = accountManager.getAccounts(null,null,null);
// txt.setText(accountsList.toString());
connectedHardwareWallet = hardwareWalletManager.getConnectedHardwareWallet();
// if(connectedHardwareWallet!=null)
// txt.setText("Ok till here");
// else
// txt.setText("empty");
txt2.setText("Done first");
}
@Override
public void onFailure(@NotNull ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof HardwareWalletException) {
// handling hardware wallet error
//txt.setText("Hardware Exception");
} else if (cause instanceof RootSeedChangedException) {
// handling root seed changed exception
//txt.setText("Root seed changed");
}
}
@Override
public void onCancelled(@NotNull InterruptedException e) {
//txt.setText("cancelled");
}
});
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* List<Account> accountsList = accountManager.getAccounts(connectedHardwareWallet.getWalletId(),
CoinType.ETH,
EthereumNetworkType.MAINNET);
txt.setText(accountsList.toString());
*/
ListenableFutureTask.Callback<Account> generatingNewAccountCallback =
new ListenableFutureTask.Callback<Account>() {
@Override
public void onSuccess(Account account) {
//Log.i(TAG, "Generated account address: " + account.getAddress());
//txt.setText(account.getAddress());
/* List<Account> accountsList = accountManager.getAccounts(connectedHardwareWallet.getWalletId(),
CoinType.ETH,
EthereumNetworkType.MAINNET);
*/
//txt.setText(accountsList.toString());
txt2.setText(account.toString());
}
@Override
public void onFailure(@NotNull ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof AccountException) {
// handling account error
txt.setText("Account Exception");
} else if (cause instanceof RootSeedChangedException) {
// handling root seed changed exception
txt.setText("Root Seed Changed");
} else if (cause instanceof RemoteClientException) {
txt.setText("Remote Client Exception");
// handling network error
} else{
txt2.setText(e.toString());
}
}
@Override
public void onCancelled(@NotNull InterruptedException e) {
txt.setText("On cancelled");
}
};
generateNewAccount()
.setCallback(generatingNewAccountCallback);
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
restoreTargets = new ArrayList<>();
restoreTargets.add(coinNetworkInfo);
ListenableFutureTask.Callback<List<CoinNetworkInfo>> restoreAccountsCallback =
new ListenableFutureTask.Callback<List<CoinNetworkInfo>>() {
@Override
public void onSuccess(List<CoinNetworkInfo> result) {
txt2.setText(result.toString());
}
@Override
public void onFailure(@NonNull final ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof AccountException) {
// handling account error
txt2.setText("A Exception");
} else if (cause instanceof RootSeedChangedException) {
// handling root seed changed exception
txt2.setText("Root Seed ");
} else if (cause instanceof RemoteClientException) {
// handling network error
txt2.setText("RC exception");
}
else{
txt2.setText("Exception unkown");
}
}
@Override
public void onCancelled(@NotNull InterruptedException exception) {
}
};
accountManager.restoreAccounts(
connectedHardwareWallet,
true,
restoreTargets)
.setCallback(restoreAccountsCallback);
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txt.setText(restoreTargets.toString());
boolean result = CoinServiceFactory.getCoinService(getApplicationContext(),coinNetworkInfo).isValidAddress("0xd9fff827c1fef8dc660c0a24f9f477c320434fdf");
txt2.setText(String.valueOf(result));
}
});
/* if(connectedHardwareWallet!=null)
Toast.makeText(this,"Done",Toast.LENGTH_LONG).show();
else
Toast.makeText(this,"Failed",Toast.LENGTH_LONG).show();
*/
//txt.setText(hWallet.toString());
//txt.setText(suportedWallets.toString());
} catch (Exception e) {
// handling exception
txt.setText(e.getMessage());
Toast.makeText(this,"Failed",Toast.LENGTH_LONG).show();
}
}
public ListenableFutureTask<Account> generateNewAccount(){
return accountManager.generateNewAccount(connectedHardwareWallet,coinNetworkInfo);
}
}