-
Notifications
You must be signed in to change notification settings - Fork 137
/
kmd.py
447 lines (381 loc) · 13 KB
/
kmd.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
from urllib.request import Request, urlopen
from urllib import parse
import urllib.error
import json
import base64
from . import encoding
from . import error
from . import transaction
from . import constants
class KMDClient:
"""
Client class for kmd. Handles all kmd requests.
Args:
kmd_token (str): kmd API token
kmd_address (str): kmd address
Attributes:
kmd_token (str)
kmd_address (str)
"""
def __init__(self, kmd_token, kmd_address):
self.kmd_token = kmd_token
self.kmd_address = kmd_address
def kmd_request(self, method, requrl, params=None, data=None):
"""
Execute a given request.
Args:
method (str): request method
requrl (str): url for the request
params (dict, optional): parameters for the request
data (dict, optional): data in the body of the request
Returns:
dict: loaded from json response body
"""
if requrl in constants.no_auth:
header = {}
else:
header = {
constants.kmd_auth_header: self.kmd_token
}
if requrl not in constants.unversioned_paths:
requrl = constants.api_version_path_prefix + requrl
if params:
requrl = requrl + "?" + parse.urlencode(params)
if data:
data = json.dumps(data, indent=2)
data = bytearray(data, "utf-8")
req = Request(
self.kmd_address+requrl, headers=header,
method=method, data=data)
resp = None
try:
resp = urlopen(req)
except urllib.error.HTTPError as e:
e = e.read().decode("utf-8")
try:
raise error.KMDHTTPError(json.loads(e)["message"])
except:
raise error.KMDHTTPError(e)
return json.loads(resp.read().decode("utf-8"))
def versions(self):
"""
Get kmd versions.
Returns:
str[]: list of versions
"""
req = "/versions"
return self.kmd_request("GET", req)["versions"]
def list_wallets(self):
"""
List all wallets hosted on node.
Returns:
dict[]: list of dictionaries containing wallet information
"""
req = "/wallets"
return self.kmd_request("GET", req)["wallets"]
def create_wallet(self, name, pswd, driver_name="sqlite",
master_deriv_key=None):
"""
Create a new wallet.
Args:
name (str): wallet name
pswd (str): wallet password
driver_name (str, optional): name of the driver
master_deriv_key (str, optional): if recovering a wallet, include
Returns:
dict: dictionary containing wallet information
"""
req = "/wallet"
query = {
"wallet_driver_name": driver_name,
"wallet_name": name,
"wallet_password": pswd
}
if master_deriv_key:
query["master_derivation_key"] = master_deriv_key
return self.kmd_request("POST", req, data=query)["wallet"]
def get_wallet(self, handle):
"""
Get wallet information.
Args:
handle (str): wallet handle token
Returns:
dict: dictionary containing wallet handle and wallet information
"""
req = "/wallet/info"
query = {"wallet_handle_token": handle}
return self.kmd_request("POST", req, data=query)["wallet_handle"]
def init_wallet_handle(self, id, password):
"""
Initialize a handle for the wallet.
Args:
id (str): wallet ID
password (str): wallet password
Returns:
str: wallet handle token
"""
req = "/wallet/init"
query = {
"wallet_id": id,
"wallet_password": password
}
return self.kmd_request("POST", req, data=query)["wallet_handle_token"]
def release_wallet_handle(self, handle):
"""
Deactivate the handle for the wallet.
Args:
handle (str): wallet handle token
Returns:
bool: True if the handle has been deactivated
"""
req = "/wallet/release"
query = {"wallet_handle_token": handle}
result = self.kmd_request("POST", req, data=query)
return result == {}
def renew_wallet_handle(self, handle):
"""
Renew the wallet handle.
Args:
handle (str): wallet handle token
Returns:
dict: dictionary containing wallet handle and wallet information
"""
req = "/wallet/renew"
query = {
"wallet_handle_token": handle
}
return self.kmd_request("POST", req, data=query)["wallet_handle"]
def rename_wallet(self, id, password, new_name):
"""
Rename the wallet.
Args:
id (str): wallet ID
password (str): wallet password
new_name (str): new name for the wallet
Returns:
dict: dictionary containing wallet information
"""
req = "/wallet/rename"
query = {
"wallet_id": id,
"wallet_password": password,
"wallet_name": new_name
}
return self.kmd_request("POST", req, data=query)["wallet"]
def export_master_derivation_key(self, handle, password):
"""
Get the wallet's master derivation key.
Args:
handle (str): wallet handle token
password (str): wallet password
Returns:
str: master derivation key
"""
req = "/master-key/export"
query = {
"wallet_handle_token": handle,
"wallet_password": password
}
result = self.kmd_request("POST", req, data=query)
return result["master_derivation_key"]
def import_key(self, handle, private_key):
"""
Import an account into the wallet.
Args:
handle (str): wallet handle token
private_key (str): private key of account to be imported
Returns:
str: base32 address of the account
"""
req = "/key/import"
query = {
"wallet_handle_token": handle,
"private_key": private_key
}
return self.kmd_request("POST", req, data=query)["address"]
def export_key(self, handle, password, address):
"""
Return an account private key.
Args:
handle (str): wallet handle token
password (str): wallet password
address (str): base32 address of the account
Returns:
str: private key
"""
req = "/key/export"
query = {
"wallet_handle_token": handle,
"wallet_password": password,
"address": address
}
return self.kmd_request("POST", req, data=query)["private_key"]
def generate_key(self, handle, display_mnemonic=True):
"""
Generate a key in the wallet.
Args:
handle (str): wallet handle token
display_mnemonic (bool, optional): whether or not the mnemonic
should be displayed
Returns:
str: base32 address of the generated account
"""
req = "/key"
query = {
"wallet_handle_token": handle
}
return self.kmd_request("POST", req, data=query)["address"]
def delete_key(self, handle, password, address):
"""
Delete a key in the wallet.
Args:
handle (str): wallet handle token
password (str): wallet password
address (str): base32 address of account to be deleted
Returns:
bool: True if the account has been deleted
"""
req = "/key"
query = {
"wallet_handle_token": handle,
"wallet_password": password,
"address": address
}
result = self.kmd_request("DELETE", req, data=query)
return result == {}
def list_keys(self, handle):
"""
List all keys in the wallet.
Args:
handle (str): wallet handle token
Returns:
str[]: list of base32 addresses in the wallet
"""
req = "/key/list"
query = {
"wallet_handle_token": handle
}
result = self.kmd_request("POST", req, data=query)
if result:
return result["addresses"]
return []
def sign_transaction(self, handle, password, txn):
"""
Sign a transaction.
Args:
handle (str): wallet handle token
password (str): wallet password
txn (Transaction): transaction to be signed
Returns:
SignedTransaction: signed transaction with signature of sender
"""
txn = encoding.msgpack_encode(txn)
req = "/transaction/sign"
query = {
"wallet_handle_token": handle,
"wallet_password": password,
"transaction": txn
}
result = self.kmd_request("POST", req, data=query)
result = result["signed_transaction"]
return encoding.msgpack_decode(result)
def list_multisig(self, handle):
"""
List all multisig accounts in the wallet.
Args:
handle (str): wallet handle token
Returns:
str[]: list of base32 multisig account addresses
"""
req = "/multisig/list"
query = {
"wallet_handle_token": handle
}
result = self.kmd_request("POST", req, data=query)
if result == {}:
return []
return result["addresses"]
def import_multisig(self, handle, multisig):
"""
Import a multisig account into the wallet.
Args:
handle (str): wallet handle token
multisig (Multisig): multisig account to be imported
Returns:
str: base32 address of the imported multisig account
"""
req = "/multisig/import"
query = {
"wallet_handle_token": handle,
"multisig_version": multisig.version,
"threshold": multisig.threshold,
"pks": [base64.b64encode(s.public_key).decode()
for s in multisig.subsigs]
}
return self.kmd_request("POST", req, data=query)["address"]
def export_multisig(self, handle, address):
"""
Export a multisig account.
Args:
handle (str): wallet token handle
address (str): base32 address of the multisig account
Returns:
Multisig: multisig object corresponding to the address
"""
req = "/multisig/export"
query = {
"wallet_handle_token": handle,
"address": address
}
result = self.kmd_request("POST", req, data=query)
pks = result["pks"]
pks = [encoding.encode_address(base64.b64decode(p)) for p in pks]
msig = transaction.Multisig(result["multisig_version"],
result["threshold"], pks)
return msig
def delete_multisig(self, handle, password, address):
"""
Delete a multisig account.
Args:
handle (str): wallet handle token
password (str): wallet password
address (str): base32 address of the multisig account to delete
Returns:
bool: True if the multisig account has been deleted
"""
req = "/multisig"
query = {
"wallet_handle_token": handle,
"wallet_password": password,
"address": address
}
result = self.kmd_request("DELETE", req, data=query)
return result == {}
def sign_multisig_transaction(self, handle, password, public_key, mtx):
"""
Sign a multisig transaction for the given public key.
Args:
handle (str): wallet handle token
password (str): wallet password
public_key (str): base32 address that is signing the transaction
mtx (MultisigTransaction): multisig transaction containing
unsigned or partially signed multisig
Returns:
MultisigTransaction: multisig transaction with added signature
"""
partial = mtx.multisig.json_dictify()
txn = encoding.msgpack_encode(mtx.transaction)
public_key = base64.b64encode(encoding.decode_address(public_key))
public_key = public_key.decode()
req = "/multisig/sign"
query = {
"wallet_handle_token": handle,
"wallet_password": password,
"transaction": txn,
"public_key": public_key,
"partial_multisig": partial
}
result = self.kmd_request("POST", req, data=query)["multisig"]
msig = encoding.msgpack_decode(result)
mtx.multisig = msig
return mtx