Skip to content

Commit

Permalink
move dhash in helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Feb 27, 2025
1 parent 67c399d commit 4785cc3
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 25 deletions.
4 changes: 2 additions & 2 deletions counterparty-core/counterpartycore/lib/cli/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from counterpartycore.lib.ledger.backendheight import BackendHeight
from counterpartycore.lib.ledger.currentstate import CurrentState
from counterpartycore.lib.parser import blocks, check, follow
from counterpartycore.lib.utils import database
from counterpartycore.lib.utils import database, helpers

logger = logging.getLogger(config.LOGGER_NAME)
D = decimal.Decimal
Expand Down Expand Up @@ -338,7 +338,7 @@ def show_params():
def generate_move_random_hash(move):
move = int(move).to_bytes(2, byteorder="big")
random_bin = os.urandom(16)
move_random_hash_bin = check.dhash(random_bin + move)
move_random_hash_bin = helpers.dhash(random_bin + move)
return binascii.hexlify(random_bin).decode("utf8"), binascii.hexlify(
move_random_hash_bin
).decode("utf8")
5 changes: 3 additions & 2 deletions counterparty-core/counterpartycore/lib/ledger/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from counterpartycore.lib.ledger.balances import get_balance
from counterpartycore.lib.ledger.caches import AssetCache, UTXOBalancesCache
from counterpartycore.lib.ledger.currentstate import ConsensusHashBuilder, CurrentState
from counterpartycore.lib.parser import check, protocol, utxosinfo
from counterpartycore.lib.parser import protocol, utxosinfo
from counterpartycore.lib.utils import helpers


@contextmanager
Expand Down Expand Up @@ -150,7 +151,7 @@ def add_to_journal(db, block_index, command, category, event, bindings):
previous_event_hash,
]
)
event_hash = binascii.hexlify(check.dhash(event_hash_content)).decode("ascii")
event_hash = binascii.hexlify(helpers.dhash(event_hash_content)).decode("ascii")
message_bindings = {
"message_index": message_index,
"block_index": block_index,
Expand Down
14 changes: 1 addition & 13 deletions counterparty-core/counterpartycore/lib/parser/check.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import binascii
import hashlib
import json
import logging

Expand All @@ -9,21 +7,11 @@
from counterpartycore.lib.ledger.currentstate import CurrentState
from counterpartycore.lib.messages.data import checkpoints
from counterpartycore.lib.utils import database
from counterpartycore.lib.utils.helpers import dhash_string

logger = logging.getLogger(config.LOGGER_NAME)


def dhash(text):
if not isinstance(text, bytes):
text = bytes(str(text), "utf-8")

return hashlib.sha256(hashlib.sha256(text).digest()).digest()


def dhash_string(text):
return binascii.hexlify(dhash(text)).decode()


def consensus_hash(db, field, previous_consensus_hash, content):
assert field in ("ledger_hash", "txlist_hash", "messages_hash")

Expand Down
13 changes: 13 additions & 0 deletions counterparty-core/counterpartycore/lib/utils/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import binascii
import decimal
import hashlib
import itertools
import json
import os
Expand Down Expand Up @@ -126,3 +128,14 @@ def is_process_alive(pid):
except OSError:
return False
return True


def dhash(text):
if not isinstance(text, bytes):
text = bytes(str(text), "utf-8")

return hashlib.sha256(hashlib.sha256(text).digest()).digest()


def dhash_string(text):
return binascii.hexlify(dhash(text)).decode()

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ def test_is_valid_tx_hash():
assert not helpers.is_valid_tx_hash(
"3f2c7ccae98af81e44c0ec419659f50d8b7d48c681e5d57fc747d0461e42ddaG"
)


def test_dhash():
assert (
helpers.dhash_string("foobar")
== "3f2c7ccae98af81e44c0ec419659f50d8b7d48c681e5d57fc747d0461e42dda1"
)

0 comments on commit 4785cc3

Please sign in to comment.