-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigchainDB_sample_code.py
147 lines (116 loc) · 4.07 KB
/
BigchainDB_sample_code.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Import modules
import bigchaindb_driver
from bigchaindb_driver import BigchainDB
from bigchaindb_driver.crypto import generate_keypair
from time import sleep
from sys import exit
# Create instance of BigChainDB
bdb__root_url = 'https://test.bigchaindb.com:443'
tokens = {}
tokens['app_id'] = 'e037d444'
tokens['app_key'] = '22cc48d79f9d5fcebcd2206af739014f'
database = BigchainDB('https://test.bigchaindb.com', headers=tokens)
# Digital asset definition
docker = {
'data': {
'docker': {
'id': 'reallyawesomedocker',
'developer': 'user1'
},
},
}
# Generate keypairs
user1, user2 = generate_keypair(), generate_keypair()
# Create digital asset
prepared_asset_tx = database.transactions.prepare(
operation='CREATE',
signers=user1.public_key,
asset=docker,
)
# Fulfill and send transaction
fulfilled_asset_tx = database.transactions.fulfill(
prepared_asset_tx,
private_keys=user1.private_key
)
sent_asset_tx = database.transactions.send(fulfilled_asset_tx)
print(sent_asset_tx == fulfilled_asset_tx) # Test
txid = fulfilled_asset_tx['id'] # Store transaction ID
# Check status of transaction
trials = 0
while trials < 100:
try:
if database.transactions.status(txid).get('status') == 'valid':
print("Asset is registered on the blockchain.")
break
except bigchaindb_driver.exceptions.NotFoundError:
trials += 1
sleep(1)
if trials == 100:
print('Tx is still being processed...Check again later.')
exit(0)
# Retrieve validated transaction
tx_retrieved = database.transactions.retrieve(prepared_asset_tx['id'])
# Prepare transfer transaction
asset_tx = fulfilled_asset_tx
asset_id = asset_tx['id']
transfer_asset = {
'id': asset_id
}
output_index = 0
output = asset_tx['outputs'][output_index]
transfer_input = {
'fulfillment': output['condition']['details'],
'fulfills': {
'output_index': output_index,
'transaction_id': asset_tx['id']
},
'owners_before': output['public_keys']
}
prepared_transfer_tx = database.transactions.prepare(
operation='TRANSFER',
asset=transfer_asset,
inputs=transfer_input,
recipients=user2.public_key
)
# Fulfill and send transaction
fulfilled_transfer_tx = database.transactions.fulfill(
prepared_transfer_tx,
private_keys=user1.private_key
)
sent_transfer_tx = database.transactions.send(fulfilled_transfer_tx)
if sent_transfer_tx['inputs'][0]['owners_before'][0] == user1.public_key and sent_transfer_tx['outputs'][0]['public_keys'][0] == user2.public_key:
print("Asset successfully transferred from User1 to User2.")
# Creating divisible asset
docker_rights = {
'data': {
'rights_to': {
'docker': {
'id': 'reallyawesomedocker',
'developer': 'user1'
}
},
'description': 'Rights to assets. 100 rights to be shared.',
},
}
developer = generate_keypair()
customer = generate_keypair()
prepared_rights_tx = database.transactions.prepare(operation = 'CREATE', signers = developer.public_key, recipients = [([developer.public_key], 100)], asset = docker_rights)
fulfilled_rights_tx = database.transactions.fulfill(prepared_rights_tx, private_keys = developer.private_key)
sent_rights_tx = database.transactions.send(fulfilled_rights_tx)
txid = fulfilled_rights_tx['id']
trials = 0
while trials < 100:
try:
if database.transactions.status(txid).get('status') == 'valid':
print("Developer successfully created 100 rights for docker, and assigned them to themselves.")
break
except bigchaindb_driver.exceptions.NotFoundError:
trials += 1
sleep(1)
if trials == 100:
print('Tx is still being processed...Check again later.')
exit(0)
developer_rights = fulfilled_rights_tx['outputs'][0]['amount']
print(developer_rights)