Author

Topic: Python library for bitcoinwallet without blockchain data? (Read 1117 times)

newbie
Activity: 13
Merit: 0
Yeah, reliance on the centralized services does cause a point of failure that you might not want. Amir's suggestion of python-obelisk might be a better choice in that case.
p4u
member
Activity: 111
Merit: 10
crypto lover
It looks nice, but I don't like that "Relies on centralized service (blockchain.info) for blockchain operations, although operations do have backups (eligius, blockr.io)".

I could also use directly an API to blockchain.info

newbie
Activity: 13
Merit: 0
Have you looked at pybitcointools? It's very nice, and does not do any blockchain related stuff. It has support for multisig and electrum style deterministic wallets. It uses blockchain.info or blockr.io to send transactions to the network.
legendary
Activity: 1526
Merit: 1134
You could also use bitcoinj via Jython. A few people have reported success with that.
p4u
member
Activity: 111
Merit: 10
crypto lover
After some kind of reverse engineering ( Tongue ), 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.

Code:
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:

Code:
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?

Code:
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__'
p4u
member
Activity: 111
Merit: 10
crypto lover
Thanks genjix, I'll take a look on python-obelisk.

Yesterday I was trying to use electrum as a python library, after reading some code I ended up with a proof of concept:

Code:
import electrum
config = electrum.SimpleConfig({'wallet_path':'/root/.electrum/wallets/default_wallet'})
net = electrum.network.Network()
net.start()
swallet = electrum.WalletStorage(config)
wallet = electrum.Wallet(swallet)
wallet.start_threads(net)
wallet.update()
ec = electrum.commands.Commands(wallet,net)
print(ec.getbalance())

Quote
root@eniac:~/btcproject# python test.py
{'confirmed': '0'}

However I'm pretty sure I'm not using properly some functions. I'm not able to find any documentation related with that. Do you guys know about something (an example would be enough) ?

edit: typo
legendary
Activity: 1232
Merit: 1076
I'm going to develop a bitcoin related application, so I need to have access to a bitcoin wallet from the python code.
I don't want to have the entire blockchain stored, so I'm looking for alternatives. What do you recommend?

Electrum seems quite good but I don't find any API documentation.

https://github.com/darkwallet/python-obelisk
p4u
member
Activity: 111
Merit: 10
crypto lover
I'm going to develop a bitcoin related application, so I need to have access to a bitcoin wallet from the python code.
I don't want to have the entire blockchain stored, so I'm looking for alternatives. What do you recommend?

Electrum seems quite good but I don't find any API documentation.
Jump to: