diff --git a/.github/workflows/erdpy.yml b/.github/workflows/erdpy.yml index 20501b86..f2119145 100644 --- a/.github/workflows/erdpy.yml +++ b/.github/workflows/erdpy.yml @@ -49,6 +49,11 @@ jobs: python3 -m pip install --upgrade pip pip3 install -r requirements.txt pip3 install pytest + - name: Install libtinfo5 + if: ${{ matrix.os != 'macos-latest' }} + run: | + sudo apt update + sudo apt install -y libtinfo5 - name: Set github_api_token run: | mkdir ~/elrondsdk diff --git a/erdpy/CHANGELOG.md b/erdpy/CHANGELOG.md index dc4d1cc4..f5a5a364 100644 --- a/erdpy/CHANGELOG.md +++ b/erdpy/CHANGELOG.md @@ -7,6 +7,9 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how ## [Unreleased] - TBD +## [2.0.5] + - [Prepare erdpy for accepting any address hrp](https://github.com/ElrondNetwork/elrond-sdk-erdpy/pull/158) + ## [2.0.4] - Fix resolving latest release of Github repositories diff --git a/erdpy/accounts.py b/erdpy/accounts.py index e3d4ce5b..1f968b98 100644 --- a/erdpy/accounts.py +++ b/erdpy/accounts.py @@ -1,6 +1,6 @@ import logging from pathlib import Path -from typing import Any, Optional +from typing import Any, Optional, Tuple import nacl.signing @@ -80,14 +80,15 @@ def sign_transaction(self, transaction: ITransaction) -> str: class Address(IAddress): - HRP = "erd" + # Default hrp value is "erd" + DEFAULT_HRP = "erd" PUBKEY_LENGTH = 32 PUBKEY_STRING_LENGTH = PUBKEY_LENGTH * 2 # hex-encoded BECH32_LENGTH = 62 - _value_hex: str - def __init__(self, value: Any): + def __init__(self, value: Any, hrp: str = DEFAULT_HRP): self._value_hex = '' + self.hrp = hrp if not value: return @@ -102,7 +103,9 @@ def __init__(self, value: Any): elif len(value) == Address.PUBKEY_STRING_LENGTH: self._value_hex = _as_string(value) elif len(value) == Address.BECH32_LENGTH: - self._value_hex = _decode_bech32(value).hex() + hrp, value_hex = _decode_bech32(value) + self.hrp = hrp + self._value_hex = value_hex.hex() else: raise errors.BadAddressFormatError(value) @@ -113,7 +116,7 @@ def hex(self) -> str: def bech32(self) -> str: self._assert_validity() pubkey = self.pubkey() - b32 = bech32.bech32_encode(self.HRP, bech32.convertbits(pubkey, 8, 5)) + b32 = bech32.bech32_encode(self.hrp, bech32.convertbits(pubkey, 8, 5)) assert isinstance(b32, str) return b32 @@ -143,10 +146,8 @@ def _as_string(value): return value.decode("utf-8") -def _decode_bech32(value): +def _decode_bech32(value) -> Tuple[str, bytes]: bech32_string = _as_string(value) hrp, value_bytes = bech32.bech32_decode(bech32_string) - if hrp != Address.HRP: - raise errors.BadAddressFormatError(value) decoded_bytes = bech32.convertbits(value_bytes, 5, 8, False) - return bytearray(decoded_bytes) + return hrp, bytearray(decoded_bytes)