Skip to main content

Account related function

This chapter introduces how to generate wallet addresses and corresponding account information such as seed, private keys, public keys, mnemonics, etc.

SDK interface intro

In the official SDK, there are mainly the following methods and account information related methods


/**
* @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 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);

Compilation platform

On the Windows platform, SDK needs to be compiled into .dll dynamic library, on the Linux platform it needs to be compiled into .so dynamic library, and on the browser side it needs to be compiled into a WSM library for account calls.

We provide compiled static libraries on both the web and Linux system sides for your direct use.

  • Linux Method details for calling are introduced in the mobile chapter
  • Web Method details for calling are introduced in the web chapter

Download or compile the SDK and place it in the platform system directory.

  • Windows is C:\Windows\System32.
  • Linux is /usr/lib64.

Generate TTOS address

The process of generating addresses and private keys is shown in the following figure

img

important

Please keep the seeds properly

Used in SDK methods are as follows:

uint8_t *export_new_seed_base64();

/*Get the private key handle from seed */
long long import_prikey_handler_from_seed(const uint8_t * seed);

char *get_addr(long long pkey);

Developers who provide code snippets integrate C/C++code into Java programs through JNI. The developer packaged and renamed the methods in C.


/*
* Class: src_sig_sig
* Method: newPkey
*/
extern "C" JNIEXPORT jbyteArray JNICALL Java_com_example_jni_sig_newSeed
(JNIEnv *env , jobject)
{
uint8_t *buf_;
int size = 0;
bool ret = ExportSeed(&buf_, &size);
if (ret == false) {
errorL("ExportEVP_PKEY error");
return nullptr;
}
// buf_ conversion jbyteArray
jbyteArray result = env->NewByteArray(size);
env->SetByteArrayRegion(result, 0, size, reinterpret_cast<const jbyte *>(buf_));
return result;
}

extern "C" JNIEXPORT jlong JNICALL Java_com_example_jni_sig_importSeed
(JNIEnv * env, jobject, jbyteArray str){
// jbyteArray conversion std::vector<uint8_t>
std::vector <uint8_t> prikey;
jsize size = env->GetArrayLength(str);
// jsize size = inputsize;
prikey.resize(size);
env->GetByteArrayRegion(str,0, size, reinterpret_cast<jbyte*>(prikey.data()));
// call GetPkeyBySeed prikey
unsigned char *prikey_arr = reinterpret_cast<unsigned char *>(prikey.data());
void *pkey = GetPkeyBySeed(prikey_arr, size);

return reinterpret_cast<jlong>(pkey);
}

extern "C" JNIEXPORT jstring JNICALL Java_com_example_jni_sig_GetAddr
(JNIEnv *env , jobject, jlong pkey){
return env->NewStringUTF(get_addr(pkey));
}

Please refer to relevant documents for specific usage and precautions of JNI.

Generate Mnemonic words

Export the corresponding private key handler based on the address named private key file, After obtaining Pkey, use the export_mnemonic_from_prikey_handler method to get mnemonics


/*Export mnemonic phrase from seed */
char *export_mnemonic_from_seed(const uint8_t * seed);

/*Import seed from mnemonics */
char *import_seed_from_mnemonic(const char *mnemonic);

Java code encapsulation method:

  /**
* get hex seed
*
* @param address
* @return
*/
public static String getHexSeed(String address) {
String seed = "";
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(path + address + ".private"));
String line = reader.readLine();
while (line != null) {
seed += line + "\n";
line = reader.readLine();
}
reader.close();
JSONObject jsonObject = JSONObject.parseObject(seed);
String seedStr = jsonObject.getString("seedStr");
return seedStr;
return seed;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

/**
* get pkey
* @param address
* @return
*/
public static long readPkey(String address) {
String seedHex = getHexSeed(address);
if(seedHex!=null){
sig sig_obj = new sig();
byte[] seedByte = sig_obj.hexStringToByteArray(seedHex);
long pkey = sig_obj.importSeed(seedByte);
return pkey;
}
return 0;
}
/**
* check base58 address
* @param address
* @return
*/
public static JSONObject addressToPriHey(String address) {
JSONObject jsonObject = new JSONObject();
String seedHex = getHexSeed(address);
if(seedHex != null){
sig sig_obj = new sig();
byte[] seedByte = sig_obj.hexStringToByteArray(seedHex);
long pkey = sig_obj.importSeed(seedByte);
String mnemo = sig_obj.ExportMnemoic(seedByte);
String pri_hex = sig_obj.ExportPriHexStr(pkey);
String pub_hex = sig_obj.GetPubStr(pkey);
jsonObject.put("mnemoni", mnemo);
jsonObject.put("pri_hex", pri_hex);
jsonObject.put("pub_hex", pub_hex);
return jsonObject;
}
return null;
}

/**
* Export Mnemonic
* @param address
* @return
*/
public static String exportMnemonic(String address) {
String seedHex = getHexSeed(address);
if(seedHex != null){
sig sig_obj = new sig();
byte[] seedByte = sig_obj.hexStringToByteArray(seedHex);
String mnemo = sig_obj.ExportMnemoic(seedByte);
return mnemo;
}
return null;
}

Generate private key && public key

/*Get the private key handle from seed */
long long import_prikey_handler_from_seed(const uint8_t * seed);

/*Get the private key from private key handle */
char *export_new_prikey_to_hex(long long pkey);

/*Get the publice key from private key handle */
char *get_pubstr_base64(long long pkey);

note
  • For account security, account related interfaces are not transmitted through the network and are only called locally.
  • Developers need to properly keep their account information.