def waitForLockTxB(self, kbv, Kbs, cb_swap_value, cb_block_confirmed, restore_height): Kbv_enc = self.encodePubkey(self.pubkey(kbv)) address_b58 = xmr_util.encode_address(Kbv_enc, self.encodePubkey(Kbs)) try: self.rpc_wallet_cb('close_wallet') except Exception as e: logging.warning('close_wallet failed %s', str(e)) params = { 'filename': address_b58, 'address': address_b58, 'viewkey': b2h(kbv[::-1]), 'restore_height': restore_height, } self.rpc_wallet_cb('generate_from_keys', params) self.rpc_wallet_cb('open_wallet', {'filename': address_b58}) # For a while after opening the wallet rpc cmds return empty data num_tries = 40 for i in range(num_tries + 1): try: current_height = self.rpc_cb('get_block_count')['count'] print('current_height', current_height) except Exception as e: logging.warning('rpc_cb failed %s', str(e)) current_height = None # If the transfer is available it will be deep enough # TODO: Make accepting current_height == None a user selectable option # Or look for all transfers and check height params = {'transfer_type': 'available'} rv = self.rpc_wallet_cb('incoming_transfers', params) print('rv', rv) if 'transfers' in rv: for transfer in rv['transfers']: if transfer['amount'] == cb_swap_value \ and (current_height is None or current_height - transfer['block_height'] > cb_block_confirmed): return True # TODO: Is it necessary to check the address? ''' rv = self.rpc_wallet_cb('get_balance') print('get_balance', rv) if 'per_subaddress' in rv: for sub_addr in rv['per_subaddress']: if sub_addr['address'] == address_b58: ''' if i >= num_tries: raise ValueError('Balance not confirming on node') time.sleep(1) return False
def publishBLockTx(self, Kbv, Kbs, output_amount, feerate): self.rpc_wallet_cb('open_wallet', {'filename': self._wallet_filename}) shared_addr = xmr_util.encode_address(Kbv, Kbs) # TODO: How to set feerate? params = { 'destinations': [{ 'amount': output_amount, 'address': shared_addr }] } rv = self.rpc_wallet_cb('transfer', params) logging.info('publishBLockTx %s to address_b58 %s', rv['tx_hash'], shared_addr) tx_hash = bytes.fromhex(rv['tx_hash']) # Debug for i in range(10): params = { 'out': True, 'pending': True, 'failed': True, 'pool': True, } rv = self.rpc_wallet_cb('get_transfers', params) logging.info('[rm] get_transfers {}'.format(dumpj(rv))) if 'pending' not in rv: break time.sleep(1) return tx_hash
def findTxB(self, kbv, Kbs, cb_swap_value, cb_block_confirmed, restore_height): Kbv = self.getPubkey(kbv) address_b58 = xmr_util.encode_address(Kbv, Kbs) try: self.rpc_wallet_cb('close_wallet') except Exception as e: logging.warning('close_wallet failed %s', str(e)) kbv_le = kbv[::-1] params = { 'restore_height': restore_height, 'filename': address_b58, 'address': address_b58, 'viewkey': b2h(kbv_le), } try: rv = self.rpc_wallet_cb('open_wallet', {'filename': address_b58}) except Exception as e: rv = self.rpc_wallet_cb('generate_from_keys', params) logging.info('generate_from_keys %s', dumpj(rv)) rv = self.rpc_wallet_cb('open_wallet', {'filename': address_b58}) rv = self.rpc_wallet_cb('refresh') ''' # Debug try: current_height = self.rpc_wallet_cb('get_block_count')['count'] logging.info('findTxB XMR current_height %d\nAddress: %s', current_height, address_b58) except Exception as e: logging.info('rpc_cb failed %s', str(e)) current_height = None # If the transfer is available it will be deep enough # and (current_height is None or current_height - transfer['block_height'] > cb_block_confirmed): ''' params = {'transfer_type': 'available'} rv = self.rpc_wallet_cb('incoming_transfers', params) if 'transfers' in rv: for transfer in rv['transfers']: if transfer['amount'] == cb_swap_value: return { 'txid': transfer['tx_hash'], 'amount': transfer['amount'], 'height': 0 if 'block_height' not in transfer else transfer['block_height'] } else: logging.warning( 'Incorrect amount detected for coin b lock txn: {}'. format(transfer['tx_hash'])) return None
def spendBLockTx(self, address_to, kbv, kbs, cb_swap_value, b_fee_rate, restore_height): Kbv = self.getPubkey(kbv) Kbs = self.getPubkey(kbs) address_b58 = xmr_util.encode_address(Kbv, Kbs) try: self.rpc_wallet_cb('close_wallet') except Exception as e: self._log.warning('close_wallet failed %s', str(e)) wallet_filename = address_b58 + '_spend' params = { 'filename': wallet_filename, 'address': address_b58, 'viewkey': b2h(kbv[::-1]), 'spendkey': b2h(kbs[::-1]), 'restore_height': restore_height, } try: self.rpc_wallet_cb('open_wallet', {'filename': wallet_filename}) except Exception as e: rv = self.rpc_wallet_cb('generate_from_keys', params) self._log.info('generate_from_keys %s', dumpj(rv)) self.rpc_wallet_cb('open_wallet', {'filename': wallet_filename}) # For a while after opening the wallet rpc cmds return empty data for i in range(10): rv = self.rpc_wallet_cb('get_balance') print('get_balance', rv) if rv['balance'] >= cb_swap_value: break time.sleep(1 + i) if rv['balance'] < cb_swap_value: self._log.error('wallet {} balance {}, expected {}'.format( wallet_filename, rv['balance'], cb_swap_value)) raise ValueError('Invalid balance') if rv['unlocked_balance'] < cb_swap_value: self._log.error( 'wallet {} balance {}, expected {}, blocks_to_unlock {}'. format(wallet_filename, rv['unlocked_balance'], cb_swap_value, rv['blocks_to_unlock'])) raise ValueError('Invalid unlocked_balance') params = {'address': address_to} if self._fee_priority > 0: params['priority'] = self._fee_priority rv = self.rpc_wallet_cb('sweep_all', params) print('sweep_all', rv) return bytes.fromhex(rv['tx_hash_list'][0])
def initialiseWallet(self, key_view, key_spend, restore_height=None): try: self.rpc_wallet_cb('open_wallet', {'filename': self._wallet_filename}) # TODO: Check address return # Wallet exists except Exception as e: pass Kbv = self.getPubkey(key_view) Kbs = self.getPubkey(key_spend) address_b58 = xmr_util.encode_address(Kbv, Kbs) params = { 'filename': self._wallet_filename, 'address': address_b58, 'viewkey': b2h(key_view[::-1]), 'spendkey': b2h(key_spend[::-1]), 'restore_height': self._restore_height, } rv = self.rpc_wallet_cb('generate_from_keys', params) logging.info('generate_from_keys %s', dumpj(rv)) self.rpc_wallet_cb('open_wallet', {'filename': self._wallet_filename})
def getAddressFromKeys(self, key_view, key_spend): pk_view = self.getPubkey(key_view) pk_spend = self.getPubkey(key_spend) return xmr_util.encode_address(pk_view, pk_spend)