IBM / android-kubernetes-blockchain

WARNING: This repository is no longer maintained :warning: This repository will not be updated. The repository will be kept available in read-only mode. Refer to https://developer.ibm.com/patterns/category/blockchain/ for other blockchain code patterns.
Apache License 2.0
124 stars 71 forks source link
android android-studio blockchain-network fitness-tracker hyperledger-fabric ibm-cloud kubernetes microservice mobile mongodb

Create an Android app with Blockchain Integration

WARNING: This repository is no longer maintained :warning:

This repository will not be updated. The repository will be kept available in read-only mode. Refer to https://developer.ibm.com/patterns/category/blockchain/ for other blockchain code patterns.

In this code pattern, we will create an Android app that tracks the user's physical steps with Blockchain capabalities using Kubernetes. The blockchain framework implementation used is Hyperledger Fabric. The users are registered to the Blockchain network anonymously and get rewarded with some "coins" for the steps they take. The users can trade their coins for some swag and these transactions are executed in the Blockchain network. This Android app was used in KubeCon Europe 2018.

This code pattern is for developers who wish to provide data anonymity and security to their users. Their users will be more confident to use their app if it gives them more control over their privacy. The developers can also extend the pattern to use the backend from different platforms.

When you have completed this code pattern, you will understand how to:

Flow

  1. The REST API is how the mobile app will interact with the blockchain network. The API will acknowledge the request and the mobile app will receive a unique key (random numbers and letters) which will be used to get the blockchain’s response later.
  2. The API just stores the request in a queue in RabbitMQ. The queue has 2 channels, which are for the user (Fitcoin org) and the seller (Shop org). The requests can either be:
    • User enrollment
    • Query data from the blockchain network (number of kubecoins of a user, products that are for sale, contracts, etc…)
    • Invoke or perform a transaction (send steps to receive kubecoins, claim a product, complete a transaction, etc…)
  3. The execution workers use the Hyperledger Fabric Node.js SDK to perform above requests. They are listening to the requests from RabbitMQ.
  4. The workers send the requests to the Blockchain network and are then processed. The blockchain network uses NFS to persist the ledger and state database.
  5. The workers receive the response and then persists it in the redis database with the unique key from (# 1).
  6. The mobile app will continue to wait for the blockchain’s response results that should be in the redis database. This is where the unique key is used. The mobile app will query the redis database with the unique key.
  7. The Registration microservice is used to create a user or update the user’s steps. When the blockchain network enrolls a user, this service uses the user id assigned by the blockchain network.
  8. When a user is registered, it calls a Cloud Function to generate a random name and avatar for the user.
  9. The data from the microservices are persisted in a MongoDB. This is where the user’s data (steps, name and avatar) and mobile assets (booklet/articles in the first view of mobile app) are persisted.
  10. The mobile assets microservice is used to query the MongoDB to get dynamic data for the mobile app. The booklet in the first view uses the database for its content.
  11. The leaderboard microservice is used to get the standings of the users.

Summary

The first time the user opens the app, he gets anonymously assigned a unique user ID in the Blockchain network. They also get assigned a random avatar and name which will be stored in MongoDB. This data will be used for the Leaderboard. The 3 microservices (Leaderboard, Mobile Assets, Registration) outside of the blockchain network are Node.js web apps to get data from the MonoDB. As users walk around, their steps will be sent to the blockchain network and they will be rewarded with "Kubecoins". These coins can be used in the blockchain network to trade assets. The assets we have are some swag (stickers, bandanas, etc.) for the KubeCon conference. Users can see and claim them using the app. Once they claim a product, they'll get a Contract ID. They'll show the Contract ID to the seller (us) and we will complete and verify the transaction in our dashboard and give them the swag. The users can also check how they are doing with their steps compared to other people in the Standings view in the mobile app or in the Kubecoin dashboard.

Included Components

Featured Technologies

Prerequisites

Steps

1. Clone the repo

$ git clone https://github.com/IBM/android-kubernetes-blockchain

2. Create IBM Cloud services

Create the following services:

3. Configure the Blockchain Network

chaincodePath: 'bcfit',
rabbitmq: 'amqps://admin:QWERTY@portal-ssl334-23.bmix-dal-yp-abc10717-6f73-4f63-b039-a1d2485c1566.devadvo-us-ibm-com.composedb.com:38919/bmix-dal-yp-abc10717-6f73-4f63-b039-a1d2485c1566',
redisUrl: 'redis://admin:QWERTY@sl-us-south-1-portal.23.dblayer.com:38916',

mongo credentials

...
env:
  - name: MONGODB_URL
    value: 'mongodb://admin:QWERTY@sl-us-south-1-po...'
...

you will need to be logged in using docker login

$ ./buildAndPushImages.sh <YOUR_DOCKERHUB_USERNAME>

4. Deploy the Blockchain Network in Kubernetes

$ ./deployNetwork.sh
$ kubectl get svc rabbitclient-api

# NAME               CLUSTER-IP      EXTERNAL-IP     PORT(S)          AGE
# rabbitclient-api   172.21.40.201   169.61.17.000   3000:30726/TCP   14m
$ export URL="http://169.61.17.000:3000"

$ curl -H "Content-Type: application/json" -X POST -d '{"type":"enroll","queue":"user_queue","params":{}}' "$URL/api/execute"

# you should get something like this
# {"status":"success","resultId":"7f90764a-8660-45f2-904d-47d8fb87a900"}
$ curl $URL/api/results/RESULT_ID

# RESULT_ID from the previous step is 7f90764a-8660-45f2-904d-47d8fb87a900
# you should get something like this
# {"status":"done","result":"{\"message\":\"success\",\"result\":{\"user\":\"50f97085-376d-40b3-8992-a9a2e6d18668\",\"txId\":\"2fcddeae9ddece5c818fed626601fba80711f3583944f8e53632972792954e09\"}}"}

# if you {"status":"pending"}
# try again or check if the resultId you copied is correct

5. Expose the backend with Kubernetes Ingress

$ bx cs cluster-get <Your cluster name here>

# You should look for these values
# ..
# Ingress Subdomain:    anthony-blockchain.us-south.containers.mybluemix.net
# Ingress Secret:       anthony-blockchain
# ..
...
spec:
  tls:
  - hosts:
    - <Your-ingress-SUBDOMAIN-here>
    secretName: <Your-ingress-SECRET-here>
  backend:
    serviceName: mobile-assets
    servicePort: 80
  rules:
  - host: <Your-ingress-SUBDOMAIN-here>
...
$ kubectl apply -f ingress-prod.yaml

6. Configure the Android app

alt text

The line would look something like:

...
String BACKEND_URL = "https://anthony-blockchain.us-south.containers.mybluemix.net";
...

7. Test the Android app

$ export URL="https://<Your-ingress-subdomain>"
$ curl -H "Content-Type: application/json" -X POST -d '{"type":"enroll","queue":"seller_queue","params":{}}' "$URL/api/execute"

# you will get something in the format of
# {"status":"success","resultId":"dfb6ef43-4eb2-467d-8c05-93ed5d0b517f"}
# using the result ID you got above, do:

$ curl "$URL/api/results/dfb6ef43-4eb2-467d-8c05-93ed5d0b517f"

# You will get:
# { "status":"done",
#   "result": "{\"message\":\"success\",\"result\":{\"user\":\"351a9993-c204-4f75-a38c-ccaba49929bc\",\"txId\":\"15698a529b7c1bc98725cb61177c621bac9de03347567c405bd68e1ff96b120a\"}}"}

# The ID of the seller would be 351a9993-c204-4f75-a38c-ccaba49929bc
$ export SELLER_ID=<seller ID you got from step above>

$ curl -H "Content-Type: application/json" -X POST -d '{"type":"invoke","queue":"seller_queue","params":{"userId": "'"$SELLER_ID"'", "fcn":"createProduct","args":["'"$SELLER_ID"'","eye-sticker","Eye sticker","100","1"]}}' "$URL/api/execute"
$ curl -H "Content-Type: application/json" -X POST -d '{"type":"invoke","queue":"seller_queue","params":{"userId": "'"$SELLER_ID"'", "fcn":"createProduct","args":["'"$SELLER_ID"'","bee-sticker","Bee sticker","100","1"]}}' "$URL/api/execute"
$ curl -H "Content-Type: application/json" -X POST -d '{"type":"invoke","queue":"seller_queue","params":{"userId": "'"$SELLER_ID"'", "fcn":"createProduct","args":["'"$SELLER_ID"'","em-sticker","M sticker","100","1"]}}' "$URL/api/execute"

Learn more about the chaincode functions for this project here

Links

Learn more

License

This code pattern is licensed under the Apache Software License, Version 2. Separate third party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 (DCO) and the Apache Software License, Version 2.

Apache Software License (ASL) FAQ