From e125827f090fd3304275b5a3815518a728c40b1a Mon Sep 17 00:00:00 2001 From: Lennart Jongeneel Date: Wed, 13 Mar 2024 18:31:46 +0100 Subject: [PATCH] Add unittests for anti fee snipping for wallets --- bitcoinlib/wallets.py | 7 ++++--- tests/test_wallets.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/bitcoinlib/wallets.py b/bitcoinlib/wallets.py index bb65ac93..25c935df 100644 --- a/bitcoinlib/wallets.py +++ b/bitcoinlib/wallets.py @@ -109,7 +109,7 @@ def wallet_exists(wallet, db_uri=None, db_password=None): def wallet_create_or_open( name, keys='', owner='', network=None, account_id=0, purpose=None, scheme='bip32', sort_keys=True, password='', witness_type=None, encoding=None, multisig=None, sigs_required=None, cosigner_id=None, - key_path=None, db_uri=None, db_cache_uri=None, db_password=None): + key_path=None, anti_fee_snipping=True, db_uri=None, db_cache_uri=None, db_password=None): """ Create a wallet with specified options if it doesn't exist, otherwise just open @@ -124,7 +124,8 @@ def wallet_create_or_open( else: return Wallet.create(name, keys, owner, network, account_id, purpose, scheme, sort_keys, password, witness_type, encoding, multisig, sigs_required, cosigner_id, - key_path, db_uri=db_uri, db_cache_uri=db_cache_uri, db_password=db_password) + key_path, anti_fee_snipping, db_uri=db_uri, db_cache_uri=db_cache_uri, + db_password=db_password) def wallet_delete(wallet, db_uri=None, force=False, db_password=None): @@ -1195,7 +1196,7 @@ def create(cls, name, keys=None, owner='', network=None, account_id=0, purpose=0 * All keys must be hardened, except for change, address_index or cosigner_id * Max length of path is 8 levels :type key_path: list, str - :param anti_fee_snipping: Set default locktime in transactions as current block height + 1 to avoid fee-snipping. Default is True + :param anti_fee_snipping: Set default locktime in transactions as current block height + 1 to avoid fee-snipping. Default is True, which will make the network more secure. You could disable it to avoid transaction fingerprinting. :type anti_fee_snipping: boolean :param db_uri: URI of the database for wallets, wallet transactions and keys :type db_uri: str diff --git a/tests/test_wallets.py b/tests/test_wallets.py index 1caafef6..715b1c3b 100644 --- a/tests/test_wallets.py +++ b/tests/test_wallets.py @@ -2266,6 +2266,24 @@ def test_wallet_transaction_replace_by_fee(self): self.assertTrue(t2.replace_by_fee) self.assertEqual(t2.inputs[0].sequence, SEQUENCE_REPLACE_BY_FEE) + def test_wallet_anti_fee_snipping(self): + w = wallet_create_or_open('antifeesnippingtestwallet', network='testnet', anti_fee_snipping=True, + db_uri=self.database_uri) + w.utxo_add(w.get_key().address, 1234567, os.urandom(32).hex(), 1) + t = w.send_to('tb1qrjtz22q59e76mhumy0p586cqukatw5vcd0xvvz', 123456) + block_height = Service(network='testnet').blockcount() + self.assertEqual(t.locktime, block_height+1) + + w2 = wallet_create_or_open('antifeesnippingtestwallet2', network='testnet', anti_fee_snipping=True) + w2.utxo_add(w2.get_key().address, 1234567, os.urandom(32).hex(), 1) + t = w2.send_to('tb1qrjtz22q59e76mhumy0p586cqukatw5vcd0xvvz', 123456, locktime=1901070183) + self.assertEqual(t.locktime, 1901070183) + + w3 = wallet_create_or_open('antifeesnippingtestwallet3', network='testnet', anti_fee_snipping=False) + w3.utxo_add(w3.get_key().address, 1234567, os.urandom(32).hex(), 1) + t = w3.send_to('tb1qrjtz22q59e76mhumy0p586cqukatw5vcd0xvvz', 123456) + self.assertEqual(t.locktime, 0) + @classmethod def tearDownClass(cls): del cls.wallet