-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_driver_2.py
81 lines (61 loc) · 1.92 KB
/
test_driver_2.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
#%%
from resdb_driver import Resdb
db_root_url = "http://127.0.0.1:18000"
db = Resdb(db_root_url)
from resdb_driver.crypto import generate_keypair
alice, bob = generate_keypair(), generate_keypair()
#%%
# create a digital asset for Alice
game_boy_token = {
"data": {
"token_for": {"game_boy": {"serial_number": "LR35902"}},
"description": "Time share token. Each token equals one hour of usage.",
},
}
#%%
# prepare the transaction with the digital asset and issue 10 tokens for Bob
prepared_token_tx = db.transactions.prepare(
operation="CREATE",
signers=alice.public_key,
recipients=[([bob.public_key], 10)],
asset=game_boy_token,
)
#%%
# fulfill the tnx
fulfilled_token_tx = db.transactions.fulfill(
prepared_token_tx, private_keys=alice.private_key
)
#%%
db.transactions.send_commit(fulfilled_token_tx)
#%%
transfer_asset = {"id": fulfilled_token_tx["id"]}
output_index = 0
output = fulfilled_token_tx["outputs"][output_index]
transfer_input = {
"fulfillment": output["condition"]["details"],
"fulfills": {"output_index": output_index, "transaction_id": transfer_asset["id"]},
"owners_before": output["public_keys"],
}
#%%
# Bob transfers 5 tokens to Alice, and is left with 5 tokens
# The sums of amounts in recipients must match the input amount (10)
prepared_transfer_tx = db.transactions.prepare(
operation="TRANSFER",
asset=transfer_asset,
inputs=transfer_input,
recipients=[([alice.public_key], 5), ([bob.public_key], 5)],
)
#%%
fulfilled_transfer_tx = db.transactions.fulfill(
prepared_transfer_tx, private_keys=bob.private_key
)
#%%
sent_transfer_tx = db.transactions.send_commit(fulfilled_transfer_tx)
# %%
## replace `testId` with the txid for the transaction to retrieve
db.transactions.retrieve(txid=sent_transfer_tx)
#%%
# TODO valide a tx object
# from resdb_driver.validate import Transaction
# t = Transaction.from_dict(fulfilled_token_tx)
# t.validate()