Quantcast
Jump to content

Sign in to follow this  
STF News

“Where’s My Crypto Coin?” Featuring Samsung Blockchain Keystore SDK

Recommended Posts

Distributed ledger-based technologies are becoming more popular and easy to use. Anyone can now build a new cryptocurrency or token in the Blockchain world. This rise in popularity and value makes crypto assets a big target for hackers. If you want to keep your valuable crypto assets safe, using a hardware cold wallet such as Trezor or Ledger Nano S has become a necessity. Unfortunately, that adds up to one more item in your pocket that you always have to carry around.

Thankfully, gone are the days of carrying clunky, old wallets. Recent Galaxy phones, such as the S10e, S10, S10+, and Fold, can now securely store your cryptocurrency wallet using the Samsung Blockchain Keystore (SBK). Along with storing your cryptocurrency wallet, the SBK SDK allows you to get your Blockchain address and sign cryptocurrency transactions.

In this article, we explore one of the key features offered by the Keystore SDK–how to get your Blockchain address from the SBK SDK and three ways to share it:

  • Display as QR code
  • Copy to clipboard
  • Share through Android’s share intent

Setting up the Project and Handling SBK Data

To set up your Android project with the SBK SDK, follow these instructions.

To use functionalities offered by the SDK, first fetch an instance of the service.

private ScwService mScwService = ScwService.getInstance();

After you have fetched the ScwService instance, you can check whether your device is Keystore-supported.

if (mScwService == null) {
    Log.e("KeystoreApp", "Keystore is not supported on this device.");
}

If the device is Keystore-supported, you can fetch the address list with getAddressList():

mScwService.getAddressList(addressListCallback, hdPathList);

The first parameter to getAddressList() is a ScwGetAddressListCallback, which is executed after getting a response from Keystore. ScwGetAddressListCallback() has two functions:

  • onSuccess(): This function is called when the address list has been fetched successfully from Keystore.
  • onFailure(): This function is called if any errors occur while fetching the address list from Keystore.
ScwService.ScwGetAddressListCallback addressListCallback = new ScwService.ScwGetAddressListCallback() {
    @Override
    public void onSuccess(List addressList) {
        //You can share your address from the address list here
    }

    @Override
    public void onFailure(int failureCode) {
        //Based on the failure code you can show appropriate alerts here
    }
};

The second parameter is an ArrayList of Hierarchical Deterministic (HD) Path(s) whose addresses you want to fetch. If you want to learn more about HD paths, please refer to BIP-44. For example, if you want to find the public address of your first five accounts, pass the following list as a parameter:

ArrayList hdPathList = new ArrayList<>();
hdPathList.add("m/44'/60'/0'/0/0");
hdPathList.add("m/44'/60'/0'/0/1");
hdPathList.add("m/44'/60'/0'/0/2");
hdPathList.add("m/44'/60'/0'/0/3");
hdPathList.add("m/44'/60'/0'/0/4");

A Sample App with the SBK SDK

Now that we are familiar with getAddressList(), let’s dive into our sample application. Features of our Public Address with SBK app are:

  • Fetch your public address from the Keystore
  • Switch between multiple public addresses
  • Display QR code of the selected account
  • Copy selected address into the clipboard
  • Send the selected address with supported applications with Android’s share intent

Initially, only the address of the first account is loaded. When you press the Add button, the HD path of a new account is added to hdPathList, and public addresses are fetched.

public void addAccount(View view) {
    //Account Index is incremented by 1 to get the new account
    accountIndex++;
    //HDPath of new account is added to hdPathList
    hdPathList.add("m/44'/60'/0'/0/" + accountIndex);
    showToast("HDPath Added to list");
    //Public Address of new account is fetched
    getPublicAddress();
}

Public addresses are fetched using the getPublicAddress() function depicted below.

If the address list is fetched successfully, onSuccess() is called, and:

  • The spinner’s previous data is cleared.
  • The newly fetched list is added to the spinner.
  • The UI is updated.

If an error occurs, it is logged and available from logcat. Common errors such as ERROR_INVALID_SCW_APP_ID can be fixed very easily by enabling Developer Mode from the Keystore application. You can find instructions on how to enable Developer Mode here.

private void getPublicAddress() {
    ScwService.ScwGetAddressListCallback addressListCallback = new ScwService.ScwGetAddressListCallback() {
        @Override
        public void onSuccess(final List publicAddressList) {
            //After Address List has been fetched Spinner is updated with new list
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //Clear existing list
                    spinnerAdapter.clear();
                    //New list is added
                    spinnerAdapter.addAll(publicAddressList);
                    spinnerAdapter.notifyDataSetChanged();
                    if (publicAddressList.size() == 1) {
                        showToast(publicAddressList.size() + " address fetched.");
                    } else {
                        showToast(publicAddressList.size() + " addresses fetched.");
                    }
                }
            });
        }

        @Override
        public void onFailure(int errorCode) {
            switch (errorCode) {
                case ScwErrorCode.ERROR_INVALID_SCW_APP_ID:
                    Log.e(LOG_TAG,"Developer option not enabled.");
                    break;
                case ScwErrorCode.ERROR_CHECK_APP_VERSION_FAILED:
                    Log.e(LOG_TAG,"Check internet connection.");
                    break;
                case ScwErrorCode.ERROR_OP_FAIL:
                    Log.e(LOG_TAG,"Operation Failed");
                    break;
                default:
                    Log.e(LOG_TAG,"Error with Error Code: "+errorCode);
                    break;
            }
        }
    };
    if (mScwService == null) {
        Log.e(LOG_TAG, "Keystore is not supported in this device.");
    } else {
        //If Keystore is supported on device address list is requested
        mScwService.getAddressList(addressListCallback, hdPathList);
    }
}

After loading all addresses into the spinner, we can now select any address from it. Once an address is selected, its QR Code is generated and displayed.

publicAddressSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
        //Get Selected Address from spinner
        selectedAddress = adapterView.getItemAtPosition(position).toString();
        selectedAddressTextView.setText(selectedAddress);
        qrCodeImageView.setImageBitmap(generateQRCode(selectedAddress));
    }

In this application, we used “ZXing” to generate the QR bitmap of the selected public address.

private Bitmap generateQRCode(String text) {
    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
    Bitmap bitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.RGB_565);
    try {
        //Text encoded to QR BitMatrix
        BitMatrix bitMatrix = multiFormatWriter.encode(text, BarcodeFormat.QR_CODE, 1000, 1000);
        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
        //QR Bitmatrix encoded to Bitmap
        bitmap = barcodeEncoder.createBitmap(bitMatrix);
    } catch (WriterException e) {
        e.printStackTrace();
    } finally {
        return bitmap;
    }
}

When you press the copy button, the address is copied to the clipboard.

public void copyAddress(View view) {
    ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clipData = ClipData.newPlainText("Public Address", selectedAddress);
    clipboardManager.setPrimaryClip(clipData);
    Toast.makeText(this, "Address Copied", Toast.LENGTH_SHORT).show();
}

We can also share the selected public address using the Android ACTION_SEND intent.

public void shareAddress(View view) {
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, selectedAddress);
    sendIntent.setType("text/plain");
    startActivity(sendIntent);
}

Conclusion

Now that you know more about the Samsung Blockchain Keystore SDK, you can use it to enrich your Blockchain application. For more resources on Keystore SDK, visit https://developer.samsung.com/blockchain/keystore/sdk.

View the full blog at its source

Share this post


Link to post
Share on other sites


Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Similar Topics

    • By STF News
      We celebrate the year's top performing apps in creativity, quality, design, and innovation at our Best of Galaxy Store Awards. Next in our blog series, we feature Sonic Forces, winner of Best Adventure Game.
      Gabor Gyarmati, Product Manager at Sonic Forces, speaks to us about how he got started in game development, the challenges the team faced developing Sonic Forces, and tips for games that stand out from the crowd. Check out Gabor’s advice on developing games to help you on your way to becoming a Best of Galaxy Store Awards 2020 winner.
      How did you get your start developing games?
      I initially got into developing games through following my brother’s game development company making strategy games for Amiga and PC almost 30 years ago. The next step came after I finished my studies, when I co-founded a development studio with my brother back in Hungary. Later on, I joined the online gaming space and spent the last 12 years developing free to play games for various PC, console, and mobile platforms.
      Please tell us about Sonic Forces: Speed Battle.
      Sonic Forces is a super-fast paced, multiplayer runner game. Players collect their favourite characters from the Sonic universe and dash through the tracks jumping and dodging obstacles, enemies, and traps. Players compete against up to 3 other players from around the world in a single-speed battle. Sonic Forces includes regular content and feature updates. The game runs an ever-changing live event schedule that offers something new and exciting such as themed events, special characters, and more. Oh, and this is all available to play for free.
      What was the development process like for Sonic Forces? What were the biggest challenges you faced?
      Sonic Forces started off with a very small team focused on making a prototype that would prove whether the game could work from a technical perspective and whether we could make it fun. We succeeded in proving this pretty quickly. Then it became a question of making sure that we kept what made the prototype fun as we built it out into a full game. We also developed good tools very early on that allowed us to rapidly build and test tracks, which was a big help when it came to development and iteration.
      In terms of challenges, one of the biggest is always keeping the game balanced. Since Sonic Forces is a true competitive, multiplayer game, we spend a lot of time and focus facilitating an ongoing, iterative cycle to keep the game fair for all players. This includes tweaking the stats of characters, their powers, the tracks, etc. and feedback from our player community is incredibly valuable in this respect.
      What do you think has made Sonic Forces such a successful game?
      I think that there are several components that have contributed to Sonic Forces’ success. As a Sonic game, the characters, the environments, the music, and the core gameplay certainly resonate with the Sonic The Hedgehog fanbase. However, Sonic Forces also has a unique competitive multiplayer element that provides a short and sweet adrenaline boost, which is a great match for the playstyle of casual, competitive, mobile players. On top of this, Sonic Forces runs a lot of attractive live events and new content updates, which has helped to drive a very loyal and engaged player base to keep returning to the game for over 2 years now.
      What is the most important thing to consider when building games for Samsung?
      The most important thing is a strong focus on delivering regular high-quality updates, features, events, and new content. This can help Samsung as a platform provider showcase quality games that are popular with their community, while also helping keep players engaged over the long term.
      How do you create games that stand out from the crowd?
      We have a very talented team that comes from a variety of different development backgrounds across handheld, console, and PC platforms. The team has many years of experience in mobile games with live operations. We utilize our cumulative experience, from both our successes and challenges along the way, and this has enabled us to create new and exciting gameplay that’s based on solid learnings.

      When designing a game, what is the most important UX consideration?
      The most important UX consideration, for me, is to keep the core gameplay easy and fun, especially if you are developing a game that will be played by a younger audience on a mobile platform. I have a young son and I like to see him start and play the games without needing any instructions. If the game passes this test, I know players will be able to play it intuitively.
      What guidance do you have for new developers looking to develop successful games?
      My advice is to do thorough research before starting any development, know who the game’s audience will be, and design a fun experience that can be scaled and expanded over time. Have your ideas, prototypes, and later versions tested by friends, and then by actual players in order to get valuable early feedback. If you plan to develop a free-to-play game, make sure to plan ahead for delivering a long-lasting experience and develop tools that will allow you to grow and maintain live operations in the future.
      How do you think game development will change in the next few years? Are there emerging technologies you are keeping an eye on?
      With the arrival of super-fast internet, cloud gaming, and rapidly growing hardware capabilities, the mobile market is transforming to become the most versatile platform for gaming. This brings a lot of potential and new opportunities for big IP games that previously only appeared on consoles to thrive on mobile platforms.
      How has working with Samsung helped your business?
      Working with Samsung continues to be a great experience for our studio. We’ve found the Samsung editorial team to be very easy to work with, and their continuous support has made it possible for Sonic Forces to receive great exposure on Galaxy Store. This helps not only to drive an increase in new installs, but we’ve also found that the players discovering Sonic Forces through Samsung’s platform tend to be very engaged in and committed to the game. We love seeing how they interact with the gameplay and contribute to the growing Sonic Forces community.
      Thanks to Gabor Gyarmati of Sonic Forces for sharing his insights on how to develop successful games and emerging opportunities to watch for in the next few years. Follow @samsung_dev on Twitter for more developer interviews and tips for building games, apps, and more for the Galaxy Store. Find out more about our Best of Galaxy Store Awards.
      View the full blog at its source


×
×
  • Create New...