Skip to content

Commit

Permalink
<feat>(sdk): impl transaction builder v2 in c sdk.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyonRay committed Nov 29, 2023
1 parent d0c730d commit 90002f8
Show file tree
Hide file tree
Showing 8 changed files with 582 additions and 3 deletions.
14 changes: 14 additions & 0 deletions bcos-c-sdk/bcos_sdk_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,18 @@ const char* bcos_sdk_get_group_chain_id(void* sdk, const char* group)

auto chainID = groupInfo->chainID();
return strdup(chainID.c_str());
}

uint32_t bcos_sdk_get_local_protocol_info(void* sdk) {
bcos_sdk_clear_last_error();
BCOS_SDK_C_PARAMS_VERIFICATION(sdk, 0);

return ((bcos::cppsdk::Sdk*)sdk)->localProtocolInfo();
}

uint32_t bcos_sdk_get_negotiated_protocol_info(void* sdk) {
bcos_sdk_clear_last_error();
BCOS_SDK_C_PARAMS_VERIFICATION(sdk, 0);

return ((bcos::cppsdk::Sdk*)sdk)->negotiatedProtocolInfo();
}
4 changes: 4 additions & 0 deletions bcos-c-sdk/bcos_sdk_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ void bcos_sdk_get_group_wasm_and_crypto(void* sdk, const char* group, int* wasm,
* @return const char* : chain id
*/
const char* bcos_sdk_get_group_chain_id(void* sdk, const char* group);

uint32_t bcos_sdk_get_local_protocol_info(void* sdk);

uint32_t bcos_sdk_get_negotiated_protocol_info(void* sdk);
// --------------------------------------------------------------------

#ifdef __cplusplus
Expand Down
32 changes: 30 additions & 2 deletions bcos-c-sdk/bcos_sdk_c_uti_receipt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ void* bcos_sdk_create_receipt_data_with_json(const char* json)
{
std::string errorMsg = boost::diagnostic_information(e);
BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_receipt_data") << LOG_DESC("exception")
<< LOG_KV("json", json)
<< LOG_KV("error", errorMsg);
<< LOG_KV("json", json) << LOG_KV("error", errorMsg);
bcos_sdk_set_last_error_msg(-1, errorMsg.c_str());
}

Expand Down Expand Up @@ -167,4 +166,33 @@ const char* bcos_sdk_calc_receipt_data_hash(int crypto_type, void* _receiptData)
bcos_sdk_set_last_error_msg(-1, errorMsg.c_str());
}
return NULL;
}

const char* bcos_sdk_calc_receipt_data_hash_with_json(int crypto_type, const char* json)
{
bcos_sdk_clear_last_error();
BCOS_SDK_C_PARAMS_VERIFICATION(json, 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
{
ReceiptBuilder builder;
auto transactionDataHash = builder.calculateReceiptDataHashWithJson(
crypto_type == BCOS_C_SDK_ECDSA_TYPE ? CryptoType::Secp256K1 : CryptoType::SM2,
std::string(json));
return strdup(bcos::toHexStringWithPrefix(transactionDataHash).c_str());
}
catch (const std::exception& e)
{
std::string errorMsg = boost::diagnostic_information(e);
BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_calc_transaction_data_hash_with_json")
<< LOG_DESC("exception") << LOG_KV("crypto_type", crypto_type)
<< LOG_KV("json", json) << LOG_KV("error", errorMsg);
bcos_sdk_set_last_error_msg(-1, errorMsg.c_str());
}
return NULL;
}
2 changes: 2 additions & 0 deletions bcos-c-sdk/bcos_sdk_c_uti_receipt.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ void bcos_sdk_destroy_receipt_data(void* transaction_data);

const char* bcos_sdk_calc_receipt_data_hash(int crypto_type, void* _receiptData);

const char* bcos_sdk_calc_receipt_data_hash_with_json(int crypto_type, const char* json);

const char* bcos_sdk_encode_receipt_data(void* receipt_data);

const char* bcos_sdk_decode_receipt_data(const char* receipt_bytes);
Expand Down
324 changes: 324 additions & 0 deletions bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp

Large diffs are not rendered by default.

200 changes: 200 additions & 0 deletions bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/**
* Copyright (C) 2022 FISCO BCOS.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @file bcos_sdk_c_uti_tx_v2.h
* @author: kyonGuo
* @date 2023/11/28
*/

#ifndef BCOS_C_SDK_BCOS_SDK_C_UTI_TX_V2_H
#define BCOS_C_SDK_BCOS_SDK_C_UTI_TX_V2_H

#include "bcos_sdk_c_common.h"

#ifdef __cplusplus
extern "C" {
#endif

enum transaction_version
{
TRANSACTION_VERSION_0 = 0,
/*
* @brief enable (value,gasPrice,gasLimit,maxFeePerGas,maxPriorityFeePerGas) fields
* @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later
*/
TRANSACTION_VERSION_1 = 1,
};

/**
* @brief create transaction data with version 1 by default
* @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later
*
* @param group_id group id
* @param chain_id chain id
* @param to contract address, if it is a contract creation transaction, it can be empty
* @param input encoded contract method and params
* @param abi contract abi, only create contract need
* @param block_limit block limit
* @param value transfer value
* @param gas_price gas price
* @param gas_limit gas limit
* @return void* transaction data pointer, you should release it after use
*/
void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chain_id,
const char* to, const char* input, const char* abi, int64_t block_limit, const char* value,
const char* gas_price, int64_t gas_limit);

/**
* @brief create eip1559 transaction data with version 1 by default
* @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later
*
* @param group_id group id
* @param chain_id chain id
* @param to contract address, if it is a contract creation transaction, it can be empty
* @param input encoded contract method and params
* @param abi contract abi, only create contract need
* @param block_limit block limit
* @param value transfer value
* @param gas_limit gas limit
* @param max_fee_per_gas max fee per gas
* @param max_priority_fee_per_gas max priority fee per gas
* @return void* transaction data pointer, you should release it after use
*/
void* bcos_sdk_create_eip1559_transaction_data(const char* group_id, const char* chain_id,
const char* to, const char* input, const char* abi, int64_t block_limit, const char* value,
int64_t gas_limit, const char* max_fee_per_gas, const char* max_priority_fee_per_gas);

/**
* @brief create transaction data with full fields
* @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later
*
* @param crypto_type 0: keccak256, 1: sm3
* @param version tx version, if version==1, then enable
* (value,gasPrice,gasLimit,maxFeePerGas,maxPriorityFeePerGas) fields
* @param group_id group id
* @param chain_id chain id
* @param to contract address, if it is a contract creation transaction, it can be empty
* @param nonce nonce, random number to avoid duplicate transactions
* @param input encoded contract method and params
* @param abi contract abi, only create contract need
* @param block_limit block limit
* @param value transfer value
* @param gas_price gas price
* @param gas_limit gas limit
* @param max_fee_per_gas max fee per gas
* @param max_priority_fee_per_gas max priority fee per gas
* @return const char* transaction data hash hex string
*/
const char* bcos_sdk_calc_transaction_data_hash_with_full_fields(int crypto_type, transaction_version version,
const char* group_id, const char* chain_id, const char* to, const char* nonce,
const char* input, const char* abi, int64_t block_limit, const char* value,
const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas,
const char* max_priority_fee_per_gas);

/**
* @brief create transaction data with json string
* @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later
*
* @param crypto_type 0: keccak256, 1: sm3
* @param json transaction data json string
* @return const char* transaction data hash hex string
* @throw exception if lack of some required fields, or some fields are invalid
*/
const char* bcos_sdk_calc_transaction_data_hash_with_json(int crypto_type, const char* json);

/**
* @brief create encoded transaction data with external signature
* @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later
*
* @param signature signature hex string, if ECDSA, it is r||s||v, if SM2, it is r||s||pk
* @param transaction_hash transactionData hash hex string
* @param version tx version, only support 0 and 1 now, if version==1, then enable (value,gasPrice,gasLimit,maxFeePerGas,maxPriorityFeePerGas) fields
* @param group_id group id
* @param chain_id chain id
* @param to contract address, if it is a contract creation transaction, it can be empty
* @param nonce nonce, random number to avoid duplicate transactions
* @param input encoded contract method and params
* @param abi contract abi, only create contract need
* @param block_limit block limit
* @param value transfer value
* @param gas_price gas price
* @param gas_limit gas limit
* @param max_fee_per_gas max fee per gas
* @param max_priority_fee_per_gas max priority fee per gas
* @param attribute transaction attribute
* @param extra_data extra data in transaction
* @return const char* encoded transaction hex string
*/
const char* bcos_sdk_create_signed_transaction_with_signature(const char* signature,
const char* transaction_hash, transaction_version version, const char* group_id, const char* chain_id,
const char* to, const char* nonce, const char* input, const char* abi, int64_t block_limit,
const char* value, const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas,
const char* max_priority_fee_per_gas, int32_t attribute, const char* extra_data);

/**
* @brief create transaction with full fields, with version 1 by default
* @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later
*
* @param key_pair key pair pointer
* @param group_id group id
* @param chain_id chain id
* @param to contract address, if it is a contract creation transaction, it can be empty
* @param nonce nonce, random number to avoid duplicate transactions
* @param input encoded contract method and params
* @param abi contract abi, only create contract need
* @param block_limit block limit
* @param value transfer value
* @param gas_price gas price
* @param gas_limit gas limit
* @param attribute transaction attribute
* @param extra_data extra data in transaction
* @param tx_hash output transaction hash hex string
* @param signed_tx output signed transaction hex string
*/
void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const char* group_id,
const char* chain_id, const char* to, const char* input, const char* abi, int64_t block_limit,
const char* value, const char* gas_price, int64_t gas_limit, int32_t attribute,
const char* extra_data, char** tx_hash, char** signed_tx);

/**
* @brief create eip1559 transaction with full fields, with version 1 by default
* @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later
*
* @param key_pair key pair pointer
* @param group_id group id
* @param chain_id chain id
* @param to contract address, if it is a contract creation transaction, it can be empty
* @param input encoded contract method and params
* @param abi contract abi, only create contract need
* @param block_limit block limit
* @param value transfer value
* @param gas_limit gas limit
* @param max_fee_per_gas max fee per gas
* @param max_priority_fee_per_gas max priority fee per gas
* @param attribute transaction attribute
* @param extra_data extra data in transaction
* @param tx_hash output transaction hash hex string
* @param signed_tx output signed transaction hex string
*/
void bcos_sdk_create_signed_eip1559_transaction_with_full_fields(void* key_pair,
const char* group_id, const char* chain_id, const char* to, const char* input, const char* abi,
int64_t block_limit, const char* value, int64_t gas_limit, const char* max_fee_per_gas,
const char* max_priority_fee_per_gas, int32_t attribute, const char* extra_data, char** tx_hash,
char** signed_tx);

#ifdef __cplusplus
}
#endif
#endif // BCOS_C_SDK_BCOS_SDK_C_UTI_TX_V2_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
bcos-sdk-jni.dll
bcos-sdk-jni.lib
libbcos-sdk-jni.dylib
libbcos-sdk-jni.so
libbcos-sdk-jni-aarch64.dylib
libbcos-sdk-jni-aarch64.so
libbcos-sdk-jni-static-aarch64.a
2 changes: 1 addition & 1 deletion vcpkg-configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"kind": "git",
"repository": "https://github.com/FISCO-BCOS/registry",
"baseline": "717d8236aae1c032cfdd9469fd8248c8e49da6d3",
"baseline": "c15466a352b8daede8d2c447670a761ad736c132",
"packages": [
"openssl",
"hsm-crypto",
Expand Down

0 comments on commit 90002f8

Please sign in to comment.