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

Commit

Permalink
Remove be and parse (#127)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
prestwich authored Feb 27, 2020
1 parent 2535e4e commit 19c07d0
Show file tree
Hide file tree
Showing 27 changed files with 202 additions and 1,143 deletions.
15 changes: 0 additions & 15 deletions c/csrc/btcspv.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,6 @@ byte_view_t btcspv_extract_input_tx_id_le(const_view_t *tx_in) {
return tx_id_le;
}

void btcspv_extract_input_tx_id_be(uint256 hash, const_view_t *tx_in) {
const_view_t le = btcspv_extract_input_tx_id_le(tx_in);
buf_rev(hash, le.loc, le.len);
}

byte_view_t btcspv_extract_tx_index_le(const_view_t *tx_in) {
byte_view_t idx = {tx_in->loc + 32, 4};
return idx;
Expand Down Expand Up @@ -361,11 +356,6 @@ byte_view_t btcspv_extract_merkle_root_le(const_view_t *header) {
return root;
}

void btcspv_extract_merkle_root_be(uint256 hash, const_view_t *header) {
const_view_t le = btcspv_extract_merkle_root_le(header);
buf_rev(hash, le.loc, le.len);
}

void btcspv_extract_target_le(uint256 target, const_view_t *header) {
uint8_t exponent = header->loc[75] - 3;
target[exponent + 0] = header->loc[72];
Expand Down Expand Up @@ -393,11 +383,6 @@ byte_view_t btcspv_extract_prev_block_hash_le(const_view_t *header) {
return prev_hash;
}

void btcspv_extract_prev_block_hash_be(uint256 hash, const_view_t *header) {
const_view_t le = btcspv_extract_prev_block_hash_le(header);
buf_rev(hash, le.loc, le.len);
}

byte_view_t btcspv_extract_timestamp_le(const_view_t *header) {
const_view_t timestamp = {header->loc + 68, 4};
return timestamp;
Expand Down
21 changes: 0 additions & 21 deletions c/csrc/btcspv.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,6 @@ byte_view_t btcspv_extract_outpoint(const_view_t *tx_in);
/// @return The tx id (little-endian bytes)
byte_view_t btcspv_extract_input_tx_id_le(const_view_t *tx_in);

/// @brief Extracts the outpoint index from an input
/// @note 32 byte tx id from outpoint
/// @param input The input
/// @warning overwrites `hash`
/// @warning caller must ensure `hash` is allocated and can hold 32 bytes
void btcspv_extract_input_tx_id_be(uint256 hash, const_view_t *tx_in);

/// @brief Extracts the LE tx input index from the input in a tx
/// @note 4 byte tx index
/// @param input The input
Expand Down Expand Up @@ -276,13 +269,6 @@ bool btcspv_validate_vout(const_view_t *vout);
/// @return The merkle root (little-endian)
byte_view_t btcspv_extract_merkle_root_le(const_view_t *header);

/// @brief Extracts the transaction merkle root from a block header
/// @note Use verifyHash256Merkle to verify proofs with this root
/// @param header The header
/// @warning overwrites `hash` with the merkle root
/// @warning caller must ensure `hash` is allocated and can hold 32 bytes
void btcspv_extract_merkle_root_be(uint256 hash, const_view_t *header);

/// @brief Extracts the target from a block header
/// @note Target is a 256 bit number encoded as a 3-byte mantissa and 1 byte exponent
/// @param header The header
Expand Down Expand Up @@ -312,13 +298,6 @@ uint64_t btcspv_calculate_difficulty(uint256 target);
/// @return The previous block's hash (little-endian)
byte_view_t btcspv_extract_prev_block_hash_le(const_view_t *header);

/// @brief Extracts the previous block's hash from a block header
/// @note Block headers do NOT include block number :(
/// @param header The header
/// @warning overwrites `hash` with the block header hash
/// @warning caller must ensure `hash` is allocated and can hold 32 bytes
void btcspv_extract_prev_block_hash_be(uint256 hash, const_view_t *header);

/// @brief Extracts the timestamp from a block header
/// @note Time is not 100% reliable
/// @param header The header
Expand Down
44 changes: 0 additions & 44 deletions c/csrc/check_btcspv.c
Original file line number Diff line number Diff line change
Expand Up @@ -781,27 +781,6 @@ START_TEST(validate_vout) {
}
END_TEST

START_TEST(extract_merkle_root_be) {
TEST_LOOP_START("extractMerkleRootBE")
uint8_t *input_buf;
const uint32_t input_len = token_as_hex_buf(&input_buf, input_tok);
const_view_t input = {input_buf, input_len};

uint8_t *expected_buf;
const uint32_t expected_len = token_as_hex_buf(&expected_buf, output_tok);
const_view_t expected = {expected_buf, expected_len};

uint8_t hash[32] = {0};
btcspv_extract_merkle_root_be(hash, &input);

ck_assert(view_eq_buf(&expected, hash, 32));

free(input_buf);
free(expected_buf);
TEST_LOOP_END
}
END_TEST

START_TEST(extract_target) {
TEST_LOOP_START("extractTarget")
uint8_t *input_buf;
Expand All @@ -828,27 +807,6 @@ START_TEST(extract_target) {
}
END_TEST

START_TEST(extract_prev_block_hash_be) {
TEST_LOOP_START("extractPrevBlockBE")
uint8_t *input_buf;
const uint32_t input_len = token_as_hex_buf(&input_buf, input_tok);
const_view_t input = {input_buf, input_len};

uint8_t *expected_buf;
const uint32_t expected_len = token_as_hex_buf(&expected_buf, output_tok);
const_view_t expected = {expected_buf, expected_len};

uint8_t hash[32] = {0};
btcspv_extract_prev_block_hash_be(hash, &input);

ck_assert(view_eq_buf(&expected, hash, 32));

free(input_buf);
free(expected_buf);
TEST_LOOP_END
}
END_TEST

START_TEST(extract_timestamp) {
TEST_LOOP_START("extractTimestamp")

Expand Down Expand Up @@ -1020,9 +978,7 @@ int main(int argc, char *argv[]) {
tcase_add_test(btcspv_case, extract_hash_error);
tcase_add_test(btcspv_case, validate_vin);
tcase_add_test(btcspv_case, validate_vout);
tcase_add_test(btcspv_case, extract_merkle_root_be);
tcase_add_test(btcspv_case, extract_target);
tcase_add_test(btcspv_case, extract_prev_block_hash_be);
tcase_add_test(btcspv_case, extract_timestamp);
tcase_add_test(btcspv_case, hash256_merkle_step);
tcase_add_test(btcspv_case, verify_hash256_merkle);
Expand Down
19 changes: 0 additions & 19 deletions golang/btcspv/bitcoin_spv.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,6 @@ func ExtractInputTxIDLE(input []byte) Hash256Digest {
return res
}

// ExtractInputTxID returns the input tx id bytes
func ExtractInputTxID(input []byte) Hash256Digest {
LE := ExtractInputTxIDLE(input)
txID := ReverseHash256Endianness(LE)
return txID
}

// ExtractTxIndexLE extracts the LE tx input index from the input in a tx
// Returns the tx index as a little endian []byte
func ExtractTxIndexLE(input []byte) []byte {
Expand Down Expand Up @@ -351,12 +344,6 @@ func ExtractMerkleRootLE(header RawHeader) Hash256Digest {
return res
}

// ExtractMerkleRootBE returns the transaction merkle root from a given block header
// The returned merkle root is big-endian
func ExtractMerkleRootBE(header RawHeader) Hash256Digest {
return ReverseHash256Endianness(ExtractMerkleRootLE(header))
}

// ExtractTarget returns the target from a given block hedaer
func ExtractTarget(header RawHeader) sdk.Uint {
// nBits encoding. 3 byte mantissa, 1 byte exponent
Expand Down Expand Up @@ -391,12 +378,6 @@ func ExtractPrevBlockHashLE(header RawHeader) Hash256Digest {
return res
}

// ExtractPrevBlockHashBE returns the previous block's hash from a block header
// Returns the hash as a big endian []byte
func ExtractPrevBlockHashBE(header RawHeader) Hash256Digest {
return ReverseHash256Endianness(ExtractPrevBlockHashLE(header))
}

// ExtractTimestampLE returns the timestamp from a block header
// It returns the timestamp as a little endian []byte
// Time is not 100% reliable
Expand Down
38 changes: 0 additions & 38 deletions golang/btcspv/bitcoin_spv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@ func (suite *UtilsSuite) TestExtractOpReturnData() {
testCase := fixtureError[i]
expected := testCase.ErrorMessage.(string)
actual, err := ExtractOpReturnData(testCase.Input.([]byte))
log.Println(testCase.Input, cap(testCase.Input.([]byte)), actual, err)
suite.Nil(actual)
suite.EqualError(err, expected)
}
Expand Down Expand Up @@ -442,17 +441,6 @@ func (suite *UtilsSuite) TestExtractInputTxIDLE() {
}
}

func (suite *UtilsSuite) TestExtractInputTxID() {
fixture := suite.Fixtures["extractInputTxId"]

for i := range fixture {
testCase := fixture[i]
expected := testCase.Output.(Hash256Digest)
actual := ExtractInputTxID(testCase.Input.([]byte))
suite.Equal(expected, actual)
}
}

func (suite *UtilsSuite) TestExtractTxIndexLE() {
fixture := suite.Fixtures["extractTxIndexLE"]

Expand Down Expand Up @@ -521,17 +509,6 @@ func (suite *UtilsSuite) TestExtractOutputAtIndex() {
suite.EqualError(err, expected)
}

func (suite *UtilsSuite) TestExtractMerkleRootBE() {
fixture := suite.Fixtures["extractMerkleRootBE"]

for i := range fixture {
testCase := fixture[i]
expected := testCase.Output.(Hash256Digest)
actual := ExtractMerkleRootBE(testCase.Input.(RawHeader))
suite.Equal(expected, actual)
}
}

func (suite *UtilsSuite) TestExtractTarget() {
fixture := suite.Fixtures["extractTarget"]

Expand All @@ -553,21 +530,6 @@ func (suite *UtilsSuite) TestExtractTarget() {
}
}

func (suite *UtilsSuite) TestExtractPrevBlockHashBE() {
fixture := suite.Fixtures["retargetAlgorithm"]

for i := range fixture {
testCase := fixture[i]
input := testCase.Input.([]interface{})
for j := range input {
h := input[j].(map[string]interface{})
actual := ExtractPrevBlockHashBE(h["hex"].(RawHeader))
expected := h["prev_block"].(Hash256Digest)
suite.Equal(expected, actual)
}
}
}

func (suite *UtilsSuite) TestExtractTimestamp() {
fixture := suite.Fixtures["extractTimestamp"]

Expand Down
36 changes: 7 additions & 29 deletions golang/btcspv/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,6 @@ type SPVProof struct {
IntermediateNodes HexBytes `json:"intermediate_nodes"`
}

// InputType an enum of types of bitcoin inputs
type InputType int

// possible input types
const (
InputNone InputType = 0
Legacy InputType = 1
Compatibility InputType = 2
Witness InputType = 3
)

// OutputType an enum of types of bitcoin outputs
type OutputType int

// possible output types
const (
OutputNone OutputType = 0
WPKH OutputType = 1
WSH OutputType = 2
OpReturn OutputType = 3
PKH OutputType = 4
SH OutputType = 5
Nonstandard OutputType = 6
)

// NewHash160Digest instantiates a Hash160Digest from a byte slice
func NewHash160Digest(b []byte) (Hash160Digest, error) {
var h Hash160Digest
Expand Down Expand Up @@ -102,16 +77,19 @@ func HeaderFromRaw(raw RawHeader, height uint32) BitcoinHeader {
digestLE := Hash256(raw[:])
digestBE := ReverseHash256Endianness(digestLE)
prevhashLE := ExtractPrevBlockHashLE(raw)
prevhash := ReverseHash256Endianness(prevhashLE)
prevhashBE := ReverseHash256Endianness(prevhashLE)
merkleRootLE := ExtractMerkleRootLE(raw)
merkleRootBE := ReverseHash256Endianness(merkleRootLE)

return BitcoinHeader{
raw,
digestBE,
digestLE,
height,
prevhash,
prevhashBE,
prevhashLE,
ExtractMerkleRootBE(raw),
ExtractMerkleRootLE(raw),
merkleRootBE,
merkleRootLE,
}
}

Expand Down
38 changes: 0 additions & 38 deletions golang/btcspv/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,6 @@ func DecodeIfHex(s string) []byte {
return res
}

// GetOutputType returns the name of the output type associated with the number
func GetOutputType(outputType OutputType) string {
var typeString string
switch outputType {
case OutputNone:
typeString = "Output None"
case WPKH:
typeString = "WPKH"
case WSH:
typeString = "WSH"
case OpReturn:
typeString = "Op Return"
case PKH:
typeString = "PKH"
case SH:
typeString = "SH"
case Nonstandard:
typeString = "Nonstandard"
}
return typeString
}

// GetInputType returns the name of the input type associated with the number
func GetInputType(inputType InputType) string {
var typeString string
switch inputType {
case InputNone:
typeString = "Input None"
case Legacy:
typeString = "Legacy"
case Compatibility:
typeString = "Compatibility"
case Witness:
typeString = "Witness"
}
return typeString
}

// EncodeP2SH turns a scripthash into an address
func EncodeP2SH(sh []byte) (string, error) {
if len(sh) != 20 {
Expand Down
Loading

0 comments on commit 19c07d0

Please sign in to comment.