From 33f45a6775310c1c38f6d99099d5c6de95839934 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Tue, 15 Jan 2019 11:08:15 -0600 Subject: [PATCH] Activate the memo functionality in SendTransactionFragment. The memo encryption was not working properly for two reasons, there is a number called nonce used to encrypt the memo and it has to be positive. The app was sometimes using a negative nonce which caused encryption problems. The second problem was that the nonce was being serialized as an hexadecimal but the node was expecting a decimal so it was not able to convert back the nonce from string to decimal. --- .../fragments/SendTransactionFragment.kt | 42 +++++++++++-------- .../bitsybitshareswallet/utils/Constants.kt | 3 ++ graphenejlib | 2 +- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt index 17387fe..3fb66ce 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt @@ -30,6 +30,7 @@ import cy.agorise.graphenej.api.calls.BroadcastTransaction import cy.agorise.graphenej.api.calls.GetAccountByName import cy.agorise.graphenej.api.calls.GetDynamicGlobalProperties import cy.agorise.graphenej.api.calls.GetRequiredFees +import cy.agorise.graphenej.crypto.SecureRandomGenerator import cy.agorise.graphenej.models.AccountProperties import cy.agorise.graphenej.models.DynamicGlobalProperties import cy.agorise.graphenej.models.JsonRpcResponse @@ -49,15 +50,22 @@ import java.util.concurrent.TimeUnit import javax.crypto.AEADBadTagException class SendTransactionFragment : ConnectedFragment(), ZXingScannerView.ResultHandler { - private val TAG = this.javaClass.simpleName - // Camera Permission - private val REQUEST_CAMERA_PERMISSION = 1 + companion object { + private const val TAG = "SendTransactionFragment" - private val RESPONSE_GET_ACCOUNT_BY_NAME = 1 - private val RESPONSE_GET_DYNAMIC_GLOBAL_PARAMETERS = 2 - private val RESPONSE_GET_REQUIRED_FEES = 3 - private val RESPONSE_BROADCAST_TRANSACTION = 4 + /** The account used to send the fees */ + private val FEE_ACCOUNT = UserAccount("1.2.390320", "agorise") + + // Camera Permission + private const val REQUEST_CAMERA_PERMISSION = 1 + + // Constants used to organize NetworkService requests + private const val RESPONSE_GET_ACCOUNT_BY_NAME = 1 + private const val RESPONSE_GET_DYNAMIC_GLOBAL_PARAMETERS = 2 + private const val RESPONSE_GET_REQUIRED_FEES = 3 + private const val RESPONSE_BROADCAST_TRANSACTION = 4 + } private var isCameraPreviewVisible = false private var isToAccountCorrect = false @@ -350,7 +358,7 @@ class SendTransactionFragment : ConnectedFragment(), ZXingScannerView.ResultHand amount += nextItem.quantity * nextItem.price } // TODO Improve pattern to account for different asset precisions - val df = DecimalFormat("####.#####") + val df = DecimalFormat("####.########") df.roundingMode = RoundingMode.CEILING df.decimalFormatSymbols = DecimalFormatSymbols(Locale.getDefault()) tietAmount.setText(df.format(amount)) @@ -415,15 +423,15 @@ class SendTransactionFragment : ConnectedFragment(), ZXingScannerView.ResultHand val privateKey = ECKey.fromPrivate(DumpedPrivateKey.fromBase58(null, wifKey).key.privKeyBytes) // Add memo if exists TODO enable memo -// val memoMsg = tietMemo.text.toString() -// if (memoMsg.isNotEmpty()) { -// val nonce = SecureRandomGenerator.getSecureRandom().nextLong().toBigInteger() -// val encryptedMemo = Memo.encryptMessage(privateKey, destinationPublicKey!!, nonce, memoMsg) -// val from = Address(ECKey.fromPublicOnly(privateKey.pubKey)) -// val to = Address(destinationPublicKey!!.key) -// val memo = Memo(from, to, nonce, encryptedMemo) -// operationBuilder.setMemo(memo) -// } + val memoMsg = tietMemo.text.toString() + if (memoMsg.isNotEmpty()) { + val nonce = Math.abs(SecureRandomGenerator.getSecureRandom().nextLong()).toBigInteger() + val encryptedMemo = Memo.encryptMessage(privateKey, destinationPublicKey!!, nonce, memoMsg) + val from = Address(ECKey.fromPublicOnly(privateKey.pubKey)) + val to = Address(destinationPublicKey!!.key) + val memo = Memo(from, to, nonce, encryptedMemo) + operationBuilder.setMemo(memo) + } val operations = ArrayList() operations.add(operationBuilder.build()) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.kt index 15eb98a..3a9a611 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.kt @@ -23,6 +23,9 @@ object Constants { /** The user selected encrypted PIN */ const val KEY_ENCRYPTED_PIN = "key_encrypted_pin" + /** The fee to send in every transfer (0.01%) */ + const val FEE_PERCENTAGE = 0.0001 + /** * LTM accounts come with an expiration date expressed as this string. * This is used to recognize such accounts from regular ones. diff --git a/graphenejlib b/graphenejlib index 13faf6c..4c7c7b2 160000 --- a/graphenejlib +++ b/graphenejlib @@ -1 +1 @@ -Subproject commit 13faf6c25369d047037b639f4b838220ace916b3 +Subproject commit 4c7c7b29b2d403e8f44a2a955e0ba22169d02a48