Checks if inp is a testnet address or if UTXO is a known testnet TxID
(inp)
| 24 | |
| 25 | |
| 26 | def is_testnet(inp): |
| 27 | '''Checks if inp is a testnet address or if UTXO is a known testnet TxID''' |
| 28 | if isinstance(inp, (list, tuple)) and len(inp) >= 1: |
| 29 | return any([is_testnet(x) for x in inp]) |
| 30 | elif not isinstance(inp, basestring): # sanity check |
| 31 | raise TypeError("Input must be str/unicode, not type %s" % str(type(inp))) |
| 32 | |
| 33 | if not inp or (inp.lower() in ("btc", "testnet")): |
| 34 | pass |
| 35 | |
| 36 | ## ADDRESSES |
| 37 | if inp[0] in "123mn": |
| 38 | if re.match("^[2mn][a-km-zA-HJ-NP-Z0-9]{26,33}$", inp): |
| 39 | return True |
| 40 | elif re.match("^[13][a-km-zA-HJ-NP-Z0-9]{26,33}$", inp): |
| 41 | return False |
| 42 | else: |
| 43 | #sys.stderr.write("Bad address format %s") |
| 44 | return None |
| 45 | |
| 46 | ## TXID |
| 47 | elif re.match('^[0-9a-fA-F]{64}$', inp): |
| 48 | base_url = "http://api.blockcypher.com/v1/btc/{network}/txs/{txid}?includesHex=false" |
| 49 | try: |
| 50 | # try testnet fetchtx |
| 51 | make_request(base_url.format(network="test3", txid=inp.lower())) |
| 52 | return True |
| 53 | except: |
| 54 | # try mainnet fetchtx |
| 55 | make_request(base_url.format(network="main", txid=inp.lower())) |
| 56 | return False |
| 57 | sys.stderr.write("TxID %s has no match for testnet or mainnet (Bad TxID)") |
| 58 | return None |
| 59 | else: |
| 60 | raise TypeError("{0} is unknown input".format(inp)) |
| 61 | |
| 62 | |
| 63 | def set_network(*args): |
no test coverage detected