Skip to content

Commit

Permalink
feat: added wrapper classes for TariTransactionKernel
Browse files Browse the repository at this point in the history
Update libwallet version

Update jniWallet.cpp to latest interface

Update FFIWallet.kt to reflect changes in jniWallet.cpp
  • Loading branch information
StriderDM committed Sep 2, 2021
1 parent 72a9d91 commit d4ca731
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 26 deletions.
1 change: 1 addition & 0 deletions app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ add_library(
jniContact.cpp
jniCommsConfig.cpp
jniCompletedTransaction.cpp
jniCompletedTransactionKernel.cpp
jniPendingInboundTransaction.cpp
jniPendingOutboundTransaction.cpp
jniCollections.cpp
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/cpp/jniCompletedTransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<TariCompletedTransaction *>(lCompletedTx);
auto result = reinterpret_cast<jlong>(
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(
Expand Down
102 changes: 102 additions & 0 deletions app/src/main/cpp/jniCompletedTransactionKernel.cpp
Original file line number Diff line number Diff line change
@@ -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 <jni.h>
#include <android/log.h>
#include <wallet.h>
#include <string>
#include <cmath>
#include <android/log.h>
#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<TariTransactionKernel *>(lKernel);
const char *pStr = transaction_kernel_get_excess_hex(pKernel, r);
setErrorCode(jEnv, error, i);
jstring result = jEnv->NewStringUTF(pStr);
string_destroy(const_cast<char *>(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<TariTransactionKernel *>(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<char *>(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<TariTransactionKernel *>(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<char *>(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<TariTransactionKernel *>(lKernel);
transaction_kernel_destroy(pKernel);
SetPointerField(jEnv, jThis, reinterpret_cast<jlong>(nullptr));
}
17 changes: 17 additions & 0 deletions app/src/main/cpp/jniWallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -488,6 +491,7 @@ Java_com_tari_android_wallet_ffi_FFIWallet_jniCreate(
invalidTxoValidationCompleteCallback,
transactionValidationCompleteCallback,
storeAndForwardMessagesReceivedCallback,
recovery,
r);
} else {
pWallet = wallet_create(
Expand All @@ -511,6 +515,7 @@ Java_com_tari_android_wallet_ffi_FFIWallet_jniCreate(
invalidTxoValidationCompleteCallback,
transactionValidationCompleteCallback,
storeAndForwardMessagesReceivedCallback,
recovery,
r);
}
setErrorCode(jEnv, error, i);
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -1363,6 +1379,7 @@ Java_com_tari_android_wallet_ffi_FFIWallet_jniTestReceiveTx(
setErrorCode(jEnv, error, i);
return result;
}
*/

extern "C"
JNIEXPORT jbyteArray JNICALL
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/java/com/tari/android/wallet/ffi/FFICompletedTx.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}

}
Loading

0 comments on commit d4ca731

Please sign in to comment.