Skip to content

Commit

Permalink
Add static method sign for BinanceSigner (#123)
Browse files Browse the repository at this point in the history
* Add static sign method

* Add TWData *_Nonnull

* Update TWRippleSigner.h

* Update BinanceSignerTests

* Add binance signing output

* Update TestBinanceTransactionSigning.kt

* Fix binance android
  • Loading branch information
vikmeup authored and hewigovens committed Mar 11, 2019
1 parent a021c6f commit fdc3729
Show file tree
Hide file tree
Showing 14 changed files with 610 additions and 108 deletions.
25 changes: 7 additions & 18 deletions JNI/cpp/BinanceSigner.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,19 @@
#include "TWJNI.h"
#include "BinanceSigner.h"

jlong JNICALL Java_wallet_core_jni_BinanceSigner_nativeCreate(JNIEnv *env, jclass thisClass, jobject input) {
jobject JNICALL Java_wallet_core_jni_BinanceSigner_sign(JNIEnv *env, jclass thisClass, jobject input) {
jclass inputClass = (*env)->GetObjectClass(env, input);
jmethodID inputToByteArrayMethodID = (*env)->GetMethodID(env, inputClass, "toByteArray", "()[B");
jbyteArray inputByteArray = (*env)->CallObjectMethod(env, input, inputToByteArrayMethodID);
TWData *inputData = TWDataCreateWithJByteArray(env, inputByteArray);
struct TWBinanceSigner *instance = TWBinanceSignerCreate(inputData);
jbyteArray resultData = TWDataJByteArray(TWBinanceSignerSign(inputData), env);
jclass resultClass = (*env)->FindClass(env, "wallet/core/jni/proto/Binance$SigningOutput");
jmethodID parseFromMethodID = (*env)->GetStaticMethodID(env, resultClass, "parseFrom", "([B)Lwallet/core/jni/proto/Binance$SigningOutput;");
jobject result = (*env)->CallStaticObjectMethod(env, resultClass, parseFromMethodID, resultData);

(*env)->DeleteLocalRef(env, resultClass);
(*env)->DeleteLocalRef(env, inputByteArray);
(*env)->DeleteLocalRef(env, inputClass);
return (jlong) instance;
}

void JNICALL Java_wallet_core_jni_BinanceSigner_nativeDelete(JNIEnv *env, jclass thisClass, jlong handle) {
TWBinanceSignerDelete((struct TWBinanceSigner *) handle);
}

jbyteArray JNICALL Java_wallet_core_jni_BinanceSigner_build(JNIEnv *env, jobject thisObject) {
jclass thisClass = (*env)->GetObjectClass(env, thisObject);
jfieldID handleFieldID = (*env)->GetFieldID(env, thisClass, "nativeHandle", "J");
struct TWBinanceSigner *instance = (struct TWBinanceSigner *) (*env)->GetLongField(env, thisObject, handleFieldID);

jbyteArray result = TWDataJByteArray(TWBinanceSignerBuild(instance), env);


(*env)->DeleteLocalRef(env, thisClass);

return result;
}
Expand Down
8 changes: 1 addition & 7 deletions JNI/cpp/BinanceSigner.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@
TW_EXTERN_C_BEGIN

JNIEXPORT
jlong JNICALL Java_wallet_core_jni_BinanceSigner_nativeCreate(JNIEnv *env, jclass thisClass, jobject input);

JNIEXPORT
void JNICALL Java_wallet_core_jni_BinanceSigner_nativeDelete(JNIEnv *env, jclass thisClass, jlong handle);

JNIEXPORT
jbyteArray JNICALL Java_wallet_core_jni_BinanceSigner_build(JNIEnv *env, jobject thisObject);
jobject JNICALL Java_wallet_core_jni_BinanceSigner_sign(JNIEnv *env, jclass thisClass, jobject input);


TW_EXTERN_C_END
Expand Down
36 changes: 1 addition & 35 deletions JNI/java/wallet/core/jni/BinanceSigner.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,11 @@ private BinanceSigner() {
static BinanceSigner createFromNative(long nativeHandle) {
BinanceSigner instance = new BinanceSigner();
instance.nativeHandle = nativeHandle;
BinanceSignerPhantomReference.register(instance, nativeHandle);
return instance;
}

static native long nativeCreate(wallet.core.jni.proto.Binance.SigningInput input);
static native void nativeDelete(long handle);

public native byte[] build();

public BinanceSigner(wallet.core.jni.proto.Binance.SigningInput input) {
nativeHandle = nativeCreate(input);
if (nativeHandle == 0) {
throw new InvalidParameterException();
}

BinanceSignerPhantomReference.register(this, nativeHandle);
}
public static native wallet.core.jni.proto.Binance.SigningOutput sign(wallet.core.jni.proto.Binance.SigningInput input);

}

class BinanceSignerPhantomReference extends java.lang.ref.PhantomReference<BinanceSigner> {
private static java.util.Set<BinanceSignerPhantomReference> references = new HashSet<BinanceSignerPhantomReference>();
private static java.lang.ref.ReferenceQueue<BinanceSigner> queue = new java.lang.ref.ReferenceQueue<BinanceSigner>();
private long nativeHandle;

private BinanceSignerPhantomReference(BinanceSigner referent, long nativeHandle) {
super(referent, queue);
this.nativeHandle = nativeHandle;
}

static void register(BinanceSigner referent, long nativeHandle) {
references.add(new BinanceSignerPhantomReference(referent, nativeHandle));
}

public static void doDeletes() {
BinanceSignerPhantomReference ref = (BinanceSignerPhantomReference) queue.poll();
for (; ref != null; ref = (BinanceSignerPhantomReference) queue.poll()) {
BinanceSigner.nativeDelete(ref.nativeHandle);
references.remove(ref);
}
}
}
Loading

0 comments on commit fdc3729

Please sign in to comment.