test: Add Wallet Unlock Context Manager

Add Context Manager to manage wallet locking/unlocking with passphrase
This commit is contained in:
Brandon Odiwuor
2023-10-09 15:30:40 +03:00
parent db283a6b6f
commit 004903ebad
7 changed files with 140 additions and 125 deletions

View File

@@ -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()