diff --git a/squeaknode/bitcoin/bitcoin_core_client.py b/squeaknode/bitcoin/bitcoin_core_client.py index 954b06f2..eb6efa7a 100644 --- a/squeaknode/bitcoin/bitcoin_core_client.py +++ b/squeaknode/bitcoin/bitcoin_core_client.py @@ -24,6 +24,7 @@ import logging import os import requests +from bitcoin.core import CBlockHeader from squeaknode.bitcoin.bitcoin_client import BitcoinClient from squeaknode.bitcoin.block_info import BlockInfo @@ -89,7 +90,7 @@ class BitcoinCoreClient(BitcoinClient): logger.debug("Got block_hash: {}".format(block_hash)) return bytes.fromhex(block_hash) - def get_block_header(self, block_hash: bytes, verbose: bool = False) -> bytes: + def get_block_header(self, block_hash: bytes, verbose: bool = False) -> CBlockHeader: payload = { "method": "getblockheader", "params": [block_hash.hex(), verbose], @@ -99,7 +100,8 @@ class BitcoinCoreClient(BitcoinClient): json_response = self.make_request(payload) result = json_response["result"] logger.debug("Got block_header: {}".format(result)) - return bytes.fromhex(result) + header_bytes = bytes.fromhex(result) + return CBlockHeader.deserialize(header_bytes) def make_request(self, payload: dict) -> dict: try: diff --git a/squeaknode/bitcoin/block_info.py b/squeaknode/bitcoin/block_info.py index e8c9fc15..c16c1d71 100644 --- a/squeaknode/bitcoin/block_info.py +++ b/squeaknode/bitcoin/block_info.py @@ -21,9 +21,11 @@ # SOFTWARE. from typing import NamedTuple +from bitcoin.core import CBlockHeader + class BlockInfo(NamedTuple): """Class for getting block info from blockchain.""" block_height: int block_hash: bytes - block_header: bytes + block_header: CBlockHeader diff --git a/squeaknode/bitcoin/util.py b/squeaknode/bitcoin/util.py deleted file mode 100644 index a803b070..00000000 --- a/squeaknode/bitcoin/util.py +++ /dev/null @@ -1,26 +0,0 @@ -# MIT License -# -# Copyright (c) 2020 Jonathan Zernik -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. -from bitcoin.core import CBlockHeader - - -def parse_block_header(header_bytes: bytes) -> CBlockHeader: - return CBlockHeader.deserialize(header_bytes) diff --git a/squeaknode/core/squeak_core.py b/squeaknode/core/squeak_core.py index 4a54ab87..9f5f62e2 100644 --- a/squeaknode/core/squeak_core.py +++ b/squeaknode/core/squeak_core.py @@ -30,7 +30,6 @@ from squeak.core import CSqueak from squeak.core.signing import CSigningKey from squeaknode.bitcoin.bitcoin_client import BitcoinClient -from squeaknode.bitcoin.util import parse_block_header from squeaknode.core.exception import InvoiceSubscriptionError from squeaknode.core.lightning_address import LightningAddressHostPort from squeaknode.core.offer import Offer @@ -125,7 +124,7 @@ class SqueakCore: squeak.nBlockHeight) if squeak.hashBlock != block_info.block_hash: raise Exception("Block hash incorrect.") - return parse_block_header(block_info.block_header) + return block_info.block_header def get_decrypted_content(self, squeak: CSqueak, secret_key: bytes) -> str: """Checks if the secret key is valid for the given squeak and returns diff --git a/tests/bitcoin/test_bitcoin_core_client.py b/tests/bitcoin/test_bitcoin_core_client.py index 2283d444..6efaedfd 100644 --- a/tests/bitcoin/test_bitcoin_core_client.py +++ b/tests/bitcoin/test_bitcoin_core_client.py @@ -21,6 +21,7 @@ # SOFTWARE. import mock import pytest +from bitcoin.core import CBlockHeader from requests import HTTPError from requests.exceptions import ConnectionError from requests.exceptions import RequestException @@ -66,10 +67,15 @@ def block_header_str(): @pytest.fixture -def block_header(block_header_str): +def block_header_bytes(block_header_str): yield bytes.fromhex(block_header_str) +@pytest.fixture +def block_header(block_header_bytes): + yield CBlockHeader.deserialize(block_header_bytes) + + @pytest.fixture def block_info(block_count, block_hash, block_header): yield BlockInfo(