forked from bitdust-io/devel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbismuth_wallet.py
165 lines (125 loc) · 4.35 KB
/
bismuth_wallet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import os
import time
#------------------------------------------------------------------------------
from twisted.internet import reactor # @UnresolvedImport
from twisted.internet.defer import Deferred # @UnresolvedImport
#------------------------------------------------------------------------------
from bitdust_forks.Bismuth.bismuthclient import bismuthclient # @UnresolvedImport
#------------------------------------------------------------------------------
from bitdust.logs import lg
from bitdust.main import settings
from bitdust.blockchain import known_bismuth_nodes
from bitdust.services import driver
#------------------------------------------------------------------------------
_Debug = True
_DebugLevel = 10
#------------------------------------------------------------------------------
_BismuthClient = None
_DataDirPath = None
#------------------------------------------------------------------------------
def init():
global _BismuthClient
global _DataDirPath
_DataDirPath = settings.ServiceDir('bismuth_blockchain')
if driver.is_enabled('service_bismuth_node'):
servers_list = ['127.0.0.1:15658']
else:
servers_list = ['{}:{}'.format(k, v) for k, v in known_bismuth_nodes.nodes_by_host().items()]
_BismuthClient = bismuthclient.BismuthClient(
servers_list=servers_list,
wallet_file=wallet_file_path(),
verbose=_Debug,
)
ret = Deferred()
reactor.callLater(0, check_create_wallet, ret) # @UndefinedVariable
return ret
def shutdown():
global _BismuthClient
global _DataDirPath
del _BismuthClient
_BismuthClient = None
_DataDirPath = None
#------------------------------------------------------------------------------
def client():
global _BismuthClient
return _BismuthClient
def data_dir():
global _DataDirPath
return _DataDirPath
def wallet_file_path(wallet_name=None):
if not wallet_name:
wallet_name = 'wallet'
return os.path.join(data_dir(), wallet_name + '_key.json')
def check_create_wallet(result_defer):
file_path = wallet_file_path()
if _Debug:
lg.args(_DebugLevel, file_path=file_path)
if os.path.isfile(file_path):
if _Debug:
lg.dbg(_DebugLevel, 'wallet file already exists')
else:
if client().new_wallet(file_path):
client().load_wallet(file_path)
else:
result_defer.errback(Exception('error creating wallet'))
return
success = None
count = 0
while True:
if count > 10:
success = False
break
try:
cur_balance = client().balance()
except Exception as e:
lg.warn(e)
time.sleep(5)
count += 1
continue
if _Debug:
lg.args(_DebugLevel, cur_balance=cur_balance, my_wallet_address=my_wallet_address())
if cur_balance == 'N/A':
time.sleep(5)
count += 1
continue
success = True
break
if not success:
result_defer.errback(Exception('error connecting to Bismuth node'))
return
result_defer.callback(True)
def my_wallet_address():
return client().address
def my_balance():
try:
_balance = float(client().balance())
except:
lg.exc()
return 'N/A'
return _balance
def latest_transactions(num, offset, for_display, mempool_included):
return client().latest_transactions(num, offset, for_display, mempool_included)
def send_transaction(recipient, amount, operation='', data='', raise_errors=False):
error_reply = []
ret = client().send(recipient=recipient, amount=amount, operation=operation, data=data, error_reply=error_reply)
if not ret:
if raise_errors:
raise Exception(error_reply)
return error_reply
return ret
def find_transaction(address=None, recipient=None, operation=None, openfield=None, limit=10, offset=0):
try:
ret = client().search_transactions(
address=address,
recipient=recipient,
operation=operation,
openfield=openfield,
limit=limit,
offset=offset,
)
except:
lg.exc()
return []
if _Debug:
lg.args(_DebugLevel, a=address, r=recipient, o=operation, d=openfield, lim=limit, ofs=offset, ret=ret)
return ret