Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Commit 19c07d0

Browse files
authored
Remove be and parse (#127)
* refactor: start prep for no_std version * refactor: remove BE functions from BTCUtils and Parse functions from ValidateSPV in all languages * bug: delete removed method declarations from header file
1 parent 2535e4e commit 19c07d0

27 files changed

+202
-1143
lines changed

c/csrc/btcspv.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,6 @@ byte_view_t btcspv_extract_input_tx_id_le(const_view_t *tx_in) {
183183
return tx_id_le;
184184
}
185185

186-
void btcspv_extract_input_tx_id_be(uint256 hash, const_view_t *tx_in) {
187-
const_view_t le = btcspv_extract_input_tx_id_le(tx_in);
188-
buf_rev(hash, le.loc, le.len);
189-
}
190-
191186
byte_view_t btcspv_extract_tx_index_le(const_view_t *tx_in) {
192187
byte_view_t idx = {tx_in->loc + 32, 4};
193188
return idx;
@@ -361,11 +356,6 @@ byte_view_t btcspv_extract_merkle_root_le(const_view_t *header) {
361356
return root;
362357
}
363358

364-
void btcspv_extract_merkle_root_be(uint256 hash, const_view_t *header) {
365-
const_view_t le = btcspv_extract_merkle_root_le(header);
366-
buf_rev(hash, le.loc, le.len);
367-
}
368-
369359
void btcspv_extract_target_le(uint256 target, const_view_t *header) {
370360
uint8_t exponent = header->loc[75] - 3;
371361
target[exponent + 0] = header->loc[72];
@@ -393,11 +383,6 @@ byte_view_t btcspv_extract_prev_block_hash_le(const_view_t *header) {
393383
return prev_hash;
394384
}
395385

396-
void btcspv_extract_prev_block_hash_be(uint256 hash, const_view_t *header) {
397-
const_view_t le = btcspv_extract_prev_block_hash_le(header);
398-
buf_rev(hash, le.loc, le.len);
399-
}
400-
401386
byte_view_t btcspv_extract_timestamp_le(const_view_t *header) {
402387
const_view_t timestamp = {header->loc + 68, 4};
403388
return timestamp;

c/csrc/btcspv.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,6 @@ byte_view_t btcspv_extract_outpoint(const_view_t *tx_in);
181181
/// @return The tx id (little-endian bytes)
182182
byte_view_t btcspv_extract_input_tx_id_le(const_view_t *tx_in);
183183

184-
/// @brief Extracts the outpoint index from an input
185-
/// @note 32 byte tx id from outpoint
186-
/// @param input The input
187-
/// @warning overwrites `hash`
188-
/// @warning caller must ensure `hash` is allocated and can hold 32 bytes
189-
void btcspv_extract_input_tx_id_be(uint256 hash, const_view_t *tx_in);
190-
191184
/// @brief Extracts the LE tx input index from the input in a tx
192185
/// @note 4 byte tx index
193186
/// @param input The input
@@ -276,13 +269,6 @@ bool btcspv_validate_vout(const_view_t *vout);
276269
/// @return The merkle root (little-endian)
277270
byte_view_t btcspv_extract_merkle_root_le(const_view_t *header);
278271

279-
/// @brief Extracts the transaction merkle root from a block header
280-
/// @note Use verifyHash256Merkle to verify proofs with this root
281-
/// @param header The header
282-
/// @warning overwrites `hash` with the merkle root
283-
/// @warning caller must ensure `hash` is allocated and can hold 32 bytes
284-
void btcspv_extract_merkle_root_be(uint256 hash, const_view_t *header);
285-
286272
/// @brief Extracts the target from a block header
287273
/// @note Target is a 256 bit number encoded as a 3-byte mantissa and 1 byte exponent
288274
/// @param header The header
@@ -312,13 +298,6 @@ uint64_t btcspv_calculate_difficulty(uint256 target);
312298
/// @return The previous block's hash (little-endian)
313299
byte_view_t btcspv_extract_prev_block_hash_le(const_view_t *header);
314300

315-
/// @brief Extracts the previous block's hash from a block header
316-
/// @note Block headers do NOT include block number :(
317-
/// @param header The header
318-
/// @warning overwrites `hash` with the block header hash
319-
/// @warning caller must ensure `hash` is allocated and can hold 32 bytes
320-
void btcspv_extract_prev_block_hash_be(uint256 hash, const_view_t *header);
321-
322301
/// @brief Extracts the timestamp from a block header
323302
/// @note Time is not 100% reliable
324303
/// @param header The header

c/csrc/check_btcspv.c

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -781,27 +781,6 @@ START_TEST(validate_vout) {
781781
}
782782
END_TEST
783783

784-
START_TEST(extract_merkle_root_be) {
785-
TEST_LOOP_START("extractMerkleRootBE")
786-
uint8_t *input_buf;
787-
const uint32_t input_len = token_as_hex_buf(&input_buf, input_tok);
788-
const_view_t input = {input_buf, input_len};
789-
790-
uint8_t *expected_buf;
791-
const uint32_t expected_len = token_as_hex_buf(&expected_buf, output_tok);
792-
const_view_t expected = {expected_buf, expected_len};
793-
794-
uint8_t hash[32] = {0};
795-
btcspv_extract_merkle_root_be(hash, &input);
796-
797-
ck_assert(view_eq_buf(&expected, hash, 32));
798-
799-
free(input_buf);
800-
free(expected_buf);
801-
TEST_LOOP_END
802-
}
803-
END_TEST
804-
805784
START_TEST(extract_target) {
806785
TEST_LOOP_START("extractTarget")
807786
uint8_t *input_buf;
@@ -828,27 +807,6 @@ START_TEST(extract_target) {
828807
}
829808
END_TEST
830809

831-
START_TEST(extract_prev_block_hash_be) {
832-
TEST_LOOP_START("extractPrevBlockBE")
833-
uint8_t *input_buf;
834-
const uint32_t input_len = token_as_hex_buf(&input_buf, input_tok);
835-
const_view_t input = {input_buf, input_len};
836-
837-
uint8_t *expected_buf;
838-
const uint32_t expected_len = token_as_hex_buf(&expected_buf, output_tok);
839-
const_view_t expected = {expected_buf, expected_len};
840-
841-
uint8_t hash[32] = {0};
842-
btcspv_extract_prev_block_hash_be(hash, &input);
843-
844-
ck_assert(view_eq_buf(&expected, hash, 32));
845-
846-
free(input_buf);
847-
free(expected_buf);
848-
TEST_LOOP_END
849-
}
850-
END_TEST
851-
852810
START_TEST(extract_timestamp) {
853811
TEST_LOOP_START("extractTimestamp")
854812

@@ -1020,9 +978,7 @@ int main(int argc, char *argv[]) {
1020978
tcase_add_test(btcspv_case, extract_hash_error);
1021979
tcase_add_test(btcspv_case, validate_vin);
1022980
tcase_add_test(btcspv_case, validate_vout);
1023-
tcase_add_test(btcspv_case, extract_merkle_root_be);
1024981
tcase_add_test(btcspv_case, extract_target);
1025-
tcase_add_test(btcspv_case, extract_prev_block_hash_be);
1026982
tcase_add_test(btcspv_case, extract_timestamp);
1027983
tcase_add_test(btcspv_case, hash256_merkle_step);
1028984
tcase_add_test(btcspv_case, verify_hash256_merkle);

golang/btcspv/bitcoin_spv.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,6 @@ func ExtractInputTxIDLE(input []byte) Hash256Digest {
176176
return res
177177
}
178178

179-
// ExtractInputTxID returns the input tx id bytes
180-
func ExtractInputTxID(input []byte) Hash256Digest {
181-
LE := ExtractInputTxIDLE(input)
182-
txID := ReverseHash256Endianness(LE)
183-
return txID
184-
}
185-
186179
// ExtractTxIndexLE extracts the LE tx input index from the input in a tx
187180
// Returns the tx index as a little endian []byte
188181
func ExtractTxIndexLE(input []byte) []byte {
@@ -351,12 +344,6 @@ func ExtractMerkleRootLE(header RawHeader) Hash256Digest {
351344
return res
352345
}
353346

354-
// ExtractMerkleRootBE returns the transaction merkle root from a given block header
355-
// The returned merkle root is big-endian
356-
func ExtractMerkleRootBE(header RawHeader) Hash256Digest {
357-
return ReverseHash256Endianness(ExtractMerkleRootLE(header))
358-
}
359-
360347
// ExtractTarget returns the target from a given block hedaer
361348
func ExtractTarget(header RawHeader) sdk.Uint {
362349
// nBits encoding. 3 byte mantissa, 1 byte exponent
@@ -391,12 +378,6 @@ func ExtractPrevBlockHashLE(header RawHeader) Hash256Digest {
391378
return res
392379
}
393380

394-
// ExtractPrevBlockHashBE returns the previous block's hash from a block header
395-
// Returns the hash as a big endian []byte
396-
func ExtractPrevBlockHashBE(header RawHeader) Hash256Digest {
397-
return ReverseHash256Endianness(ExtractPrevBlockHashLE(header))
398-
}
399-
400381
// ExtractTimestampLE returns the timestamp from a block header
401382
// It returns the timestamp as a little endian []byte
402383
// Time is not 100% reliable

golang/btcspv/bitcoin_spv_test.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,6 @@ func (suite *UtilsSuite) TestExtractOpReturnData() {
346346
testCase := fixtureError[i]
347347
expected := testCase.ErrorMessage.(string)
348348
actual, err := ExtractOpReturnData(testCase.Input.([]byte))
349-
log.Println(testCase.Input, cap(testCase.Input.([]byte)), actual, err)
350349
suite.Nil(actual)
351350
suite.EqualError(err, expected)
352351
}
@@ -442,17 +441,6 @@ func (suite *UtilsSuite) TestExtractInputTxIDLE() {
442441
}
443442
}
444443

445-
func (suite *UtilsSuite) TestExtractInputTxID() {
446-
fixture := suite.Fixtures["extractInputTxId"]
447-
448-
for i := range fixture {
449-
testCase := fixture[i]
450-
expected := testCase.Output.(Hash256Digest)
451-
actual := ExtractInputTxID(testCase.Input.([]byte))
452-
suite.Equal(expected, actual)
453-
}
454-
}
455-
456444
func (suite *UtilsSuite) TestExtractTxIndexLE() {
457445
fixture := suite.Fixtures["extractTxIndexLE"]
458446

@@ -521,17 +509,6 @@ func (suite *UtilsSuite) TestExtractOutputAtIndex() {
521509
suite.EqualError(err, expected)
522510
}
523511

524-
func (suite *UtilsSuite) TestExtractMerkleRootBE() {
525-
fixture := suite.Fixtures["extractMerkleRootBE"]
526-
527-
for i := range fixture {
528-
testCase := fixture[i]
529-
expected := testCase.Output.(Hash256Digest)
530-
actual := ExtractMerkleRootBE(testCase.Input.(RawHeader))
531-
suite.Equal(expected, actual)
532-
}
533-
}
534-
535512
func (suite *UtilsSuite) TestExtractTarget() {
536513
fixture := suite.Fixtures["extractTarget"]
537514

@@ -553,21 +530,6 @@ func (suite *UtilsSuite) TestExtractTarget() {
553530
}
554531
}
555532

556-
func (suite *UtilsSuite) TestExtractPrevBlockHashBE() {
557-
fixture := suite.Fixtures["retargetAlgorithm"]
558-
559-
for i := range fixture {
560-
testCase := fixture[i]
561-
input := testCase.Input.([]interface{})
562-
for j := range input {
563-
h := input[j].(map[string]interface{})
564-
actual := ExtractPrevBlockHashBE(h["hex"].(RawHeader))
565-
expected := h["prev_block"].(Hash256Digest)
566-
suite.Equal(expected, actual)
567-
}
568-
}
569-
}
570-
571533
func (suite *UtilsSuite) TestExtractTimestamp() {
572534
fixture := suite.Fixtures["extractTimestamp"]
573535

golang/btcspv/types.go

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,6 @@ type SPVProof struct {
4242
IntermediateNodes HexBytes `json:"intermediate_nodes"`
4343
}
4444

45-
// InputType an enum of types of bitcoin inputs
46-
type InputType int
47-
48-
// possible input types
49-
const (
50-
InputNone InputType = 0
51-
Legacy InputType = 1
52-
Compatibility InputType = 2
53-
Witness InputType = 3
54-
)
55-
56-
// OutputType an enum of types of bitcoin outputs
57-
type OutputType int
58-
59-
// possible output types
60-
const (
61-
OutputNone OutputType = 0
62-
WPKH OutputType = 1
63-
WSH OutputType = 2
64-
OpReturn OutputType = 3
65-
PKH OutputType = 4
66-
SH OutputType = 5
67-
Nonstandard OutputType = 6
68-
)
69-
7045
// NewHash160Digest instantiates a Hash160Digest from a byte slice
7146
func NewHash160Digest(b []byte) (Hash160Digest, error) {
7247
var h Hash160Digest
@@ -102,16 +77,19 @@ func HeaderFromRaw(raw RawHeader, height uint32) BitcoinHeader {
10277
digestLE := Hash256(raw[:])
10378
digestBE := ReverseHash256Endianness(digestLE)
10479
prevhashLE := ExtractPrevBlockHashLE(raw)
105-
prevhash := ReverseHash256Endianness(prevhashLE)
80+
prevhashBE := ReverseHash256Endianness(prevhashLE)
81+
merkleRootLE := ExtractMerkleRootLE(raw)
82+
merkleRootBE := ReverseHash256Endianness(merkleRootLE)
83+
10684
return BitcoinHeader{
10785
raw,
10886
digestBE,
10987
digestLE,
11088
height,
111-
prevhash,
89+
prevhashBE,
11290
prevhashLE,
113-
ExtractMerkleRootBE(raw),
114-
ExtractMerkleRootLE(raw),
91+
merkleRootBE,
92+
merkleRootLE,
11593
}
11694
}
11795

golang/btcspv/utils.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,6 @@ func DecodeIfHex(s string) []byte {
3232
return res
3333
}
3434

35-
// GetOutputType returns the name of the output type associated with the number
36-
func GetOutputType(outputType OutputType) string {
37-
var typeString string
38-
switch outputType {
39-
case OutputNone:
40-
typeString = "Output None"
41-
case WPKH:
42-
typeString = "WPKH"
43-
case WSH:
44-
typeString = "WSH"
45-
case OpReturn:
46-
typeString = "Op Return"
47-
case PKH:
48-
typeString = "PKH"
49-
case SH:
50-
typeString = "SH"
51-
case Nonstandard:
52-
typeString = "Nonstandard"
53-
}
54-
return typeString
55-
}
56-
57-
// GetInputType returns the name of the input type associated with the number
58-
func GetInputType(inputType InputType) string {
59-
var typeString string
60-
switch inputType {
61-
case InputNone:
62-
typeString = "Input None"
63-
case Legacy:
64-
typeString = "Legacy"
65-
case Compatibility:
66-
typeString = "Compatibility"
67-
case Witness:
68-
typeString = "Witness"
69-
}
70-
return typeString
71-
}
72-
7335
// EncodeP2SH turns a scripthash into an address
7436
func EncodeP2SH(sh []byte) (string, error) {
7537
if len(sh) != 20 {

0 commit comments

Comments
 (0)