From 4e8d5cf6bfde5f36740a8d1ce819b6bc63999644 Mon Sep 17 00:00:00 2001 From: Ursify <35331253+Ursify@users.noreply.github.com> Date: Sun, 6 Dec 2020 00:45:15 +1100 Subject: [PATCH] Base 32 Pattern Check (#37) * 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 --- examples/signature/atomic_swap.py | 2 +- examples/signature/atomic_swap.teal | 2 +- pyteal/ast/bytes_test.py | 21 +++++++++++++++++++++ pyteal/types.py | 5 ++--- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/examples/signature/atomic_swap.py b/examples/signature/atomic_swap.py index 14b7f2140..5b9bc6c57 100644 --- a/examples/signature/atomic_swap.py +++ b/examples/signature/atomic_swap.py @@ -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, diff --git a/examples/signature/atomic_swap.teal b/examples/signature/atomic_swap.teal index 3cb7377fe..762e0250d 100644 --- a/examples/signature/atomic_swap.teal +++ b/examples/signature/atomic_swap.teal @@ -19,7 +19,7 @@ addr 6ZHGHH5Z5CTPCF5WCESXMGRSVK7QJETR63M3NY5FJCUYDHO57VTCMJOBGY == arg 0 sha256 -byte base32(23232323232323) +byte base32(2323232323232323) == && txn Receiver diff --git a/pyteal/ast/bytes_test.py b/pyteal/ast/bytes_test.py index 3a3c19cec..6126eb3d3 100644 --- a/pyteal/ast/bytes_test.py +++ b/pyteal/ast/bytes_test.py @@ -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", "?????") diff --git a/pyteal/types.py b/pyteal/types.py index 08bf1ec66..c9f95cbc6 100644 --- a/pyteal/types.py +++ b/pyteal/types.py @@ -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):