mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-10 14:08:40 +01:00
test: Add Wallet Unlock Context Manager
Add Context Manager to manage wallet locking/unlocking with passphrase
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user