| 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 | # make sure the tx we got matches to the hash we requested |
| 39 | if raw[4] == 0: |
| 40 | raw = raw[:4] + raw[6:] |
| 41 | tx = Tx.parse(BytesIO(raw), testnet=testnet) |
| 42 | tx.locktime = little_endian_to_int(raw[-4:]) |
| 43 | else: |
| 44 | tx = Tx.parse(BytesIO(raw), testnet=testnet) |
| 45 | if tx.id() != tx_id: |
| 46 | raise ValueError('not the same id: {} vs {}'.format(tx.id(), tx_id)) |
| 47 | cls.cache[tx_id] = tx |
| 48 | cls.cache[tx_id].testnet = testnet |
| 49 | return cls.cache[tx_id] |
| 50 | |
| 51 | @classmethod |
| 52 | def load_cache(cls, filename): |