Skip to content

Commit 5624544

Browse files
committed
ZKMA demo app first commit.
Features include: - Create seed - Restore seed - Clear seed - Sign message
1 parent 4eb9511 commit 5624544

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1374
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.gradle/*
2+
.idea/*
3+
local.properties
4+
.DS_Store

app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
4+
5+
android {
6+
compileSdkVersion 29
7+
buildToolsVersion "29.0.3"
8+
9+
defaultConfig {
10+
applicationId "com.htc.zion.demoapp"
11+
minSdkVersion 26
12+
targetSdkVersion 29
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
kotlinOptions {
30+
jvmTarget = '1.8'
31+
}
32+
buildFeatures {
33+
viewBinding = true
34+
}
35+
}
36+
37+
allprojects {
38+
repositories {
39+
flatDir {
40+
dirs 'libs'
41+
}
42+
}
43+
}
44+
45+
46+
dependencies {
47+
implementation fileTree(dir: "libs", include: ["*.jar"])
48+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
49+
implementation 'androidx.core:core-ktx:1.3.0'
50+
implementation 'androidx.appcompat:appcompat:1.1.0'
51+
implementation 'com.google.android.material:material:1.1.0'
52+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
53+
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
54+
implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
55+
testImplementation 'junit:junit:4.12'
56+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
57+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
58+
59+
implementation 'com.google.code.gson:gson:2.8.6'
60+
implementation(name:'HtcWalletSDK-Htc_partner1-release', ext:'aar')
61+
}
6.7 MB
Binary file not shown.

app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.htc.zion.demoapp
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.htc.zion.demoapp", appContext.packageName)
23+
}
24+
}

app/src/main/AndroidManifest.xml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.htc.zion.demoapp">
4+
5+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/AppTheme">
14+
<activity
15+
android:name=".MainActivity"
16+
android:label="@string/app_name"
17+
android:theme="@style/AppTheme.NoActionBar">
18+
<intent-filter>
19+
<action android:name="android.intent.action.MAIN" />
20+
21+
<category android:name="android.intent.category.LAUNCHER" />
22+
</intent-filter>
23+
</activity>
24+
</application>
25+
26+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.htc.zion.demoapp
2+
3+
import android.app.Activity.RESULT_OK
4+
import android.content.Context
5+
import android.content.Intent
6+
import android.os.Bundle
7+
import android.util.Log
8+
import android.view.LayoutInflater
9+
import android.view.View
10+
import android.view.ViewGroup
11+
import androidx.fragment.app.Fragment
12+
import com.google.android.material.snackbar.Snackbar
13+
import com.google.gson.Gson
14+
import com.htc.htcwalletsdk.Native.Type.ByteArrayHolder
15+
import com.htc.zion.demoapp.Utils.Companion.LOG_TAG
16+
import com.htc.zion.demoapp.Utils.Companion.convertToHex
17+
import com.htc.zion.demoapp.Utils.Companion.sha256
18+
import com.htc.zion.demoapp.data.EthereumJsonTemplate
19+
import com.htc.zion.demoapp.data.Message
20+
import com.htc.zion.demoapp.databinding.FragmentFirstBinding
21+
import kotlinx.android.synthetic.main.fragment_first.*
22+
import java.util.concurrent.Executors
23+
24+
/**
25+
* A simple [Fragment] subclass as the default destination in the navigation.
26+
*/
27+
class FirstFragment : Fragment() {
28+
val SELECT_PHOTO = 1
29+
val COIN_TYPE_ETHEREUM = 60
30+
31+
private val tzExecutor by lazy {
32+
Executors.newSingleThreadExecutor()
33+
}
34+
35+
override fun onCreateView(
36+
inflater: LayoutInflater,
37+
container: ViewGroup?,
38+
savedInstanceState: Bundle?
39+
): View? {
40+
// Inflate the layout for this fragment
41+
val binding = FragmentFirstBinding.inflate(inflater, container, false)
42+
return binding.root
43+
}
44+
45+
override fun onCreate(savedInstanceState: Bundle?) {
46+
super.onCreate(savedInstanceState)
47+
initZkmaSdk(this.requireContext())
48+
}
49+
50+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
51+
super.onViewCreated(view, savedInstanceState)
52+
53+
btn_create_seed.setOnClickListener {
54+
tzExecutor.execute {
55+
val uniqueId = Utils.getZkmaSdkUniqueId(view.context)
56+
if (ZkmaSdkHelper.isSeedExists(uniqueId)) {
57+
activity?.runOnUiThread {
58+
Snackbar.make(view, R.string.msg_seed_exists, Snackbar.LENGTH_LONG)
59+
.setAction("Action", null).show()
60+
}
61+
} else {
62+
val result = ZkmaSdkHelper.createSeed(uniqueId)
63+
Log.i(LOG_TAG, "createSeed(), result=$result")
64+
}
65+
}
66+
}
67+
68+
btn_restore_seed.setOnClickListener {
69+
tzExecutor.execute {
70+
val uniqueId = Utils.getZkmaSdkUniqueId(view.context)
71+
if (ZkmaSdkHelper.isSeedExists(uniqueId)) {
72+
activity?.runOnUiThread {
73+
Snackbar.make(view, R.string.msg_seed_exists, Snackbar.LENGTH_LONG)
74+
.setAction("Action", null).show()
75+
}
76+
} else {
77+
val result = ZkmaSdkHelper.restoreSeed(uniqueId)
78+
Log.i(LOG_TAG, "restoreSeed(), result=$result")
79+
}
80+
}
81+
}
82+
83+
btn_sign_photo.setOnClickListener {
84+
val intent = Intent(
85+
Intent.ACTION_PICK,
86+
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
87+
)
88+
intent.type = "image/*"
89+
startActivityForResult(intent, SELECT_PHOTO)
90+
}
91+
}
92+
93+
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
94+
super.onActivityResult(requestCode, resultCode, data)
95+
96+
if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK) {
97+
data?.data?.let { uri ->
98+
tzExecutor.execute {
99+
Utils.loadImage(this.requireContext(), uri)?.let { bitmap ->
100+
bitmap.convertToHex()
101+
}?.let { bitmapHex ->
102+
bitmapHex.sha256()
103+
}?.let { bitmapHash ->
104+
val photoData = EthereumJsonTemplate(Message(bitmapHash))
105+
Log.i(LOG_TAG, "EthereumJson=$photoData")
106+
val sig = ByteArrayHolder()
107+
val result = ZkmaSdkHelper.signMessage(
108+
Utils.getZkmaSdkUniqueId(this.requireContext()),
109+
COIN_TYPE_ETHEREUM,
110+
Gson().toJson(photoData),
111+
sig
112+
)
113+
Log.i(
114+
LOG_TAG,
115+
"signMessage(), result=$result, sig=${sig.byteArray.toString()}"
116+
)
117+
}
118+
}
119+
}
120+
}
121+
}
122+
123+
private fun initZkmaSdk(context: Context) {
124+
tzExecutor.execute {
125+
ZkmaSdkHelper.initSdk(context)
126+
}
127+
}
128+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.htc.zion.demoapp
2+
3+
import android.Manifest
4+
import android.app.Activity
5+
import android.content.pm.PackageManager
6+
import android.os.Bundle
7+
import android.util.Log
8+
import android.view.Menu
9+
import android.view.MenuItem
10+
import android.widget.Toast
11+
import androidx.appcompat.app.AppCompatActivity
12+
import androidx.core.app.ActivityCompat
13+
import com.google.android.material.snackbar.Snackbar
14+
import com.htc.zion.demoapp.databinding.ActivityMainBinding
15+
import kotlinx.android.synthetic.main.activity_main.*
16+
import java.util.concurrent.Executors
17+
18+
class MainActivity : AppCompatActivity() {
19+
private val tzExecutor by lazy {
20+
Executors.newSingleThreadExecutor()
21+
}
22+
23+
// Storage Permissions
24+
private val REQUEST_EXTERNAL_STORAGE = 1
25+
private val PERMISSIONS_STORAGE = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)
26+
27+
override fun onCreate(savedInstanceState: Bundle?) {
28+
super.onCreate(savedInstanceState)
29+
val binding = ActivityMainBinding.inflate(this.layoutInflater)
30+
setContentView(binding.root)
31+
setSupportActionBar(toolbar)
32+
33+
fab.setOnClickListener { view ->
34+
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
35+
.setAction("Action", null).show()
36+
}
37+
38+
verifyStoragePermissions(this)
39+
}
40+
41+
override fun onCreateOptionsMenu(menu: Menu): Boolean {
42+
// Inflate the menu; this adds items to the action bar if it is present.
43+
menuInflater.inflate(R.menu.menu_main, menu)
44+
return true
45+
}
46+
47+
override fun onOptionsItemSelected(item: MenuItem): Boolean {
48+
// Handle action bar item clicks here. The action bar will
49+
// automatically handle clicks on the Home/Up button, so long
50+
// as you specify a parent activity in AndroidManifest.xml.
51+
return when (item.itemId) {
52+
R.id.action_clear_seed -> {
53+
tzExecutor.execute {
54+
val uniqueId = Utils.getZkmaSdkUniqueId(this)
55+
if (ZkmaSdkHelper.isSeedExists(uniqueId)) {
56+
ZkmaSdkHelper.clearSeed(uniqueId)
57+
} else {
58+
Log.w(Utils.LOG_TAG, "clearSeed(), uniqueId is illegal!")
59+
}
60+
}
61+
true
62+
}
63+
else -> super.onOptionsItemSelected(item)
64+
}
65+
}
66+
67+
override fun onRequestPermissionsResult(
68+
requestCode: Int,
69+
permissions: Array<out String>,
70+
grantResults: IntArray
71+
) {
72+
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
73+
if (requestCode == REQUEST_EXTERNAL_STORAGE) {
74+
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
75+
Toast.makeText(this, "Permission denied!", Toast.LENGTH_LONG)
76+
.show()
77+
}
78+
}
79+
}
80+
81+
private fun verifyStoragePermissions(activity: Activity) {
82+
// Check if we have read permission
83+
val permission =
84+
ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE)
85+
if (permission != PackageManager.PERMISSION_GRANTED) {
86+
// We don't have permission so prompt the user
87+
ActivityCompat.requestPermissions(
88+
activity,
89+
PERMISSIONS_STORAGE,
90+
REQUEST_EXTERNAL_STORAGE
91+
)
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)