def test_sweep(self): cmds = MocCommands() cmds.wallet.add_address_transaction(120000) cmds.wallet.add_address_transaction(120000) unspent = [] for tx in cmds.wallet.transactions.values(): for output in tx._outputs: unspent.append((tx.hash(), output)) cmds.network = MocNetwork({ 'blockchain.address.listunspent': lambda _: [{ 'tx_hash': tx_hash, 'tx_pos': 1, 'address': address, 'coinbase': False, 'height': 2, 'is_claim': False, 'is_support': False, 'is_update': False, 'prevout_hash': 'df303881e9014cce89c7acf55b124372e22979284baa99bb9fa178a9d35c97cb', 'prevout_n': 0, 'value': amount } for (tx_hash, (type, address, amount)) in unspent] }) destination = cmds.wallet.create_new_address() result = cmds.sweep(self.private_key, destination) self.assertEqual(True, result['success']) sent = Transaction(result['tx']) sent.deserialize() self.assertEqual(len(sent._outputs), 1) self.assertEqual(sent._outputs[0][2], 230000)
def get_input_tx(self, tx_hash): # First look up an input transaction in the wallet where it # will likely be. If co-signing a transaction it may not have # all the input txs, in which case we ask the network. tx = self.transactions.get(tx_hash) if not tx: request = ('blockchain.transaction.get', [tx_hash]) # FIXME: what if offline? tx = Transaction(self.network.synchronous_get(request)) return tx
def tx_response(self, response): params, result = self.parse_response(response) if not params: return tx_hash, tx_height = params assert tx_hash == hash_encode(Hash(result.decode('hex'))) tx = Transaction(result) try: tx.deserialize() except Exception: log.info("cannot deserialize transaction, skipping: %s", tx_hash) return self.wallet.receive_tx_callback(tx_hash, tx, tx_height) self.requested_tx.remove((tx_hash, tx_height)) log.info("received tx %s height: %d bytes: %d", tx_hash, tx_height, len(tx.raw)) # callbacks self.network.trigger_callback('new_transaction', tx) if not self.requested_tx: self.network.trigger_callback('updated')
def main(self): add_menu() welcome = 'Use the menu to scan a transaction.' droid.fullShow(qr_layout(welcome)) while True: event = droid.eventWait().result if not event: continue elif event["name"] == "key": if event["data"]["key"] == '4': if self.qr_data: self.show_qr(None) self.show_title(welcome) else: break elif event["name"] == "seed": password = self.get_password() if password is False: modal_dialog('Error','incorrect password') continue seed = wallet.get_mnemonic(password) modal_dialog('Your seed is', seed) elif event["name"] == "password": self.change_password_dialog() elif event["name"] == "xpub": mpk = wallet.get_master_public_key() self.show_qr(mpk) self.show_title('master public key') droid.setClipboard(mpk) droid.makeToast("Master public key copied to clipboard") elif event["name"] == "scan": r = droid.scanBarcode() r = r.result if not r: continue data = r['extras']['SCAN_RESULT'] data = base_decode(data.encode('utf8'), None, base=43) data = ''.join(chr(ord(b)) for b in data).encode('hex') tx = Transaction(data) #except: # modal_dialog('Error', 'Cannot parse transaction') # continue if not wallet.can_sign(tx): modal_dialog('Error', 'Cannot sign this transaction') continue lines = map(lambda x: x[0] + u'\t\t' + format_satoshis(x[1]) if x[1] else x[0], tx.get_outputs()) if not modal_question('Sign?', '\n'.join(lines)): continue password = self.get_password() if password is False: modal_dialog('Error','incorrect password') continue droid.dialogCreateSpinnerProgress("Signing") droid.dialogShow() wallet.sign_transaction(tx, password) droid.dialogDismiss() data = base_encode(str(tx).decode('hex'), base=43) self.show_qr(data) self.show_title('Signed Transaction') droid.makeToast("Bye!")