def parse_tx(arg, parser, tx_db, network): # hex transaction id tx = None if TX_ID_RE.match(arg): if tx_db is None: tx_db = create_tx_db(network) tx = tx_db.get(h2b_rev(arg)) if not tx: parser.error("can't find Tx with id %s" % arg) return tx, tx_db # hex transaction data try: return Tx.from_hex(arg), tx_db except Exception: pass if os.path.exists(arg): try: with open(arg, "rb") as f: if f.name.endswith("hex"): f = io.BytesIO(codecs.getreader("hex_codec")(f).read()) tx = Tx.parse(f) tx.parse_unspents(f) except Exception: pass return tx, tx_db
def tx_for_tx_hash(self, tx_hash): """ returns the pycoin_grs.tx object for tx_hash """ try: url_append = "?token=%s&includeHex=true" % self.api_key url = self.base_url("txs/%s%s" % (b2h_rev(tx_hash), url_append)) result = json.loads(urlopen(url).read().decode("utf8")) tx = Tx.parse(io.BytesIO(h2b(result.get("hex")))) return tx except: raise Exception
def get(self, key): for path in self.paths_for_hash(key): try: tx = Tx.parse(open(path, "rb")) if tx and tx.hash() == key: return tx except IOError: pass for method in self.lookup_methods: try: tx = method(key) if tx and tx.hash() == key: self.put(tx) return tx except Exception: pass return None
def test_validate_multisig(self): # this is a transaction in the block chain # the unspents are included too, so it can be validated f = io.BytesIO( h2b("01000000025718fb915fb8b3a802bb699ddf04dd91261ef6715f5f2820a2b1b9b7e38b" "4f27000000004a004830450221008c2107ed4e026ab4319a591e8d9ec37719cdea0539" "51c660566e3a3399428af502202ecd823d5f74a77cc2159d8af2d3ea5d36a702fef9a7" "edaaf562aef22ac35da401ffffffff038f52231b994efb980382e4d804efeadaee13cf" "e01abe0d969038ccb45ec17000000000490047304402200487cd787fde9b337ab87f9f" "e54b9fd46d5d1692aa58e97147a4fe757f6f944202203cbcfb9c0fc4e3c453938bbea9" "e5ae64030cf7a97fafaf460ea2cb54ed5651b501ffffffff0100093d00000000001976" "a9144dc39248253538b93d3a0eb122d16882b998145888ac0000000002000000000000" "004751210351efb6e91a31221652105d032a2508275f374cea63939ad72f1b1e02f477" "da782100f2b7816db49d55d24df7bdffdbc1e203b424e8cd39f5651ab938e5e4a19356" "9e52ae404b4c00000000004751210351efb6e91a31221652105d032a2508275f374cea" "63939ad72f1b1e02f477da7821004f0331742bbc917ba2056a3b8a857ea47ec088dd10" "475ea311302112c9d24a7152ae")) tx = Tx.parse(f) tx.parse_unspents(f) self.assertEqual( tx.id(), "70c4e749f2b8b907875d1483ae43e8a6790b0c8397bbb33682e3602617f9a77a") self.assertEqual(tx.bad_signature_count(), 0)
def test_recognize_multisig(self): h = ( "010000000139c92b102879eb95f14e7344e4dd7d481e1238b1bfb1fa0f735068d2927b" "231400000000910047304402208fc06d216ebb4b6a3a3e0f906e1512c372fa8a9c2a92" "505d04e9b451ea7acd0c0220764303bb7e514ddd77855949d941c934e9cbda8e3c3827" "bfdb5777477e73885b014730440220569ec6d2e81625dd18c73920e0079cdb4c1d67d3" "d7616759eb0c18cf566b3d3402201c60318f0a62e3ba85ca0f158d4dfe63c0779269eb" "6765b6fc939fc51e7a8ea901ffffffff0140787d01000000001976a914641ad5051edd" "97029a003fe9efb29359fcee409d88ac0000000040787d0100000000c952410496ec45" "f878b62c46c4be8e336dff7cc58df9b502178cc240eb3d31b1266f69f5767071aa3e01" "7d1b82a0bb28dab5e27d4d8e9725b3e68ed5f8a2d45c730621e34104cc71eb30d653c0" "c3163990c47b976f3fb3f37cccdcbedb169a1dfef58bbfbfaff7d8a473e7e2e6d317b8" "7bafe8bde97e3cf8f065dec022b51d11fcdd0d348ac4410461cbdcc5409fb4b4d42b51" "d33381354d80e550078cb532a34bfa2fcfdeb7d76519aecc62770f5b0e4ef8551946d8" "a540911abe3e7854a26f39f58b25c15342af53ae") f = io.BytesIO(h2b(h)) tx = Tx.parse(f) tx.parse_unspents(f) self.assertEqual( tx.id(), "10c61e258e0a2b19b245a96a2d0a1538fe81cd4ecd547e0a3df7ed6fd3761ada") the_script = tx.unspents[0].script s = script_obj_from_script(tx.unspents[0].script) self.assertEqual(s.script(), the_script)
def tx_from_b64(h): f = io.BytesIO(binascii.a2b_base64(h.encode("utf8"))) return Tx.parse(f)
def tx_for_tx_hash(self, tx_hash): "Get a Tx by its hash." url = self.base_url("get_tx", b2h_rev(tx_hash)) r = json.loads(urlopen(url).read().decode("utf8")) tx = Tx.parse(io.BytesIO(h2b(r.get("data").get("tx_hex")))) return tx