Skip to content

Commit

Permalink
sync code from master to 3.13.0 (#4791)
Browse files Browse the repository at this point in the history
  • Loading branch information
morebtcg authored Jan 16, 2025
2 parents 00e41d3 + aa67109 commit da823e4
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 12 deletions.
16 changes: 16 additions & 0 deletions bcos-crypto/bcos-crypto/signature/secp256k1/Secp256k1Crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ static const std::unique_ptr<secp256k1_context, decltype(&secp256k1_context_dest
g_SECP256K1_CTX{secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY),
&secp256k1_context_destroy};

inline void checkSigLen(bytesConstRef _signatureData)
{
if (SECP256K1_SIGNATURE_LEN != _signatureData.size())
{
std::ostringstream oss;
oss << "invalid signature length : current length is " << _signatureData.size();
std::string errMsg = oss.str();
CRYPTO_LOG(WARNING) << LOG_DESC("recoverAddress failed") << LOG_KV("message", errMsg);
BOOST_THROW_EXCEPTION(InvalidSignature() << errinfo_comment(errMsg));
}
}

std::shared_ptr<bytes> bcos::crypto::secp256k1Sign(
const KeyPairInterface& _keyPair, const HashType& _hash)
{
Expand Down Expand Up @@ -74,6 +86,7 @@ std::shared_ptr<bytes> bcos::crypto::secp256k1Sign(
bool bcos::crypto::secp256k1Verify(
const PublicPtr& _pubKey, const HashType& _hash, bytesConstRef _signatureData)
{
checkSigLen(_signatureData);
#if 0
CInputBuffer publicKey{_pubKey->constData(), _pubKey->size()};
CInputBuffer msgHash{(const char*)_hash.data(), HashType::SIZE};
Expand Down Expand Up @@ -128,6 +141,7 @@ KeyPairInterface::UniquePtr bcos::crypto::secp256k1GenerateKeyPair()

void secp256k1RecoverPrimitive(const HashType& _hash, char* _pubKey, bytesConstRef _signatureData)
{
checkSigLen(_signatureData);
secp256k1_ecdsa_recoverable_signature sig;
secp256k1_ecdsa_recoverable_signature_parse_compact(g_SECP256K1_CTX.get(), &sig,
_signatureData.data(), (int)_signatureData[SECP256K1_SIGNATURE_V]);
Expand All @@ -149,6 +163,7 @@ void secp256k1RecoverPrimitive(const HashType& _hash, char* _pubKey, bytesConstR

PublicPtr bcos::crypto::secp256k1Recover(const HashType& _hash, bytesConstRef _signatureData)
{
checkSigLen(_signatureData);
#if 0
CInputBuffer msgHash{(const char*)_hash.data(), HashType::SIZE};
CInputBuffer signature{(const char*)_signatureData.data(), _signatureData.size()};
Expand Down Expand Up @@ -209,6 +224,7 @@ std::pair<bool, bytes> bcos::crypto::secp256k1Recover(Hash::Ptr _hashImpl, bytes
std::pair<bool, bytes> Secp256k1Crypto::recoverAddress(
crypto::Hash& _hashImpl, const HashType& _hash, bytesConstRef _signatureData) const
{
checkSigLen(_signatureData);
std::array<unsigned char, SECP256K1_UNCOMPRESS_PUBLICKEY_LEN> data{};
secp256k1_ecdsa_recoverable_signature sig;
secp256k1_pubkey pubkey;
Expand Down
6 changes: 6 additions & 0 deletions bcos-crypto/test/unittests/SignatureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ BOOST_AUTO_TEST_CASE(testSecp256k1SignAndVerify)
auto encodedData = signatureData->encode();
BOOST_CHECK_THROW(secp256k1Crypto->recover(hashData, ref(*encodedData)), InvalidSignature);

// check4: invalid sig: len is not equal to 65(r+s+v)
bytesConstRef invalidSigData = bytesConstRef(signData->data(), signData->size() - 1);
BOOST_CHECK_THROW(
secp256k1Verify(keyPair->publicKey(), hashData, invalidSigData), InvalidSignature);
BOOST_CHECK_THROW(
secp256k1Crypto->recoverAddress(*hashImpl, hashData, invalidSigData), InvalidSignature);

// test signatureData encode and decode
encodedData = signatureData->encode();
Expand Down
3 changes: 3 additions & 0 deletions bcos-framework/bcos-framework/protocol/Protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,13 @@ enum ProtocolVersion : uint32_t
enum class BlockVersion : uint32_t
{
V3_13_0_VERSION = 0x030d0000, // 3.13.0
V3_12_3_VERSION = 0x030c0300, // 3.12.3
V3_12_2_VERSION = 0x030c0200, // 3.12.2
V3_12_1_VERSION = 0x030c0100, // 3.12.1
V3_12_0_VERSION = 0x030c0000, // 3.12.0
V3_11_0_VERSION = 0x030b0000,
V3_10_4_VERSION = 0x030a0400,
V3_10_3_VERSION = 0x030a0300,
V3_10_2_VERSION = 0x030a0200,
V3_10_0_VERSION = 0x030a0000,
V3_9_0_VERSION = 0x03090000,
Expand Down
7 changes: 7 additions & 0 deletions bcos-rpc/bcos-rpc/web3jsonrpc/model/BlockResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace bcos::rpc
{
result["number"] = toQuantity(block->blockHeader()->number());
result["hash"] = block->blockHeader()->hash().hexPrefixed();
// Only one parent block in BCOS. It is empty for genesis block
for (const auto& info : block->blockHeader()->parentInfo())
{
result["parentHash"] = info.blockHash.hexPrefixed();
Expand All @@ -54,6 +55,12 @@ namespace bcos::rpc
toChecksumAddress(addrString, addrHash);
result["miner"] = "0x" + addrString;
}
// genesis block
if (block->blockHeader()->number() == 0)
{
result["miner"] = "0x0000000000000000000000000000000000000000";
result["parentHash"] = "0x0000000000000000000000000000000000000000000000000000000000000000";
}
result["difficulty"] = "0x0";
result["totalDifficulty"] = "0x0";
result["extraData"] = toHexStringWithPrefix(block->blockHeader()->extraData());
Expand Down
8 changes: 4 additions & 4 deletions bcos-rpc/bcos-rpc/web3jsonrpc/model/ReceiptResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ namespace bcos::rpc
blockNumber = block->blockHeader()->number();
for (; transactionIndex < block->transactionsHashSize(); transactionIndex++)
{
if (block->transactionHash(transactionIndex) == tx->hash())
{
break;
}
if (transactionIndex <= block->receiptsSize())
{
cumulativeGasUsed += block->receipt(transactionIndex)->gasUsed();
}
if (block->transactionHash(transactionIndex) == tx->hash())
{
break;
}
}
}
result["transactionIndex"] = toQuantity(transactionIndex);
Expand Down
2 changes: 1 addition & 1 deletion bcos-rpc/bcos-rpc/web3jsonrpc/model/TransactionResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static void combineTxResponse(Json::Value& result, bcos::protocol::Transaction::
{
result["type"] = toQuantity(0);
// web3 tools do not compatible with too long hex
// result["nonce"] = tx->nonce();
result["nonce"] = "0x" + std::string(tx->nonce());
result["value"] = std::string(tx->value().empty() ? "0x0" : tx->value());
result["maxPriorityFeePerGas"] =
std::string(tx->maxPriorityFeePerGas().empty() ? "0x0" : tx->maxPriorityFeePerGas());
Expand Down
1 change: 1 addition & 0 deletions libinitializer/FrontServiceInitializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ void FrontServiceInitializer::initMsgHandlers(bcos::consensus::ConsensusInterfac
<< LOG_KV("tx", transaction ? transaction->hash().hex() : "")
<< LOG_KV("messageID", messageID);
}
transaction->forceSender({}); // must clear sender here for future verify
task::wait(
[](decltype(txpool) txpool, decltype(transaction) transaction) -> task::Task<void> {
try
Expand Down
6 changes: 0 additions & 6 deletions tools/BcosBuilder/src/controller/binary_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def __init__(self, version, binary_path, use_cdn, node_type):
else:
self.binary_postfix = "-linux-x86_64.tgz"
self.mtail_binary_name = "mtail_3.0.0-rc49_Linux_x86_64.tar.gz"
self.cdn_link_header = "https://github.com/FISCO-BCOS"
if node_type == "pro":
self.binary_list = ["BcosRpcService",
"BcosGatewayService", "BcosNodeService"]
Expand All @@ -35,11 +34,6 @@ def __init__(self, version, binary_path, use_cdn, node_type):
self.last_percent = 0
self.download_prefix = "https://github.com/FISCO-BCOS/FISCO-BCOS/releases/download/"
self.mtail_download_url = "https://github.com/google/mtail/releases/download/v3.0.0-rc49/%s" % self.mtail_binary_name
if self.use_cdn is True:
self.download_prefix = "%s/FISCO-BCOS/releases/" % (
self.cdn_link_header)
self.mtail_download_url = "%s/FISCO-BCOS/tools/mtail/%s" % (
self.cdn_link_header, self.mtail_binary_name)

def download_all_binary(self):
utilities.print_badge(
Expand Down
4 changes: 3 additions & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
},
{
"name": "aws-sdk-cpp",
"features": ["kms"]
"features": [
"kms"
]
},
"wedprcrypto",
"range-v3",
Expand Down

0 comments on commit da823e4

Please sign in to comment.