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:
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.
Create a Kubernetes cluster with IBM Cloud Kubernetes Service to deploy in cloud.
Install Node.js
Install Docker by following the instructions here for your preferrerd operating system. You would need docker if you want to build and use your own images.
Install Android Studio.
$ git clone https://github.com/IBM/android-kubernetes-blockchain
Create the following services:
containers/blockchain/configuration/config.js
, modify the values for rabbitmq
and redis
(starting on line 12) with your own credentials.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',
leaderboard-api.yaml
, mobile-assets.yaml
, registeree-api.yaml
in the environment variable of MONGODB_URL
...
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>
kubectl
commands. You will need to point your kubectl
CLI to your IKS.$ ./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"}
resultId
you got above. You should get a user ID.$ 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
$ 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
# ..
ingress-prod.yaml
to use the provided subdomain you have...
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
android
folder in the Android Studio IDE. The project is configured to do REST calls to the Kubernetes backend. You would need to change the BACKEND_URL
variables to your own Ingress Subdomain in these Java files:
The line would look something like:
...
String BACKEND_URL = "https://anthony-blockchain.us-south.containers.mybluemix.net";
...
$ 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
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.