Skip to content

Idea: Adding a checksum to verify the integrity of each record in the BIP-329 .jsonl file. #7

@xavierfiechter

Description

@xavierfiechter
import json
import hashlib

def calculate_checksum(record):
    # Exclude the checksum field itself
    record_without_checksum = {k: v for k, v in record.items() if k != "checksum"}
    # Sort keys to ensure deterministic order
    serialized_record = json.dumps(record_without_checksum, sort_keys=True)
    # Compute SHA-256 hash
    return hashlib.sha256(serialized_record.encode('utf-8')).hexdigest()

# Example usage
record = { 
    "type": "output", 
    "ref": "f91d0a8a78462bc59398f2c5d7a84fcff491c26ba54c4833478b202796c8aafd:1", 
    "label": "Output", 
    "spendable": "false" 
}

# Add the checksum to the record
record["checksum"] = calculate_checksum(record)

# Simulate a change to the record
modified_record = { 
    "type": "output", 
    "ref": "f91d0a8a78462bc59398f2c5d7a84fcff491c26ba54c4833478b202796c8aafd:1", 
    "label": "Outp", # simulate truncated data 
    "spendable": "false" 
}

# Check if the modified record's checksum matches
is_changed = record.get("checksum") != calculate_checksum(modified_record)

print("Record has", "changed" if is_changed else "not changed")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions