Skip to content

Commit

Permalink
[REF] Allow filter by key in send methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mccwdev committed May 31, 2019
1 parent 167cc94 commit 5609e09
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
17 changes: 11 additions & 6 deletions bitcoinlib/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2878,6 +2878,8 @@ def select_inputs(self, amount, variance=None, input_key_id=None, account_id=Non
:type amount: int
:param variance: Allowed difference in total input value. Default is dust amount of selected network.
:type variance: int
:param input_key_id: Limit UTXO's search for inputs to this key_id. Only valid if no input array is specified
:type input_key_id: int
:param account_id: Account ID
:type account_id: int
:param network: Network name. Leave empty for default network
Expand All @@ -2901,7 +2903,7 @@ def select_inputs(self, amount, variance=None, input_key_id=None, account_id=Non
DbKey.network_name == network, DbKey.public != '',
DbTransactionOutput.spent.op("IS")(False), DbTransaction.confirmations >= min_confirms)
if input_key_id:
utxo_query.filter(DbKey.id == input_key_id)
utxo_query = utxo_query.filter(DbKey.id == input_key_id)
utxos = utxo_query.order_by(DbTransaction.confirmations.desc()).all()
if not utxos:
raise WalletError("Create transaction: No unspent transaction outputs found")
Expand Down Expand Up @@ -2965,6 +2967,8 @@ def transaction_create(self, output_arr, input_arr=None, input_key_id=None, acco
:type output_arr: list
:param input_arr: List of inputs tuples with reference to a UTXO, a wallet key and value. The format is [(tx_hash, output_n, key_ids, value, signatures, unlocking_script, address)]
:type input_arr: list
:param input_key_id: Limit UTXO's search for inputs to this key_id. Only valid if no input array is specified
:type input_key_id: int
:param account_id: Account ID
:type account_id: int
:param network: Network name. Leave empty for default network
Expand Down Expand Up @@ -3023,9 +3027,8 @@ def transaction_create(self, output_arr, input_arr=None, input_key_id=None, acco
sequence = 0xfffffffe
amount_total_input = 0
if input_arr is None:
selected_utxos = self.select_inputs(amount_total_output + fee_estimate, input_key_id,
self.network.dust_amount, account_id, network, min_confirms, max_utxos,
False)
selected_utxos = self.select_inputs(amount_total_output + fee_estimate, self.network.dust_amount, input_key_id,
account_id, network, min_confirms, max_utxos, False)
if not selected_utxos:
logger.warning("Not enough unspent transaction outputs found")
return False
Expand Down Expand Up @@ -3275,7 +3278,7 @@ def send(self, output_arr, input_arr=None, input_key_id=None, account_id=None, n
transaction.send(offline)
return transaction

def send_to(self, to_address, amount, account_id=None, network=None, fee=None, min_confirms=0,
def send_to(self, to_address, amount, input_key_id=None, account_id=None, network=None, fee=None, min_confirms=0,
priv_keys=None, locktime=0, offline=False):
"""
Create transaction and send it with default Service objects sendrawtransaction method
Expand All @@ -3284,6 +3287,8 @@ def send_to(self, to_address, amount, account_id=None, network=None, fee=None, m
:type to_address: str, Address, HDKey, HDWalletKey
:param amount: Output is smallest denominator for this network (ie: Satoshi's for Bitcoin)
:type amount: int
:param input_key_id: Limit UTXO's search for inputs to this key_id. Only valid if no input array is specified
:type input_key_id: int
:param account_id: Account ID, default is last used
:type account_id: int
:param network: Network name. Leave empty for default network
Expand All @@ -3303,7 +3308,7 @@ def send_to(self, to_address, amount, account_id=None, network=None, fee=None, m
"""

outputs = [(to_address, amount)]
return self.send(outputs, account_id=account_id, network=network, fee=fee,
return self.send(outputs, input_key_id=input_key_id, account_id=account_id, network=network, fee=fee,
min_confirms=min_confirms, priv_keys=priv_keys, locktime=locktime, offline=offline)

def sweep(self, to_address, account_id=None, input_key_id=None, network=None, max_utxos=999, min_confirms=0,
Expand Down
11 changes: 11 additions & 0 deletions tests/test_wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,17 @@ def test_wallet_transaction_restore_saved_tx(self):
t = w.sweep(to.address, offline=True)
self.assertEqual(t.save(), tx_id)

def test_wallet_transaction_send_keyid(self):
w = HDWallet.create('wallet_send_key_id', witness_type='segwit', network='bitcoinlib_test',
databasefile=DATABASEFILE_UNITTESTS)
keys = w.get_key(number_of_keys=2)
w.utxos_update()
t = w.send_to('blt1qtk5swtntg8gvtsyr3kkx3mjcs5ncav84exjvde', 150000000, input_key_id=keys[1].key_id)
self.assertEqual(t.inputs[0].address, keys[1].address)
self.assertTrue(t.verified)
self.assertFalse(w.send_to('blt1qtk5swtntg8gvtsyr3kkx3mjcs5ncav84exjvde', 250000000,
input_key_id=keys[0].key_id))


class TestWalletDash(unittest.TestCase):

Expand Down

0 comments on commit 5609e09

Please sign in to comment.