Skip to content

Commit 27e5d1f

Browse files
Add block_deserialization in btcd
1 parent dd3399e commit 27e5d1f

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

modules/btcd/btcd_wrapper/libbtcd_wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ extern "C" {
8787

8888
extern int BTCDEvalScript(ByteArray scriptData, uint32_t flags);
8989
extern char* BTCDScriptAsm(ByteArray scriptData);
90+
extern char* BTCDDesBlock(ByteArray scriptData);
9091

9192
#ifdef __cplusplus
9293
}

modules/btcd/btcd_wrapper/libscript.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ extern "C" {
8787

8888
extern int BTCDEvalScript(ByteArray scriptData, uint32_t flags);
8989
extern char* BTCDScriptAsm(ByteArray scriptData);
90+
extern char* BTCDDesBlock(ByteArray scriptData);
9091

9192
#ifdef __cplusplus
9293
}

modules/btcd/btcd_wrapper/wrapper.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ typedef struct {
1212
import "C"
1313
import (
1414
"log"
15+
"math/big"
1516
"unsafe"
1617

18+
"github.com/btcsuite/btcd/blockchain"
19+
"github.com/btcsuite/btcd/btcutil"
1720
"github.com/btcsuite/btcd/txscript"
1821
"github.com/btcsuite/btcd/wire"
1922
)
@@ -58,4 +61,28 @@ func BTCDScriptAsm(scriptData C.ByteArray) *C.char {
5861
return C.CString(disasm)
5962
}
6063

64+
//export BTCDDesBlock
65+
func BTCDDesBlock(scriptData C.ByteArray) *C.char {
66+
buffer := C.GoBytes(unsafe.Pointer(scriptData.data), scriptData.length)
67+
68+
block, err := btcutil.NewBlockFromBytes(buffer)
69+
if err != nil {
70+
return C.CString("0")
71+
}
72+
73+
// Easiest possible PoW
74+
powLimit := new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil)
75+
err = blockchain.CheckBlockSanity(block, powLimit, blockchain.NewMedianTime())
76+
if err != nil {
77+
return C.CString("0")
78+
}
79+
80+
err = blockchain.ValidateWitnessCommitment(block)
81+
if err != nil {
82+
return C.CString("0")
83+
}
84+
85+
return C.CString("true")
86+
}
87+
6188
func main() {}

modules/btcd/module.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,16 @@ namespace bitcoinfuzz
3131
return res;
3232
}
3333

34+
std::optional<std::vector<bool>> Btcd::deserialize_block(std::span<const uint8_t> buffer) const
35+
{
36+
ByteArray script_data{
37+
.data = reinterpret_cast<char *>(const_cast<uint8_t *>(buffer.data())),
38+
.length = static_cast<int>(buffer.size())};
39+
40+
std::string result{BTCDDesBlock(script_data)};
41+
std::vector<bool> final_result{"true" == result};
42+
return final_result;
43+
}
44+
3445
} // namespace module
3546
} // namespace bitcoinfuzz

modules/btcd/module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <cstdint>
66
#include <bitcoinfuzz/basemodule.h>
77

8-
98
namespace bitcoinfuzz
109
{
1110
namespace module
@@ -16,6 +15,7 @@ namespace bitcoinfuzz
1615
Btcd(void);
1716
std::optional<bool> script_eval(const std::vector<uint8_t>& input_data, unsigned int flags, size_t version) const override;
1817
std::optional<std::string> script_asm(std::span<const uint8_t> buffer) const override;
18+
std::optional<std::vector<bool>> deserialize_block(std::span<const uint8_t> buffer) const override;
1919
~Btcd() noexcept override = default;
2020
};
2121

0 commit comments

Comments
 (0)