Skip to main content

Account related function

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

SDK interface intro

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


char *get_version();

/*Get the private key handle from private key base64*/
long long import_base64_prikey_handler(const char *buf, int buf_size);

/*Get the private key handle from seed base58*/
long long import_base64_seed_handler(const char *buf, int buf_size);

/*Get a new base64 private key*/
char *export_new_prikey_base64();

/*Export new seed base64*/
uint8_t *export_new_seed_base64();

/*Get the private key handle from private key base 16*/
long long import_prikey_handler_from_hex(const char *str);

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

/*Get a new base 16 private key*/
char *export_new_prikey_to_hex(long long pkey);

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

/*Get private key handle from mnemonic phrase*/
long long import_prikey_handler_from_mnemonic(const char *mnemonic);

/*Get addr by private key handle*/
char *get_addr(long long pkey);

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

char *sig_tx(const char * message,int msize, long long pkey);

char * sig_contract_tx(const char *message ,int msize, long long pkey);

/*Release the private key handle*/
void free_prikey_handler(long long pkey);

int verif_by_public_str(const char * pubstr,int pubsize,const char * message,int messagesize,const char * signature,int signature_size);

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

The following is a complete wallet generation function in Java.

  • Generate a private key file named after the wallet address
    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"
}

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
* Signature: ()Ljava/lang/String;
*/
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);

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.