-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
281 lines (250 loc) · 9.29 KB
/
api.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import requests
class IdenaAPI:
_host = "localhost"
_port = "9009"
_timeout = 3
_api_key = None
def __init__(self, host=_host, port=_port, timeout=_timeout, api_key=_api_key):
self._host = host
self._port = port
self._timeout = timeout
self._api_key = api_key
self.url = f"http://{host}:{port}"
def _request(self, url, payload):
try:
return requests.post(url, json=payload, timeout=self._timeout).json()
except Exception as e:
return {"error": {"message": str(e), "code": 0}}
def identities(self, api_key=None):
""" List all identities (not only validated ones) """
payload = {
"key": api_key if api_key else self._api_key,
"method": "dna_identities",
"id": 1
}
return self._request(self.url, payload)
def identity(self, address, api_key=None):
""" Show info about identity for given address """
payload = {
"key": api_key if api_key else self._api_key,
"method": "dna_identity",
"params": [address],
"id": 1
}
return self._request(self.url, payload)
def epoch(self, api_key=None):
""" Details about the current epoch """
payload = {
"key": api_key if api_key else self._api_key,
"method": "dna_epoch",
"id": 1
}
return self._request(self.url, payload)
def ceremony_intervals(self, api_key=None):
""" Show info about validation ceremony """
payload = {
"key": api_key if api_key else self._api_key,
"method": "dna_ceremonyIntervals",
"id": 1
}
return self._request(self.url, payload)
def address(self, api_key=None):
""" Show address for current identity """
payload = {
"key": api_key if api_key else self._api_key,
"method": "dna_getCoinbaseAddr",
"id": 1
}
return self._request(self.url, payload)
def balance(self, address, api_key=None):
""" Show DNA balance for address """
payload = {
"key": api_key if api_key else self._api_key,
"method": "dna_getBalance",
"params": [address],
"id": 1
}
return self._request(self.url, payload)
def transaction(self, trx_hash, api_key=None):
""" Details about a specific transaction """
payload = {
"key": api_key if api_key else self._api_key,
"method": "bcn_transaction",
"params": [trx_hash],
"id": 1
}
return self._request(self.url, payload)
def transactions(self, address, count, api_key=None):
""" List specific number of transactions for given address """
payload = {
"key": api_key if api_key else self._api_key,
"method": "bcn_transactions",
"params": [{"address": f"{address}", "count": int(count)}],
"id": 1
}
return self._request(self.url, payload)
def pending_transactions(self, address, count, api_key=None):
""" List specific number of pending transactions for given address """
payload = {
"key": api_key if api_key else self._api_key,
"method": "bcn_pendingTransactions",
"params": [{"address": f"{address}", "count": int(count)}],
"id": 1
}
return self._request(self.url, payload)
def kill_identity(self, address, api_key=None):
""" Kill your identity """
payload = {
"key": api_key if api_key else self._api_key,
"method": "dna_sendTransaction",
"params": [{"type": 3, "from": f"{address}", "to": f"{address}"}],
"id": 1
}
return self._request(self.url, payload)
def go_online(self, api_key=None):
""" Go online, serve as a valid node and start mining """
payload = {
"key": api_key if api_key else self._api_key,
"method": "dna_becomeOnline",
"params": [{}],
"id": 1
}
return self._request(self.url, payload)
def go_offline(self, api_key=None):
""" Go offline, do not serve as a node and stop mining """
payload = {
"key": api_key if api_key else self._api_key,
"method": "dna_becomeOffline",
"params": [{}],
"id": 1
}
return self._request(self.url, payload)
# TODO: Untested!
def send_invite(self, to_address, amount, api_key=None):
""" Send invite code to given address """
payload = {
"key": api_key if api_key else self._api_key,
"method": "dna_sendInvite",
"params": [{"to": to_address, "amount": amount}],
"id": 1
}
return self._request(self.url, payload)
# TODO: Untested!
def activate_invite(self, to_address, key, api_key=None):
""" Send invite code to given address """
payload = {
"key": api_key if api_key else self._api_key,
"method": "dna_activateInvite",
"params": [{"to": to_address, "key": key}],
"id": 1
}
return self._request(self.url, payload)
# TODO: Untested!
def fetch_flip_short_hashes(self, api_key=None):
""" Get hashes for flips in short session """
payload = {
"key": api_key if api_key else self._api_key,
"method": "flip_shortHashes",
"id": 1
}
return self._request(self.url, payload)
# TODO: Untested!
def fetch_flip_long_hashes(self, api_key=None):
""" Get hashes for flips in long session """
payload = {
"key": api_key if api_key else self._api_key,
"method": "flip_longHashes",
"id": 1
}
return self._request(self.url, payload)
# TODO: Untested!
def get_flip(self, flip_hash, api_key=None):
""" Show info about flip by providing his hash """
payload = {
"key": api_key if api_key else self._api_key,
"method": "flip_get",
"params": [flip_hash],
"id": 1
}
return self._request(self.url, payload)
# TODO: Untested!
def submit_short_answers(self, answers, nonce, epoch, api_key=None):
""" Show info about flip by providing his hash """
payload = {
"key": api_key if api_key else self._api_key,
"method": "flip_submitShortAnswers",
"params": [{answers, nonce, epoch}],
"id": 1
}
return self._request(self.url, payload)
# TODO: Untested!
def submit_long_answers(self, answers, nonce, epoch, api_key=None):
""" Show info about flip by providing his hash """
payload = {
"key": api_key if api_key else self._api_key,
"method": "flip_submitLongAnswers",
"params": [{answers, nonce, epoch}],
"id": 1
}
return self._request(self.url, payload)
# TODO: Untested!
def submit_flip(self, flip_hex, pair_id, api_key=None):
""" """
payload = {
"key": api_key if api_key else self._api_key,
"method": "flip_submit",
"params": [{"hex": flip_hex, "pair": pair_id}],
"id": 1
}
return self._request(self.url, payload)
def send(self, from_address, to_address, amount, api_key=None):
""" Send amount of DNA from address to address """
payload = {
"key": api_key if api_key else self._api_key,
"method": "dna_sendTransaction",
"params": [{"from": from_address, "to": to_address, "amount": amount}],
"id": 1
}
return self._request(self.url, payload)
def sync_status(self, api_key=None):
""" Show if node is synchronized """
payload = {
"key": api_key if api_key else self._api_key,
"method": "bcn_syncing",
"id": 1
}
return self._request(self.url, payload)
def node_version(self, api_key=None):
""" Show node version """
payload = {
"key": api_key if api_key else self._api_key,
"method": "dna_version",
"id": 1
}
return self._request(self.url, payload)
def import_key(self, key, password, api_key=None):
""" Import private key to manage specific identity """
payload = {
"key": api_key if api_key else self._api_key,
"method": "dna_importKey",
"params": [{"key": key, "password": password}],
"id": 1
}
return self._request(self.url, payload)
def export_key(self, password, api_key=None):
""" Export private key to backup your identity """
payload = {
"key": api_key if api_key else self._api_key,
"method": "dna_exportKey",
"params": [password],
"id": 1
}
return self._request(self.url, payload)
def enode(self, api_key=None):
""" Get info abut enode: ID, IP and Port """
payload = {
"key": api_key if api_key else self._api_key,
"method": "net_enode",
"id": 1
}
return self._request(self.url, payload)