Leading Bitcoin Wallet

The Python Console

Electrum offers a Python console in its graphical user interface (GUI), providing access to most Electrum commands. This feature allows for powerful scripting and direct interaction with your wallet.

Using the Python Console

  1. The console returns Python objects, though they may be displayed as JSON for readability.
  2. Example: Let’s use the listunspent() command to view unspent outputs in the wallet:
>> listunspent()
[
{
"address": "12cmY5RHRgx8KkUKASDcDYRotget9FNso3",
"index": 0,
"raw_output_script": "76a91411bbdc6e3a27c44644d83f783ca7df3bdc2778e688ac",
"tx_hash": "e7029df9ac8735b04e8e957d0ce73987b5c9c5e920ec4a445130cdeca654f096",
"value": 0.01
},
{
"address": "1GavSCND6TB7HuCnJSTEbHEmCctNGeJwXF",
"index": 0,
"raw_output_script": "76a914aaf437e25805f288141bfcdc27887ee5492bd13188ac",
"tx_hash": "b30edf57ca2a31560b5b6e8dfe567734eb9f7d3259bb334653276efe520735df",
"value": 9.04735316
}
]
  1. While the output appears as JSON, it’s actually a Python object. Assigning the result to a variable reveals its true nature as a Python list of dictionaries.
u = listunspent()
u
[{'tx_hash': 'e7029df9ac8735b04e8e957d0ce73987b5c9c5e920ec4a445130cdeca654f096', 'index': 0, 'raw_output_script': '76a91411bbdc6e3a27c44644d83f783ca7df3bdc2778e688ac', 'value': 0.01, 'address': '12cmY5RHRgx8KkUKASDcDYRotget9FNso3'}, {'tx_hash': 'b30edf57ca2a31560b5b6e8dfe567734eb9f7d3259bb334653276efe520735df', 'index': 0, 'raw_output_script': '76a914aaf437e25805f288141bfcdc27887ee5492bd13188ac', 'value': 9.04735316, 'address': '1GavSCND6TB7HuCnJSTEbHEmCctNGeJwXF'}]
  1. You can use Python functions with Electrum commands. For example, to extract addresses from unspent outputs:
>> map(lambda x:x.get('address'), listunspent())
[
"12cmY5RHRgx8KkUKASDcDYRotget9FNso3",
"1GavSCND6TB7HuCnJSTEbHEmCctNGeJwXF"
]
  1. Combine multiple commands for complex operations. This example dumps private keys for addresses with unspent outputs:
>> dumpprivkeys( map(lambda x:x.get('address'), listunspent()) )
{
"12cmY5RHRgx8KkUKASDcDYRotget9FNso3": "***************************************************",
"1GavSCND6TB7HuCnJSTEbHEmCctNGeJwXF": "***************************************************"
}

Note: dumpprivkey will prompt for your password if your wallet is encrypted.

  1. GUI methods are available through the window variable. For instance, to display a QR code:
window.show_qrcode(dumpprivkey(listunspent()[0]['address']))

Table of Contents