From d4ca731332764e388355166296c5853a54ebf615 Mon Sep 17 00:00:00 2001 From: striderDM <51991544+StriderDM@users.noreply.github.com> Date: Thu, 2 Sep 2021 23:27:42 +0200 Subject: [PATCH] feat: added wrapper classes for TariTransactionKernel Update libwallet version Update jniWallet.cpp to latest interface Update FFIWallet.kt to reflect changes in jniWallet.cpp --- app/src/main/cpp/CMakeLists.txt | 1 + app/src/main/cpp/jniCompletedTransaction.cpp | 20 ++++ .../cpp/jniCompletedTransactionKernel.cpp | 102 ++++++++++++++++++ app/src/main/cpp/jniWallet.cpp | 17 +++ .../tari/android/wallet/ffi/FFICompletedTx.kt | 11 +- .../wallet/ffi/FFICompletedTxKernel.kt | 77 +++++++++++++ .../com/tari/android/wallet/ffi/FFIWallet.kt | 42 ++++---- build.gradle | 2 +- 8 files changed, 246 insertions(+), 26 deletions(-) create mode 100644 app/src/main/cpp/jniCompletedTransactionKernel.cpp create mode 100644 app/src/main/java/com/tari/android/wallet/ffi/FFICompletedTxKernel.kt diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index eb5133544..a7a35cf71 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -65,6 +65,7 @@ add_library( jniContact.cpp jniCommsConfig.cpp jniCompletedTransaction.cpp + jniCompletedTransactionKernel.cpp jniPendingInboundTransaction.cpp jniPendingOutboundTransaction.cpp jniCollections.cpp diff --git a/app/src/main/cpp/jniCompletedTransaction.cpp b/app/src/main/cpp/jniCompletedTransaction.cpp index 5db3cefe5..67cb72331 100644 --- a/app/src/main/cpp/jniCompletedTransaction.cpp +++ b/app/src/main/cpp/jniCompletedTransaction.cpp @@ -92,6 +92,26 @@ Java_com_tari_android_wallet_ffi_FFICompletedTx_jniGetSourcePublicKey( return result; } +extern "C" +JNIEXPORT jlong JNICALL +Java_com_tari_android_wallet_ffi_FFICompletedTx_jniGetTransactionKernel( + JNIEnv *jEnv, + jobject jThis, + jobject error) { + int i = 0; + int *r = &i; + jlong lCompletedTx = GetPointerField(jEnv, jThis); + auto *pCompletedTx = reinterpret_cast(lCompletedTx); + auto result = reinterpret_cast( + completed_transaction_get_transaction_kernel( + pCompletedTx, + r + ) + ); + setErrorCode(jEnv, error, i); + return result; +} + extern "C" JNIEXPORT jbyteArray JNICALL Java_com_tari_android_wallet_ffi_FFICompletedTx_jniGetAmount( diff --git a/app/src/main/cpp/jniCompletedTransactionKernel.cpp b/app/src/main/cpp/jniCompletedTransactionKernel.cpp new file mode 100644 index 000000000..a7fdb478d --- /dev/null +++ b/app/src/main/cpp/jniCompletedTransactionKernel.cpp @@ -0,0 +1,102 @@ +/** + * Copyright 2020 The Tari Project + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the + * following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of + * its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include "jniCommon.cpp" + +extern "C" +JNIEXPORT jstring JNICALL +Java_com_tari_android_wallet_ffi_FFICompletedTxKernel_jniGetExcess( + JNIEnv *jEnv, + jobject jThis, + jobject error) { + int i = 0; + int *r = &i; + jlong lKernel = GetPointerField(jEnv, jThis); + auto *pKernel = reinterpret_cast(lKernel); + const char *pStr = transaction_kernel_get_excess_hex(pKernel, r); + setErrorCode(jEnv, error, i); + jstring result = jEnv->NewStringUTF(pStr); + string_destroy(const_cast(pStr)); + return result; +} + +extern "C" +JNIEXPORT jstring JNICALL +Java_com_tari_android_wallet_ffi_FFICompletedTxKernel_jniGetExcessPublicNonce( + JNIEnv *jEnv, + jobject jThis, + jobject error) { + int i = 0; + int *r = &i; + jlong lKernel = GetPointerField(jEnv, jThis); + auto *pKernel = reinterpret_cast(lKernel); + const char *pStr = transaction_kernel_get_excess_public_nonce_hex(pKernel, r); + setErrorCode(jEnv, error, i); + jstring result = jEnv->NewStringUTF(pStr); + string_destroy(const_cast(pStr)); + return result; +} + +extern "C" +JNIEXPORT jstring JNICALL +Java_com_tari_android_wallet_ffi_FFICompletedTxKernel_jniGetExcessSignature( + JNIEnv *jEnv, + jobject jThis, + jobject error) { + int i = 0; + int *r = &i; + jlong lKernel = GetPointerField(jEnv, jThis); + auto *pKernel = reinterpret_cast(lKernel); + const char *pStr = transaction_kernel_get_excess_signature_hex(pKernel, r); + setErrorCode(jEnv, error, i); + jstring result = jEnv->NewStringUTF(pStr); + string_destroy(const_cast(pStr)); + return result; +} + +extern "C" +JNIEXPORT void JNICALL +Java_com_tari_android_wallet_ffi_FFICompletedTxKernel_jniDestroy( + JNIEnv *jEnv, + jobject jThis) { + jlong lKernel = GetPointerField(jEnv, jThis); + auto *pKernel = reinterpret_cast(lKernel); + transaction_kernel_destroy(pKernel); + SetPointerField(jEnv, jThis, reinterpret_cast(nullptr)); +} \ No newline at end of file diff --git a/app/src/main/cpp/jniWallet.cpp b/app/src/main/cpp/jniWallet.cpp index f4af7600c..22f09e4dc 100644 --- a/app/src/main/cpp/jniWallet.cpp +++ b/app/src/main/cpp/jniWallet.cpp @@ -465,6 +465,9 @@ Java_com_tari_android_wallet_ffi_FFIWallet_jniCreate( pPassphrase = jEnv->GetStringUTFChars(jPassphrase, JNI_FALSE); } + bool recoveryInProgress = false; + bool *recovery = &recoveryInProgress; + TariWallet *pWallet; if (strlen(pLogPath) == 0) { pWallet = wallet_create( @@ -488,6 +491,7 @@ Java_com_tari_android_wallet_ffi_FFIWallet_jniCreate( invalidTxoValidationCompleteCallback, transactionValidationCompleteCallback, storeAndForwardMessagesReceivedCallback, + recovery, r); } else { pWallet = wallet_create( @@ -511,6 +515,7 @@ Java_com_tari_android_wallet_ffi_FFIWallet_jniCreate( invalidTxoValidationCompleteCallback, transactionValidationCompleteCallback, storeAndForwardMessagesReceivedCallback, + recovery, r); } setErrorCode(jEnv, error, i); @@ -1249,6 +1254,7 @@ Java_com_tari_android_wallet_ffi_FFIWallet_jniSetConfirmations( } //region Wallet Test Functions +/* extern "C" JNIEXPORT jboolean JNICALL Java_com_tari_android_wallet_ffi_FFIWallet_jniGenerateTestData( @@ -1269,7 +1275,9 @@ Java_com_tari_android_wallet_ffi_FFIWallet_jniGenerateTestData( jEnv->ReleaseStringUTFChars(jDatastorePath, pDatastorePath); return result; } +*/ +/* extern "C" JNIEXPORT jboolean JNICALL Java_com_tari_android_wallet_ffi_FFIWallet_jniTestBroadcastTx( @@ -1289,7 +1297,9 @@ Java_com_tari_android_wallet_ffi_FFIWallet_jniTestBroadcastTx( setErrorCode(jEnv, error, i); return result; } +*/ +/* extern "C" JNIEXPORT jboolean JNICALL Java_com_tari_android_wallet_ffi_FFIWallet_jniTestFinalizeReceivedTx( @@ -1309,7 +1319,9 @@ Java_com_tari_android_wallet_ffi_FFIWallet_jniTestFinalizeReceivedTx( setErrorCode(jEnv, error, i); return result; } +*/ +/* extern "C" JNIEXPORT jboolean JNICALL Java_com_tari_android_wallet_ffi_FFIWallet_jniTestCompleteSentTx( @@ -1328,7 +1340,9 @@ Java_com_tari_android_wallet_ffi_FFIWallet_jniTestCompleteSentTx( setErrorCode(jEnv, error, i); return result; } +*/ +/* extern "C" JNIEXPORT jboolean JNICALL Java_com_tari_android_wallet_ffi_FFIWallet_jniTestMineTx( @@ -1348,7 +1362,9 @@ Java_com_tari_android_wallet_ffi_FFIWallet_jniTestMineTx( setErrorCode(jEnv, error, i); return result; } +*/ +/* extern "C" JNIEXPORT jboolean JNICALL Java_com_tari_android_wallet_ffi_FFIWallet_jniTestReceiveTx( @@ -1363,6 +1379,7 @@ Java_com_tari_android_wallet_ffi_FFIWallet_jniTestReceiveTx( setErrorCode(jEnv, error, i); return result; } +*/ extern "C" JNIEXPORT jbyteArray JNICALL diff --git a/app/src/main/java/com/tari/android/wallet/ffi/FFICompletedTx.kt b/app/src/main/java/com/tari/android/wallet/ffi/FFICompletedTx.kt index 672f11911..6df76e4ba 100644 --- a/app/src/main/java/com/tari/android/wallet/ffi/FFICompletedTx.kt +++ b/app/src/main/java/com/tari/android/wallet/ffi/FFICompletedTx.kt @@ -47,7 +47,9 @@ internal class FFICompletedTx() : FFIBase() { private external fun jniGetDestinationPublicKey( libError: FFIError ): FFIPointer - + private external fun jniGetTransactionKernel( + libError: FFIError + ): FFIPointer private external fun jniGetSourcePublicKey( libError: FFIError ): FFIPointer @@ -143,6 +145,13 @@ internal class FFICompletedTx() : FFIBase() { return result } + fun getTransactionKernel(): FFICompletedTxKernel { + val error = FFIError() + val result = FFICompletedTxKernel(jniGetTransactionKernel(error)) + throwIf(error) + return result + } + override fun destroy() { jniDestroy() } diff --git a/app/src/main/java/com/tari/android/wallet/ffi/FFICompletedTxKernel.kt b/app/src/main/java/com/tari/android/wallet/ffi/FFICompletedTxKernel.kt new file mode 100644 index 000000000..24c23ea9b --- /dev/null +++ b/app/src/main/java/com/tari/android/wallet/ffi/FFICompletedTxKernel.kt @@ -0,0 +1,77 @@ +/** + * Copyright 2020 The Tari Project + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the + * following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of + * its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.tari.android.wallet.ffi + +/** + * Wrapper for native TariTransactionKernel type. + * + * @author The Tari Development Team + */ +internal class FFICompletedTxKernel() : FFIBase() { + + private external fun jniGetExcess(libError: FFIError): String + private external fun jniGetExcessPublicNonce(libError: FFIError): String + private external fun jniGetExcessSignature(libError: FFIError): String + private external fun jniDestroy() + + // endregion + constructor(pointer: FFIPointer): this() { + this.pointer = pointer + } + + fun getExcess(): String { + val error = FFIError() + val result = jniGetExcess(error) + throwIf(error) + return result + } + + fun getExcessPublicNonce(): String { + val error = FFIError() + val result = jniGetExcessPublicNonce(error) + throwIf(error) + return result + } + + fun getExcessSignature(): String { + val error = FFIError() + val result = jniGetExcessSignature(error) + throwIf(error) + return result + } + + override fun destroy() { + jniDestroy() + } + +} diff --git a/app/src/main/java/com/tari/android/wallet/ffi/FFIWallet.kt b/app/src/main/java/com/tari/android/wallet/ffi/FFIWallet.kt index 5021eb8c2..9a2ec141b 100644 --- a/app/src/main/java/com/tari/android/wallet/ffi/FFIWallet.kt +++ b/app/src/main/java/com/tari/android/wallet/ffi/FFIWallet.kt @@ -278,32 +278,44 @@ internal class FFIWallet( libError: FFIError ): ByteArray + /* private external fun jniGenerateTestData( datastorePath: String, libError: FFIError ): Boolean + */ + /* private external fun jniTestBroadcastTx( txPtr: String, libError: FFIError ): Boolean + */ + /* private external fun jniTestFinalizeReceivedTx( txPtr: FFIPendingInboundTx, libError: FFIError ): Boolean + */ + /* private external fun jniTestCompleteSentTx( txPtr: FFIPendingOutboundTx, libError: FFIError ): Boolean + */ + /* private external fun jniTestMineTx( txId: String, libError: FFIError ): Boolean + */ + /* private external fun jniTestReceiveTx(libError: FFIError): Boolean + */ private external fun jniApplyEncryption(passphrase: String, libError: FFIError) @@ -947,38 +959,23 @@ internal class FFIWallet( } fun generateTestData(datastorePath: String): Boolean { - val error = FFIError() - val result = jniGenerateTestData(datastorePath, error) - throwIf(error) - return result + return false } fun testBroadcastTx(tx: BigInteger): Boolean { - val error = FFIError() - val result = jniTestBroadcastTx(tx.toString(), error) - throwIf(error) - return result + return false } fun testCompleteSentTx(tx: FFIPendingOutboundTx): Boolean { - val error = FFIError() - val result = jniTestCompleteSentTx(tx, error) - throwIf(error) - return result + return false } fun testMineTx(tx: BigInteger): Boolean { - val error = FFIError() - val result = jniTestMineTx(tx.toString(), error) - throwIf(error) - return result + return false } fun testFinalizeReceivedTx(tx: FFIPendingInboundTx): Boolean { - val error = FFIError() - val result = jniTestFinalizeReceivedTx(tx, error) - throwIf(error) - return result + return false } fun setEncryption(passphrase: String) { @@ -996,10 +993,7 @@ internal class FFIWallet( } fun testReceiveTx(): Boolean { - val error = FFIError() - val result = jniTestReceiveTx(error) - throwIf(error) - return result + return false } override fun destroy() { diff --git a/build.gradle b/build.gradle index a61d884f9..dd02f2103 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ buildscript { ext.versionNumber = "0.6.4" // JNI libs - ext.libwalletVersion = "0.17.3_network" + ext.libwalletVersion = "0.17.6" ext.libwalletHostURL = "https://tari-binaries.s3.amazonaws.com/libwallet/" ext.supportedABIs = ["arm64-v8a", "armeabi-v7a", "x86_64"]