Skip to content

Commit

Permalink
Add unittests for anti fee snipping for wallets
Browse files Browse the repository at this point in the history
  • Loading branch information
mccwdev committed Mar 13, 2024
1 parent b1e5d98 commit e125827
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
7 changes: 4 additions & 3 deletions bitcoinlib/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions tests/test_wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e125827

Please sign in to comment.