def get_scriptsig(*args, **kwargs): """Return scriptSig for 'txid:index'""" if len(args) == 1 and ':' in args[0]: txid, vout = args[0].split(':') elif len(args) == 2 and args[0][:8] == '01000000' and str(args[1]).isdigit(): txh, vout = args[0], int(args[1]) network = kwargs.get('network', 'btc') try: txo = deserialize(fetchtx(txid, network)) except: txo = deserialize(txh) scriptsig = reduce(access, ["ins", vout, "script"], txo) return scriptsig
def get_scriptpubkey(*args, **kwargs): """Return scriptPubKey for 'txid:index'""" # TODO: can use biteasy to retrieve a Tx's SPK if len(args) == 1 and ':' in args[0]: txid, vout = args[0].split(':') elif len(args) == 2 and args[0][:8] == '01000000' and str(args[1]).isdigit(): txh, vout = args[0], int(args[1]) network = kwargs.get('network', 'btc') try: txo = deserialize(fetchtx(txid, network)) except: txo = deserialize(txh) script_pubkey = reduce(access, ["outs", vout, "script"], txo) return script_pubkey
def get_script(*args, **kwargs): # last param can be 'ins', 'outs' if args[-1] in ("ins", "outs"): source = str(args[-1]) args = args[:-1] else: source = None if isinstance(args[0], str) and ':' in args[0]: txid, vout = args[0].split(':') elif (len(args) == 2 and not source) or len(args) == 3: txh, vout = str(args[0]), int(args[1]) network = kwargs.get('network', 'btc') try: txo = deserialize(fetchtx(txid, network)) except: txo = deserialize(txh) if source is None: scriptsig, script_pk = [], [] for inp in txo['ins']: scriptsig.append(inp['script']) for outp in txo['outs']: script_pk.append(inp['script']) return {'ins': scriptsig, 'outs': script_pk} return access(txo, "ins")[vout]['script'] if source == 'ins' else access(txo, "outs")[vout]['script']
def get_script(*args, **kwargs): """Extract scripts from txhex; specify 'ins' or 'outs', otherwise default is both""" from bitcoin.bci import set_network if len(args) > 1 and args[-1] in ("ins", "outs", "both"): source = args[-1] args = args[:-1] else: source = "both" if is_inp(args[0]): txid, vout = args[0].split(':') network = set_network(txid) if not kwargs.get( 'network', None) else kwargs.get('network') txo = deserialize(fetchtx(txid, network)) # try fetching txi elif len(args) == 3 and source != 'both': txhex, vout = str(args[0]), int(args[1]) txo = deserialize(txhex) elif len(args) == 2 and source == 'both': txhex, vout = str(args[0]), None txo = deserialize(txhex) elif (1 <= len(args) <= 2) and is_txhex(args[0]): # for no vout txo = deserialize(args[0]) vout = None scriptsig, script_pk = [], [] for inp in txo['ins']: scriptsig.append(inp['script']) for outp in txo['outs']: script_pk.append(outp['script']) if source == 'ins': # return scriptsig return scriptsig if not vout else scriptsig[int(vout)] elif source == 'outs': # return scriptpubkey return script_pk if not vout else script_pk[int(vout)] elif source == 'both': # return BOTH ins & outs return {'ins': scriptsig, 'outs': script_pk} else: raise Exception( "Bad source {0} type: choose 'ins', 'outs' or 'both'".format( source))
def get_script(*args, **kwargs): """Extract scripts from txhex; specify 'ins' or 'outs', otherwise default is both""" from bitcoin.bci import set_network if len(args) > 1 and args[-1] in ("ins", "outs", "both"): source = args[-1] args = args[:-1] else: source = "both" if is_inp(args[0]): txid, vout = args[0].split(':') network = set_network(txid) if not kwargs.get('network', None) else kwargs.get('network') txo = deserialize(fetchtx(txid, network)) # try fetching txi elif len(args) == 3 and source != 'both': txhex, vout = str(args[0]), int(args[1]) txo = deserialize(txhex) elif len(args) == 2 and source == 'both': txhex, vout = str(args[0]), None txo = deserialize(txhex) elif (1<= len(args) <= 2) and is_txhex(args[0]): # for no vout txo = deserialize(args[0]) vout = None scriptsig, script_pk = [], [] for inp in txo['ins']: scriptsig.append(inp['script']) for outp in txo['outs']: script_pk.append(outp['script']) if source == 'ins': # return scriptsig return scriptsig if not vout else scriptsig[int(vout)] elif source == 'outs': # return scriptpubkey return script_pk if not vout else script_pk[int(vout)] elif source == 'both': # return BOTH ins & outs return {'ins': scriptsig, 'outs': script_pk} else: raise Exception("Bad source {0} type: choose 'ins', 'outs' or 'both'".format(source))