forked from algorand/py-algorand-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_integration.py
501 lines (392 loc) · 17.6 KB
/
test_integration.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import base64
import unittest
from examples import tokens
import os
from algosdk import kmd
from algosdk.future import transaction
from algosdk import encoding
from algosdk import algod
from algosdk import account
from algosdk import mnemonic
from algosdk import error
from algosdk import auction
from algosdk import constants
from algosdk import wallet
wallet_name = "unencrypted-default-wallet"
wallet_pswd = ""
class TestIntegration(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.acl = algod.AlgodClient(tokens.algod_token, tokens.algod_address)
cls.kcl = kmd.KMDClient(tokens.kmd_token, tokens.kmd_address)
w = wallet.Wallet(wallet_name, wallet_pswd, cls.kcl)
keys = w.list_keys()
max_balance = 0
cls.account_0 = ""
for k in keys:
account_info = cls.acl.account_info(k)
if account_info["amount"] > max_balance:
max_balance = account_info["amount"]
cls.account_0 = k
def test_auction(self):
# get the default wallet
wallets = self.kcl.list_wallets()
wallet_id = None
for w in wallets:
if w["name"] == wallet_name:
wallet_id = w["id"]
# get a new handle for the wallet
handle = self.kcl.init_wallet_handle(wallet_id, wallet_pswd)
# generate account with kmd
account_1 = self.kcl.generate_key(handle, False)
# get self.account_0 private key
private_key_0 = self.kcl.export_key(handle, wallet_pswd,
self.account_0)
# create bid
bid = auction.Bid(self.account_0, 10000, 260,
"bid_id", account_1, "auc_id")
sb = bid.sign(private_key_0)
nf = auction.NoteField(sb, constants.note_field_type_bid)
# get suggested parameters and fee
gh = self.acl.versions()['genesis_hash_b64']
rnd = int(self.acl.status()['lastRound'])
sp = transaction.SuggestedParams(0, rnd, rnd + 100, gh)
# create transaction
txn = transaction.PaymentTxn(self.account_0, sp,
account_1, 100000, note=base64.b64decode(
encoding.msgpack_encode(nf)))
# sign transaction with account
signed_account = txn.sign(private_key_0)
# send transaction
send = self.acl.send_transaction(signed_account)
self.assertEqual(send, txn.get_txid())
del_1 = self.kcl.delete_key(handle, wallet_pswd, account_1)
self.assertTrue(del_1)
def test_handle(self):
# create wallet; should raise error since wallet already exists
self.assertRaises(error.KMDHTTPError, self.kcl.create_wallet,
wallet_name, wallet_pswd)
# get the wallet ID
wallets = self.kcl.list_wallets()
wallet_id = None
for w in wallets:
if w["name"] == wallet_name:
wallet_id = w["id"]
# rename wallet
self.assertEqual(wallet_name + "newname", self.kcl.rename_wallet(
wallet_id, wallet_pswd,
wallet_name + "newname")["name"])
# change it back
self.assertEqual(wallet_name, self.kcl.rename_wallet(wallet_id,
wallet_pswd, wallet_name)["name"])
# get a new handle for the wallet
handle = self.kcl.init_wallet_handle(wallet_id, wallet_pswd)
# get wallet
self.assertIn("expires_seconds", self.kcl.get_wallet(handle))
# renew the handle
renewed_handle = self.kcl.renew_wallet_handle(handle)
self.assertIn("expires_seconds", renewed_handle)
# release the handle
released = self.kcl.release_wallet_handle(handle)
self.assertTrue(released)
# check that the handle has been released
self.assertRaises(error.KMDHTTPError, self.kcl.get_wallet, handle)
def test_transaction(self):
# get the default wallet
wallets = self.kcl.list_wallets()
wallet_id = None
for w in wallets:
if w["name"] == wallet_name:
wallet_id = w["id"]
# get a new handle for the wallet
handle = self.kcl.init_wallet_handle(wallet_id, wallet_pswd)
# generate account and check if it's valid
private_key_1, account_1 = account.generate_account()
self.assertTrue(encoding.is_valid_address(account_1))
# import generated account
import_key = self.kcl.import_key(handle, private_key_1)
self.assertEqual(import_key, account_1)
# generate account with kmd
account_2 = self.kcl.generate_key(handle, False)
# get suggested parameters and fee
gh = self.acl.versions()['genesis_hash_b64']
rnd = int(self.acl.status()['lastRound'])
sp = transaction.SuggestedParams(0, rnd, rnd + 100, gh)
# create transaction
txn = transaction.PaymentTxn(self.account_0, sp,
account_1, 100000)
# sign transaction with kmd
signed_kmd = self.kcl.sign_transaction(handle, wallet_pswd, txn)
# get self.account_0 private key
private_key_0 = self.kcl.export_key(handle, wallet_pswd,
self.account_0)
# sign transaction with account
signed_account = txn.sign(private_key_0)
txid = txn.get_txid()
# check that signing both ways results in the same thing
self.assertEqual(encoding.msgpack_encode(signed_account),
encoding.msgpack_encode(signed_kmd))
# send the transaction
send = self.acl.send_transaction(signed_account)
self.assertEqual(send, txid)
# get transaction info in pending transactions
self.assertEqual(self.acl.pending_transaction_info(txid)["tx"], txid)
# wait for transaction to send
self.acl.status_after_block(sp.first+2)
# get transaction info two different ways
info_1 = self.acl.transactions_by_address(self.account_0, sp.first-2,
sp.first+2)
info_2 = self.acl.transaction_info(self.account_0, txid)
self.assertIn("transactions", info_1)
self.assertIn("type", info_2)
# delete accounts
del_1 = self.kcl.delete_key(handle, wallet_pswd, account_1)
del_2 = self.kcl.delete_key(handle, wallet_pswd, account_2)
self.assertTrue(del_1)
self.assertTrue(del_2)
def test_multisig(self):
# get the default wallet
wallets = self.kcl.list_wallets()
wallet_id = None
for w in wallets:
if w["name"] == wallet_name:
wallet_id = w["id"]
# get a new handle for the wallet
handle = self.kcl.init_wallet_handle(wallet_id, wallet_pswd)
# generate two accounts with kmd
account_1 = self.kcl.generate_key(handle, False)
account_2 = self.kcl.generate_key(handle, False)
# get their private keys
private_key_1 = self.kcl.export_key(handle, wallet_pswd, account_1)
private_key_2 = self.kcl.export_key(handle, wallet_pswd, account_2)
# get suggested parameters and fee
gh = self.acl.versions()['genesis_hash_b64']
rnd = int(self.acl.status()['lastRound'])
sp = transaction.SuggestedParams(0, rnd, rnd + 100, gh)
# create multisig account and transaction
msig = transaction.Multisig(1, 2, [account_1, account_2])
txn = transaction.PaymentTxn(msig.address(), sp,
self.account_0, 1000)
# check that the multisig account is valid
msig.validate()
# import multisig account
msig_address = self.kcl.import_multisig(handle, msig)
# export multisig account
exported = self.kcl.export_multisig(handle, msig_address)
self.assertEqual(len(exported.subsigs), 2)
# create multisig transaction
mtx = transaction.MultisigTransaction(txn, msig)
# sign using kmd
msig_1 = self.kcl.sign_multisig_transaction(handle, wallet_pswd,
account_1, mtx)
signed_kmd = self.kcl.sign_multisig_transaction(handle, wallet_pswd,
account_2, msig_1)
# sign offline
mtx1 = transaction.MultisigTransaction(txn, msig)
mtx1.sign(private_key_1)
mtx2 = transaction.MultisigTransaction(txn, msig)
mtx2.sign(private_key_2)
signed_account = transaction.MultisigTransaction.merge([mtx1, mtx2])
# check that they are the same
self.assertEqual(encoding.msgpack_encode(signed_account),
encoding.msgpack_encode(signed_kmd))
# delete accounts
del_1 = self.kcl.delete_key(handle, wallet_pswd, account_1)
del_2 = self.kcl.delete_key(handle, wallet_pswd, account_2)
del_3 = self.kcl.delete_multisig(handle, wallet_pswd, msig_address)
self.assertTrue(del_1)
self.assertTrue(del_2)
self.assertTrue(del_3)
def test_wallet_info(self):
# get the default wallet
wallets = self.kcl.list_wallets()
wallet_id = None
for w in wallets:
if w["name"] == wallet_name:
wallet_id = w["id"]
# get a new handle for the wallet
handle = self.kcl.init_wallet_handle(wallet_id, wallet_pswd)
# test listKeys
list_keys = self.kcl.list_keys(handle)
self.assertIn(self.account_0, list_keys)
# test listMultisig
list_multisig = self.kcl.list_multisig(handle)
self.assertIsInstance(list_multisig, list)
# either addresses are listed or there are no multisig accounts
# test getting the master derivation key
mdk = self.kcl.export_master_derivation_key(handle, wallet_pswd)
self.assertIsInstance(mdk, str)
def test_wallet(self):
# initialize wallet
w = wallet.Wallet(wallet_name, wallet_pswd, self.kcl)
# get master derivation key
mdk = w.export_master_derivation_key()
# get mnemonic
mn = w.get_mnemonic()
# make sure mnemonic can be converted back to mdk
self.assertEqual(mdk, mnemonic.to_master_derivation_key(mn))
# generate account with account and check if it's valid
private_key_1, account_1 = account.generate_account()
# import generated account
import_key = w.import_key(private_key_1)
self.assertEqual(import_key, account_1)
# check that the account is in the wallet
keys = w.list_keys()
self.assertIn(account_1, keys)
# generate account with kmd
account_2 = w.generate_key()
private_key_2 = w.export_key(account_2)
# get suggested parameters and fee
gh = self.acl.versions()['genesis_hash_b64']
rnd = int(self.acl.status()['lastRound'])
sp = transaction.SuggestedParams(0, rnd, rnd + 100, gh)
# create transaction
txn = transaction.PaymentTxn(self.account_0, sp,
account_1, 100000)
# sign transaction with wallet
signed_kmd = w.sign_transaction(txn)
# get self.account_0 private key
private_key_0 = w.export_key(self.account_0)
# sign transaction with account
signed_account = txn.sign(private_key_0)
# check that signing both ways results in the same thing
self.assertEqual(encoding.msgpack_encode(signed_account),
encoding.msgpack_encode(signed_kmd))
# create multisig account and transaction
msig = transaction.Multisig(1, 2, [account_1, account_2])
txn = transaction.PaymentTxn(msig.address(), sp,
self.account_0, 1000)
# import multisig account
msig_address = w.import_multisig(msig)
# check that the multisig account is listed
msigs = w.list_multisig()
self.assertIn(msig_address, msigs)
# export multisig account
exported = w.export_multisig(msig_address)
self.assertEqual(len(exported.subsigs), 2)
# create multisig transaction
mtx = transaction.MultisigTransaction(txn, msig)
# sign the multisig using kmd
msig_1 = w.sign_multisig_transaction(account_1, mtx)
signed_kmd = w.sign_multisig_transaction(account_2, msig_1)
# sign the multisig offline
mtx1 = transaction.MultisigTransaction(txn, msig)
mtx1.sign(private_key_1)
mtx2 = transaction.MultisigTransaction(txn, msig)
mtx2.sign(private_key_2)
signed_account = transaction.MultisigTransaction.merge([mtx1, mtx2])
# check that they are the same
self.assertEqual(encoding.msgpack_encode(signed_account),
encoding.msgpack_encode(signed_kmd))
# delete accounts
del_1 = w.delete_key(account_1)
del_2 = w.delete_key(account_2)
del_3 = w.delete_multisig(msig_address)
self.assertTrue(del_1)
self.assertTrue(del_2)
self.assertTrue(del_3)
# test renaming the wallet
w.rename(wallet_name + "1")
self.assertEqual(wallet_name + "1", w.info()["wallet"]["name"])
w.rename(wallet_name)
self.assertEqual(wallet_name, w.info()["wallet"]["name"])
# test releasing the handle
w.release_handle()
self.assertRaises(error.KMDHTTPError, self.kcl.get_wallet, w.handle)
# test handle automation
w.info()
def test_file_read_write(self):
# get suggested parameters and fee
gh = self.acl.versions()['genesis_hash_b64']
rnd = int(self.acl.status()['lastRound'])
sp = transaction.SuggestedParams(0, rnd, rnd + 100, gh)
# create transaction
txn = transaction.PaymentTxn(self.account_0, sp,
self.account_0, 1000)
# get private key
w = wallet.Wallet(wallet_name, wallet_pswd, self.kcl)
private_key = w.export_key(self.account_0)
# sign transaction
stx = txn.sign(private_key)
# write to file
dir_path = os.path.dirname(os.path.realpath(__file__))
transaction.write_to_file([txn, stx], dir_path + "/raw.tx")
# read from file
txns = transaction.retrieve_from_file(dir_path + "/raw.tx")
# check that the transactions are still the same
self.assertEqual(encoding.msgpack_encode(txn),
encoding.msgpack_encode(txns[0]))
self.assertEqual(encoding.msgpack_encode(stx),
encoding.msgpack_encode(txns[1]))
# delete the file
os.remove("raw.tx")
def test_health(self):
result = self.acl.health()
self.assertEqual(result, None)
def test_status_after_block(self):
last_round = self.acl.status()["lastRound"]
curr_round = self.acl.status_after_block(last_round)["lastRound"]
self.assertEqual(last_round+1, curr_round)
def test_pending_transactions(self):
result = self.acl.pending_transactions(0)
self.assertIn("truncatedTxns", result)
def test_algod_versions(self):
result = self.acl.versions()
self.assertIn("versions", result)
def test_ledger_supply(self):
result = self.acl.ledger_supply()
self.assertIn("totalMoney", result)
def test_block_info(self):
last_round = self.acl.status()["lastRound"]
result = self.acl.block_info(last_round)
self.assertIn("hash", result)
def test_kmd_versions(self):
result = self.kcl.versions()
self.assertIn("v1", result)
def test_suggested_fee(self):
result = self.acl.suggested_fee()
self.assertIn("fee", result)
def test_transaction_group(self):
# get the default wallet
wallets = self.kcl.list_wallets()
wallet_id = None
for w in wallets:
if w["name"] == wallet_name:
wallet_id = w["id"]
# get a new handle for the wallet
handle = self.kcl.init_wallet_handle(wallet_id, wallet_pswd)
# get private key
private_key_0 = self.kcl.export_key(handle, wallet_pswd,
self.account_0)
# get suggested parameters and fee
gh = self.acl.versions()['genesis_hash_b64']
rnd = int(self.acl.status()['lastRound'])
sp = transaction.SuggestedParams(0, rnd, rnd + 100, gh)
# create transaction
txn = transaction.PaymentTxn(self.account_0, sp,
self.account_0, 1000)
# calculate group id
gid = transaction.calculate_group_id([txn])
txn.group = gid
# sign using kmd
stxn1 = self.kcl.sign_transaction(handle, wallet_pswd, txn)
# sign using transaction call
stxn2 = txn.sign(private_key_0)
# check that they are the same
self.assertEqual(encoding.msgpack_encode(stxn1),
encoding.msgpack_encode(stxn2))
try:
send = self.acl.send_transactions([stxn1])
self.assertEqual(send, txn.get_txid())
except error.AlgodHTTPError as ex:
self.assertIn(
"TransactionPool.Remember: transaction groups not supported",
str(ex))
if __name__ == "__main__":
to_run = [TestIntegration]
loader = unittest.TestLoader()
suites = [loader.loadTestsFromTestCase(test_class)
for test_class in to_run]
suite = unittest.TestSuite(suites)
runner = unittest.TextTestRunner(verbosity=2)
results = runner.run(suite)