Skip to content

Commit

Permalink
Base 32 Pattern Check (#37)
Browse files Browse the repository at this point in the history
* Base 32 Pattern Check

BASE 32 works in groups of 8 Characters.
Valid Final 8 Characters(with / without padding):
CC======
CC
CCCC====
CCCC
CCCCC===
CCCCC
CCCCCCC=
CCCCCCC
CCCCCCCC
Invalid Final 8 Characters(with / without padding):
C=======
C
CCCCCC==
CCCCCC

Invalid 8 characters group should throw an error, since it won't be possible to uncoded them back.

* Allow empty string & update tests

* Add tests for incorrect padding

* Fix on Empty String

Empty string was not raising TealInputError.

* Allowing Empty strings

Sorry about that, first I understood that the issue was that empty strings were allowed. I rolled it back.

Co-authored-by: Jason Paulos <jasonpaulos@users.noreply.github.com>
  • Loading branch information
Ursify and jasonpaulos committed Mar 1, 2021
1 parent 158202a commit 4e8d5cf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
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 @@ -86,6 +86,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

0 comments on commit 4e8d5cf

Please sign in to comment.