Exemplo n.º 1
0
 def select_utxos(self, mixdepth, amount):
     utxo_list = self.get_utxos_by_mixdepth()[mixdepth]
     unspent = [{"utxo": utxo, "value": addrval["value"]} for utxo, addrval in utxo_list.iteritems()]
     inputs = btc.select(unspent, amount)
     debug("for mixdepth=" + str(mixdepth) + " amount=" + str(amount) + " selected:")
     debug(pprint.pformat(inputs))
     return dict([(i["utxo"], {"value": i["value"], "address": utxo_list[i["utxo"]]["address"]}) for i in inputs])
Exemplo n.º 2
0
	def select_utxos(self, mixdepth, amount):
		utxo_list = self.get_utxos_by_mixdepth()[mixdepth]
		unspent = [{'utxo': utxo, 'value': self.unspent[utxo]['value']}
			for utxo in utxo_list]
		inputs = btc.select(unspent, amount)
		debug('for mixdepth=' + str(mixdepth) + ' amount=' + str(amount) + ' selected:')
		debug(pprint.pformat(inputs))
		return dict([(i['utxo'], {'value': i['value'], 'address':
			self.unspent[i['utxo']]['address']}) for i in inputs])
Exemplo n.º 3
0
def send(args):
    if len(args.outputs) % 2 != 0:
        raise Exception("When sending, there must be an even number of arguments " +
                        "for the outputs (address, price)")
    unspents = bitcoin.BlockchainInfo.unspent_xpub(args.xpub)
    def btctosatoshi(vs):
        return int(float(vs)*100000000.0)
    fee = btctosatoshi(args.fee)
    if fee < 0:
        fee = int(0.0001*100000000) #todo do something to estimated fee...make it negative or something though... DONT
    outaddrval = [(args.outputs[2*i],btctosatoshi(args.outputs[2*i+1])) for i in range(len(args.outputs)//2)]
    outtotalval = sum([o[1] for o in outaddrval])
    changeindex = check_outputs_max_index(unspents,1)
    changeaddress = bitcoin.pubtoaddr(bitcoin.bip32_descend(args.xpub, 1, changeindex))

    unspents = bitcoin.select(unspents, outtotalval+fee)
    #unspents cull using outtotalval
    unspenttotalval = sum([u['value'] for u in unspents])
    changeamount = unspenttotalval - (outtotalval + abs(fee))

    if changeamount < 0:
        raise Exception("There is unlikely to be enough unspent outputs to cover the transaction and fees")
    out = {}
    outs = outaddrval
    if changeamount > 0:
        outs += [[changeaddress, changeamount]]
    #print(unspents)
    #print(outs)
    otx = [{'address':o[0], 'value':o[1]} for o in outs]
    tx = bitcoin.mktx(unspents, otx)

    #compute all the underlying addresses and pubkeys into a string hash
    #estimate the fee
    #build the transaction
    out['tx'] = tx
    out['keys'] = dict([(u['output'], u['xpub']) for u in unspents])
    #print(bitcoin.deserialize(tx))
    json.dump(out, sys.stdout)