element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Internet of Things
  • Technologies
  • More
Internet of Things
Blog Blockchain - Analyse the Infineon Android Demo App - part b: Retrieve Ethereum Crypto Currency Balance
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Internet of Things to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 17 Mar 2020 1:01 PM Date Created
  • Views 868 views
  • Likes 4 likes
  • Comments 1 comment
  • android
  • infineon
  • security_2go
  • rt
  • cryptocurrency
Related
Recommended

Blockchain - Analyse the Infineon Android Demo App - part b: Retrieve Ethereum Crypto Currency Balance

Jan Cumps
Jan Cumps
17 Mar 2020

This post is part of the Infineon Blockchain Starter Kit road test.

Blockchain - outside of the bitcoin context - is new to me.

Follow along with me on this path to learn the technology....

 

 

Let's dive further into the example Android App "coinfinity". Retrieve crypto coin balance information from the Ethereum test network.

image

 

Prerequisite: an Infinion Security 2Go smart card loaded with some Ethereum Ropsten test coins. See this post for instructions.

 

I'm picking up from where we left in the first part: Blockchain - Analyse the Infineon Android Demo App - part a: Detect Card.

You tapped the card to the back. It's detected and the 3 action buttons are enabled.

 

A little refresher of what first happened when the card was tapped:

 

        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        // ...
        IsoDep isoDep = IsoDep.get(tag);
        // ...

        try {
            SharedPreferences pref = this.getSharedPreferences(PREFERENCE_FILENAME, Context.MODE_PRIVATE);
            pubKeyString = NfcUtils.readPublicKeyOrCreateIfNotExists(IsoTagWrapper.of(isoDep),
                    pref.getInt(KEY_INDEX_OF_CARD, 1)).getPublicKeyInHexWithoutPrefix();
            isoDep.close();
        } catch (IOException | NfcCardException e) {
            // ...
        }
       // ...
        // use web3j to format this public key as ETH address
        ethAddress = Keys.toChecksumAddress(Keys.getAddress(pubKeyString));

 

 

At this point, the following info from your card / keys is known to the app:

    private String pubKeyString;
    private String ethAddress;

 

Contact the Block Chain to Retrieve the Balance

 

Several blockchains support a web3j interface. So does the Etherium one.

In the previous blog (see snippet above) a web3j call was used to fetch the 20 byte address (checksummed address) from the public key.

But that's a utility call. It doesn't check your transactions.

Now we'll see how web3j can retrieve the card balance (the balance on the etherium address related to the card / key you tapped.

 

    public void updateBalance() throws Exception {
        Log.d(TAG, "reading ETH balance..");
        ethBalance = EthereumUtils.getBalance(ethAddress, UiUtils.getFullNodeUrl(this));
        Log.d(TAG, String.format("reading ETH balance finished: %s", balance.toString()));
    }

 

 

The EthereumUtils class is the one that will call web3j.

The UiUtils class holds the URL for us. For the Ropsten network, this is https://ropsten.infura.io/v3/7b40d72779e541a498cb0da69aa418a2

This URL is built out of constants. I do not know what the 7b40d72779e541a498cb0da69aa418a2 value means.

 

    private static final String HTTPS = "https://";
    private static final String BASEURL = ".infura.io/v3/7b40d72779e541a498cb0da69aa418a2";
    public static final String MAINNET_URI = HTTPS + "mainnet" + BASEURL;
    public static final String ROPSTEN_URI = HTTPS + "ropsten" + BASEURL;

 

I'm currently treating it as a magic value. I'll look it up when the need is there - or if you are interested maybe you can look it up and post in the comments.

 

   public static EthBalanceBean getBalance(String ethAddress, String url) throws Exception {
        Web3j web3 = Web3jFactory.build(new HttpService(url));

        BigInteger wei = getBalanceFromApi(web3, ethAddress, DefaultBlockParameterName.LATEST);
        if (wei == null) {
            wei = new BigInteger("0");
        }
        BigDecimal ether = Convert.fromWei(wei.toString(), Convert.Unit.ETHER);

        BigInteger unconfirmedWei = getBalanceFromApi(web3, ethAddress, DefaultBlockParameterName.PENDING);
        if (unconfirmedWei == null) {
            unconfirmedWei = new BigInteger("0");
        }
        unconfirmedWei = unconfirmedWei.subtract(wei);
        BigDecimal unconfirmedEther = Convert.fromWei(unconfirmedWei.toString(), Convert.Unit.ETHER);

        return new EthBalanceBean(wei, ether, unconfirmedWei, unconfirmedEther);
    }

 

    private static BigInteger getBalanceFromApi(Web3j web3, String ethAddress,
                                                DefaultBlockParameterName defaultBlockParameterName) throws Exception {
        BigInteger wei = null;
        EthGetBalance ethGetBalance = web3
                .ethGetBalance(ethAddress, defaultBlockParameterName)
                .sendAsync().get();
        if (ethGetBalance != null) {
            wei = ethGetBalance.getBalance();
        }

        return wei;
    }

 

Excuse me for not digging into the downstream code and that's template code. Easier for you to drill into it via Android Studio than for me to explain it.

Short story is that the web3j Request class is used to build the request and send it asynchronious.

image

 

This is the result:

image

This gets decoded using

BigInteger(value.substring(2), 16);

 

Resulting in: 59685000000000000

 

Format for Display on the App

 

That gets converted again:

BigDecimal ether = Convert.fromWei(wei.toString(), Convert.Unit.ETHER);

 

image

Prepared for display:

image

 

... and when the app refreshes its screen, the balance is shown:

 

image

 

I haven't shown the full trace here. Talking through it isn't half the value of stepping through it via Android Studio's debugger.

So I tried to stick to the process of getting the card info, create the web3j parameters for budget retrieval and parsing the results.

What you haven't seen is how web3j works internally to build the web call and get the async reply.

That's not blockchain related. I don't want to mix that into the discussion, yet wanted to show some of the intermediate variables for your understanding.

Enough hooks to go look into the code and APIs yourself.

 

Now that I revealed my Etherium address, feel free to try and fetch some of my balance, or donate some image. It's Ropsten coins so they are free.

You can (in the true spirit of an open blockchain) see all transactions for that account. And that's one of the 255 keys on my Infineon 2Go smart card.

 

 

Related Blog
Blockchain - HyperLedger Fabric pt 1: post and search transactions on a distributed trusted ledger
Blockchain - Hyperledger Burrow: set up a distributed ledger
Blockchain - Get Some Crypto Currency on an Infineon Security 2Go Card
Blockchain - Debug the Infineon Demo App with Android Studio and Your Phone
Blockchain - Analyse the Infineon Android Demo App - part a: Detect Card
Blockchain - Analyse the Infineon Android Demo App - part b: Retrieve Ethereum Crypto Currency Balance
Blockchain - Analyse the Infineon Android Demo App - part c: Transaction, Move Crypto Currency to Another Account
Blockchain - Talk Directly to the Infineon 2Go Smart Cards API
Blockchain - Smart Contract Test
Road Test notepad: Infineon Blockchain Starter Kit
Infineon Blockchain Starter Kit - Review
Blockchain - HyperLedger Fabric pt 2: application using blockchain and smart contract
Blockchain - HyperLedger Fabric pt 3: web console
  • Sign in to reply

Top Comments

  • Jan Cumps
    Jan Cumps over 5 years ago +1
    The EthereumUtils class is the one that will call web3j. The UiUtils class holds the URL for us. For the Ropsten network, this is https://ropsten.infura.io/v3/7b40d72779e541a498cb0da69aa418a2 This URL…
  • Jan Cumps
    Jan Cumps over 5 years ago

    The EthereumUtils class is the one that will call web3j.

    The UiUtils class holds the URL for us. For the Ropsten network, this is https://ropsten.infura.io/v3/7b40d72779e541a498cb0da69aa418a2

    This URL is built out of constants. I do not know what the 7b40d72779e541a498cb0da69aa418a2 value means.

     

    The magical value turns out to be a project ID. See https://infura.io/docs/gettingStarted/makeRequests.md

    I don't know how to define or find that yet, but at least it isn't a magic number anymore. It has a meaning.

     

    edit - you get the project id when signing up: https://infura.io/docs

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube