Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<feat>(csdk,jni): add jni not use keypair pointer feature. #233

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions bcos-c-sdk/bcos_sdk_c_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void bcos_sdk_c_handle_response(
struct bcos_sdk_c_bytes* create_bytes_struct(uint32_t field_size, const char* field_data)
{
bcos_sdk_clear_last_error();
if (field_size <= 0)
if (field_size <= 0 || field_data == nullptr)
{
return nullptr;
}
Expand All @@ -264,18 +264,27 @@ struct bcos_sdk_c_bytes* create_bytes_struct(uint32_t field_size, const char* fi
(struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes));
uint32_t length = field_size;
uint8_t* buffer = (uint8_t*)malloc(length);
memcpy(buffer, field_data, length);
if (field_bytes == nullptr || buffer == nullptr)
{
std::string msg = "malloc failed";
bcos_sdk_set_last_error_msg(-1, msg.c_str());
BCOS_LOG(WARNING) << LOG_BADGE("create_bytes_struct") << LOG_DESC("malloc failed")
<< LOG_KV("field_size", field_size)
<< LOG_KV("field_data", field_data);
return nullptr;
}
memmove(buffer, field_data, length);
field_bytes->buffer = buffer;
field_bytes->length = length;

return field_bytes;
}
catch (const std::exception& e)
{
std::string errorMsg = boost::diagnostic_information(e);
std::string msg = boost::diagnostic_information(e);
BCOS_LOG(WARNING) << LOG_BADGE("create_bytes_struct") << LOG_DESC("exception")
<< LOG_KV("error", errorMsg);
bcos_sdk_set_last_error_msg(-1, errorMsg.c_str());
<< LOG_KV("error", msg);
bcos_sdk_set_last_error_msg(-1, msg.c_str());
}

return nullptr;
Expand All @@ -301,11 +310,10 @@ struct bcos_sdk_c_bytes* bytes_struct_copy(const struct bcos_sdk_c_bytes* bytes_
}
catch (const std::exception& e)
{
std::string errorMsg = boost::diagnostic_information(e);
std::string msg = boost::diagnostic_information(e);
BCOS_LOG(WARNING) << LOG_BADGE("bytes_struct_copy") << LOG_DESC("exception")
<< LOG_KV("bytes_struct_src", bytes_struct_src)
<< LOG_KV("error", errorMsg);
bcos_sdk_set_last_error_msg(-1, errorMsg.c_str());
<< LOG_KV("bytes_struct_src", bytes_struct_src) << LOG_KV("error", msg);
bcos_sdk_set_last_error_msg(-1, msg.c_str());
}

return nullptr;
Expand Down
8 changes: 8 additions & 0 deletions bcos-c-sdk/bcos_sdk_c_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ struct bcos_sdk_c_bytes
* @brief: transaction data
*
*/

struct bcos_key_pair
{
struct bcos_sdk_c_bytes* pri;
struct bcos_sdk_c_bytes* pub;
uint8_t type;
};

struct bcos_sdk_c_transaction_data
{
int32_t version;
Expand Down
126 changes: 126 additions & 0 deletions bcos-c-sdk/bcos_sdk_c_uti_keypair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,47 @@ void* bcos_sdk_create_keypair(int crypto_type)
}
}


struct bcos_key_pair* bcos_sdk_create_raw_keypair(int crypto_type)
{
bcos_sdk_clear_last_error();
BCOS_SDK_C_PARAMS_VERIFY_CONDITION(
(crypto_type == BCOS_C_SDK_ECDSA_TYPE || crypto_type == BCOS_C_SDK_SM_TYPE),
"invalid crypto type, it must be BCOS_C_SDK_ECDSA_TYPE(ecdsa crypto type) or "
"BCOS_C_SDK_SM_TYPE(sm crypto type)",
NULL);
try
{
auto keyPairBuilder = std::make_shared<KeyPairBuilder>();
auto keyPair = keyPairBuilder->genKeyPair(
crypto_type == BCOS_C_SDK_ECDSA_TYPE ? CryptoType::Secp256K1 : CryptoType::SM2);
auto* bcosKeyPair = (struct bcos_key_pair*)malloc(sizeof(struct bcos_key_pair));
if (bcosKeyPair == nullptr)
{
std::string errorMsg = "malloc failed";
bcos_sdk_set_last_error_msg(-1, errorMsg.c_str());
BCOS_LOG(ERROR) << LOG_BADGE("bcos_sdk_create_raw_keypair")
<< LOG_KV("errorMsg", errorMsg);
return NULL;
}
struct bcos_sdk_c_bytes* pub_bytes = create_bytes_struct(
keyPair->publicKey()->data().size(), (const char*)keyPair->publicKey()->data().data());
struct bcos_sdk_c_bytes* pri_bytes = create_bytes_struct(
keyPair->secretKey()->data().size(), (const char*)keyPair->secretKey()->data().data());
bcosKeyPair->pub = pub_bytes;
bcosKeyPair->pri = pri_bytes;
bcosKeyPair->type = crypto_type;
return bcosKeyPair;
}
catch (const std::exception& e)
{
std::string msg = boost::diagnostic_information(e);
bcos_sdk_set_last_error_msg(-1, msg.c_str());
BCOS_LOG(ERROR) << LOG_BADGE("bcos_sdk_create_keypair") << LOG_KV("errorMsg", msg);
return NULL;
}
}

/**
* @brief : create hsm key pair used for transaction sign
*
Expand Down Expand Up @@ -136,6 +177,50 @@ void* bcos_sdk_create_keypair_by_private_key(int crypto_type, void* private_key,
}
}

struct bcos_key_pair* bcos_sdk_create_raw_keypair_by_private_key(
int crypto_type, void* private_key, unsigned len)
{
bcos_sdk_clear_last_error();
BCOS_SDK_C_PARAMS_VERIFICATION(private_key, NULL);
BCOS_SDK_C_PARAMS_VERIFY_CONDITION(
(len > 0), "invalid private key length, it must greater than zero", NULL);
BCOS_SDK_C_PARAMS_VERIFY_CONDITION(
(crypto_type == BCOS_C_SDK_ECDSA_TYPE || crypto_type == BCOS_C_SDK_SM_TYPE),
"invalid crypto type, it must be BCOS_C_SDK_ECDSA_TYPE(ecdsa crypto type) or "
"BCOS_C_SDK_SM_TYPE(sm crypto type)",
NULL);
try
{
auto priBytes = bcos::bytes((bcos::byte*)private_key, (bcos::byte*)private_key + len);
/*
ECDSA_TYPE 1,
SM_TYPE 2
*/
KeyPairBuilder keyPairBuilder{};
auto keyPair = keyPairBuilder.genKeyPair(
crypto_type == BCOS_C_SDK_ECDSA_TYPE ? CryptoType::Secp256K1 : CryptoType::SM2,
bcos::ref(priBytes));
auto* bcosKeyPair = (struct bcos_key_pair*)malloc(sizeof(struct bcos_key_pair));
struct bcos_sdk_c_bytes* pub_bytes = create_bytes_struct(
keyPair->publicKey()->data().size(), (const char*)keyPair->publicKey()->data().data());
struct bcos_sdk_c_bytes* pri_bytes = create_bytes_struct(
keyPair->secretKey()->data().size(), (const char*)keyPair->secretKey()->data().data());
bcosKeyPair->pub = pub_bytes;
bcosKeyPair->pri = pri_bytes;
bcosKeyPair->type = crypto_type;
return bcosKeyPair;
}
catch (const std::exception& e)
{
std::string errorMsg = boost::diagnostic_information(e);
bcos_sdk_set_last_error_msg(-1, errorMsg.c_str());

BCOS_LOG(ERROR) << LOG_BADGE("bcos_sdk_create_keypair_by_private_key")
<< LOG_KV("crypto_type", crypto_type) << LOG_KV("errorMsg", errorMsg);
return NULL;
}
}

/**
* @brief : create hsm key pair used for transaction sign
*
Expand Down Expand Up @@ -212,6 +297,47 @@ void* bcos_sdk_create_keypair_by_hex_private_key(int crypto_type, const char* pr
}
}

struct bcos_key_pair* bcos_sdk_create_raw_keypair_by_hex_private_key(
int crypto_type, const char* private_key)
{
bcos_sdk_clear_last_error();

BCOS_SDK_C_PARAMS_VERIFICATION(private_key, NULL);
BCOS_SDK_C_PARAMS_VERIFY_CONDITION(
(crypto_type == BCOS_C_SDK_ECDSA_TYPE || crypto_type == BCOS_C_SDK_SM_TYPE),
"invalid crypto type, it must be 0(ecdsa crypto type) or 1(sm crypto type)", NULL)
try
{
auto priBytes = fromHexString(std::string(private_key));
/*
ECDSA_TYPE 1,
SM_TYPE 2
*/
KeyPairBuilder keyPairBuilder{};
auto keyPair = keyPairBuilder.genKeyPair(
crypto_type == BCOS_C_SDK_ECDSA_TYPE ? CryptoType::Secp256K1 : CryptoType::SM2,
bytesConstRef((byte*)priBytes->data(), priBytes->size()));
auto* bcosKeyPair = (struct bcos_key_pair*)malloc(sizeof(struct bcos_key_pair));
struct bcos_sdk_c_bytes* pub_bytes = create_bytes_struct(
keyPair->publicKey()->data().size(), (const char*)keyPair->publicKey()->data().data());
struct bcos_sdk_c_bytes* pri_bytes = create_bytes_struct(
keyPair->secretKey()->data().size(), (const char*)keyPair->secretKey()->data().data());
bcosKeyPair->pub = pub_bytes;
bcosKeyPair->pri = pri_bytes;
bcosKeyPair->type = crypto_type;
return bcosKeyPair;
}
catch (const std::exception& e)
{
std::string errorMsg = boost::diagnostic_information(e);
bcos_sdk_set_last_error_msg(-1, errorMsg.c_str());

BCOS_LOG(ERROR) << LOG_BADGE("bcos_sdk_create_keypair_by_hex_private_key")
<< LOG_KV("crypto_type", crypto_type) << LOG_KV("errorMsg", errorMsg);
return NULL;
}
}

/**
* @brief : create hsm key pair used for transaction sign
*
Expand Down
10 changes: 10 additions & 0 deletions bcos-c-sdk/bcos_sdk_c_uti_keypair.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#ifndef __INCLUDE_BCOS_SDK_C_UTIL_KEYPAIR__
#define __INCLUDE_BCOS_SDK_C_UTIL_KEYPAIR__

#include "bcos_sdk_c_common.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -34,6 +36,8 @@ extern "C" {
*/
void* bcos_sdk_create_keypair(int crypto_type);

struct bcos_key_pair* bcos_sdk_create_raw_keypair(int crypto_type);

/**
* @brief : create hsm key pair used for transaction sign
*
Expand All @@ -54,6 +58,9 @@ void* bcos_sdk_create_hsm_keypair(const char* hsm_lib_path);
*/
void* bcos_sdk_create_keypair_by_private_key(int crypto_type, void* private_key, unsigned length);

struct bcos_key_pair* bcos_sdk_create_raw_keypair_by_private_key(
int crypto_type, void* private_key, unsigned length);

/**
* @brief : create hsm key pair used for transaction sign
*
Expand All @@ -76,6 +83,9 @@ void* bcos_sdk_create_hsm_keypair_by_private_key(
*/
void* bcos_sdk_create_keypair_by_hex_private_key(int crypto_type, const char* private_key);

struct bcos_key_pair* bcos_sdk_create_raw_keypair_by_hex_private_key(
int crypto_type, const char* private_key);

/**
* @brief : create hsm key pair used for transaction sign
*
Expand Down
33 changes: 33 additions & 0 deletions bcos-c-sdk/bcos_sdk_c_uti_signature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "bcos_sdk_c_uti_signature.h"
#include "bcos_sdk_c_error.h"
#include <bcos-cpp-sdk/utilities/crypto/KeyPairBuilder.h>
#include <bcos-cpp-sdk/utilities/crypto/Signature.h>
#include <bcos-crypto/hash/SM3.h>
#include <bcos-utilities/Common.h>
Expand Down Expand Up @@ -75,6 +76,38 @@ struct bcos_sdk_c_signature_result bcos_sdk_sign(
}
}

void bcos_sdk_sign_with_keypair_struct(struct bcos_key_pair* key_pair, const char* hash,
struct bcos_sdk_c_signature_result* signatureResult)
{
// if sign failed, return unassigned struct
// the function caller knows sign failed according to the function called
// bcos_sdk_get_last_error(if sign failed, return -1)
BCOS_SDK_C_PARAMS_VERIFICATION(key_pair, );
BCOS_SDK_C_PARAMS_VERIFICATION(hash, );

try
{
bcos::crypto::HashType hashType(hash);
bcos::cppsdk::utilities::Signature signature{};
KeyPairBuilder builder;
auto keyPair = builder.genKeyPair(static_cast<crypto::KeyPairType>(key_pair->type),
bcos::bytesConstRef(key_pair->pri->buffer, key_pair->pri->length));
auto signatureData = signature.sign(*keyPair, hashType);
memmove(signatureResult->r, signatureData->data(), 32);
memmove(signatureResult->s, signatureData->data() + 32, 32);
memmove(signatureResult->v, signatureData->data() + 64, signatureData->size() - 64);

return;
}
catch (const std::exception& e)
{
std::string errorMsg = boost::diagnostic_information(e);
bcos_sdk_set_last_error_msg(-1, errorMsg.c_str());
BCOS_LOG(ERROR) << LOG_BADGE("bcos_sdk_sign") << LOG_KV("errorMsg", errorMsg);
return;
}
}

/**
* @brief : verify the signature data of transaction
*
Expand Down
3 changes: 3 additions & 0 deletions bcos-c-sdk/bcos_sdk_c_uti_signature.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ extern "C" {
struct bcos_sdk_c_signature_result bcos_sdk_sign(
void* key_pair, const char* hash, const char* hsm_lib_path);

void bcos_sdk_sign_with_keypair_struct(
struct bcos_key_pair* key_pair, const char* hash, struct bcos_sdk_c_signature_result*);

/**
* @brief : verify the signature data of transaction
*
Expand Down
46 changes: 46 additions & 0 deletions bcos-c-sdk/bcos_sdk_c_uti_tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <bcos-c-sdk/bcos_sdk_c_common.h>
#include <bcos-cpp-sdk/Sdk.h>
#include <bcos-cpp-sdk/utilities/crypto/Common.h>
#include <bcos-cpp-sdk/utilities/crypto/KeyPairBuilder.h>
#include <bcos-cpp-sdk/utilities/tx/TransactionBuilder.h>
#include <bcos-cpp-sdk/utilities/tx/TransactionBuilderService.h>
#include <cstring>
Expand Down Expand Up @@ -315,6 +316,51 @@ void bcos_sdk_create_signed_transaction_ver_extra_data(void* key_pair, const cha
}
}

void bcos_sdk_create_signed_transaction_with_keypair_struct(struct bcos_key_pair* key_pair,
const char* group_id, const char* chain_id, const char* to, const char* data, const char* abi,
int64_t block_limit, int32_t attribute, const char* extra_data, char** tx_hash,
char** signed_tx)
{
bcos_sdk_clear_last_error();
BCOS_SDK_C_PARAMS_VERIFICATION(key_pair, );
BCOS_SDK_C_PARAMS_VERIFICATION(key_pair->pri, );
BCOS_SDK_C_PARAMS_VERIFICATION(key_pair->pri->buffer, );
BCOS_SDK_C_PARAMS_VERIFICATION(group_id, );
BCOS_SDK_C_PARAMS_VERIFICATION(chain_id, );
BCOS_SDK_C_PARAMS_VERIFICATION(data, );
BCOS_SDK_C_PARAMS_VERIFICATION(tx_hash, );
BCOS_SDK_C_PARAMS_VERIFICATION(signed_tx, );

BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must >= 0", );

try
{
auto bytesData = fromHexString(data);
TransactionBuilder builder{};

KeyPairBuilder keyPairBuilder{};
auto key = keyPairBuilder.genKeyPair(static_cast<crypto::KeyPairType>(key_pair->type),
bcos::bytesConstRef(key_pair->pri->buffer, key_pair->pri->length));
auto r = builder.createSignedTransaction(*key, std::string(group_id), std::string(chain_id),
std::string(to ? to : ""), *bytesData, std::string(abi ? abi : ""), block_limit,
attribute, extra_data ? std::string(extra_data) : std::string());

*tx_hash = strdup(r.first.c_str());
*signed_tx = strdup(r.second.c_str());
}
catch (const std::exception& e)
{
std::string errorMsg = boost::diagnostic_information(e);
BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_signed_transaction_ver_extra_data")
<< LOG_DESC("exception") << LOG_KV("key_pair", key_pair)
<< LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id)
<< LOG_KV("to", to) << LOG_KV("data", data) << LOG_KV("abi", abi)
<< LOG_KV("block_limit", block_limit) << LOG_KV("attribute", attribute)
<< LOG_KV("extra_data", extra_data) << LOG_KV("error", errorMsg);
bcos_sdk_set_last_error_msg(-1, errorMsg.c_str());
}
}

/**
* @brief
*
Expand Down
5 changes: 5 additions & 0 deletions bcos-c-sdk/bcos_sdk_c_uti_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ void bcos_sdk_create_signed_transaction_ver_extra_data(void* key_pair, const cha
const char* chain_id, const char* to, const char* data, const char* abi, int64_t block_limit,
int32_t attribute, const char* extra_data, char** tx_hash, char** signed_tx);

void bcos_sdk_create_signed_transaction_with_keypair_struct(struct bcos_key_pair* key_pair,
const char* group_id, const char* chain_id, const char* to, const char* data, const char* abi,
int64_t block_limit, int32_t attribute, const char* extra_data, char** tx_hash,
char** signed_tx);

/**
* @brief
*
Expand Down
6 changes: 4 additions & 2 deletions bcos-c-sdk/bcos_sdk_c_uti_tx_struct_v1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ struct bcos_sdk_c_transaction_data_v1* convert_tars_transaction_data_to_struct_v
transaction_data_struct_v1->base.input = input_bytes;
transaction_data_struct_v1->base.version = tars_transaction_data->version;
transaction_data_struct_v1->base.block_limit = tars_transaction_data->blockLimit;
transaction_data_struct_v1->base.chain_id = my_strdup(tars_transaction_data->chainID.data());
transaction_data_struct_v1->base.group_id = my_strdup(tars_transaction_data->groupID.data());
transaction_data_struct_v1->base.chain_id =
my_strdup(tars_transaction_data->chainID.data());
transaction_data_struct_v1->base.group_id =
my_strdup(tars_transaction_data->groupID.data());
transaction_data_struct_v1->base.nonce = my_strdup(tars_transaction_data->nonce.data());
transaction_data_struct_v1->base.to = my_strdup(tars_transaction_data->to.data());
transaction_data_struct_v1->base.abi = my_strdup(tars_transaction_data->abi.data());
Expand Down
Loading
Loading