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

Base 32 Pattern Check #37

Merged
merged 5 commits into from
Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/signature/atomic_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

alice = Addr("6ZHGHH5Z5CTPCF5WCESXMGRSVK7QJETR63M3NY5FJCUYDHO57VTCMJOBGY")
bob = Addr("7Z5PWO2C6LFNQFGHWKSK5H47IQP5OJW2M3HA2QPXTY3WTNP5NU2MHBW27M")
secret = Bytes("base32", "23232323232323")
secret = Bytes("base32", "2323232323232323")
timeout = 3000

def htlc(tmpl_seller=alice,
Expand Down
2 changes: 1 addition & 1 deletion examples/signature/atomic_swap.teal
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ addr 6ZHGHH5Z5CTPCF5WCESXMGRSVK7QJETR63M3NY5FJCUYDHO57VTCMJOBGY
==
arg 0
sha256
byte base32(23232323232323)
byte base32(2323232323232323)
==
&&
txn Receiver
Expand Down
21 changes: 21 additions & 0 deletions pyteal/ast/bytes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,27 @@ def test_bytes_invalid():
with pytest.raises(TealInputError):
Bytes("base32", "Zm9vYmE=")

with pytest.raises(TealInputError):
Bytes("base32", "MFRGG====")

with pytest.raises(TealInputError):
Bytes("base32", "MFRGG==")

with pytest.raises(TealInputError):
Bytes("base32", "CCCCCC==")

with pytest.raises(TealInputError):
Bytes("base32", "CCCCCC")

with pytest.raises(TealInputError):
Bytes("base32", "C=======")

with pytest.raises(TealInputError):
Bytes("base32", "C")

with pytest.raises(TealInputError):
Bytes("base32", "=")

with pytest.raises(TealInputError):
Bytes("base64", "?????")

Expand Down
5 changes: 2 additions & 3 deletions pyteal/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ def valid_address(address:str):
def valid_base32(s:str):
""" check if s is a valid base32 encoding string
"""
noPaddingPattern = re.compile(r'[A-Z2-7]*')
paddingPattern = re.compile(r'^(?:[A-Z2-7]{8})*(?:([A-Z2-7]{8})|([A-Z2-7]{2}[=]{6})|([A-Z2-7]{4}[=]{4})|([A-Z2-7]{5}[=]{3})|([A-Z2-7]{7}[=]{1}))')
pattern = re.compile(r'^(?:[A-Z2-7]{8})*(?:([A-Z2-7]{2}([=]{6})?)|([A-Z2-7]{4}([=]{4})?)|([A-Z2-7]{5}([=]{3})?)|([A-Z2-7]{7}([=]{1})?))?')

if noPaddingPattern.fullmatch(s) is None and paddingPattern.fullmatch(s) is None:
if pattern.fullmatch(s) is None:
raise TealInputError("{} is not a valid RFC 4648 base 32 string".format(s))

def valid_base64(s:str):
Expand Down