|
| 1 | +"""Recover a revocation registry.""" |
| 2 | + |
| 3 | +import hashlib |
| 4 | +import importlib |
| 5 | +import logging |
| 6 | +import tempfile |
| 7 | +import time |
| 8 | + |
| 9 | +import aiohttp |
| 10 | +import base58 |
| 11 | + |
| 12 | + |
| 13 | +LOGGER = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +""" |
| 17 | +This module calculates a new ledger accumulator, based on the revocation status |
| 18 | +on the ledger vs revocations recorded in the wallet. |
| 19 | +The calculated transaction can be written to the ledger to get the ledger back |
| 20 | +in sync with the wallet. |
| 21 | +This function can be used if there were previous revocation errors (i.e. the |
| 22 | +credential revocation was successfully written to the wallet but the ledger write |
| 23 | +failed.) |
| 24 | +""" |
| 25 | + |
| 26 | + |
| 27 | +class RevocRecoveryException(Exception): |
| 28 | + """Raise exception generating the recovery transaction.""" |
| 29 | + |
| 30 | + |
| 31 | +async def fetch_txns(genesis_txns, registry_id): |
| 32 | + """Fetch tails file and revocation registry information.""" |
| 33 | + |
| 34 | + try: |
| 35 | + vdr_module = importlib.import_module("indy_vdr") |
| 36 | + credx_module = importlib.import_module("indy_credx") |
| 37 | + except Exception as e: |
| 38 | + raise RevocRecoveryException(f"Failed to import library {e}") |
| 39 | + |
| 40 | + pool = await vdr_module.open_pool(transactions=genesis_txns) |
| 41 | + LOGGER.debug("Connected to pool") |
| 42 | + |
| 43 | + LOGGER.debug("Fetch registry: %s", registry_id) |
| 44 | + fetch = vdr_module.ledger.build_get_revoc_reg_def_request(None, registry_id) |
| 45 | + result = await pool.submit_request(fetch) |
| 46 | + if not result["data"]: |
| 47 | + raise RevocRecoveryException(f"Registry definition not found for {registry_id}") |
| 48 | + data = result["data"] |
| 49 | + data["ver"] = "1.0" |
| 50 | + defn = credx_module.RevocationRegistryDefinition.load(data) |
| 51 | + LOGGER.debug("Tails URL: %s", defn.tails_location) |
| 52 | + |
| 53 | + async with aiohttp.ClientSession() as session: |
| 54 | + data = await session.get(defn.tails_location) |
| 55 | + tails_data = await data.read() |
| 56 | + tails_hash = base58.b58encode(hashlib.sha256(tails_data).digest()).decode( |
| 57 | + "utf-8" |
| 58 | + ) |
| 59 | + if tails_hash != defn.tails_hash: |
| 60 | + raise RevocRecoveryException( |
| 61 | + f"Tails hash mismatch {tails_hash} {defn.tails_hash}" |
| 62 | + ) |
| 63 | + else: |
| 64 | + LOGGER.debug("Checked tails hash: %s", tails_hash) |
| 65 | + tails_temp = tempfile.NamedTemporaryFile(delete=False) |
| 66 | + tails_temp.write(tails_data) |
| 67 | + tails_temp.close() |
| 68 | + |
| 69 | + to_timestamp = int(time.time()) |
| 70 | + fetch = vdr_module.ledger.build_get_revoc_reg_delta_request( |
| 71 | + None, registry_id, None, to_timestamp |
| 72 | + ) |
| 73 | + result = await pool.submit_request(fetch) |
| 74 | + if not result["data"]: |
| 75 | + raise RevocRecoveryException("Error fetching delta from ledger") |
| 76 | + |
| 77 | + accum_to = result["data"]["value"]["accum_to"] |
| 78 | + accum_to["ver"] = "1.0" |
| 79 | + delta = credx_module.RevocationRegistryDelta.load(accum_to) |
| 80 | + registry = credx_module.RevocationRegistry.load(accum_to) |
| 81 | + LOGGER.debug("Ledger registry state: %s", registry.to_json()) |
| 82 | + revoked = set(result["data"]["value"]["revoked"]) |
| 83 | + LOGGER.debug("Ledger revoked indexes: %s", revoked) |
| 84 | + |
| 85 | + return defn, registry, delta, revoked, tails_temp |
| 86 | + |
| 87 | + |
| 88 | +async def generate_ledger_rrrecovery_txn(genesis_txns, registry_id, set_revoked): |
| 89 | + """Generate a new ledger accum entry, based on wallet vs ledger revocation state.""" |
| 90 | + |
| 91 | + new_delta = None |
| 92 | + |
| 93 | + ledger_data = await fetch_txns(genesis_txns, registry_id) |
| 94 | + if not ledger_data: |
| 95 | + return new_delta |
| 96 | + defn, registry, delta, prev_revoked, tails_temp = ledger_data |
| 97 | + |
| 98 | + set_revoked = set(set_revoked) |
| 99 | + mismatch = prev_revoked - set_revoked |
| 100 | + if mismatch: |
| 101 | + LOGGER.warn( |
| 102 | + "Credential index(es) revoked on the ledger, but not in wallet: %s", |
| 103 | + mismatch, |
| 104 | + ) |
| 105 | + |
| 106 | + updates = set_revoked - prev_revoked |
| 107 | + if not updates: |
| 108 | + LOGGER.debug("No updates to perform") |
| 109 | + else: |
| 110 | + LOGGER.debug("New revoked indexes: %s", updates) |
| 111 | + |
| 112 | + LOGGER.debug("tails_temp: %s", tails_temp.name) |
| 113 | + update_registry = registry.copy() |
| 114 | + new_delta = update_registry.update(defn, [], updates, tails_temp.name) |
| 115 | + |
| 116 | + LOGGER.debug("New delta:") |
| 117 | + LOGGER.debug(new_delta.to_json()) |
| 118 | + |
| 119 | + return new_delta |
0 commit comments