Skip to content

Commit 601f250

Browse files
committed
Clean up
1 parent 60f61e0 commit 601f250

File tree

9 files changed

+276
-362
lines changed

9 files changed

+276
-362
lines changed

.idea/.name

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+169-283
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<project name="outside" basedir=".">
1+
<project name="dispatch-labs-java-sdk" basedir=".">
22

33
<!-- Target release -->
44
<target name="release">

src/main/java/dispatchlabs/Sdk.java

-11
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,6 @@ public static void main(String args[]) {
8484
Account toAccount = Account.create();
8585
*/
8686

87-
88-
byte[] msg = new byte[32];
89-
for (int i=0; i<32; i++) {
90-
msg[i] = (byte)0xcc;
91-
}
92-
93-
94-
NativeSecp256k1.sign(Utils.toByteArray("2093fde230170efc92b2c122b8b831b30f916dd5568b50a427caa76e13e7effd"), msg);
95-
96-
97-
9887
Sdk sdk = new Sdk("10.0.1.2");
9988

10089
List<Contact> contacts = sdk.getDelegates();

src/main/java/dispatchlabs/crypto/Crypto.java

+3-8
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,12 @@ public class Crypto {
1212
/**
1313
*
1414
* @param privateKey
15-
* @param bytes
15+
* @param hash
1616
* @return
1717
* @throws Exception
1818
*/
19-
public static byte[] sign(byte[] privateKey, byte[] bytes) throws Exception {
20-
/*
21-
byte[] signature = NativeSecp256k1.Sign(bytes, privateKey);
22-
signature = Arrays.copyOfRange(signature, 5, signature.length);
23-
return signature;
24-
*/
25-
return null;
19+
public static byte[] sign(byte[] privateKey, byte[] hash) throws Exception {
20+
return NativeSecp256k1.sign(privateKey, hash);
2621
}
2722

2823
/**

src/main/java/org/bitcoin/NativeSecp256k1.java

+23-24
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,32 @@
1717

1818
package org.bitcoin;
1919

20-
import dispatchlabs.utils.Utils;
21-
2220
import java.nio.ByteBuffer;
2321
import java.nio.ByteOrder;
2422

2523
import java.util.concurrent.locks.Lock;
2624
import java.util.concurrent.locks.ReentrantReadWriteLock;
2725

2826
/**
29-
* <p>This class holds native methods to handle ECDSA verification.</p>
30-
*
31-
* <p>You can find an example library that can be used for this at https://github.com/bitcoin/secp256k1</p>
3227
*
33-
* <p>To build secp256k1 for use with bitcoinj, run
34-
* `./configure --enable-jni --enable-experimental --enable-module-ecdh`
35-
* and `make` then copy `.libs/libsecp256k1.so` to your system library path
36-
* or point the JVM to the folder containing it with -Djava.library.path
37-
* </p>
3828
*/
3929
public class NativeSecp256k1 {
4030

41-
private static final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
42-
private static final Lock r = rwl.readLock();
43-
private static final Lock w = rwl.writeLock();
31+
/**
32+
* Class level-declarations.
33+
*/
34+
private static final ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
35+
private static final Lock readLock = reentrantReadWriteLock.readLock();
36+
private static final Lock writeLock = reentrantReadWriteLock.writeLock();
4437
private static ThreadLocal<ByteBuffer> nativeECDSABuffer = new ThreadLocal<ByteBuffer>();
4538

39+
/**
40+
*
41+
* @param privateKey
42+
* @param hash
43+
* @return
44+
* @throws Exception
45+
*/
4646
public static byte[] sign(byte[] privateKey, byte[] hash) throws Exception {
4747
ByteBuffer byteBuff = nativeECDSABuffer.get();
4848
if (byteBuff == null || byteBuff.capacity() < 64) {
@@ -54,20 +54,19 @@ public static byte[] sign(byte[] privateKey, byte[] hash) throws Exception {
5454
byteBuff.put(privateKey);
5555
byteBuff.put(hash);
5656

57-
r.lock();
57+
readLock.lock();
5858
try {
59-
byte[] ret = secp256k1_ecdsa_sign_recoverable(byteBuff, Secp256k1Context.getContext());
60-
61-
System.out.println(Utils.toHexString(ret));
62-
63-
int fook =0;
59+
return secp256k1_sign_and_serialize_compact(byteBuff, Secp256k1Context.getContext());
6460
} finally {
65-
r.unlock();
61+
readLock.unlock();
6662
}
67-
68-
return null;
6963
}
7064

71-
private static native byte[] secp256k1_ecdsa_sign_recoverable(ByteBuffer byteBuff, long context);
72-
private static native byte[] secp256k1_ecdsa_recoverable_signature_serialize_compact(ByteBuffer byteBuff, long context);
65+
/**
66+
*
67+
* @param byteBuff
68+
* @param context
69+
* @return
70+
*/
71+
private static native byte[] secp256k1_sign_and_serialize_compact(ByteBuffer byteBuff, long context);
7372
}

src/main/java/org/bitcoin/Secp256k1Context.java

+40-24
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,51 @@
1717
package org.bitcoin;
1818

1919
/**
20-
* This class holds the context reference used in native methods
20+
* This class holds the context reference used in native methods
2121
* to handle ECDSA operations.
2222
*/
2323
public class Secp256k1Context {
24-
private static final boolean enabled; //true if the library is loaded
25-
private static final long context; //ref to pointer to context obj
2624

27-
static { //static initializer
28-
boolean isEnabled = true;
29-
long contextRef = -1;
30-
try {
31-
System.loadLibrary("secp256k1");
32-
contextRef = secp256k1_init_context();
33-
} catch (UnsatisfiedLinkError e) {
34-
System.out.println("UnsatisfiedLinkError: " + e.toString());
35-
isEnabled = false;
36-
}
37-
enabled = isEnabled;
38-
context = contextRef;
39-
}
25+
/**
26+
* Class level-declarations.
27+
*/
28+
private static boolean enabled;
29+
private static long context = -1;
4030

41-
public static boolean isEnabled() {
42-
return enabled;
43-
}
31+
/**
32+
*
33+
*/
34+
static {
35+
try {
36+
System.loadLibrary("secp256k1");
37+
context = secp256k1_init_context();
38+
enabled = true;
39+
} catch (UnsatisfiedLinkError e) {
40+
System.out.println(e.toString());
41+
System.out.println("make sure to set -Djava.library.path to the the secp256k1 library path");
42+
enabled = false;
43+
}
44+
}
4445

45-
public static long getContext() {
46-
if(!enabled) return -1; //sanity check
47-
return context;
48-
}
46+
/**
47+
*
48+
* @return
49+
*/
50+
public static boolean isEnabled() {
51+
return enabled;
52+
}
4953

50-
private static native long secp256k1_init_context();
54+
/**
55+
*
56+
* @return
57+
*/
58+
public static long getContext() {
59+
return context;
60+
}
61+
62+
/**
63+
*
64+
* @return
65+
*/
66+
private static native long secp256k1_init_context();
5167
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <jni.h>
2+
#include <stdlib.h>
3+
#include <stdint.h>
4+
#include <string.h>
5+
#include <stdio.h>
6+
#include "org_bitcoin_NativeSecp256k1.h"
7+
#include "include/secp256k1.h"
8+
#include "include/secp256k1_ecdh.h"
9+
#include "include/secp256k1_recovery.h"
10+
#include "scalar_impl.h"
11+
12+
/*
13+
* Class: org_bitcoin_NativeSecp256k1
14+
* Method: secp256k1_sign_and_serialize_compact
15+
* Signature: (Ljava/nio/ByteBuffer;J)[B
16+
*/
17+
JNIEXPORT jbyteArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1sign_1and_1serialize_1compact(JNIEnv *env, jclass jClass, jobject byteBufferObject, jlong contextObject)
18+
{
19+
secp256k1_context *context = (secp256k1_context*)(uintptr_t)contextObject;
20+
unsigned char* buffer = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject);
21+
const unsigned char* privateKey = (unsigned char*) buffer;
22+
const unsigned char* hash = (unsigned char*) (buffer + 32);
23+
24+
// Create signature.
25+
secp256k1_ecdsa_recoverable_signature sigstruct;
26+
int ret = secp256k1_ecdsa_sign_recoverable(context, &sigstruct, hash, privateKey, NULL, NULL);
27+
28+
// Serialize and compact signature.
29+
unsigned char signature[65];
30+
int recid;
31+
secp256k1_ecdsa_recoverable_signature_serialize_compact(context, &signature[0], &recid, &sigstruct);
32+
signature[64] = recid;
33+
34+
jbyteArray jByteArray = (*env)->NewByteArray(env, 65);
35+
(*env)->SetByteArrayRegion(env, jByteArray, 0, 65, (jbyte*)signature);
36+
return jByteArray;
37+
}

src/main/java/org/bitcoin/org_bitcoin_NativeSecp256k1.h

+2-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)