def test_spend_block(self): block = self.unspent_blocks[0] address = 'HNXsVtRUmwDCtpcCJUrH4QiHo9kUKx199A' script = create_base_script(address).get_script() resp = (yield self.web.post('create_tx', { 'inputs': [ { 'tx_id': block.hash_hex, 'index': 0, } ], 'outputs': [ { 'address': address, 'value': 6400, } ] })).json_value() self.assertEqual(resp['success'], True) data = resp['data'] hex_data = resp['hex_data'] struct_bytes = bytes.fromhex(hex_data) tx = Transaction.create_from_struct(struct_bytes) tx_data = tx.to_json() del tx_data['hash'] del tx_data['nonce'] self.assertEqual(data, tx_data) self.assertEqual(len(tx.inputs), 1) self.assertEqual(tx.inputs[0].tx_id, block.hash) self.assertEqual(tx.inputs[0].index, 0) self.assertEqual(tx.inputs[0].data, b'') self.assertEqual(len(tx.outputs), 1) self.assertEqual(tx.outputs[0].value, 6400) self.assertEqual(tx.outputs[0].token_data, 0) self.assertEqual(tx.outputs[0].script, script)
def test_spend_tx_by_script(self): src_tx = self.unspent_tx address = 'HNXsVtRUmwDCtpcCJUrH4QiHo9kUKx199A' script = create_base_script(address).get_script() script_str = base64.b64encode(script).decode('utf-8') resp = (yield self.web.post( 'create_tx', { 'inputs': [{ 'tx_id': src_tx.hash_hex, 'index': 1, }], 'outputs': [{ 'script': script_str, 'value': 100, }] })).json_value() self.assertEqual(resp['success'], True) data = resp['data'] hex_data = resp['hex_data'] struct_bytes = bytes.fromhex(hex_data) tx = Transaction.create_from_struct(struct_bytes) tx_data = tx.to_json() del tx_data['hash'] del tx_data['nonce'] self.assertEqual(data, tx_data) self.assertEqual(len(tx.inputs), 1) self.assertEqual(tx.inputs[0].tx_id, src_tx.hash) self.assertEqual(tx.inputs[0].index, 1) self.assertEqual(tx.inputs[0].data, b'') self.assertEqual(len(tx.outputs), 1) self.assertEqual(tx.outputs[0].value, 100) self.assertEqual(tx.outputs[0].token_data, 0) self.assertEqual(tx.outputs[0].script, script)
def test_multisig_base_script(self): import base58 addrs = [ 'hJBjKVU5HqfbNbikgfdhDmmBe9TMwx9Eft', ] for addr in addrs: script = create_base_script(addr) self.assertIsInstance(script, MultiSig) self.assertEqual(script.get_type(), 'MultiSig') self.assertEqual(script.get_address(), addr) self.assertEqual(script.get_timelock(), None) baddress = base58.b58decode(addr) script2 = create_output_script(baddress) self.assertEqual(script2, script.get_script())
def test_p2pkh_base_script(self): import base58 addrs = [ 'HNXsVtRUmwDCtpcCJUrH4QiHo9kUKx199A', 'HGov979VaeyMQ92ubYcnVooP6qPzUJU8Ro', ] for addr in addrs: script = create_base_script(addr) self.assertIsInstance(script, P2PKH) self.assertEqual(script.get_type(), 'P2PKH') self.assertEqual(script.get_address(), addr) self.assertEqual(script.get_timelock(), None) baddress = base58.b58decode(addr) script2 = create_output_script(baddress) self.assertEqual(script2, script.get_script())
def render_GET(self, request): """ Get request /validate_address/<address> that returns a script if address is valid. """ request.setHeader(b'content-type', b'application/json; charset=utf-8') set_cors(request, 'GET') try: base_script = create_base_script(self.address) except Exception as e: ret = { 'valid': False, 'error': type(e).__name__, 'msg': str(e), } else: ret = { 'valid': True, 'script': base64.b64encode(base_script.get_script()).decode('ascii'), 'address': base_script.get_address(), 'type': base_script.get_type().lower(), } return json_dumpb(ret)