Bitcoin Forum
May 28, 2024, 09:53:17 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Python library for bitcoinwallet without blockchain data?  (Read 1102 times)
p4u (OP)
Member
**
Offline Offline

Activity: 111
Merit: 10

crypto lover


View Profile
April 28, 2014, 08:43:16 PM
 #1

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.
genjix
Legendary
*
Offline Offline

Activity: 1232
Merit: 1076


View Profile
April 30, 2014, 03:56:36 AM
 #2

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 (OP)
Member
**
Offline Offline

Activity: 111
Merit: 10

crypto lover


View Profile
April 30, 2014, 07:17:21 AM
 #3

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
p4u (OP)
Member
**
Offline Offline

Activity: 111
Merit: 10

crypto lover


View Profile
April 30, 2014, 08:57:13 AM
 #4

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 <module>
    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__'
Mike Hearn
Legendary
*
Offline Offline

Activity: 1526
Merit: 1129


View Profile
April 30, 2014, 12:02:42 PM
 #5

You could also use bitcoinj via Jython. A few people have reported success with that.
christianlundkvist
Newbie
*
Offline Offline

Activity: 13
Merit: 0


View Profile
May 01, 2014, 06:23:37 PM
 #6

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.
p4u (OP)
Member
**
Offline Offline

Activity: 111
Merit: 10

crypto lover


View Profile
May 01, 2014, 07:06:53 PM
 #7

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

christianlundkvist
Newbie
*
Offline Offline

Activity: 13
Merit: 0


View Profile
May 01, 2014, 11:28:01 PM
 #8

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.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!