Skip to content

Commit

Permalink
Rename offline parameter for broadcast in send methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryp Toon committed Apr 19, 2024
1 parent 3de9927 commit 96006b5
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 67 deletions.
5 changes: 4 additions & 1 deletion bitcoinlib/tools/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ def benchmark_wallets_multisig():
w.get_key(number_of_keys=2)
w.utxos_update()
to_address = HDKey(network=network).address()
t = w.sweep(to_address, offline=True)
if BITCOINLIB_VERSION >= '0.7.0':
t = w.sweep(to_address, broadcast=False)
else:
t = w.sweep(to_address, offline=True)
key_pool = [i for i in range(0, n_keys - 1) if i != pk_n]
while len(t.inputs[0].signatures) < sigs_req:
co_id = random.choice(key_pool)
Expand Down
6 changes: 3 additions & 3 deletions bitcoinlib/tools/clw.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,11 @@ def main():
if not args.quiet:
print_transaction(wt)
elif args.sweep:
offline = True
broadcast = False
print("Sweep wallet. Send all funds to %s" % args.sweep, file=output_to)
if args.push:
offline = False
wt = wlt.sweep(args.sweep, offline=offline, network=args.network, fee_per_kb=args.fee_per_kb, fee=args.fee,
broadcast = True
wt = wlt.sweep(args.sweep, broadcast=broadcast, network=args.network, fee_per_kb=args.fee_per_kb, fee=args.fee,
replace_by_fee=args.rbf)
if not wt:
raise WalletError("Error occurred when sweeping wallet: %s. Are UTXO's available and updated?" % wt)
Expand Down
36 changes: 18 additions & 18 deletions bitcoinlib/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,12 +816,12 @@ def sign(self, keys=None, index_n=0, multisig_key_n=None, hash_type=SIGHASH_ALL,
self.verify()
self.error = ""

def send(self, offline=False):
def send(self, broadcast=True):
"""
Verify and push transaction to network. Update UTXO's in database after successful send
:param offline: Just return the transaction object and do not send it when offline = True. Default is False
:type offline: bool
:param broadcast: Verify transaction and broadcast, if set to False the transaction is verified but not broadcasted, i. Default is True
:type broadcast: bool
:return None:
Expand All @@ -832,7 +832,7 @@ def send(self, offline=False):
self.error = "Cannot verify transaction"
return None

if offline:
if not broadcast:
return None

srv = Service(network=self.network.name, wallet_name=self.hdwallet.name, providers=self.hdwallet.providers,
Expand Down Expand Up @@ -4056,7 +4056,7 @@ def transaction_import_raw(self, rawtx, network=None):
return rt

def send(self, output_arr, input_arr=None, input_key_id=None, account_id=None, network=None, fee=None,
min_confirms=1, priv_keys=None, max_utxos=None, locktime=0, offline=True, number_of_change_outputs=1,
min_confirms=1, priv_keys=None, max_utxos=None, locktime=0, broadcast=False, number_of_change_outputs=1,
replace_by_fee=False):
"""
Create a new transaction with specified outputs and push it to the network.
Expand All @@ -4066,7 +4066,7 @@ def send(self, output_arr, input_arr=None, input_key_id=None, account_id=None, n
Uses the :func:`transaction_create` method to create a new transaction, and uses a random service client to send the transaction.
>>> w = Wallet('bitcoinlib_legacy_wallet_test')
>>> t = w.send([('1J9GDZMKEr3ZTj8q6pwtMy4Arvt92FDBTb', 200000)], offline=True)
>>> t = w.send([('1J9GDZMKEr3ZTj8q6pwtMy4Arvt92FDBTb', 200000)])
>>> t
<WalletTransaction(input_count=1, output_count=2, status=new, network=bitcoin)>
>>> t.outputs # doctest:+ELLIPSIS
Expand All @@ -4092,8 +4092,8 @@ def send(self, output_arr, input_arr=None, input_key_id=None, account_id=None, n
:type max_utxos: int
:param locktime: Transaction level locktime. Locks the transaction until a specified block (value from 1 to 5 million) or until a certain time (Timestamp in seconds after 1-jan-1970). Default value is 0 for transactions without locktime
:type locktime: int
:param offline: Just return the transaction object and do not send it when offline = True. Default is True
:type offline: bool
:param broadcast: Just return the transaction object and do not send it when broadcast = False. Default is False
:type broadcast: bool
:param number_of_change_outputs: Number of change outputs to create when there is a change value. Default is 1. Use 0 for random number of outputs: between 1 and 5 depending on send and change amount
:type number_of_change_outputs: int
:param replace_by_fee: Signal replace by fee and allow to send a new transaction with higher fees. Sets sequence value to SEQUENCE_REPLACE_BY_FEE
Expand Down Expand Up @@ -4128,18 +4128,18 @@ def send(self, output_arr, input_arr=None, input_key_id=None, account_id=None, n
transaction.calc_weight_units()
transaction.fee_per_kb = int(float(transaction.fee) / float(transaction.vsize) * 1000)
transaction.txid = transaction.signature_hash()[::-1].hex()
transaction.send(offline)
transaction.send(broadcast)
return transaction

def send_to(self, to_address, amount, input_key_id=None, account_id=None, network=None, fee=None, min_confirms=1,
priv_keys=None, locktime=0, offline=True, number_of_change_outputs=1, replace_by_fee=False):
priv_keys=None, locktime=0, broadcast=False, number_of_change_outputs=1, replace_by_fee=False):
"""
Create transaction and send it with default Service objects :func:`services.sendrawtransaction` method.
Wrapper for wallet :func:`send` method.
>>> w = Wallet('bitcoinlib_legacy_wallet_test')
>>> t = w.send_to('1J9GDZMKEr3ZTj8q6pwtMy4Arvt92FDBTb', 200000, offline=True)
>>> t = w.send_to('1J9GDZMKEr3ZTj8q6pwtMy4Arvt92FDBTb', 200000)
>>> t
<WalletTransaction(input_count=1, output_count=2, status=new, network=bitcoin)>
>>> t.outputs # doctest:+ELLIPSIS
Expand All @@ -4163,8 +4163,8 @@ def send_to(self, to_address, amount, input_key_id=None, account_id=None, networ
:type priv_keys: HDKey, list
:param locktime: Transaction level locktime. Locks the transaction until a specified block (value from 1 to 5 million) or until a certain time (Timestamp in seconds after 1-jan-1970). Default value is 0 for transactions without locktime
:type locktime: int
:param offline: Just return the transaction object and do not send it when offline = True. Default is True
:type offline: bool
:param broadcast: Just return the transaction object and do not send it when broadcast = False. Default is False
:type broadcast: bool
:param number_of_change_outputs: Number of change outputs to create when there is a change value. Default is 1. Use 0 for random number of outputs: between 1 and 5 depending on send and change amount
:type number_of_change_outputs: int
:param replace_by_fee: Signal replace by fee and allow to send a new transaction with higher fees. Sets sequence value to SEQUENCE_REPLACE_BY_FEE
Expand All @@ -4175,11 +4175,11 @@ def send_to(self, to_address, amount, input_key_id=None, account_id=None, networ

outputs = [(to_address, amount)]
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,
min_confirms=min_confirms, priv_keys=priv_keys, locktime=locktime, broadcast=broadcast,
number_of_change_outputs=number_of_change_outputs, replace_by_fee=replace_by_fee)

def sweep(self, to_address, account_id=None, input_key_id=None, network=None, max_utxos=999, min_confirms=1,
fee_per_kb=None, fee=None, locktime=0, offline=True, replace_by_fee=False):
fee_per_kb=None, fee=None, locktime=0, broadcast=False, replace_by_fee=False):
"""
Sweep all unspent transaction outputs (UTXO's) and send them to one or more output addresses.
Expand Down Expand Up @@ -4216,8 +4216,8 @@ def sweep(self, to_address, account_id=None, input_key_id=None, network=None, ma
:type fee: int, str
:param locktime: Transaction level locktime. Locks the transaction until a specified block (value from 1 to 5 million) or until a certain time (Timestamp in seconds after 1-jan-1970). Default value is 0 for transactions without locktime
:type locktime: int
:param offline: Just return the transaction object and do not send it when offline = True. Default is True
:type offline: bool
:param broadcast: Just return the transaction object and do not send it when broadcast = False. Default is False
:type broadcast: bool
:param replace_by_fee: Signal replace by fee and allow to send a new transaction with higher fees. Sets sequence value to SEQUENCE_REPLACE_BY_FEE
:type replace_by_fee: bool
Expand Down Expand Up @@ -4271,7 +4271,7 @@ def sweep(self, to_address, account_id=None, input_key_id=None, network=None, ma
"outputs, use amount value = 0 to indicate a change/rest output")

return self.send(to_list, input_arr, network=network, fee=fee, min_confirms=min_confirms, locktime=locktime,
offline=offline, replace_by_fee=replace_by_fee)
broadcast=broadcast, replace_by_fee=replace_by_fee)

def wif(self, is_private=False, account_id=0):
"""
Expand Down
2 changes: 1 addition & 1 deletion examples/wallet_bitcoind_connected_wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
else:
print("Found testnet coins. Wallet balance: %d" % w.balance())
# Send some coins to our own wallet
t = w.send_to(w.get_key().address, 1000, fee=200, offline=False)
t = w.send_to(w.get_key().address, 1000, fee=200, broadcast=True)
t.info()

# If you now run bitcoin-cli listunspent 0, you should see the 1 or 2 new utxo's for this transaction.
2 changes: 1 addition & 1 deletion examples/wallet_multisig_3of5.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
print("\nNew unspent outputs found!")
print("Now a new transaction will be created to sweep this wallet and send bitcoins to a testnet faucet")
send_to_address = '2NGZrVvZG92qGYqzTLjCAewvPZ7JE8S8VxE'
t = wallet3o5.sweep(send_to_address, min_confirms=0, offline=True)
t = wallet3o5.sweep(send_to_address, min_confirms=0, broadcast=False)
print("Now send the raw transaction hex to one of the other cosigners to sign using sign_raw.py")
print("Raw transaction: %s" % t.raw_hex())
else:
Expand Down
8 changes: 4 additions & 4 deletions examples/wallets_segwit_testnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
print("Balance to low, please deposit at least %s to %s" %
(((tx_fee+tx_amount)*4)-w1.balance(), w1_key.address))
print("Sending transaction from wallet #1 to wallet #2:")
t = w1.send_to(w2_key.address, 4 * tx_amount, fee=tx_fee, offline=False)
t = w1.send_to(w2_key.address, 4 * tx_amount, fee=tx_fee, broadcast=True)
t.info()

while True:
Expand All @@ -79,7 +79,7 @@
sleep(1)
if w2.utxos():
print("Sending transaction from wallet #2 to wallet #3:")
t2 = w2.send_to(w3_key.address, 3 * tx_amount, fee=tx_fee, offline=False)
t2 = w2.send_to(w3_key.address, 3 * tx_amount, fee=tx_fee, broadcast=True)
t2.info()
break

Expand All @@ -89,7 +89,7 @@
sleep(1)
if w3.utxos():
print("Sending transaction from wallet #3 to wallet #4:")
t3 = w3.send_to(w4_key.address, 2 * tx_amount, fee=tx_fee, offline=False)
t3 = w3.send_to(w4_key.address, 2 * tx_amount, fee=tx_fee, broadcast=True)
t3.sign(wif2)
t3.send()
t3.info()
Expand All @@ -101,7 +101,7 @@
sleep(1)
if w4.utxos():
print("Sending transaction from wallet #4 to wallet #1:")
t4 = w4.send_to(w1_key.address, tx_amount, fee=tx_fee, offline=False)
t4 = w4.send_to(w1_key.address, tx_amount, fee=tx_fee, broadcast=True)
t4.sign(wif2)
t4.send()
t4.info()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,7 @@ def test_transaction_multisig_same_sigs_for_keys(self):
'aeffffffff02a0acb903000000001976a9146170e2cc18a4415f807cc4b29c50e52bd1157c4b88ac787bb6030000000017a' \
'914bb87f55537ee62a232f042f39fbc0d86b77d07fb8700000000'
t = Transaction.parse(bytes.fromhex(traw))
t.inputs[0].value = 972612109
t.inputs[0].value = 124798308
self.assertTrue(t.verify())

def test_transaction_multisig_1_key_15_signatures(self):
Expand Down
Loading

0 comments on commit 96006b5

Please sign in to comment.