Use DO SDK with Android
Java calling DO SDK requires familiarity with the use of Java JNA.
Step​
1. Import the SDK​
Download the source code and import it into your project to compile it into a dynamic library.
/**
* @brief Create hexadecimal seed
* @return jbyteArray : Hexadecimal seed
*/
JNIEXPORT jbyteArray JNICALL Java_com_example_jni_sig_newSeed
(JNIEnv *env , jobject);
/**
* @brief Export a hexadecimal array of private keys based on the private key
* @param jlong : private key
* @return jstring : Private key hexadecimal array
*/
JNIEXPORT jstring JNICALL Java_com_example_jni_sig_ExportPriHexStr
(JNIEnv *, jobject, jlong);
/**
* @brief Export private key based on hexadecimal array of private keys
* @param jstring : Private key hexadecimal array
* @return jlong : private key
*/
JNIEXPORT jlong JNICALL Java_com_example_jni_sig_ImportPriHexStr
(JNIEnv *, jobject, jstring);
/**
* @brief Export private key based on hexadecimal array of private keys
* @param jbyteArray : Private key hexadecimal array
* @return jlong : private key
*/
JNIEXPORT jlong JNICALL Java_com_example_jni_sig_importSeed
(JNIEnv * env, jobject, jbyteArray str);
/**
* @brief Export mnemonic words based on the hexadecimal array of seeds
* @param jbyteArray : The hexadecimal array of seeds
* @return jstring : Mnemonic words
*/
JNIEXPORT jstring JNICALL Java_com_example_jni_sig_ExportMnemoic
(JNIEnv *, jobject, jbyteArray);
/**
* @brief Export the hexadecimal array of seeds based on mnemonic words
* @param jstring : Mnemonic words
* @return jbyteArray : The hexadecimal array of seeds
*/
JNIEXPORT jbyteArray JNICALL Java_com_example_jni_sig_ImportMnemoic
(JNIEnv *, jobject, jstring);
/**
* @brief Obtain the public key base64
* @param jlong : public key
* @return jstring : Public key base64
*/
JNIEXPORT jstring JNICALL Java_com_example_jni_sig_GetPubStr
(JNIEnv *, jobject, jlong);
/**
* @brief Obtain the public key base64
* @param jlong pkey: public key
* @return jstring : Public key base64
*/
JNIEXPORT jstring JNICALL Java_com_example_jni_sig_GetContractPubStr
(JNIEnv *env, jobject, jlong pkey);
/**
* @brief Obtain address based on private key
* @param jlong : Private key
* @return jstring : address
*/
JNIEXPORT jstring JNICALL Java_com_example_jni_sig_GetAddr
(JNIEnv *, jobject, jlong);
/**
* @brief Sign the data
* @param jlong : Private key
* @param jstring : Data to be signed
* @return jstring : Signed data
*/
JNIEXPORT jstring JNICALL Java_com_example_jni_sig_sigmessage
(JNIEnv *, jobject, jlong, jstring);
/**
* @brief Sign the data
* @param jlong pkey : Private key
* @param jstring message : Data to be signed
* @return jstring : Signed data
*/
JNIEXPORT jstring JNICALL Java_com_example_jni_sig_sigMessage
(JNIEnv *env, jobject, jlong pkey, jstring message);
/**
* @brief verify signature
* @param jlong : Private key
* @param jstring : Signed information
* @param jstring : Verification results
* @return jboolean : Verify if the signature was successful
*/
JNIEXPORT jboolean JNICALL Java_com_example_jni_sig_vefmessage
(JNIEnv *, jobject, jlong, jstring, jstring);
/**
* @brief Sign the `txJson` in the `result` field of the transaction return value
* @param jlong : Private key
* @param jstring : "txJson" information
* @return jstring : Return the signed `txjson`
*/
JNIEXPORT jstring JNICALL Java_com_example_jni_sig_sigTx
(JNIEnv *, jobject, jlong, jstring);
/**
* @brief Encrypt the base64 string
* @param jstring : The base64 string to be encrypted
* @return jstring : Encrypted data
*/
JNIEXPORT jstring JNICALL Java_com_example_jni_sig_Base64Encode
(JNIEnv *, jobject, jstring);
/**
* @brief Decrypt the encrypted base64 string
* @param jstring : The base64 string to be decrypted
* @return jstring : Decrypted data
*/
JNIEXPORT jstring JNICALL Java_com_example_jni_sig_Base64Decode
(JNIEnv *, jobject, jstring);
/**
* @brief Release private key
* @param jlong pkey : private key
*/
JNIEXPORT void JNICALL Java_com_example_jni_sig_freePrikey
(JNIEnv *env, jobject, jlong pkey);
2. Use the SDK​
Write Java code according to SDK methods.
Java use​
The corresponding Java method and file directory should be consistent with the C++ method ——com_example_jni
.
package com.example.jni;
public class sig {
public native byte[] newSeed();
public native String ExportPriHexStr(long pkey);
public native long ImportPriHexStr(String hexStr);
public native long importSeed(byte[] str);
public native String ExportMnemoic(byte[] seed);
public native long ImportMnemoic(String Mnstr);
public native String GetPubStr(long pkey);
public native String GetContractPubStr(long pkey);
public native String GetAddr(long pkey);
public native String sigmessage(long pkey, String message);
public native String sigMessage(long pkey, String message);
public native boolean vefmessage(long pkey, String message, String signature);
public native String sigTx(long pkey, String tx);
public native String Base64Encode(String str);
public native String Base64Decode(String str);
public native void freePrikey(long pkey);
}
Example​
The following example demonstrates the generation of addresses, mnemonics, private keys, public keys, and seeds.
/**
* Generate account
* @return
*/
public static void newWallet() {
System.loadLibrary("JNI");//
JSONO jsonObject = new JSON();
sig sig_obj = new sig();
byte[] seed = sig_obj.newSeed();
long pkey = sig_obj.importSeed(seed);
String base58addr = sig_obj.GetAddr(pkey);
String seedStr = sig_obj.byteArrayToHexString(seed);
String mnemo = sig_obj.ExportMnemoic(seed);
String pri_hex = sig_obj.ExportPriHexStr(pkey);
String pub_hex = sig_obj.GetPubStr(pkey);
//Account information storage
jsonObject.put("seedStr", seedStr);
jsonObject.put("addr", base58addr);
jsonObject.put("mnemoni", mnemo);
jsonObject.put("pri_hex", pri_hex);
jsonObject.put("pub_hex", pub_hex);
}