After some kind of reverse engineering (
), here is the code which works.
It is an example to learn how to use electrum as a python library. Please, correct anything you think I'm doing wrong.
Hope it would be useful for someone trying to understand how the electrum library works.
import electrum
import sys
import time
import socket
# wallet '/root/.electrum/wallets/default_wallet'
CONFIG = electrum.SimpleConfig({
'wallet_path':'/root/.electrum/wallets/default_wallet',
'verbose': True
})
CONFIG.set_key('server', electrum.pick_random_server())
electrum.set_verbosity(CONFIG.get('verbose'))
def syncwallet(wallet,net):
global CONFIG
wallet.start_threads(net)
wallet.update()
max = 10
while not wallet.is_up_to_date():
max-=1
if max < 1:
print("ERROR, cannot connect to electrum servers")
sys.exit(1)
time.sleep(1)
wallet.synchronize()
def get_net():
global CONFIG
net = electrum.network.Network(CONFIG)
net.setDaemon(True)
net.start()
net.random_server()
net.wait_until_connected()
return net
def get_wallet():
global CONFIG
swallet = electrum.WalletStorage(CONFIG)
wallet = electrum.Wallet(swallet)
#print(wallet.storage.path)
return wallet
wallet = get_wallet()
net = get_net()
syncwallet(wallet,net)
cmd = electrum.commands.Commands(wallet,net)
addr = cmd.listaddresses()[0]
print(cmd.getaddressbalance(addr))
print(cmd.getaddressunspent(addr))
print(cmd.getaddresshistory(addr))
#cmd.validateaddress(addr)
#cmd.payto(address,amount,fee)
#cmd.paytomany(outputs,fee)
if wallet: wallet.stop_threads()
if net: net.stop()
Here an example of execution:
root@eniac:~/btcproject# python test.py
wallet path /root/.electrum/wallets/default_wallet
blocks: 298434
connected to electrum.be 50002
connected to electrum.hachre.de 50002
sending subscriptions to electrum.hachre.de:50002:s
connected to e2.pdmc.net 50002
connected to electrum.novit.ro 50002
connected to jwu42.hopto.org 50002
connected to electrum.mindspot.org 50002
synchronizer: connected to electrum.hachre.de:50002:s
0
[]
[]
Sometimes I got this error which I think is because of a server connection error.
I don't know how to handle it, any suggestion?
root@eniac:~/btcproject# python test.py
wallet path /root/.electrum/wallets/default_wallet
blocks: 298434
connected to electrum.novit.ro 50002
connected to bitcoin.epicinet.net 50002
connected to electrum.no-ip.org 50002
synchronizer: connected to ecdsa.org:50002:s
interface: trying to send while not connected
Traceback (most recent call last):
File "test.py", line 50, in
print(cmd.getaddressbalance(addr))
File "/usr/local/lib/python2.7/dist-packages/Electrum-1.9.8-py2.7.egg/electrum/commands.py", line 228, in getaddressbalance
out = self.network.synchronous_get([ ('blockchain.address.get_balance',[addr]) ])[0]
File "/usr/local/lib/python2.7/dist-packages/Electrum-1.9.8-py2.7.egg/electrum/network.py", line 408, in synchronous_get
return self.interface.synchronous_get(requests)
File "/usr/local/lib/python2.7/dist-packages/Electrum-1.9.8-py2.7.egg/electrum/interface.py", line 603, in synchronous_get
id2 = ids[:]
TypeError: 'NoneType' object has no attribute '__getitem__'