├── .gitignore ├── LICENSE ├── README.md ├── app ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── de │ │ │ └── petendi │ │ │ └── ethereum │ │ │ └── android │ │ │ └── sample │ │ │ ├── AccountBalanceActivity.java │ │ │ ├── SampleListActivity.java │ │ │ ├── SimpleStorageActivity.java │ │ │ └── contract │ │ │ └── SimpleOwnedStorage.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── activity_sample_list.xml │ │ └── activity_simple_storage.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ ├── java │ └── de │ │ └── petendi │ │ └── ethereum │ │ └── android │ │ └── sample │ │ └── contract │ │ └── SimpleOwnedStorageTest.java │ └── resources │ └── SimpleOwnedStorage.sol ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | 28 | # Android Studio Navigation editor temp files 29 | .navigation/ 30 | 31 | # Android Studio captures folder 32 | captures/ 33 | .idea/ 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ethereum Android Samples 2 | This repo contains samples how to build applications using the blockchain via Ethereum Android. 3 | 4 | ## Account Balance Sample 5 | 6 | This sample shows how to use the low level RPC commands to interact with an Ethereum node 7 | 8 | 9 | The general approach is always the following: 10 | 11 | * create a ```WrappedRequest``` with the desired RPC command and the corresponding parameters (see https://github.com/ethereum/wiki/wiki/JSON-RPC) 12 | * either use the synchronous ```send``` or ```sendAsync``` method to retrieve a ```WrappedResponse``` 13 | * check ```WrappedResponse.isSuccess()``` and use ```WrappedResponse.getErrorMessage()``` in case it failed 14 | * if it succeeded ```WrappedResponse.getResponse()``` contains the response of the RPC call 15 | 16 | ## Simple Storage Sample 17 | 18 | This sample guides you through all steps to interact with a smart contract. 19 | 20 | ### Compile the smart contract code 21 | 22 | To interact with an existing contract you will need to have the ABI, if you want to deploy a new contract you also need its bytecode. 23 | You can use any compiler you want to achieve this. For the sake of simplicity we misuse Junit test functionality. 24 | 25 | Check ```SimpleOwnedStorageTest.compile()``` which print both bytecode and ABI to ```System.out```. These are used as constants in ```SimpleStorageActivity```. 26 | 27 | ### Deploy the smart contract 28 | 29 | Deploying a smart contract involves 2 steps: 30 | 31 | #### Create the transaction 32 | 33 | 34 | ethereumAndroid.contracts().create(String contractBytecode, String contractAbi, Object... constructorParams) 35 | 36 | This will create a hex encoded unsigned transaction String which already includes the right nonce for the users identity. 37 | 38 | #### Submit the transaction 39 | 40 | As this is a write operation, it will cost the user Ether and the user will need to approve the transaction by signing it and submitting it to the network 41 | 42 | ethereumAndroid.submitTransaction(Activity parentActivity, int requestCode, String transactionString) 43 | 44 | This will open an Activity where the user can approve the transaction. You will receive the outcome of the operation via 45 | 46 | parentActivity.onActivityResult(int requestCode, int resultCode, Intent data) 47 | 48 | In case ```resultCode == RESULT_OK``` the result Intent will contain the transaction hash: 49 | 50 | String transaction = data.getStringExtra("transaction"); 51 | 52 | It the result was not OK the Intent will contain the error message instead: 53 | 54 | String error = data.getStringExtra("error"); 55 | 56 | ### Check for the transaction receipt 57 | 58 | Retrieve the transaction receipt which contains the address of the created contract. 59 | 60 | 61 | WrappedRequest wrappedRequest = new WrappedRequest(); 62 | wrappedRequest.setCommand(RpcCommand.eth_getTransactionReceipt.toString()); 63 | wrappedRequest.setParameters(new Object[]{transaction}); 64 | 65 | 66 | The response will contain a map containing key-value-pairs of the transaction receipt, where you can read the contract address from. 67 | 68 | HashMap transactionObject = (HashMap) response.getResponse(); 69 | String contractAddress = transactionObject.get("contractAddress"); 70 | 71 | ### Interact with an existing contract 72 | 73 | In order to easily interact with an existing contract you need the contract ABI and a Java interface containing the operations you would like to use. 74 | This Java interface can contain a subset of the contract ABI, the method needs to be named exactly as the ABI function name and have the same input and output parameters. 75 | 76 | 77 | For read operations (functions which are marked with ___constant___) this is all you need to do. 78 | 79 | 80 | Write operations are represented as ```PendingTransaction``` which gives the ability to create the unsigned transaction and decode the result of the transaction once it is picked up by the network. 81 | 82 | 83 | #### Read the stored value 84 | 85 | SimpleOwnedStorage simpleOwnedStorage = ethereumAndroid.contracts().bind(contractAddress, CONTRACT_ABI, SimpleOwnedStorage.class); 86 | String value = simpleOwnedStorage.get(); 87 | 88 | #### Write a new value 89 | 90 | ___Create the transaction___ 91 | 92 | SimpleOwnedStorage simpleOwnedStorage = ethereumAndroid.contracts().bind(contractAddress, CONTRACT_ABI, SimpleOwnedStorage.class); 93 | PendingTransaction pendingWrite = simpleOwnedStorage.set("a new value"); 94 | 95 | ___Submit the transaction___ 96 | 97 | ethereumAndroid.submitTransaction(Activity parentActivity, int requestCode, pendingWrite.getUnsignedTransaction()) 98 | 99 | ___Check the activity result___ 100 | 101 | parentActivity.onActivityResult(int requestCode, int resultCode, Intent data) 102 | 103 | In the sample case instead of waiting for the transaction receipt you can also just read the value again and check if it has already changed. 104 | 105 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | 7 | defaultConfig { 8 | applicationId "de.petendi.ethereum.android.sample" 9 | minSdkVersion 15 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | } 18 | } 19 | 20 | lintOptions { 21 | abortOnError false 22 | } 23 | packagingOptions { 24 | exclude 'META-INF/LICENSE.txt' 25 | exclude 'META-INF/NOTICE.txt' 26 | exclude 'META-INF/LICENSE' 27 | exclude 'META-INF/NOTICE' 28 | } 29 | } 30 | 31 | dependencies { 32 | compile fileTree(dir: 'libs', include: ['*.jar']) 33 | compile 'de.petendi:ethereum-android-lib:0.2.1' 34 | compile 'com.android.support:appcompat-v7:23.4.0' 35 | compile 'com.android.support:design:23.4.0' 36 | testCompile 'org.ethereum:ethereumj-core:1.2.0-RELEASE' 37 | testCompile 'commons-io:commons-io:2.4' 38 | testCompile 'junit:junit:4.12' 39 | } 40 | 41 | 42 | configurations.all { 43 | resolutionStrategy { 44 | force 'com.fasterxml.jackson.core:jackson-databind:2.7.1-1' 45 | force 'com.fasterxml.jackson.core:jackson-annotations:2.7.0' 46 | force 'com.fasterxml.jackson.core:jackson-core:2.7.1' 47 | } 48 | } 49 | 50 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/ubuntu/.android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | 23 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/java/de/petendi/ethereum/android/sample/AccountBalanceActivity.java: -------------------------------------------------------------------------------- 1 | package de.petendi.ethereum.android.sample; 2 | 3 | import android.animation.Animator; 4 | import android.animation.AnimatorListenerAdapter; 5 | import android.annotation.TargetApi; 6 | import android.os.AsyncTask; 7 | import android.os.Build; 8 | import android.os.Bundle; 9 | import android.support.v7.app.AppCompatActivity; 10 | import android.text.TextUtils; 11 | import android.view.View; 12 | import android.view.View.OnClickListener; 13 | import android.widget.AutoCompleteTextView; 14 | import android.widget.Button; 15 | import android.widget.TextView; 16 | import android.widget.Toast; 17 | 18 | import java.lang.reflect.Field; 19 | 20 | import de.petendi.ethereum.android.EthereumAndroid; 21 | import de.petendi.ethereum.android.EthereumAndroidCallback; 22 | import de.petendi.ethereum.android.EthereumAndroidFactory; 23 | import de.petendi.ethereum.android.EthereumNotInstalledException; 24 | import de.petendi.ethereum.android.Utils; 25 | import de.petendi.ethereum.android.service.model.RpcCommand; 26 | import de.petendi.ethereum.android.service.model.ServiceError; 27 | import de.petendi.ethereum.android.service.model.WrappedRequest; 28 | import de.petendi.ethereum.android.service.model.WrappedResponse; 29 | 30 | public class AccountBalanceActivity extends AppCompatActivity implements EthereumAndroidCallback { 31 | 32 | private class MyAsyncTask extends AsyncTask { 33 | private final WrappedRequest request; 34 | 35 | private MyAsyncTask(WrappedRequest request) { 36 | this.request = request; 37 | } 38 | 39 | 40 | @Override 41 | protected WrappedResponse doInBackground(Void... voids) { 42 | return ethereumAndroid.send(request); 43 | } 44 | 45 | @Override 46 | protected void onPostExecute(final WrappedResponse wrappedResponse) { 47 | showResponse(wrappedResponse); 48 | } 49 | } 50 | 51 | private AutoCompleteTextView accountInput; 52 | private View progressView; 53 | private View formView; 54 | 55 | private EthereumAndroid ethereumAndroid; 56 | 57 | @Override 58 | protected void onCreate(Bundle savedInstanceState) { 59 | super.onCreate(savedInstanceState); 60 | //this is a hack to disable the signature check so that it also connects 61 | //to development versions of Ethereum Android 62 | try { 63 | Field devField = EthereumAndroidFactory.class.getDeclaredField("DEV"); 64 | devField.setAccessible(true); 65 | devField.set(null,true); 66 | } catch (NoSuchFieldException e) { 67 | e.printStackTrace(); 68 | } catch (IllegalAccessException e) { 69 | e.printStackTrace(); 70 | } 71 | EthereumAndroidFactory ethereumAndroidFactory = new EthereumAndroidFactory(this); 72 | try { 73 | ethereumAndroid = ethereumAndroidFactory.create(this); 74 | } catch (EthereumNotInstalledException e) { 75 | Toast.makeText(this,R.string.ethereum_ethereum_not_installed,Toast.LENGTH_LONG).show(); 76 | finish(); 77 | } 78 | setContentView(R.layout.activity_main); 79 | accountInput = (AutoCompleteTextView) findViewById(R.id.address_input); 80 | Button requestButton = (Button) findViewById(R.id.request_account); 81 | requestButton.setOnClickListener(new OnClickListener() { 82 | @Override 83 | public void onClick(View view) { 84 | request(); 85 | } 86 | }); 87 | Button requestSyncButton = (Button) findViewById(R.id.request_account_sync); 88 | requestSyncButton.setOnClickListener(new OnClickListener() { 89 | @Override 90 | public void onClick(View view) { 91 | requestSync(); 92 | } 93 | }); 94 | formView = findViewById(R.id.form); 95 | progressView = findViewById(R.id.progress); 96 | } 97 | 98 | @Override 99 | protected void onDestroy() { 100 | super.onDestroy(); 101 | if(ethereumAndroid!=null) { 102 | ethereumAndroid.release(); 103 | } 104 | } 105 | 106 | private void request() { 107 | 108 | accountInput.setError(null); 109 | String accountAddress = accountInput.getText().toString(); 110 | boolean cancel = false; 111 | View focusView = null; 112 | if (TextUtils.isEmpty(accountAddress)) { 113 | accountInput.setError(getString(R.string.error_field_required)); 114 | focusView = accountInput; 115 | cancel = true; 116 | } 117 | if (cancel) { 118 | focusView.requestFocus(); 119 | } else { 120 | showProgress(true); 121 | WrappedRequest wrappedRequest = new WrappedRequest(); 122 | wrappedRequest.setCommand(RpcCommand.eth_getBalance.toString()); 123 | wrappedRequest.setParameters(new String[]{accountAddress, "latest"}); 124 | ethereumAndroid.sendAsync(wrappedRequest); 125 | } 126 | } 127 | 128 | private void requestSync() { 129 | 130 | accountInput.setError(null); 131 | String accountAddress = accountInput.getText().toString(); 132 | boolean cancel = false; 133 | View focusView = null; 134 | if (TextUtils.isEmpty(accountAddress)) { 135 | accountInput.setError(getString(R.string.error_field_required)); 136 | focusView = accountInput; 137 | cancel = true; 138 | } 139 | if (cancel) { 140 | focusView.requestFocus(); 141 | } else { 142 | showProgress(true); 143 | WrappedRequest wrappedRequest = new WrappedRequest(); 144 | wrappedRequest.setCommand(RpcCommand.eth_getBalance.toString()); 145 | wrappedRequest.setParameters(new String[]{accountAddress, "latest"}); 146 | new MyAsyncTask(wrappedRequest).execute(); 147 | } 148 | } 149 | 150 | 151 | @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) 152 | private void showProgress(final boolean show) { 153 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { 154 | int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime); 155 | 156 | formView.setVisibility(show ? View.GONE : View.VISIBLE); 157 | formView.animate().setDuration(shortAnimTime).alpha( 158 | show ? 0 : 1).setListener(new AnimatorListenerAdapter() { 159 | @Override 160 | public void onAnimationEnd(Animator animation) { 161 | formView.setVisibility(show ? View.GONE : View.VISIBLE); 162 | } 163 | }); 164 | 165 | progressView.setVisibility(show ? View.VISIBLE : View.GONE); 166 | progressView.animate().setDuration(shortAnimTime).alpha( 167 | show ? 1 : 0).setListener(new AnimatorListenerAdapter() { 168 | @Override 169 | public void onAnimationEnd(Animator animation) { 170 | progressView.setVisibility(show ? View.VISIBLE : View.GONE); 171 | } 172 | }); 173 | } else { 174 | progressView.setVisibility(show ? View.VISIBLE : View.GONE); 175 | formView.setVisibility(show ? View.GONE : View.VISIBLE); 176 | } 177 | } 178 | 179 | 180 | @Override 181 | public void handleResponse(int i, final WrappedResponse response) { 182 | Runnable updateUiTask = new Runnable() { 183 | @Override 184 | public void run() { 185 | showResponse(response); 186 | } 187 | }; 188 | runOnUiThread(updateUiTask); 189 | 190 | } 191 | 192 | private void showResponse(final WrappedResponse response) { 193 | showProgress(false); 194 | TextView balanceTextView = (TextView) findViewById(R.id.account_balance); 195 | if(response.isSuccess()) { 196 | String balance = getString(R.string.balance); 197 | balanceTextView.setText(balance + " " + Utils.fromHexString((String)response.getResponse())); 198 | } else { 199 | balanceTextView.setText(response.getErrorMessage()); 200 | } 201 | } 202 | 203 | @Override 204 | public void handleError(int i, final ServiceError error) { 205 | Runnable updateUiTask = new Runnable() { 206 | @Override 207 | public void run() { 208 | showProgress(false); 209 | TextView balanceTextView = (TextView) findViewById(R.id.account_balance); 210 | String errorMsg = getString(R.string.error_occured); 211 | balanceTextView.setText(errorMsg + " " + error.getErrorMessage()); 212 | } 213 | }; 214 | runOnUiThread(updateUiTask); 215 | } 216 | 217 | 218 | } 219 | 220 | -------------------------------------------------------------------------------- /app/src/main/java/de/petendi/ethereum/android/sample/SampleListActivity.java: -------------------------------------------------------------------------------- 1 | package de.petendi.ethereum.android.sample; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.Button; 10 | 11 | public class SampleListActivity extends AppCompatActivity { 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | setContentView(R.layout.activity_sample_list); 17 | setButtons(); 18 | } 19 | 20 | private void setButtons() { 21 | 22 | final View.OnClickListener clickListener = new View.OnClickListener() { 23 | @Override 24 | public void onClick(View v) { 25 | int id = v.getId(); 26 | Class activityClass; 27 | switch(id) { 28 | case R.id.sample_account_balance_button: 29 | activityClass = AccountBalanceActivity.class; 30 | break; 31 | case R.id.sample_simplestorage_button: 32 | activityClass = SimpleStorageActivity.class; 33 | break; 34 | default: 35 | throw new IllegalArgumentException("unknown button: " + id); 36 | 37 | } 38 | Intent intent = new Intent(SampleListActivity.this,activityClass); 39 | SampleListActivity.this.startActivity(intent); 40 | } 41 | }; 42 | ViewGroup parent = (ViewGroup) findViewById(R.id.samplelist_layout); 43 | for (int i = 0; i < parent.getChildCount(); i++) { 44 | View view = parent.getChildAt(i); 45 | if (view instanceof Button) { 46 | Button button = (Button) view; 47 | button.setOnClickListener(clickListener); 48 | 49 | } 50 | } 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/java/de/petendi/ethereum/android/sample/SimpleStorageActivity.java: -------------------------------------------------------------------------------- 1 | package de.petendi.ethereum.android.sample; 2 | 3 | import android.content.Intent; 4 | import android.content.SharedPreferences; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.text.TextUtils; 8 | import android.util.Base64; 9 | import android.view.View; 10 | import android.widget.AutoCompleteTextView; 11 | import android.widget.Button; 12 | import android.widget.Toast; 13 | 14 | import java.lang.reflect.Field; 15 | import java.math.BigInteger; 16 | import java.util.HashMap; 17 | 18 | import de.petendi.ethereum.android.EthereumAndroid; 19 | import de.petendi.ethereum.android.EthereumAndroidFactory; 20 | import de.petendi.ethereum.android.EthereumNotInstalledException; 21 | import de.petendi.ethereum.android.Utils; 22 | import de.petendi.ethereum.android.contract.PendingTransaction; 23 | import de.petendi.ethereum.android.contract.model.ResponseNotOKException; 24 | import de.petendi.ethereum.android.sample.contract.SimpleOwnedStorage; 25 | import de.petendi.ethereum.android.service.model.RpcCommand; 26 | import de.petendi.ethereum.android.service.model.WrappedRequest; 27 | import de.petendi.ethereum.android.service.model.WrappedResponse; 28 | 29 | public class SimpleStorageActivity extends AppCompatActivity { 30 | 31 | private enum State { 32 | NO_CONTRACT_DEPLOYED, 33 | CONTRACT_NOT_MINED_YET, 34 | CONTRACT_DEPLOYED 35 | } 36 | 37 | private final static String CONTRACT_BYTECODCE = "6060604052604051610485380380610485833981016040528080518201919060200150505b5b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908302179055505b8060016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a057805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518260005055916020019190600101906100b2565b5b5090506100fc91906100de565b808211156100f857600081815060009055506001016100de565b5090565b505033600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908302179055505b5061034c806101396000396000f360606040526000357c0100000000000000000000000000000000000000000000000000000000900480634ed3885e1461004f5780636d4ce63c146100a5578063b387ef92146101205761004d565b005b6100a36004808035906020019082018035906020019191908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050909091905050610215565b005b6100b26004805050610159565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600f02600301f150905090810190601f1680156101125780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b61012d600480505061031d565b604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b602060405190810160405280600081526020015060016000508054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102065780601f106101db57610100808354040283529160200191610206565b820191906000526020600020905b8154815290600101906020018083116101e957829003601f168201915b50505050509050610212565b90565b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610319578060016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102ba57805160ff19168380011785556102eb565b828001600101855582156102eb579182015b828111156102ea5782518260005055916020019190600101906102cc565b5b50905061031691906102f8565b8082111561031257600081815060009055506001016102f8565b5090565b50505b5b50565b6000600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050610349565b9056"; 38 | private final static String CONTRACT_ABI = "[{\"constant\":false,\"inputs\":[{\"name\":\"d\",\"type\":\"string\"}],\"name\":\"set\",\"outputs\":[],\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"currentOwner\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"type\":\"function\"},{\"inputs\":[{\"name\":\"d\",\"type\":\"string\"}],\"type\":\"constructor\"}]"; 39 | 40 | private static final String SIMPLE_STORAGE_PREFS = "simple_storage"; 41 | private static final String CONTRACT_ADDRESS = "contractAddress"; 42 | private static final String TRANSACTION = "transaction"; 43 | private final static int REQUEST_CODE_DEPLOY = 753; 44 | private final static int REQUEST_CODE_WRITE = 754; 45 | 46 | 47 | private EthereumAndroid ethereumAndroid; 48 | private State currentState = State.NO_CONTRACT_DEPLOYED; 49 | 50 | 51 | @Override 52 | protected void onCreate(Bundle savedInstanceState) { 53 | super.onCreate(savedInstanceState); 54 | //this is a hack to disable the signature check so that it also connects 55 | //to development versions of Ethereum Android 56 | try { 57 | Field devField = EthereumAndroidFactory.class.getDeclaredField("DEV"); 58 | devField.setAccessible(true); 59 | devField.set(null, true); 60 | } catch (NoSuchFieldException e) { 61 | e.printStackTrace(); 62 | } catch (IllegalAccessException e) { 63 | e.printStackTrace(); 64 | } 65 | initialize(); 66 | setContentView(R.layout.activity_simple_storage); 67 | applyState(); 68 | } 69 | 70 | private void initialize() { 71 | EthereumAndroidFactory ethereumAndroidFactory = new EthereumAndroidFactory(this); 72 | try { 73 | ethereumAndroid = ethereumAndroidFactory.create(); 74 | } catch (EthereumNotInstalledException e) { 75 | Toast.makeText(this, R.string.ethereum_ethereum_not_installed, Toast.LENGTH_LONG).show(); 76 | finish(); 77 | } 78 | } 79 | 80 | @Override 81 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 82 | super.onActivityResult(requestCode, resultCode, data); 83 | if (requestCode == REQUEST_CODE_DEPLOY) { 84 | if (resultCode == RESULT_OK) { 85 | String transaction = data.getStringExtra(TRANSACTION); 86 | Toast.makeText(this, "deploy contract transaction: " + transaction, Toast.LENGTH_LONG).show(); 87 | getSharedPreferences(SIMPLE_STORAGE_PREFS, MODE_PRIVATE).edit().putString(TRANSACTION, transaction).commit(); 88 | applyState(); 89 | } else { 90 | String error = data.getStringExtra("error"); 91 | Toast.makeText(this, "deploying contract failed " + error, Toast.LENGTH_LONG).show(); 92 | } 93 | } else if (requestCode == REQUEST_CODE_WRITE) { 94 | if (resultCode == RESULT_OK) { 95 | String transaction = data.getStringExtra(TRANSACTION); 96 | Toast.makeText(this, "write value transaction " + transaction, Toast.LENGTH_LONG).show(); 97 | getSharedPreferences(SIMPLE_STORAGE_PREFS, MODE_PRIVATE).edit().putString(TRANSACTION, transaction).commit(); 98 | } else { 99 | String error = data.getStringExtra("error"); 100 | Toast.makeText(this, "write value failed " + error, Toast.LENGTH_LONG).show(); 101 | } 102 | } 103 | } 104 | 105 | 106 | private void applyState() { 107 | SharedPreferences prefs = getSharedPreferences(SIMPLE_STORAGE_PREFS, MODE_PRIVATE); 108 | String transaction = prefs.getString(TRANSACTION, null); 109 | if (transaction == null) { 110 | currentState = State.NO_CONTRACT_DEPLOYED; 111 | } else { 112 | String contractAddress = prefs.getString(CONTRACT_ADDRESS, null); 113 | if (contractAddress == null) { 114 | currentState = State.CONTRACT_NOT_MINED_YET; 115 | } else { 116 | currentState = State.CONTRACT_DEPLOYED; 117 | } 118 | } 119 | 120 | Button buttonRead = (Button) findViewById(R.id.read); 121 | buttonRead.setOnClickListener(new View.OnClickListener() { 122 | @Override 123 | public void onClick(View v) { 124 | readValue(); 125 | } 126 | }); 127 | 128 | Button buttonWrite = (Button) findViewById(R.id.write); 129 | buttonWrite.setOnClickListener(new View.OnClickListener() { 130 | @Override 131 | public void onClick(View v) { 132 | writeValue(); 133 | } 134 | }); 135 | 136 | Button buttonDeploy = (Button) findViewById(R.id.deploy_contract); 137 | buttonDeploy.setOnClickListener(new View.OnClickListener() { 138 | @Override 139 | public void onClick(View v) { 140 | deployContract(); 141 | } 142 | }); 143 | 144 | Button buttonReadReceipt = (Button) findViewById(R.id.read_receipt); 145 | buttonReadReceipt.setOnClickListener(new View.OnClickListener() { 146 | @Override 147 | public void onClick(View v) { 148 | readContractAddress(); 149 | } 150 | }); 151 | 152 | 153 | Button buttonReadOwner = (Button) findViewById(R.id.readOwner); 154 | buttonReadOwner.setOnClickListener(new View.OnClickListener() { 155 | @Override 156 | public void onClick(View v) { 157 | readOwner(); 158 | } 159 | }); 160 | 161 | AutoCompleteTextView valueTextview = (AutoCompleteTextView) findViewById(R.id.storage_input); 162 | 163 | switch (currentState) { 164 | case NO_CONTRACT_DEPLOYED: 165 | buttonWrite.setVisibility(View.GONE); 166 | buttonRead.setVisibility(View.GONE); 167 | buttonReadOwner.setVisibility(View.GONE); 168 | buttonDeploy.setVisibility(View.VISIBLE); 169 | buttonReadReceipt.setVisibility(View.GONE); 170 | valueTextview.setVisibility(View.GONE); 171 | break; 172 | case CONTRACT_NOT_MINED_YET: 173 | buttonWrite.setVisibility(View.GONE); 174 | buttonRead.setVisibility(View.GONE); 175 | buttonReadOwner.setVisibility(View.GONE); 176 | buttonDeploy.setVisibility(View.GONE); 177 | buttonReadReceipt.setVisibility(View.VISIBLE); 178 | valueTextview.setVisibility(View.GONE); 179 | break; 180 | case CONTRACT_DEPLOYED: 181 | buttonWrite.setVisibility(View.VISIBLE); 182 | buttonRead.setVisibility(View.VISIBLE); 183 | buttonReadOwner.setVisibility(View.VISIBLE); 184 | buttonDeploy.setVisibility(View.GONE); 185 | buttonReadReceipt.setVisibility(View.GONE); 186 | valueTextview.setVisibility(View.VISIBLE); 187 | break; 188 | } 189 | 190 | } 191 | 192 | 193 | private void readOwner() { 194 | SharedPreferences prefs = getSharedPreferences(SIMPLE_STORAGE_PREFS, MODE_PRIVATE); 195 | String contractAddress = prefs.getString(CONTRACT_ADDRESS, null); 196 | final SimpleOwnedStorage simpleOwnedStorage = ethereumAndroid.contracts().bind(contractAddress, CONTRACT_ABI, SimpleOwnedStorage.class); 197 | Runnable readTask = new Runnable() { 198 | @Override 199 | public void run() { 200 | String currentOwner; 201 | try { 202 | currentOwner = simpleOwnedStorage.currentOwner(); 203 | } catch(Exception e) { 204 | showError(e); 205 | return; 206 | } 207 | final byte[] owner = Base64.decode(currentOwner,Base64.DEFAULT); 208 | Runnable showResult = new Runnable() { 209 | @Override 210 | public void run() { 211 | Toast.makeText(SimpleStorageActivity.this, "owner: " + Utils.toHexString(new BigInteger(owner)), Toast.LENGTH_LONG).show(); 212 | } 213 | }; 214 | SimpleStorageActivity.this.runOnUiThread(showResult); 215 | } 216 | }; 217 | new Thread(readTask, "read owner thread").start(); 218 | } 219 | 220 | private void showError(final Exception e) { 221 | final String message; 222 | 223 | if (e instanceof ResponseNotOKException) { 224 | message = ((ResponseNotOKException) e).getErrorMessage(); 225 | } else { 226 | message = e.getMessage(); 227 | } 228 | Runnable showResult = new Runnable() { 229 | @Override 230 | public void run() { 231 | Toast.makeText(SimpleStorageActivity.this, "an error occurred: " + message, Toast.LENGTH_LONG).show(); 232 | if(!ethereumAndroid.hasServiceConnection()) { 233 | initialize(); 234 | } 235 | } 236 | }; 237 | SimpleStorageActivity.this.runOnUiThread(showResult); 238 | } 239 | 240 | private void readValue() { 241 | SharedPreferences prefs = getSharedPreferences(SIMPLE_STORAGE_PREFS, MODE_PRIVATE); 242 | String contractAddress = prefs.getString(CONTRACT_ADDRESS, null); 243 | final SimpleOwnedStorage simpleOwnedStorage = ethereumAndroid.contracts().bind(contractAddress, CONTRACT_ABI, SimpleOwnedStorage.class); 244 | Runnable readTask = new Runnable() { 245 | @Override 246 | public void run() { 247 | final String value; 248 | try { 249 | value = simpleOwnedStorage.get(); 250 | } catch (Exception e) { 251 | showError(e); 252 | return; 253 | } 254 | Runnable showResult = new Runnable() { 255 | @Override 256 | public void run() { 257 | Toast.makeText(SimpleStorageActivity.this, "stored value: " + value, Toast.LENGTH_LONG).show(); 258 | } 259 | }; 260 | SimpleStorageActivity.this.runOnUiThread(showResult); 261 | } 262 | }; 263 | new Thread(readTask, "read contract data thread").start(); 264 | } 265 | 266 | private void writeValue() { 267 | AutoCompleteTextView valueTextview = (AutoCompleteTextView) findViewById(R.id.storage_input); 268 | final String value = valueTextview.getText().toString(); 269 | if (TextUtils.isEmpty(value)) { 270 | valueTextview.setError(getString(R.string.error_field_required)); 271 | } else { 272 | SharedPreferences prefs = getSharedPreferences(SIMPLE_STORAGE_PREFS, MODE_PRIVATE); 273 | final String contractAddress = prefs.getString(CONTRACT_ADDRESS, null); 274 | final SimpleOwnedStorage simpleOwnedStorage = ethereumAndroid.contracts().bind(contractAddress, CONTRACT_ABI, SimpleOwnedStorage.class); 275 | Runnable writeTask = new Runnable() { 276 | @Override 277 | public void run() { 278 | final PendingTransaction pendingWrite; 279 | try { 280 | pendingWrite = simpleOwnedStorage.set(value); 281 | } catch (Exception e) { 282 | showError(e); 283 | return; 284 | } 285 | Runnable transactionTask = new Runnable() { 286 | @Override 287 | public void run() { 288 | ethereumAndroid.submitTransaction(SimpleStorageActivity.this, REQUEST_CODE_WRITE, pendingWrite.getUnsignedTransaction()); 289 | } 290 | }; 291 | SimpleStorageActivity.this.runOnUiThread(transactionTask); 292 | } 293 | }; 294 | new Thread(writeTask, "write contract data thread").start(); 295 | } 296 | } 297 | 298 | private void readContractAddress() { 299 | final SharedPreferences prefs = getSharedPreferences(SIMPLE_STORAGE_PREFS, MODE_PRIVATE); 300 | final String transaction = prefs.getString(TRANSACTION, null); 301 | Runnable readTask = new Runnable() { 302 | @Override 303 | public void run() { 304 | WrappedRequest wrappedRequest = new WrappedRequest(); 305 | wrappedRequest.setCommand(RpcCommand.eth_getTransactionReceipt.toString()); 306 | wrappedRequest.setParameters(new Object[]{transaction}); 307 | final WrappedResponse response = ethereumAndroid.send(wrappedRequest); 308 | if (response.isSuccess()) { 309 | HashMap transactionObject = (HashMap) response.getResponse(); 310 | final String contractAddress = transactionObject.get(CONTRACT_ADDRESS); 311 | if (contractAddress != null) { 312 | prefs.edit().putString(CONTRACT_ADDRESS, contractAddress).commit(); 313 | Runnable updateStateTask = new Runnable() { 314 | @Override 315 | public void run() { 316 | applyState(); 317 | } 318 | }; 319 | runOnUiThread(updateStateTask); 320 | } 321 | } else { 322 | Runnable showErrorTask = new Runnable() { 323 | @Override 324 | public void run() { 325 | Toast.makeText(SimpleStorageActivity.this, "reading address failed " + response.getErrorMessage(), Toast.LENGTH_LONG).show(); 326 | } 327 | }; 328 | runOnUiThread(showErrorTask); 329 | } 330 | } 331 | }; 332 | new Thread(readTask, "read contract address thread").start(); 333 | } 334 | 335 | private void deployContract() { 336 | Runnable deployContractTask = new Runnable() { 337 | @Override 338 | public void run() { 339 | final String transaction = ethereumAndroid.contracts().create(CONTRACT_BYTECODCE, CONTRACT_ABI, "initial value"); 340 | Runnable submitTransactionTask = new Runnable() { 341 | @Override 342 | public void run() { 343 | ethereumAndroid.submitTransaction(SimpleStorageActivity.this, REQUEST_CODE_DEPLOY, transaction); 344 | } 345 | }; 346 | runOnUiThread(submitTransactionTask); 347 | } 348 | }; 349 | new Thread(deployContractTask, "create contract thread").start(); 350 | } 351 | } 352 | -------------------------------------------------------------------------------- /app/src/main/java/de/petendi/ethereum/android/sample/contract/SimpleOwnedStorage.java: -------------------------------------------------------------------------------- 1 | package de.petendi.ethereum.android.sample.contract; 2 | 3 | 4 | import de.petendi.ethereum.android.contract.PendingTransaction; 5 | 6 | 7 | public interface SimpleOwnedStorage { 8 | 9 | String currentOwner(); 10 | 11 | String get(); 12 | 13 | PendingTransaction set(String data); 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 21 | 22 | 26 | 27 | 31 | 32 | 35 | 36 | 44 | 45 | 46 | 47 |