Skip to content

Commit

Permalink
Remove pre-Shelly addresses early
Browse files Browse the repository at this point in the history
Exit loops early when working with pre-Shelly addresses. This
might be better positioned in the code when we try to retrieve staking
addresses, but let's see.
  • Loading branch information
ross-spencer committed Nov 27, 2024
1 parent dca2a78 commit e14dbb5
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/simple_sign/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ class ValidTx:
BACKENDS: Final[list] = ["kupo"]


def _check_bech32_prefix(addr: str) -> bool:
"""Enable quick removal of pre-Shelly addresses.
The assumption in this library is that messages will be signed
using a Bech32 encoding and in the short term, there is only
support for `addr` and `stake`. Others might be needed and so we
wrap this capability in this function.
The full list of prefixes is here:
* https://github.com/cardano-foundation/CIPs/tree/master/CIP-0005#specification
"""
if addr.strip().startswith("addr"):
return True
if addr.strip().startswith("stake"):
return True
return False


def _sum_dict(key: str, value: int, accumulator: dict):
"""Increment values in a given dictionary"""
if key not in accumulator:
Expand Down Expand Up @@ -156,6 +176,8 @@ def retrieve_staked_holders(self, token_policy: str, addr: str = None) -> list:
addresses_with_fact = {}
for item in unspent:
unspent_addr = item["address"]
if not _check_bech32_prefix(unspent_addr):
continue
unspent_staking = _get_staking_from_addr(unspent_addr)
if addr and addr not in (unspent_addr, unspent_staking):
# don't process further than we have to if we're only
Expand Down Expand Up @@ -185,6 +207,8 @@ def retrieve_nft_holders(
holders = {}
for item in unspent:
unspent_addr = item["address"]
if not _check_bech32_prefix(unspent_addr):
continue
unspent_staking = _get_staking_from_addr(unspent_addr)
if addr and addr not in (unspent_addr, unspent_staking):
# don't process further than we have to if we're only
Expand Down

0 comments on commit e14dbb5

Please sign in to comment.