| 28 | |
| 29 | @classmethod |
| 30 | def fetch(cls, tx_id, testnet=False, fresh=False): |
| 31 | if fresh or (tx_id not in cls.cache): |
| 32 | url = '{}/tx/{}/hex'.format(cls.get_url(testnet), tx_id) |
| 33 | response = requests.get(url) |
| 34 | try: |
| 35 | raw = bytes.fromhex(response.text.strip()) |
| 36 | except ValueError: |
| 37 | raise ValueError('unexpected response: {}'.format(response.text)) |
| 38 | tx = Tx.parse(BytesIO(raw), testnet=testnet) |
| 39 | # make sure the tx we got matches to the hash we requested |
| 40 | if tx.segwit: |
| 41 | computed = tx.id() |
| 42 | else: |
| 43 | computed = hash256(raw)[::-1].hex() |
| 44 | if computed != tx_id: |
| 45 | raise RuntimeError('server lied: {} vs {}'.format(computed, tx_id)) |
| 46 | cls.cache[tx_id] = tx |
| 47 | cls.cache[tx_id].testnet = testnet |
| 48 | return cls.cache[tx_id] |
| 49 | |
| 50 | @classmethod |
| 51 | def load_cache(cls, filename): |