Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#256Create Bitget #90

Closed
wants to merge 1 commit into from
Closed

Conversation

Allenqel
Copy link

import hashlib

def sha256_hash(data):
"""Hashes the input data using SHA-256."""
return hashlib.sha256(data.encode('utf-8')).hexdigest()

def build_merkle_root(leaf_hashes):
"""Builds the Merkle root from a list of leaf hashes."""
if len(leaf_hashes) == 1:
return leaf_hashes[0] # Root reached

# If odd number of leaves, duplicate the last one
if len(leaf_hashes) % 2 != 0:
    leaf_hashes.append(leaf_hashes[-1])

# Pairwise hash leaves
parent_hashes = []
for i in range(0, len(leaf_hashes), 2):
    combined_hash = sha256_hash(leaf_hashes[i] + leaf_hashes[i + 1])
    parent_hashes.append(combined_hash)

return build_merkle_root(parent_hashes)

Provided Merkle leaves (from the data)

merkle_leaves = [
"8cf0243a2c76fe0b", # Path 1
"8306844dff98ba79", # Path 2
"94d0d60f7cdce5fe", # Path 3
"cb575fb1eb6462f9", # Self
]

Compute Merkle Root

calculated_merkle_root = build_merkle_root(merkle_leaves) print(f"Calculated Merkle Root: {calculated_merkle_root}")

import hashlib

def sha256_hash(data):
    """Hashes the input data using SHA-256."""
    return hashlib.sha256(data.encode('utf-8')).hexdigest()

def build_merkle_root(leaf_hashes):
    """Builds the Merkle root from a list of leaf hashes."""
    if len(leaf_hashes) == 1:
        return leaf_hashes[0]  # Root reached

    # If odd number of leaves, duplicate the last one
    if len(leaf_hashes) % 2 != 0:
        leaf_hashes.append(leaf_hashes[-1])

    # Pairwise hash leaves
    parent_hashes = []
    for i in range(0, len(leaf_hashes), 2):
        combined_hash = sha256_hash(leaf_hashes[i] + leaf_hashes[i + 1])
        parent_hashes.append(combined_hash)

    return build_merkle_root(parent_hashes)

# Provided Merkle leaves (from the data)
merkle_leaves = [
    "8cf0243a2c76fe0b",  # Path 1
    "8306844dff98ba79",  # Path 2
    "94d0d60f7cdce5fe",  # Path 3
    "cb575fb1eb6462f9",  # Self
]

# Compute Merkle Root
calculated_merkle_root = build_merkle_root(merkle_leaves)
print(f"Calculated Merkle Root: {calculated_merkle_root}")
@Allenqel
Copy link
Author

#90

@Saingsophea
Copy link

If odd number of leaves, duplicate the last one

if len(leaf_hashes) % 2 != 0:
leaf_hashes.append(leaf_hashes[-1])

Pairwise hash leaves

parent_hashes = []
for i in range(0, len(leaf_hashes), 2):
combined_hash = sha256_hash(leaf_hashes[i] + leaf_hashes[i + 1])
parent_hashes.append(combined_hash)

return build_merkle_root(parent_hashes)

@Allenqel Allenqel changed the base branch from main to feature/v1.0.3 December 14, 2024 02:12
@Saingsophea
Copy link

@Saingsophea

@juliehtet
Copy link

juliehtet commented Dec 24, 2024 via email

@Allenqel Allenqel closed this Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants