Merge bitcoin/bitcoin#28617: test: Add Wallet Unlock Context Manager

004903ebad test: Add Wallet Unlock Context Manager (Brandon Odiwuor)

Pull request description:

  Fixes #28601, see https://github.com/bitcoin/bitcoin/pull/28403#discussion_r1325426430

  Add Context Manager to manage the locking and unlocking of locked wallets with a passphrase during testing.

ACKs for top commit:
  kevkevinpal:
    lgtm ACK [004903e](https://github.com/bitcoin/bitcoin/pull/28617/commits/004903ebade38ba47c5ddc17b756d605b963528e)
  maflcko:
    lgtm ACK 004903ebad

Tree-SHA512: ab234c167e71531df0d974ff9a31d444f7ce2a1d05aba5ea868cc9452f139845eeb24ca058d88f058bc02482b762adf2d99e63a6640b872cc71a57a0068abfe8
This commit is contained in:
fanquake committed 2023-10-19 10:23:44 +01:00
commit 091d29c495
7 files changed
+140 -125

No files matched your search

@@ -122,3 +122,22 @@ def generate_keypair(compressed=True, wif=False):
if wif:
privkey = bytes_to_wif(privkey.get_bytes(), compressed)
return privkey, pubkey
class WalletUnlock():
"""
A context manager for unlocking a wallet with a passphrase and automatically locking it afterward.
"""
MAXIMUM_TIMEOUT = 999000
def __init__(self, wallet, passphrase, timeout=MAXIMUM_TIMEOUT):
self.wallet = wallet
self.passphrase = passphrase
self.timeout = timeout
def __enter__(self):
self.wallet.walletpassphrase(self.passphrase, self.timeout)
def __exit__(self, *args):
_ = args
self.wallet.walletlock()