예제 #1
0
    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
예제 #2
0
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)
예제 #3
0
 def get_tx(self, txid):
     url = self._get_url(self.RAW_TX_URLS)
     req = request.Request(url.format(txid=txid))
     
     try:
         with request.urlopen(req) as resp:
             return resp.read().decode()
     except HTTPError as e:
         resp = e.read().decode()
         raise UpstreamError(resp)
예제 #4
0
    def get_utxos(self, address: str) -> List['Output']:
        from cryptotools.BTC.transaction import Output

        response = self.rpc_call('scantxoutset', ['start', [f"addr({address})"]])['result']
        
        if not response['success']:
            raise UpstreamError(response['error'])
        outs = response['unspents']
        outputs = []
        for out in outs:
            sats = btc_to_satoshi(out['amount'])
            outputs.append(Output(value=sats, script=hex_to_bytes(out['scriptPubKey'])))
        return outputs
예제 #5
0
    def broadcast(self, rawtx: str):

        url = self._get_url(self.BROADCAST_URLS)

        data = rawtx.encode('ascii')
        req = request.Request(url, data)

        try:
            with request.urlopen(req) as response:
                resp = response.read()
        except HTTPError as e:
            resp = e.read().decode()
            raise UpstreamError(resp)
        
        return True
예제 #6
0
    def rpc_call(self, method: str, params: list = None):
        payload = {
                "jsonrpc": "2.0",
                "method": method,
                "params": params,
            }

        data = json.dumps(payload).encode()
        req = request.Request(self.url, data=data, headers=self.headers)
        try:
            with request.urlopen(req) as resp:
                return json.loads(resp.read().decode())
        except HTTPError as e:
            error = json.loads(e.read().decode())
            raise UpstreamError(error['error']['message'])
예제 #7
0
    def get_utxos(self, address: str):
        from cryptotools.BTC.transaction import Output

        url = self._get_url(self.UTXO_URLS)
        req = request.Request(url.format(address=address))
        outputs = []
        try:
            with request.urlopen(req) as resp:
                data = json.loads(resp.read().decode())
        except HTTPError as e:
            resp = e.read().decode()
            raise UpstreamError(resp)
        else:
            for item in data:
                out = Output(value=item['value'], script=b"")
                out.parent_id = hex_to_bytes(item['txid'])
                out.tx_index = item['vout']
                outputs.append(out)
            return outputs
예제 #8
0
    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)
예제 #9
0
    def get_utxos(self, address):
        from cryptotools.BTC.transaction import Output

        url = self._get_url(self.UTXO_URLS)
        req = request.Request(url.format(address=address))
        outputs = []
        try:
            with 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':
                return []
            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)
            return outputs