def test_valid_flow_control(self): valid_tests = ( ('1 1', 'IF IF 1 ELSE 0 ENDIF ENDIF'), ('1 0', 'IF IF 1 ELSE 0 ENDIF ENDIF'), ('1 1', 'IF IF 1 ELSE 0 ENDIF ELSE IF 0 ELSE 1 ENDIF ENDIF'), ('0 0', 'IF IF 1 ELSE 0 ENDIF ELSE IF 0 ELSE 1 ENDIF ENDIF'), ('1 0', 'NOTIF IF 1 ELSE 0 ENDIF ENDIF'), ('1 1', 'NOTIF IF 1 ELSE 0 ENDIF ENDIF'), ('1 0', 'NOTIF IF 1 ELSE 0 ENDIF ELSE IF 0 ELSE 1 ENDIF ENDIF'), ('0 1', 'NOTIF IF 1 ELSE 0 ENDIF ELSE IF 0 ELSE 1 ENDIF ENDIF'), ) valid_scripts = [] for script_sig, script_pubkey in valid_tests: script_sig, _ = transform_human(script_sig) script_pubkey, _ = transform_human(script_pubkey) script_sig = Script.from_human(script_sig) script_pubkey = Script.from_human(script_pubkey) valid_scripts.append((script_sig, script_pubkey)) execution = ScriptExecution() for script_sig, script_pubkey in valid_scripts: tx = build_spending_tx(script_sig, build_crediting_tx(script_pubkey)) _ = execution.evaluate(script_pubkey, txTo=tx, inIdx=0) self.assertTrue(execution.script_passed) self.assertTrue(execution.script_verified)
def test_invalid_flow_control(self): invalid_tests = ( ('0 1', 'IF IF 1 ELSE 0 ENDIF ENDIF'), ('0 0', 'IF IF 1 ELSE 0 ENDIF ENDIF'), ('1 0', 'IF IF 1 ELSE 0 ENDIF ELSE IF 0 ELSE 1 ENDIF ENDIF'), ('0 1', 'IF IF 1 ELSE 0 ENDIF ELSE IF 0 ELSE 1 ENDIF ENDIF'), ('0 0', 'NOTIF IF 1 ELSE 0 ENDIF ENDIF'), ('0 1', 'NOTIF IF 1 ELSE 0 ENDIF ENDIF'), ('1 1', 'NOTIF IF 1 ELSE 0 ENDIF ELSE IF 0 ELSE 1 ENDIF ENDIF'), ('0 0', 'NOTIF IF 1 ELSE 0 ENDIF ELSE IF 0 ELSE 1 ENDIF ENDIF'), ) invalid_scripts = [] for script_sig, script_pubkey in invalid_tests: script_sig, _ = transform_human(script_sig) script_pubkey, _ = transform_human(script_pubkey) script_sig = Script.from_human(script_sig) script_pubkey = Script.from_human(script_pubkey) invalid_scripts.append((script_sig, script_pubkey)) execution = ScriptExecution() for script_sig, script_pubkey in invalid_scripts: tx = build_spending_tx(script_sig, build_crediting_tx(script_pubkey)) _ = execution.evaluate(script_pubkey, txTo=tx, inIdx=0) self.assertFalse(execution.script_passed) self.assertFalse(execution.script_verified)
def test_hex_transform(self): hex_tests = [('5', '0x05'), ('0x20', '0x20'), ('1 2 0x89 3', '0x01 0x02 0x89 0x03'), ('1 0x2 0x89 3', '0x01 0x02 0x89 0x03')] for text, expected in hex_tests: result, _ = transform_human(text) self.assertEqual(expected, result)
def test_opcode_transform(self): ops_tests = [ ('ADD', 'OP_ADD'), ('0x2 0x5 DUP', '0x02 0x05 OP_DUP'), ('1 2 ADD', '0x01 0x02 OP_ADD') ] for text, expected in ops_tests: result, _ = transform_human(text) self.assertEqual(expected, result)
def test_hex_transform(self): hex_tests = [ ('5', '0x05'), ('0x20', '0x20'), ('1 2 0x89 3', '0x01 0x02 0x89 0x03') ] for text, expected in hex_tests: result, _ = transform_human(text) self.assertEqual(expected, result)
def set_data(self, text, fmt): script = None if fmt == 'Hex' and len(text) % 2 == 0: try: script = Script(text.decode('hex')) except Exception: pass elif fmt == 'Human': txt, self.context = transform_human(text) script = Script.from_human(txt) self.script = script
def test_string_literal_transform_and_instantiate_script(self): str_tests = ( ('"a"', '"a"', '0161'), ('2 "2"', '0x02 "2"', '01020132'), ('"1 2"', '"1 2"', '03312032'), ('0 "1 2"', '0x00 "1 2"', '010003312032'), ('0 "1 2" 3', '0x00 "1 2" 0x03', '0100033120320103'), ('"2 3 4"', '"2 3 4"', '053220332034'), ('1 "2 3 4" 5', '0x01 "2 3 4" 0x05', '01010532203320340105'), ) for text, expected, expected_hex in str_tests: txt, _ = transform_human(text) self.assertEqual(expected, txt) s = Script.from_human(txt) self.assertEqual(expected_hex, s.get_hex())
def test_string_literal_transform_and_instantiate_script(self): str_tests = ( ('"a"', '"a"', '0161'), ('2 "2"', '0x02 "2"', '01020132'), ('"1 2"', '"1 2"', '03312032'), ('0 "1 2"', '0x00 "1 2"', '010003312032'), ('0 "1 2" 3', '0x00 "1 2" 0x03', '0100033120320103'), ('"2 3 4"', '"2 3 4"', '053220332034'), ('1 "2 3 4" 5', '0x01 "2 3 4" 0x05','01010532203320340105'), ) for text, expected, expected_hex in str_tests: txt, _ = transform_human(text) self.assertEqual(expected, txt) s = Script.from_human(txt) self.assertEqual(expected_hex, s.get_hex())
def test_transform_human(self): variables = { 'numberOne': '0x01', 'stringOne': '"1"', } human_tests = [ ('0x02 "test" 0x03', '010204746573740103'), ('$numberOne 0x01', '01010101'), ('0x10 $stringOne 0x11', '011001310111'), # nonexistent variable ('$one 0x05', '04246f6e650105') ] for text, expected_hex in human_tests: txt, _ = transform_human(text, variables) s = Script.from_human(txt) self.assertEqual(s.get_hex(), expected_hex)
def test_variable_transform(self): variables = {'seven': '0x7'} scr = '$seven 0x07 OP_EQUAL' self.assertEqual('0x07 0x07 OP_EQUAL', transform_human(scr, variables)[0])
def test_opcode_transform(self): ops_tests = [('ADD', 'OP_ADD'), ('0x2 0x5 DUP', '0x02 0x05 OP_DUP'), ('1 2 ADD', '0x01 0x02 OP_ADD')] for text, expected in ops_tests: result, _ = transform_human(text) self.assertEqual(expected, result)
def transform_human_script(text, main_window): """Transform user input into something Script can read. Main window is needed for tool integration.""" variables = main_window.plugin_handler.get_plugin('Variables').dock.data return transform_human(text, variables)
def transform_human_script(text, main_window): """Transform user input into something Script can read. Main window is needed for tool integration.""" variables = main_window.plugin_handler.get_plugin('Variables').ui.data return transform_human(text, variables)