Last active
August 29, 2015 14:04
-
-
Save chill117/5e11a07a93262f4ef2a8 to your computer and use it in GitHub Desktop.
Automatically adjusts the gap limit in the default wallet of Electrum so that the given address is included in the list of addresses.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Place this file in the Electrum scripts directory. Then run from command line, like this: `~/.electrum/scripts/adjust_gap_limit_to_find_address.py`. | |
import sys | |
from electrum import bitcoin, Network, print_json, SimpleConfig, Wallet, WalletStorage | |
try: | |
addr = sys.argv[1] | |
except Exception: | |
print 'Usage: ' + sys.argv[0] + ' <bitcoin_address>' | |
sys.exit(1) | |
if not bitcoin.is_valid(addr): | |
print 'Not a valid bitcoin address' | |
sys.exit(1) | |
config = SimpleConfig() | |
storage = WalletStorage(config) | |
wallet = Wallet(storage) | |
found = True | |
try: | |
wallet.get_address_index(addr) | |
except Exception: | |
found = False | |
if found: | |
print 'Found!' | |
sys.exit(0) | |
else: | |
def get_address_by_index(index): | |
if hasattr(wallet, 'default_account') and callable(getattr(wallet, 'default_account')): | |
default_account = wallet.default_account() | |
else: | |
default_account = wallet.accounts[0] | |
return default_account.get_address(0, index) | |
def get_index_of_last_used_address(): | |
addresses = wallet.addresses(False) | |
for address in addresses: | |
if wallet.history.get(address): | |
return addresses.index(address) | |
def synchronize(): | |
network = Network(config) | |
network.start(True) | |
if not network.is_connected: | |
print 'Not connected' | |
sys.exit(1) | |
wallet.start_threads(network) | |
wallet.restore(lambda x: x) | |
wallet.save_accounts() | |
addresses = wallet.addresses(False) | |
current_gap_limit = config.get('gap_limit') | |
current_total = len(addresses) | |
index = current_total | |
while True: | |
# Prevent an infinite loop. | |
if (current_total - (index + 1)) > 1000: | |
break | |
index += 1 | |
address = get_address_by_index(index) | |
if address == addr: | |
found = True | |
break | |
index_of_last_used_address = get_index_of_last_used_address() | |
new_gap_limit = (index - index_of_last_used_address) | |
if new_gap_limit == current_gap_limit: | |
print 'Gap limit unchanged.' | |
else: | |
wallet.storage.put('gap_limit', new_gap_limit) | |
print 'Gap limit changed to ' + str(new_gap_limit) + '.' | |
# Make sure the gap limit is set correctly in the wallet object. | |
wallet.gap_limit = new_gap_limit | |
synchronize() | |
if not found: | |
print 'Failed to find address.' | |
sys.exit(1) | |
else: | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment