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

Pubkey converter refactor #158

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .github/workflows/erdpy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions erdpy/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 11 additions & 10 deletions erdpy/accounts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from pathlib import Path
from typing import Any, Optional
from typing import Any, Optional, Tuple

import nacl.signing

Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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

Expand Down Expand Up @@ -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)