def utxos(self): if self._outputs is None: import urllib.request import json url = network('utxo_url').format(address=self.address) req = urllib.request.Request(url) outputs = [] try: with urllib.request.urlopen(req) as resp: data = json.loads(resp.read().decode()) except HTTPError as e: resp = e.read().decode() if resp == 'No free outputs to spend': self._outputs = [] else: raise UpstreamError(resp) else: for item in data['unspent_outputs']: out = Output(value=item['value'], script=hex_to_bytes(item['script'])) out.parent_id = hex_to_bytes(item['tx_hash_big_endian']) out.tx_index = item['tx_output_n'] outputs.append(out) self._outputs = outputs return self._outputs
def send(source, to, fee, private): addr = Address(source) prv_to_addr = private.to_public().to_address(addr.type().value) assert source == prv_to_addr, 'This private key does not correspond to the given address' tx = addr.send(to=to, fee=fee, private=private) assert tx.verify( ), 'Something went wrong, could not verify signed transaction' result = tx.broadcast() if result == 'Transaction Submitted': return bytes_to_hex(tx.txid()[::-1]) raise UpstreamError(result)
def get(cls, txhash): """Construct a transaction from it's tx id by getting the raw data from blockchain.info""" import urllib.request from urllib.error import HTTPError if isinstance(txhash, bytes): txhash = bytes_to_hex(txhash) url = network('rawtx_url').format(txid=txhash) req = urllib.request.Request(url) sleep(0.1) try: with urllib.request.urlopen(req) as resp: try: return cls.from_hex(resp.read().decode()) except SerializationError as e: e.txhash = txhash raise e except HTTPError as e: resp = e.read().decode() raise UpstreamError(resp)