Skip to content

Commit

Permalink
Merge pull request #236 from FISCO-BCOS/release-3.7.1
Browse files Browse the repository at this point in the history
Release 3.7.1
  • Loading branch information
kyonRay authored Dec 13, 2024
2 parents 88c8087 + 871d2d1 commit 56c568c
Show file tree
Hide file tree
Showing 42 changed files with 1,241 additions and 104 deletions.
33 changes: 24 additions & 9 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ macos-12 ]
os: [ macos-13 ]
# env:
# VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
steps:
Expand Down Expand Up @@ -134,32 +134,47 @@ jobs:
build_with_centos:
name: build_with_centos
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
container:
- centos-7.9
container:
image: docker.io/centos:7
volumes:
- /usr/local/share/vcpkg:/usr/local/share/vcpkg
# env:
- /node20217:/node20217:rw,rshared
- ${{ matrix.container == 'centos-7.9' && '/node20217:/__e/node20:ro,rshared' || ' ' }}
# env:
# VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
steps:
- uses: actions/checkout@v3
- name: install nodejs20glibc2.17
if: ${{ matrix.container == 'centos-7.9' }}
run: |
curl -LO https://unofficial-builds.nodejs.org/download/release/v20.9.0/node-v20.9.0-linux-x64-glibc-217.tar.xz
tar -xf node-v20.9.0-linux-x64-glibc-217.tar.xz --strip-components 1 -C /node20217
- uses: actions/checkout@v4
with:
fetch-depth: 5
- name: install rust language
uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2021-06-17
toolchain: nightly-2022-07-28
override: true
- uses: actions/cache@v3
id: cache
id: deps_cache
with:
path: |
deps/
/usr/local/share/vcpkg/buildtrees
/usr/local/share/vcpkg/packages
key: vcpkg-centos-v1-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }}
/home/runner/.ccache
/Users/runner/.ccache/
key: centos-notest-all-${{ matrix.os }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/cpp_full_node_workflow.yml') }}
restore-keys: |
vcpkg-centos-v1-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }}
vcpkg-centos-v1-notest-${{ runner.temp }}-${{ github.base_ref }}-
vcpkg-centos-v1-notest-${{ runner.temp }}-
centos-notest-all-${{ matrix.os }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/cpp_full_node_workflow.yml') }}
centos-notest-all-${{ matrix.os }}-${{ github.base_ref }}-
centos-notest-all-${{ matrix.os }}-
- name: install CentOS dependencies
run: |
rpm -ivh https://cbs.centos.org/kojifiles/packages/centos-release-scl-rh/2/3.el7.centos/noarch/centos-release-scl-rh-2-3.el7.centos.noarch.rpm
Expand Down
27 changes: 18 additions & 9 deletions bcos-c-sdk/bcos_sdk_c_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "bcos_sdk_c_error.h"
#include <bcos-utilities/Common.h>
#include <bcos-utilities/Error.h>
#include <bcos-utilities/BoostLog.h>

using namespace bcos;

Expand Down Expand Up @@ -253,7 +254,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 +265,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 +311,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
Loading

0 comments on commit 56c568c

Please sign in to comment.