Skip to content

Commit

Permalink
deprecate stuffs, prototype SanityCheckProgram
Browse files Browse the repository at this point in the history
  • Loading branch information
ahangsu committed Aug 19, 2022
1 parent c24ecd7 commit e024476
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
4 changes: 2 additions & 2 deletions algosdk/future/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2505,7 +2505,7 @@ class LogicSig:
"""

def __init__(self, program, args=None):
if not program or not logic.check_program(program, args):
if not program or not logic.sanity_check_program(program):
raise error.InvalidProgram()
self.logic = program
self.args = args
Expand Down Expand Up @@ -2548,7 +2548,7 @@ def verify(self, public_key):
return False

try:
logic.check_program(self.logic, self.args)
logic.sanity_check_program(self.logic)
except error.InvalidProgram:
return False

Expand Down
62 changes: 62 additions & 0 deletions algosdk/logic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import binascii
import json
import os

Expand All @@ -12,8 +13,31 @@
opcodes = None


def sanity_check_program(program):
if not program:
raise error.InvalidProgram("empty program")

try:
if base64.encodebytes(base64.b64decode(str(program))) == str(program):
return False
except binascii.Error:
pass

try:
encoding.decode_address(str(program))
return False
except error.WrongChecksumError:
pass
except error.WrongKeyLengthError:
pass

return True


def check_program(program, args=None):
"""
NOTE: This class is deprecated
Performs program checking for max length and cost
Args:
Expand All @@ -31,6 +55,9 @@ def check_program(program, args=None):


def read_program(program, args=None):
"""
NOTE: This class is deprecated
"""
global spec, opcodes
intcblock_opcode = 32
bytecblock_opcode = 38
Expand Down Expand Up @@ -107,11 +134,17 @@ def read_program(program, args=None):


def check_int_const_block(program, pc):
"""
NOTE: This class is deprecated
"""
size, _ = read_int_const_block(program, pc)
return size


def read_int_const_block(program, pc):
"""
NOTE: This class is deprecated
"""
size = 1
ints = []
num_ints, bytes_used = parse_uvarint(program[pc + size :])
Expand All @@ -134,11 +167,17 @@ def read_int_const_block(program, pc):


def check_byte_const_block(program, pc):
"""
NOTE: This class is deprecated
"""
size, _ = read_byte_const_block(program, pc)
return size


def read_byte_const_block(program, pc):
"""
NOTE: This class is deprecated
"""
size = 1
bytearrays = []
num_ints, bytes_used = parse_uvarint(program[pc + size :])
Expand All @@ -164,11 +203,17 @@ def read_byte_const_block(program, pc):


def check_push_int_block(program, pc):
"""
NOTE: This class is deprecated
"""
size, _ = read_push_int_block(program, pc)
return size


def read_push_int_block(program, pc):
"""
NOTE: This class is deprecated
"""
size = 1
single_int, bytes_used = parse_uvarint(program[pc + size :])
if bytes_used <= 0:
Expand All @@ -180,11 +225,17 @@ def read_push_int_block(program, pc):


def check_push_byte_block(program, pc):
"""
NOTE: This class is deprecated
"""
size, _ = read_push_byte_block(program, pc)
return size


def read_push_byte_block(program, pc):
"""
NOTE: This class is deprecated
"""
size = 1
item_len, bytes_used = parse_uvarint(program[pc + size :])
if bytes_used <= 0:
Expand All @@ -200,6 +251,9 @@ def read_push_byte_block(program, pc):


def parse_uvarint(buf):
"""
NOTE: This class is deprecated
"""
x = 0
s = 0
for i, b in enumerate(buf):
Expand All @@ -215,6 +269,8 @@ def parse_uvarint(buf):

def address(program):
"""
NOTE: This class is deprecated
Return the address of the program.
Args:
Expand All @@ -230,6 +286,8 @@ def address(program):

def teal_sign(private_key, data, contract_addr):
"""
NOTE: This class is deprecated
Return the signature suitable for ed25519verify TEAL opcode
Args:
Expand All @@ -254,6 +312,8 @@ def teal_sign(private_key, data, contract_addr):

def teal_sign_from_program(private_key, data, program):
"""
NOTE: This class is deprecated
Return the signature suitable for ed25519verify TEAL opcode
Args:
Expand All @@ -270,6 +330,8 @@ def teal_sign_from_program(private_key, data, program):

def get_application_address(appID: int) -> str:
"""
NOTE: This class is deprecated
Return the escrow address of an application.
Args:
Expand Down
2 changes: 1 addition & 1 deletion algosdk/testing/dryrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def build_dryrun_request(
):
"""
Helper function for creation DryrunRequest object from a program.
By default it uses logic sig mode
By default, it uses logic sig mode
and if app_idx / on_complete are set then application call is made
Args:
Expand Down
4 changes: 2 additions & 2 deletions algosdk/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ class LogicSig:
"""

def __init__(self, program, args=None):
if not program or not logic.check_program(program, args):
if not program or not logic.sanity_check_program(program):
raise error.InvalidProgram()
self.logic = program
self.args = args
Expand Down Expand Up @@ -1361,7 +1361,7 @@ def verify(self, public_key):
return False

try:
logic.check_program(self.logic, self.args)
logic.sanity_check_program(self.logic)
except error.InvalidProgram:
return False

Expand Down

0 comments on commit e024476

Please sign in to comment.