def listunspent(fn): try: w = Wallet(fn).fromFile('') utxo = json.loads(w.get_status("utxo")) info = _CONNECTION.getinfo() for u in utxo: u.update( {"confirmations": str(int(info['blocks']) - int(u["height"]))}) return json.dumps(utxo, sort_keys=True, indent=4) except: pass
def sync(self, fn, passphrase, full=False): """ Traverse the blockchain and update an index of all data belonging to addresses in this wallet """ wallet = Wallet(fn).fromFile(passphrase) if full: wallet.update_status("height", str(self.parameters.start_height)) utxo = [] wallet.update_status("utxo", json.dumps(utxo)) start_block = wallet.block_height() # Unspent transaction outputs utxo = json.loads(wallet.get_status("utxo")) # Spent transaction outputs stxo = [] addresses = [] transaction_value = [] for k in wallet.Keys: addresses.append(k.address()) info = self.getinfo() sys.stdout.write('Syncing.') sys.stdout.flush() wallet.update_status("current", "sync") try: current_status = wallet.get_status("current") except: current_status = 'ready' if current_status == 'sync': wallet.update_status("updated", str(int(time.time()))) while info["blocks"] > start_block: block = self.getblockbynumber(start_block) if start_block > 0: for tx_hash in block["tx"]: raw = self.getrawtransaction(tx_hash) tx = self.decoderawtransaction(raw) """ Let's extract everything we need from this TX Create an index to this block and store all the data we need Throw out the data we don't need """ this_tx_reward = 0 this_tx_value = 0 this_tx_fees = 0 this_tx_unmodified_value = 0 this_tx_total_vin = 0 this_tx_total_vout = 0 proof_of_stake = False # Gather up all of the inputs for addresses we own for txin in tx["vin"]: # Let's look back in time and see if this is our address if not ("coinbase" in txin): raw = self.getrawtransaction(txin["txid"]) txin_tx = self.decoderawtransaction(raw) # Now lets loop through the old TX vout's to find us or not for txout in txin_tx["vout"]: if txout["n"] == txin["vout"]: # We have a match, this one is ours we spent, so collect it intersection = list( set(txout["scriptPubKey"] ["addresses"]) & set(addresses)) if len(intersection) > 0: stxo.append({ "txhash_in": txin["txid"], "txhash_out": tx_hash, "address": intersection[0], "value": txout["scriptPubKey"]["value"] }) this_tx_total_vin += Decimal( txout["scriptPubKey"]["value"]) # Gather up all of the outputs for addresses we own for txout in tx["vout"]: try: intersection = list( set(txout["scriptPubKey"]["addresses"]) & set(addresses)) except: # No addresses in this txout intersection = () is_mine = (len(intersection) > 0) if is_mine: proof_of_stake = (txout['n'] == 0 and txout['value'] == 0) if proof_of_stake and ( txout['scriptPubKey']['type'] == 'nonstandard' and txout['value'] > 0 and txout['scriptPubKey']['asm'] == ""): this_tx_reward = txout["value"] else: for a in intersection: _new_val = txout["value"] utxo.append({ "tx_hash": tx_hash, "value": _new_val, "address": a, "height": block["height"] }) this_tx_unmodified_value += _new_val this_tx_value = this_tx_unmodified_value - this_tx_total_vin - this_tx_reward this_tx_fees = this_tx_unmodified_value - this_tx_total_vin - this_tx_reward - this_tx_value if this_tx_value > 0: transaction_value.append({ "txhash": tx_hash, "value": this_tx_value, "fees": this_tx_fees, "reward": this_tx_reward }) info = self.getinfo() wallet.update_status("height", str(int(start_block))) if wallet.block_height() % 5 == 0: sys.stdout.write('.') sys.stdout.flush() start_block += 1 for tx in utxo: for _tx in stxo: if tx["tx_hash"] == _tx["txhash_in"]: utxo.remove(tx) print "\n" wallet.update_status("utxo", json.dumps(utxo, cls=DecimalEncoder)) wallet.update_status("current", "ready") wallet.update_status("updated", str(int(time.time()))) return json.loads(wallet.get_status("utxo"))