diff src/bitcaviar_plus/helpers.py @ 12:6a0a8cce058e

refactor code
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Tue, 26 Oct 2021 09:38:38 +0200
parents src/puppy/helpers.py@6e9f420c117e
children 30535f42d0ff
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/bitcaviar_plus/helpers.py	Tue Oct 26 09:38:38 2021 +0200
@@ -0,0 +1,41 @@
+import hashlib
+
+"""
+Helper methods
+"""
+
+
+def __get_var_int(f):
+    """
+    A VarInt (variable integer) is a field used in transaction data to indicate the number of upcoming fields,
+    or the length of an upcoming field.
+    More info: https://learnmeabitcoin.com/technical/varint
+    :param f: buffer, required
+    :return: string
+    """
+
+    prefix = f.read(1).hex()
+
+    if int(prefix, 16) == 253:
+        number_of_transactions = f.read(2)[::-1].hex()
+    elif int(prefix, 16) == 254:
+        number_of_transactions = f.read(4)[::-1].hex()
+    elif int(prefix, 16) == 255:
+        number_of_transactions = f.read(8)[::-1].hex()
+    else:
+        number_of_transactions = prefix
+
+    return number_of_transactions
+
+
+def __compute_hash(data):
+    """
+    Get hash
+    :param data: bytes, required
+    :return: string
+    """
+
+    h = hashlib.sha256(data).digest()
+    h = hashlib.sha256(h).digest()
+
+    return h[::-1].hex()