Skip to main content

Mobile

This chapter introduces which methods SDK calls on different devices.

Android

If using the native Java language on the Android end, calling C++ libraries requires the use of Java's built-in JNI tools. Java and C++ programs are interrelated and cannot be completely separated. So the methods of C++and Java need to be consistent.

C++ function

/**
* @brief Creating a hexadecimal seed
* @return jbyteArray: Hexadecimal seeds
*/
JNIEXPORT jbyteArray JNICALL Java_com_example_jni_sig_newSeed
(JNIEnv *env , jobject);


/**
* @brief Derive private key hexadecimal array from 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 Derive private key from private key hexadecimal array
* @param jstring: Private key hexadecimal array
* @return jlong: Private key
*/
JNIEXPORT jlong JNICALL Java_com_example_jni_sig_ImportPriHexStr
(JNIEnv *, jobject, jstring);


/**
* @brief Derive private key from private key hexadecimal array
* @param jbyteArray: Derive private key from private key hexadecimal array
* @return jlong: private key
*/
JNIEXPORT jlong JNICALL Java_com_example_jni_sig_importSeed
(JNIEnv * env, jobject, jbyteArray str);


/**
* @brief Derive mnemonics from seeded hexadecimal arrays
* @param jbyteArray: Hexadecimal array of seeds
* @return jstring : mnemonic word
*/
JNIEXPORT jstring JNICALL Java_com_example_jni_sig_ExportMnemoic
(JNIEnv *, jobject, jbyteArray);

/**
* @brief Derive a hexadecimal array of seeds based on auxiliary words
* @param jstring : mnemonic
* @return jbyteArray : Hexadecimal array of seeds
*/
JNIEXPORT jbyteArray JNICALL Java_com_example_jni_sig_ImportMnemoic
(JNIEnv *, jobject, jstring);


/**
* @brief Get Public Key base64
* @param jlong : Public Key
* @return jstring : Public Keybase64
*/
JNIEXPORT jstring JNICALL Java_com_example_jni_sig_GetPubStr
(JNIEnv *, jobject, jlong);


/**
* @brief Get Public Key base64
* @param jlong pkey: Public Key
* @return jstring : Public Keybase64
*/
JNIEXPORT jstring JNICALL Java_com_example_jni_sig_GetContractPubStr
(JNIEnv *env, jobject, jlong pkey);


/**
* @brief Get address from private key
* @param jlong : private key
* @return jstring : address
*/
JNIEXPORT jstring JNICALL Java_com_example_jni_sig_GetAddr
(JNIEnv *, jobject, jlong);


/**
* @brief Data signing
* @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 Data signing
* @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 message
* @param jstring : Results of verifying signatures
* @return jboolean : Whether successful for verifying signatures
*/
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"massage
* @return jstring : Returns signed txjson
*/
JNIEXPORT jstring JNICALL Java_com_example_jni_sig_sigTx
(JNIEnv *, jobject, jlong, jstring);


/**
* @brief Encrypting base64 strings
* @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 decrypt
* @return jstring : decrypted data
*/
JNIEXPORT jstring JNICALL Java_com_example_jni_sig_Base64Decode
(JNIEnv *, jobject, jstring);


/**
* @brief Release the private key
* @param jlong pkey :private key
*/
JNIEXPORT void JNICALL Java_com_example_jni_sig_freePrikey
(JNIEnv *env, jobject, jlong pkey);

Java function

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);

}

Java code example

The following is a complete wallet generation function in Java.

  • Load dynamic library
    static {
String systemType = System.getProperty("os.name");
String ext = (systemType.toLowerCase().indexOf("win") != -1) ? ".dll" : ".so";
if (ext.equals(".so")) {
try {
System.loadLibrary("tfs_sdk_jni_v0.1.0");
} catch (Exception e) {
System.out.println("Failed to load so library");
}
} else {
try {
System.loadLibrary("libtfs_sdk_jni_v0.1.0");
} catch (Exception e) {
System.out.println("Failed to load dll library");
}
}
}
  • Information required to generate a new wallet account
    public static JSONObject newWallet() {
JSONObject jsonObject = new JSONObject();
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);
jsonObject.put("seedStr", seedStr);
jsonObject.put("addr", base58addr);
jsonObject.put("mnemoni", mnemo);
jsonObject.put("pri_hex", pri_hex);
jsonObject.put("pub_hex", pub_hex);
return jsonObject;
}

The format for generating JSON content is as follows:

{
"seedStr": "d0cdc4517112aaafd77160d3fa7a841d",
"pri_hex": "92620780460751e8aac1e289cfa3e4e77ed366e5939b22302f863414c5b03752",
"pub_hex": "MCowBQYDK2VwAyEAuOmhl9PiwwviYtwW1ZVzl8e2LGC+4KA5fsQmJyt/XTo=",
"addr": "0x6298D7a621bF8C37E74a51c6b3E9c788975d9cF1",
"mnemoni": "speak hotel begin timber click fitness fruit clown stadium stable patient depth"
}

IOS

tips:

All interfaces that return a char* value, except the get_version interface, use free to free up the heap space occupied by the return value.

/**
* @brief Returns the sdk version number
* @return const char*: version information
*/
const char *get_version();

/**
* @brief Pass in the base64 encoding of the private key and return the private key handle.
* @param buf: Base64 encoding of the private key
* @param buf_size: The size of the base64 encoding of the private key
* @return long long: private key handle
*/
long long import_base64_prikey_handler(const char *buf, int buf_size);

/**
* @brief Export the base64 encoding of the private key
* @return char*: Base64 encoding of private key
*/
char *export_new_prikey_base64();

/**
* @brief Generates a seed and returns the hexadecimal string of the seed
* @return char*: Hexadecimal string of the seed
*/
char *export_new_seed();

/**
* @brief Pass in the hexadecimal code of the private key and return the private key handle.
* @param str: Hexadecimal encoding of the private key
* @return long long private key handle
*/
long long import_prikey_handler_from_hex(const char *str);

/**
* @brief Get private key handle from hexadecimal seed string
* @param seed:
* @return long long
*/
long long import_prikey_handler_from_seed(char *seed);

/**
* @brief Pass in the private key handle and return the hexadecimal code of the private key.
*
* @param pkey: handle
* @return char* Hexadecimal encoding of the private key
*/
char *export_new_prikey_to_hex(long long pkey);

/**
* @brief Pass in the seed, return mnemonics
* @param seed: seed
* @return char* private key handle
*/
char *export_mnemonic_from_seed(const char *seed);

/**
* @brief Pass in the seed mnemonic and return the hexadecimal seed.
* @param mnemonic: seed mnemonic
* @return char* seed
*/
char *import_seed_from_mnemonic(const char *mnemonic);

/**
* @brief Pass in the private key handle and return the address of the private key's addr account.
* @param pkey: private key handle
* @return char* addrAccount Address
*/
char *get_addr(long long pkey);

/**
* @brief Pass in the private key handle, return the base64 encoding of the public key
* @param pkey: Private key handle
* @return char* Base64 encoding of the public key
*/
char *get_pubstr_base64(long long pkey);

/**
* @brief Release the private key handle
* @param pkey: private key handle
*/
void free_prikey_handler(long long pkey);

/**
* @brief Sign the "txJson" in the "result" field of the transaction return value.
* @param txjson: "txJson"information
* @param pkey: Private key handle
* @return char* Signed message
*/
char* txJsonSign(const char* txjson, void *pkey);

/**
* @brief Sign the message
* @param message message to be signed
* @param mesage_size Message length
* @param pkey private key handle
* @return char* post-signature information
*/
char *sign(const char *message, int mesage_size, long long pkey);

/**
* @brief Signature verification of information
* @param pubstr public key
* @param pubsize Public key length
* @param message Signed message
* @param messagesize Length of signed message
* @param signature message to be signed
* @param signature_size Length of message to be signed
* @return int 0 is success, -1 is failure
*/
int verif_by_public_str(const char *pubstr, int pubsize, const char *message, int messagesize, const char *signature, int signature_size);