obelisk

Electrum server using libbitcoin as its backend
git clone https://git.parazyd.org/obelisk
Log | Files | Refs | README | LICENSE

commit 818cd111a50530edc1e2661432a67e52fd16f70e
parent 97e95d1f84dfaeaeb70230b0db5676308e51557d
Author: parazyd <parazyd@dyne.org>
Date:   Fri,  9 Apr 2021 01:40:26 +0200

Merge hashes.py with util.py

Diffstat:
Delectrumobelisk/hashes.py | 37-------------------------------------
Melectrumobelisk/merkle.py | 2+-
Melectrumobelisk/protocol.py | 4+++-
Melectrumobelisk/util.py | 20++++++++++++++++++++
4 files changed, 24 insertions(+), 39 deletions(-)

diff --git a/electrumobelisk/hashes.py b/electrumobelisk/hashes.py @@ -1,37 +0,0 @@ -#!/usr/bin/env python3 -# Copyright (C) 2020-2021 Ivan J. <parazyd@dyne.org> -# -# This file is part of obelisk -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License version 3 -# as published by the Free Software Foundation. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. -""" Cryptographic hash functions and helpers """ -import hashlib - -_sha256 = hashlib.sha256 - - -def sha256(inp): - """ Simple wrapper of hashlib sha256. """ - return _sha256(inp).digest() - - -def double_sha256(inp): - """ sha256 of sha256, as used extensively in bitcoin """ - return sha256(sha256(inp)) - - -def hash_to_hex_str(inp): - """Convert a big-endian binary hash to displayed hex string. - Display form of a binary hash is reversed and converted to hex. - """ - return bytes(reversed(inp)).hex() diff --git a/electrumobelisk/merkle.py b/electrumobelisk/merkle.py @@ -17,7 +17,7 @@ """Module for calculating merkle branches""" from math import ceil, log -from electrumobelisk.hashes import double_sha256 +from electrumobelisk.util import double_sha256 def branch_length(hash_count): diff --git a/electrumobelisk/protocol.py b/electrumobelisk/protocol.py @@ -21,7 +21,6 @@ import asyncio import json from binascii import unhexlify -from electrumobelisk.hashes import sha256, double_sha256, hash_to_hex_str from electrumobelisk.merkle import merkle_branch from electrumobelisk.util import ( block_to_header, @@ -30,6 +29,9 @@ from electrumobelisk.util import ( is_hex_str, is_non_negative_integer, safe_hexlify, + sha256, + double_sha256, + hash_to_hex_str, ) from electrumobelisk.zeromq import Client diff --git a/electrumobelisk/util.py b/electrumobelisk/util.py @@ -15,8 +15,11 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. """Utility functions""" +import hashlib from binascii import hexlify +_sha256 = hashlib.sha256 + def is_integer(val): """Check if val is of type int""" @@ -87,3 +90,20 @@ def block_to_header(block): # bits = block_header[72:76] # nonce = block_header[76:80] return block_header + + +def sha256(inp): + """ Simple wrapper of hashlib sha256. """ + return _sha256(inp).digest() + + +def double_sha256(inp): + """ sha256 of sha256, as used extensively in bitcoin """ + return sha256(sha256(inp)) + + +def hash_to_hex_str(inp): + """Convert a big-endian binary hash to displayed hex string. + Display form of a binary hash is reversed and converted to hex. + """ + return bytes(reversed(inp)).hex()