For those who wish to assemble transaction payloads "by hand", with examples in Python.
Note
The contents are presented for BigchainDB 0.8. The transaction schema is constantly evolving at this stage and the current contents may be outdated by a new release.
Submitting a transaction to a BigchainDB node consists of three main steps:
- Preparing the transaction payload;
- Fulfilling the prepared transaction payload; and
- Sending the transaction payload via HTTPS.
Step 1 and 2 can be performed offline on the client. That is, they do not require any connection to any BigchainDB node.
For convenience's sake, some utilites are provided to prepare and fulfill a transaction via the ~.bigchaindb_driver.BigchainDB
class, and via the ~bigchaindb_driver.offchain
module. For an introduction on using these utilities, see the basic-usage
or advanced-usage
sections.
The rest of this document will guide you through completing steps 1 and 2 manually by revisiting some of the examples provided in the usage sections. We will:
- provide all values, including the default ones;
- generate the transaction id;
- learn to use crypto-conditions to generate a condition that locks the transaction, hence protecting it from being consumed by an unauthorized user;
- learn to use crypto-conditions to generate a fulfillment that unlocks the transaction asset, and consequently enact an ownership transfer.
In order to perform all of the above, we'll use the following Python libraries:
json
: to serialize the transaction dictionary into a JSON formatted string;- sha3: to hash the serialized transaction; and
- cryptoconditions: to create conditions and fulfillments
For detailled documentation on the transaction schema, please consult The Transaction Model and The Transaction Schema.
From the point of view of Python, a transaction is simply a dictionary with a number of nested structures.
The first level has three keys:
id
-- astr
;version
-- anint
; andtransaction
-- adict
Because a transaction must be signed before being sent, the id
is required to be provided by the client.
When you assemble the payload you'll have:
- payload = {
'version': 1, 'transaction': {...},
}
whose id
can be generated by hashing the above with SHA-3's SHA256 algorithm.
Important
Implications of Signed Payloads
Because transactions are signed by the client submitting them, various values that could traditionally be generated on the server side need to be generated on the client side.
These values include:
- transaction id, which is a hash of the entire payload, without the signature(s)
- asset id
- metadata id
- any optional value, such as
version
which defaults to1
This makes the assembling of a payload more involved as one needs to provide all values regardless of whether there are defaults or not.
The transaction body is made up of the following keys:
asset
--dict
metadata
--dict
operation
--str
conditions
--list
ofdict
fulfillments
--list
ofdict
asset = {
'data': {},
'divisible': False,
'refillable': False,
'updatable': False,
'id': '',
}
Example of an asset payload:
asset = {
'data': {
'bicycle': {
'manufacturer': 'bkfab',
'serial_number': 'abcd1234',
},
},
'divisible': False,
'refillable': False,
'updatable': False,
'id': '7ab63c48-4c24-41df-a1bd-934bb609a7f7',
}
Note
In many client-server architectures, the values for the keys:
'divisible'
'refillable'
'updatable'
'id'
could all be generated on the server side.
In the case of BigchainDB, because we rely on cryptographic signatures, the payloads need to be fully prepared and signed on the client side. This prevents the server(s) from tempering with the provided data.
metadata = {
'data': {},
'id': '',
}
Example of a metadata payload:
metadata = {
'data': {
'planet': 'earth',
},
'id': 'ad8c83bd-9192-43b3-b636-af93a3a6b07c',
}
Note
In many client-server architectures, the value of the 'id'
could be generated on the server side.
In the case of BigchainDB, because we rely on cryptographic signatures, the payloads need to be fully prepared and signed on the client side. This prevents the server(s) from tempering with the provided data.
operation = '<operation>'
<operation>
must be one of 'CREATE'
, 'TRANSFER'
, or 'GENESIS'
Important
Case sensitive; all letters must be capitalized.
The purpose of the condition is to lock the transaction, such that a valid fulfillment is required to unlock it. In the case of signature-based schemes, the lock is basically a public key, such that in order to unlock the transaction one needs to have the private key.
Example of a condition payload:
{
'amount': 1,
'cid': 0,
'condition': {
'details': {
'bitmask': 32,
'public_key': '8L6ngTZ5ixuFEr1GiunrFNWtGkft4swWWArXjWJu2Uwc',
'signature': None,
'type': 'fulfillment',
'type_id': 4,
},
'uri': 'cc:4:20:bOZjTedaOgPsbYjh3QeOEQCj1o1lIvVefR71sS8egnM:96'
},
'owners_after': ['8L6ngTZ5ixuFEr1GiunrFNWtGkft4swWWArXjWJu2Uwc'],
}
A fulfillment payload is first prepared without its fulfillment uri (e.g., containing the signature), and included in the transaction payload, which will be hashed to generate the transaction id.
In a second step, after the transaction id has been generated, the fulfillment URI (e.g. containing a signature) can be added.
Moreover, payloads for CREATE
operations are a bit different.
Note
We hope to be able to simplify the payload structure and validation, such that this is no longer required.
Point to issues addressing the topic.
Example of a fulfillment payload before fulfilling it, for a CREATE operation:
fulfillment = {
'fid': 0,
'fulfillment': None,
'input': None,
'owners_before': ['8L6ngTZ5ixuFEr1GiunrFNWtGkft4swWWArXjWJu2Uwc'],
}
Note
Because it is a CREATE
operation, the 'input'
field is set to None
.
Example of a fulfillment payload after fulfilling it:
Recall that in order to prepare a transaction, we had to do something similar to:
In [0]: from bigchaindb_driver.crypto import generate_keypair
In [0]: from bigchaindb_driver.offchain import prepare_transaction
In [0]: alice = generate_keypair()
- In [0]: bicycle = {
...: 'data': { ...: 'bicycle': { ...: 'serial_number': 'abcd1234', ...: 'manufacturer': 'bkfab', ...: }, ...: }, ...: }
In [0]: metadata = {'planet': 'earth'}
- In [0]: prepared_creation_tx = prepare_transaction(
...: operation='CREATE', ...: owners_before=alice.verifying_key, ...: asset=bicycle, ...: metadata=metadata, ...: )
and the payload of the prepared transaction looked similar to:
In [0]: prepared_creation_tx
Note alice
's public key:
In [0]: alice.verifying_key
We are now going to craft this payload by hand.
Extract asset id and metadata id:
In [0]: asset_id = prepared_creation_tx['transaction']['asset']['id']
In [0]: metadata_id = prepared_creation_tx['transaction']['metadata']['id']
- In [0]: asset = {
...: 'data': { ...: 'bicycle': { ...: 'manufacturer': 'bkfab', ...: 'serial_number': 'abcd1234', ...: }, ...: }, ...: 'divisible': False, ...: 'refillable': False, ...: 'updatable': False, ...: 'id': asset_id, ...: }
- In [0]: metadata = {
...: 'data': { ...: 'planet': 'earth', ...: }, ...: 'id': metadata_id, ...: }
In [0]: operation = 'CREATE'
Important
Case sensitive; all letters must be capitalized.
The purpose of the condition is to lock the transaction, such that a valid fulfillment is required to unlock it. In the case of signature-based schemes, the lock is basically a public key, such that in order to unlock the transaction one needs to have the private key.
Let's review the condition payload of the prepared transaction, to see what we are aiming for:
In [0]: prepared_creation_tx['transaction']['conditions'][0]
The difficult parts are the condition details and URI. We''ll now see how to generate them using the cryptoconditions
library:
In [0]: from cryptoconditions import Ed25519Fulfillment
In [0]: ed25519 = Ed25519Fulfillment(public_key=alice.verifying_key)
generate the condition uri:
In [0]: ed25519.condition_uri
So now you have a condition URI for Alice's public key.
As for the details:
In [0]: ed25519.to_dict()
We can now easily assemble the dict
for the condition:
- In [0]: condition = {
...: 'amount': 1, ...: 'cid': 0, ...: 'condition': { ...: 'details': ed25519.to_dict(), ...: 'uri': ed25519.condition_uri, ...: }, ...: 'owners_after': (alice.verifying_key,), ...: }
Let's recap and set the conditions
key:
In [0]: from cryptoconditions import Ed25519Fulfillment
In [0]: ed25519 = Ed25519Fulfillment(public_key=alice.verifying_key)
- In [0]: condition = {
...: 'amount': 1, ...: 'cid': 0, ...: 'condition': { ...: 'details': ed25519.to_dict(), ...: 'uri': ed25519.condition_uri, ...: }, ...: 'owners_after': (alice.verifying_key,), ...: }
In [0]: conditions = (condition,)
The key part is the condition URI:
In [0]: ed25519.condition_uri
To know more about its meaning, you may read the cryptoconditions internet draft.
The fulfillment for a CREATE
operation is somewhat special:
- In [0]: fulfillment = {
...: 'fid': 0, ...: 'fulfillment': None, ...: 'input': None, ...: 'owners_before': (alice.verifying_key,) ...: }
- The input field is empty because it's a
CREATE
operation; - The
'fulfillemnt'
value isNone
as it will be set during the fulfillment step; and - The
'owners_before'
field identifies the issuer(s) of the asset that is being created.
The fulfillments
value is simply a list or tuple of all fulfillments:
In [0]: fulfillments = (fulfillment,)
Note
You may rightfully observe that the prepared_creation_tx
fulfillment generated via the prepare_transaction
function differs:
In [0]: prepared_creation_tx['transaction']['fulfillments'][0]
More precisely, the value of 'fulfillment'
:
In [0]: prepared_creation_tx['transaction']['fulfillments'][0]['fulfillment']
The quick answer is that it simply is not needed, and can be set to None
.
Putting it all together:
- In [0]: handcrafted_creation_tx = {
...: 'transaction': { ...: 'asset': asset, ...: 'metadata': metadata, ...: 'operation': operation, ...: 'conditions': conditions, ...: 'fulfillments': fulfillments, ...: }, ...: 'version': 1, ...: }
In [0]: handcrafted_creation_tx
We're missing the id
, and we'll generate it soon, but before that, let's recap how we've put all the code together to generate the above payload:
from cryptoconditions import Ed25519Fulfillment
from bigchaindb_driver.crypto import CryptoKeypair
alice = CryptoKeypair(
verifying_key=alice.verifying_key,
signing_key=alice.signing_key,
)
operation = 'CREATE'
asset = {
'data': {
'bicycle': {
'manufacturer': 'bkfab',
'serial_number': 'abcd1234',
},
},
'divisible': False,
'refillable': False,
'updatable': False,
'id': asset_id,
}
metadata = {
'data': {
'planet': 'earth',
},
'id': metadata_id,
}
ed25519 = Ed25519Fulfillment(public_key=alice.verifying_key)
condition = {
'amount': 1,
'cid': 0,
'condition': {
'details': ed25519.to_dict(),
'uri': ed25519.condition_uri,
},
'owners_after': (alice.verifying_key,),
}
conditions = (condition,)
fulfillment = {
'fid': 0,
'fulfillment': None,
'input': None,
'owners_before': (alice.verifying_key,)
}
fulfillments = (fulfillment,)
handcrafted_creation_tx = {
'transaction': {
'asset': asset,
'metadata': metadata,
'operation': operation,
'conditions': conditions,
'fulfillments': fulfillments,
},
'version': 1,
}
In [0]: import json
In [0]: from sha3 import sha3_256
- In [0]: json_str_tx = json.dumps(
...: handcrafted_creation_tx, ...: sort_keys=True, ...: separators=(',', ':'), ...: ensure_ascii=False, ...: )
In [0]: txid = sha3_256(json_str_tx.encode()).hexdigest()
In [0]: handcrafted_creation_tx['id'] = txid
Compare this to the txid of the transaction generated via prepare_transaction()
:
In [0]: txid == prepared_creation_tx['id']
You may observe that
In [0]: handcrafted_creation_tx == prepared_creation_tx
In [0]: from copy import deepcopy
In [0]: # back up
In [0]: prepared_creation_tx_bk = deepcopy(prepared_creation_tx)
In [0]: # set fulfillment to None
In [0]: prepared_creation_tx['transaction']['fulfillments'][0]['fulfillment'] = None
In [0]: handcrafted_creation_tx == prepared_creation_tx
Are still not equal because we used tuples instead of lists.
In [0]: # serialize to json str
In [0]: json_str_handcrafted_tx = json.dumps(handcrafted_creation_tx, sort_keys=True)
In [0]: json_str_prepared_tx = json.dumps(prepared_creation_tx, sort_keys=True)
In [0]: json_str_handcrafted_tx == json_str_prepared_tx
In [0]: prepared_creation_tx = prepared_creation_tx_bk
The full handcrafted yet-to-be-fulfilled transaction payload:
In [0]: handcrafted_creation_tx
In [0]: from cryptoconditions.crypto import Ed25519SigningKey
In [0]: from bigchaindb_driver.offchain import fulfill_transaction
- In [0]: fulfilled_creation_tx = fulfill_transaction(
...: prepared_creation_tx, ...: private_keys=alice.signing_key, ...: )
In [0]: sk = Ed25519SigningKey(alice.signing_key)
- In [0]: message = json.dumps(
...: handcrafted_creation_tx, ...: sort_keys=True, ...: separators=(',', ':'), ...: ensure_ascii=False, ...: )
In [0]: ed25519.sign(message.encode(), sk)
In [0]: fulfillment = ed25519.serialize_uri()
In [0]: handcrafted_creation_tx['transaction']['fulfillments'][0]['fulfillment'] = fulfillment
Let's check this:
In [0]: fulfilled_creation_tx['transaction']['fulfillments'][0]['fulfillment'] == fulfillment
In [0]: json.dumps(fulfilled_creation_tx, sort_keys=True) == json.dumps(handcrafted_creation_tx, sort_keys=True)
Handcrafting a 'CREATE'
transaction can be done as follows:
import json
from uuid import uuid4
import sha3
import cryptoconditions
from bigchaindb_driver.crypto import generate_keypair
alice = generate_keypair()
operation = 'CREATE'
asset_id = str(uuid4())
asset = {
'data': {
'bicycle': {
'manufacturer': 'bkfab',
'serial_number': 'abcd1234',
},
},
'divisible': False,
'refillable': False,
'updatable': False,
'id': asset_id,
}
metadata_id = str(uuid4())
metadata = {
'data': {
'planet': 'earth',
},
'id': metadata_id,
}
ed25519 = cryptoconditions.Ed25519Fulfillment(public_key=alice.verifying_key)
condition = {
'amount': 1,
'cid': 0,
'condition': {
'details': ed25519.to_dict(),
'uri': ed25519.condition_uri,
},
'owners_after': (alice.verifying_key,),
}
conditions = (condition,)
fulfillment = {
'fid': 0,
'fulfillment': None,
'input': None,
'owners_before': (alice.verifying_key,)
}
fulfillments = (fulfillment,)
handcrafted_creation_tx = {
'transaction': {
'asset': asset,
'metadata': metadata,
'operation': operation,
'conditions': conditions,
'fulfillments': fulfillments,
},
'version': 1,
}
json_str_tx = json.dumps(
handcrafted_creation_tx,
sort_keys=True,
separators=(',', ':'),
ensure_ascii=False,
)
creation_txid = sha3.sha3_256(json_str_tx.encode()).hexdigest()
handcrafted_creation_tx['id'] = creation_txid
sk = cryptoconditions.crypto.Ed25519SigningKey(alice.signing_key)
message = json.dumps(
handcrafted_creation_tx,
sort_keys=True,
separators=(',', ':'),
ensure_ascii=False,
)
ed25519.sign(message.encode(), sk)
fulfillment = ed25519.serialize_uri()
handcrafted_creation_tx['transaction']['fulfillments'][0]['fulfillment'] = fulfillment
Sending it over to a BigchainDB node:
from bigchaindb_driver import BigchainDB
bdb = BigchainDB('http://bdb-server:9984/api/v1')
returned_creation_tx = bdb.transactions.send(handcrafted_creation_tx)
A few checks:
>>> json.dumps(returned_creation_tx, sort_keys=True) == json.dumps(handcrafted_creation_tx, sort_keys=True)
True
>>> bdb.transactions.status(creation_txid)
{'status': 'valid'}
Tip
When checking for the status of a transaction, one should keep in mind tiny delays before a transaction reaches a valid status.
In the bicycle transfer example <bicycle-transfer>
, we showed that the transfer transaction was prepared and fulfilled as follows:
In [0]: creation_tx = fulfilled_creation_tx
In [0]: bob = generate_keypair()
In [0]: cid = 0
In [0]: condition = creation_tx['transaction']['conditions'][cid]
- In [0]: transfer_input = {
...: 'fulfillment': condition['condition']['details'], ...: 'input': { ...: 'cid': cid, ...: 'txid': creation_tx['id'], ...: }, ...: 'owners_before': condition['owners_after'], ...: }
- In [0]: prepared_transfer_tx = prepare_transaction(
...: operation='TRANSFER', ...: asset=creation_tx['transaction']['asset'], ...: inputs=transfer_input, ...: owners_after=bob.verifying_key, ...: )
- In [0]: fulfilled_transfer_tx = fulfill_transaction(
...: prepared_transfer_tx, ...: private_keys=alice.signing_key, ...: )
In [0]: fulfilled_transfer_tx
Our goal is now to handcraft a payload equal to fulfilled_transfer_tx
with the help of
json
: to serialize the transaction dictionary into a JSON formatted string.- sha3: to hash the serialized transaction
- cryptoconditions: to create conditions and fulfillments
In [0]: asset = {'id': asset_id}
In [0]: metadata = None
In [0]: operation = 'TRANSFER'
In [0]: from cryptoconditions import Ed25519Fulfillment
In [0]: ed25519 = Ed25519Fulfillment(public_key=bob.verifying_key)
- In [0]: condition = {
...: 'amount': 1, ...: 'cid': 0, ...: 'condition': { ...: 'details': ed25519.to_dict(), ...: 'uri': ed25519.condition_uri, ...: }, ...: 'owners_after': (bob.verifying_key,), ...: }
In [0]: conditions = (condition,)
- In [0]: fulfillment = {
...: 'fid': 0, ...: 'fulfillment': None, ...: 'input': { ...: 'txid': creation_tx['id'], ...: 'cid': 0, ...: }, ...: 'owners_before': (alice.verifying_key,) ...: }
In [0]: fulfillments = (fulfillment,)
A few notes:
- The
input
field points to the condition that needs to be fulfilled; - The
'fulfillment'
value isNone
as it will be set during the fulfillment step; and - The
'owners_before'
field identifies the fulfiller(s).
Putting it all together:
- In [0]: handcrafted_transfer_tx = {
...: 'transaction': { ...: 'asset': asset, ...: 'metadata': metadata, ...: 'operation': operation, ...: 'conditions': conditions, ...: 'fulfillments': fulfillments, ...: }, ...: 'version': 1, ...: }
In [0]: handcrafted_transfer_tx
We're missing the id
, and we'll generate it, but before, let's recap how we've put all the code together to generate the above payload:
from cryptoconditions import Ed25519Fulfillment
from bigchaindb_driver.crypto import CryptoKeypair
bob = CryptoKeypair(
verifying_key=bob.verifying_key,
signing_key=bob.signing_key,
)
operation = 'TRANSFER'
asset = {'id': asset_id}
metadata = None
ed25519 = Ed25519Fulfillment(public_key=bob.verifying_key)
condition = {
'amount': 1,
'cid': 0,
'condition': {
'details': ed25519.to_dict(),
'uri': ed25519.condition_uri,
},
'owners_after': (bob.verifying_key,),
}
conditions = (condition,)
fulfillment = {
'fid': 0,
'fulfillment': None,
'input': {
'txid': creation_tx['id'],
'cid': 0,
},
'owners_before': (alice.verifying_key,)
}
fulfillments = (fulfillment,)
handcrafted_transfer_tx = {
'transaction': {
'asset': asset,
'metadata': metadata,
'operation': operation,
'conditions': conditions,
'fulfillments': fulfillments,
},
'version': 1,
}
In [0]: import json
In [0]: from sha3 import sha3_256
- In [0]: json_str_tx = json.dumps(
...: handcrafted_transfer_tx, ...: sort_keys=True, ...: separators=(',', ':'), ...: ensure_ascii=False, ...: )
In [0]: txid = sha3_256(json_str_tx.encode()).hexdigest()
In [0]: handcrafted_transfer_tx['id'] = txid
Compare this to the txid of the transaction generated via prepare_transaction()
In [0]: txid == prepared_transfer_tx['id']
You may observe that
In [0]: handcrafted_transfer_tx == prepared_transfer_tx
In [0]: from copy import deepcopy
In [0]: # back up
In [0]: prepared_transfer_tx_bk = deepcopy(prepared_transfer_tx)
In [0]: # set fulfillment to None
In [0]: prepared_transfer_tx['transaction']['fulfillments'][0]['fulfillment'] = None
In [0]: handcrafted_transfer_tx == prepared_transfer_tx
Are still not equal because we used tuples instead of lists.
In [0]: # serialize to json str
In [0]: json_str_handcrafted_tx = json.dumps(handcrafted_transfer_tx, sort_keys=True)
In [0]: json_str_prepared_tx = json.dumps(prepared_transfer_tx, sort_keys=True)
In [0]: json_str_handcrafted_tx == json_str_prepared_tx
In [0]: prepared_transfer_tx = prepared_transfer_tx_bk
The full handcrafted yet-to-be-fulfilled transaction payload:
In [0]: handcrafted_transfer_tx
In [0]: from cryptoconditions.crypto import Ed25519SigningKey
In [0]: from bigchaindb_driver.offchain import fulfill_transaction
- In [0]: fulfilled_transfer_tx = fulfill_transaction(
...: prepared_transfer_tx, ...: private_keys=alice.signing_key, ...: )
In [0]: sk = Ed25519SigningKey(alice.signing_key)
- In [0]: message = json.dumps(
...: handcrafted_transfer_tx, ...: sort_keys=True, ...: separators=(',', ':'), ...: ensure_ascii=False, ...: )
In [0]: ed25519.sign(message.encode(), sk)
In [0]: fulfillment = ed25519.serialize_uri()
In [0]: handcrafted_transfer_tx['transaction']['fulfillments'][0]['fulfillment'] = fulfillment
Let's check this:
In [0]: fulfilled_transfer_tx['transaction']['fulfillments'][0]['fulfillment'] == fulfillment
In [0]: json.dumps(fulfilled_transfer_tx, sort_keys=True) == json.dumps(handcrafted_transfer_tx, sort_keys=True)
import json
import sha3
import cryptoconditions
from bigchaindb_driver.crypto import generate_keypair
bob = generate_keypair()
operation = 'TRANSFER'
asset = {'id': asset_id}
metadata = None
ed25519 = cryptoconditions.Ed25519Fulfillment(public_key=bob.verifying_key)
condition = {
'amount': 1,
'cid': 0,
'condition': {
'details': ed25519.to_dict(),
'uri': ed25519.condition_uri,
},
'owners_after': (bob.verifying_key,),
}
conditions = (condition,)
fulfillment = {
'fid': 0,
'fulfillment': None,
'input': {
'txid': creation_txid,
'cid': 0,
},
'owners_before': (alice.verifying_key,)
}
fulfillments = (fulfillment,)
handcrafted_transfer_tx = {
'transaction': {
'asset': asset,
'metadata': metadata,
'operation': operation,
'conditions': conditions,
'fulfillments': fulfillments,
},
'version': 1,
}
json_str_tx = json.dumps(
handcrafted_transfer_tx,
sort_keys=True,
separators=(',', ':'),
ensure_ascii=False,
)
transfer_txid = sha3.sha3_256(json_str_tx.encode()).hexdigest()
handcrafted_transfer_tx['id'] = transfer_txid
sk = cryptoconditions.crypto.Ed25519SigningKey(alice.signing_key)
message = json.dumps(
handcrafted_transfer_tx,
sort_keys=True,
separators=(',', ':'),
ensure_ascii=False,
)
ed25519.sign(message.encode(), sk)
fulfillment = ed25519.serialize_uri()
handcrafted_transfer_tx['transaction']['fulfillments'][0]['fulfillment'] = fulfillment
Sending it over to a BigchainDB node:
from bigchaindb_driver import BigchainDB
bdb = BigchainDB('http://bdb-server:9984/api/v1')
returned_transfer_tx = bdb.transactions.send(handcrafted_transfer_tx)
A few checks:
>>> json.dumps(returned_transfer_tx, sort_keys=True) == json.dumps(handcrafted_transfer_tx, sort_keys=True)
True
>>> bdb.transactions.status(transfer_txid)
{'status': 'valid'}
Tip
When checking for the status of a transaction, one should keep in mind tiny delays before a transaction reaches a valid status.
Handcrafting the 'CREATE'
transaction:
import json
from uuid import uuid4
import sha3
import cryptoconditions
from bigchaindb_driver.crypto import generate_keypair
bob, carly = generate_keypair(), generate_keypair()
asset_id = str(uuid4())
asset = {
'divisible': True,
'data': {
'token_for': {
'bicycle': {
'manufacturer': 'bkfab',
'serial_number': 'abcd1234',
},
'description': 'time share token. each token equals 1 hour of riding.'
},
},
'refillable': False,
'updatable': False,
'id': asset_id,
}
# CRYPTO-CONDITIONS: instantiate an Ed25519 crypto-condition for carly
ed25519 = cryptoconditions.Ed25519Fulfillment(public_key=carly.verifying_key)
# CRYPTO-CONDITIONS: generate the condition uri
condition_uri = ed25519.condition.serialize_uri()
# CRYPTO-CONDITIONS: get the unsigned fulfillment dictionary (details)
unsigned_fulfillment_dict = ed25519.to_dict()
condition = {
'amount': 10,
'cid': 0,
'condition': {
'details': unsigned_fulfillment_dict,
'uri': condition_uri,
},
'owners_after': (carly.verifying_key,),
}
fulfillment = {
'fid': 0,
'fulfillment': None,
'input': None,
'owners_before': (bob.verifying_key,)
}
token_creation_tx = {
'transaction': {
'asset': asset,
'metadata': None,
'operation': 'CREATE',
'conditions': (condition,),
'fulfillments': (fulfillment,),
},
'version': 1,
}
# JSON: serialize the id-less transaction to a json formatted string
json_str_tx = json.dumps(
token_creation_tx,
sort_keys=True,
separators=(',', ':'),
ensure_ascii=False,
)
# SHA3: hash the serialized id-less transaction to generate the id
creation_txid = sha3.sha3_256(json_str_tx.encode()).hexdigest()
# add the id
token_creation_tx['id'] = creation_txid
# JSON: serialize the transaction-with-id to a json formatted string
message = json.dumps(
token_creation_tx,
sort_keys=True,
separators=(',', ':'),
ensure_ascii=False,
)
# CRYPTO-CONDITIONS: sign the serialized transaction-with-id
ed25519.sign(message.encode(),
cryptoconditions.crypto.Ed25519SigningKey(bob.signing_key))
# CRYPTO-CONDITIONS: generate the fulfillment uri
fulfillment_uri = ed25519.serialize_uri()
# add the fulfillment uri (signature)
token_creation_tx['transaction']['fulfillments'][0]['fulfillment'] = fulfillment_uri
Sending it over to a BigchainDB node:
from bigchaindb_driver import BigchainDB
bdb = BigchainDB('http://bdb-server:9984/api/v1')
returned_creation_tx = bdb.transactions.send(token_creation_tx)
A few checks:
>>> json.dumps(returned_creation_tx, sort_keys=True) == json.dumps(token_creation_tx, sort_keys=True)
True
>>> token_creation_tx['transaction']['fulfillments'][0]['owners_before'][0] == bob.verifying_key
True
>>> token_creation_tx['transaction']['conditions'][0]['owners_after'][0] == carly.verifying_key
True
>>> token_creation_tx['transaction']['conditions'][0]['amount'] == 10
True
>>> bdb.transactions.status(creation_txid)
{'status': 'valid'}
Tip
When checking for the status of a transaction, one should keep in mind tiny delays before a transaction reaches a valid status.
Now Carly wants to ride the bicycle for 2 hours so she needs to send 2 tokens to Bob:
# CRYPTO-CONDITIONS: instantiate an Ed25519 crypto-condition for carly
bob_ed25519 = cryptoconditions.Ed25519Fulfillment(public_key=bob.verifying_key)
# CRYPTO-CONDITIONS: instantiate an Ed25519 crypto-condition for carly
carly_ed25519 = cryptoconditions.Ed25519Fulfillment(public_key=carly.verifying_key)
# CRYPTO-CONDITIONS: generate the condition uris
bob_condition_uri = bob_ed25519.condition.serialize_uri()
carly_condition_uri = carly_ed25519.condition.serialize_uri()
# CRYPTO-CONDITIONS: get the unsigned fulfillment dictionary (details)
bob_unsigned_fulfillment_dict = bob_ed25519.to_dict()
carly_unsigned_fulfillment_dict = carly_ed25519.to_dict()
bob_condition = {
'amount': 2,
'cid': 0,
'condition': {
'details': bob_unsigned_fulfillment_dict,
'uri': bob_condition_uri,
},
'owners_after': (bob.verifying_key,),
}
carly_condition = {
'amount': 8,
'cid': 1,
'condition': {
'details': carly_unsigned_fulfillment_dict,
'uri': carly_condition_uri,
},
'owners_after': (carly.verifying_key,),
}
fulfillment = {
'fid': 0,
'fulfillment': None,
'input': {
'txid': token_creation_tx['id'],
'cid': 0,
},
'owners_before': (carly.verifying_key,)
}
token_transfer_tx = {
'transaction': {
'asset': {'id': asset_id},
'metadata': None,
'operation': 'TRANSFER',
'conditions': (bob_condition, carly_condition),
'fulfillments': (fulfillment,),
},
'version': 1,
}
# JSON: serialize the id-less transaction to a json formatted string
json_str_tx = json.dumps(
token_transfer_tx,
sort_keys=True,
separators=(',', ':'),
ensure_ascii=False,
)
# SHA3: hash the serialized id-less transaction to generate the id
transfer_txid = sha3.sha3_256(json_str_tx.encode()).hexdigest()
# add the id
token_transfer_tx['id'] = transfer_txid
# JSON: serialize the transaction-with-id to a json formatted string
message = json.dumps(
token_transfer_tx,
sort_keys=True,
separators=(',', ':'),
ensure_ascii=False,
)
# CRYPTO-CONDITIONS: sign the serialized transaction-with-id for bob
carly_ed25519.sign(message.encode(),
cryptoconditions.crypto.Ed25519SigningKey(carly.signing_key))
# CRYPTO-CONDITIONS: generate bob's fulfillment uri
fulfillment_uri = carly_ed25519.serialize_uri()
# add bob's fulfillment uri (signature)
token_transfer_tx['transaction']['fulfillments'][0]['fulfillment'] = fulfillment_uri
Sending it over to a BigchainDB node:
bdb = BigchainDB('http://bdb-server:9984/api/v1')
returned_transfer_tx = bdb.transactions.send(token_transfer_tx)
A few checks:
>>> json.dumps(returned_transfer_tx, sort_keys=True) == json.dumps(token_transfer_tx, sort_keys=True)
True
>>> token_transfer_tx['transaction']['fulfillments'][0]['owners_before'][0] == carly.verifying_key
True
>>> bdb.transactions.status(creation_txid)
{'status': 'valid'}
Tip
When checking for the status of a transaction, one should keep in mind tiny delays before a transaction reaches a valid status.
We'll re-use the example, to compare our work.
Say alice
and bob
own a car together:
In [0]: from bigchaindb_driver.crypto import generate_keypair
In [0]: from bigchaindb_driver import offchain
In [0]: alice, bob = generate_keypair(), generate_keypair()
In [0]: car_asset = {'data': {'car': {'vin': '5YJRE11B781000196'}}}
- In [0]: car_creation_tx = offchain.prepare_transaction(
...: operation='CREATE', ...: owners_before=alice.verifying_key, ...: owners_after=(alice.verifying_key, bob.verifying_key), ...: asset=car_asset, ...: )
- In [0]: signed_car_creation_tx = offchain.fulfill_transaction(
...: car_creation_tx, ...: private_keys=alice.signing_key, ...: )
In [0]: signed_car_creation_tx
sent_car_tx = bdb.transactions.send(signed_car_creation_tx)
One day, alice
and bob
, having figured out how to teleport themselves, and realizing they no longer need their car, wish to transfer the ownership of their car over to carol
:
In [0]: carol = generate_keypair()
In [0]: cid = 0
In [0]: condition = signed_car_creation_tx['transaction']['conditions'][cid]
- In [0]: input = {
...: 'fulfillment': condition['condition']['details'], ...: 'input': { ...: 'cid': cid, ...: 'txid': signed_car_creation_tx['id'], ...: }, ...: 'owners_before': condition['owners_after'], ...: }
In [0]: asset = signed_car_creation_tx['transaction']['asset']
- In [0]: car_transfer_tx = offchain.prepare_transaction(
...: operation='TRANSFER', ...: owners_after=carol.verifying_key, ...: asset=asset, ...: inputs=input_, ...: )
- In [0]: signed_car_transfer_tx = offchain.fulfill_transaction(
...: car_transfer_tx, private_keys=[alice.signing_key, bob.signing_key] ...: )
In [0]: signed_car_transfer_tx
Sending the transaction to a BigchainDB node:
sent_car_transfer_tx = bdb.transactions.send(signed_car_transfer_tx)
In order to do this manually, let's first import the necessary tools (json, sha3, and cryptoconditions):
In [0]: import json
In [0]: from sha3 import sha3_256
In [0]: from cryptoconditions import Ed25519Fulfillment, ThresholdSha256Fulfillment
In [0]: from cryptoconditions.crypto import Ed25519SigningKey
Create the asset, setting all values:
In [0]: car_asset_id = signed_car_creation_tx['transaction']['asset']['id']
- In [0]: car_asset = {
...: 'data': {'car': {'vin': '5YJRE11B781000196'}}, ...: 'divisible': False, ...: 'refillable': False, ...: 'updatable': False, ...: 'id': car_asset_id, ...: }
Generate the condition:
In [0]: alice_ed25519 = Ed25519Fulfillment(public_key=alice.verifying_key)
In [0]: bob_ed25519 = Ed25519Fulfillment(public_key=bob.verifying_key)
In [0]: threshold_sha256 = ThresholdSha256Fulfillment(threshold=2)
In [0]: threshold_sha256.add_subfulfillment(alice_ed25519)
In [0]: threshold_sha256.add_subfulfillment(bob_ed25519)
In [0]: unsigned_subfulfillments_dict = threshold_sha256.to_dict()
In [0]: condition_uri = threshold_sha256.condition.serialize_uri()
- In [0]: condition = {
...: 'amount': 1, ...: 'cid': 0, ...: 'condition': { ...: 'details': unsigned_subfulfillments_dict, ...: 'uri': condition_uri, ...: }, ...: 'owners_after': (alice.verifying_key, bob.verifying_key), ...: }
Tip
The condition uri
could have been generated in a slightly different way, which may be more intuitive to you. You can think of the threshold condition containing sub conditions:
In [0]: alt_threshold_sha256 = ThresholdSha256Fulfillment(threshold=2)
In [0]: alt_threshold_sha256.add_subcondition(alice_ed25519.condition)
In [0]: alt_threshold_sha256.add_subcondition(bob_ed25519.condition)
In [0]: alt_threshold_sha256.condition.serialize_uri() == condition_uri
The details
on the other hand holds the associated fulfillments not yet fulfilled.
The yet to be fulfilled fulfillment:
- In [0]: fulfillment = {
...: 'fid': 0, ...: 'fulfillment': None, ...: 'input': None, ...: 'owners_before': (alice.verifying_key,), ...: }
Craft the payload:
- In [0]: handcrafted_car_creation_tx = {
...: 'transaction': { ...: 'asset': car_asset, ...: 'metadata': None, ...: 'operation': 'CREATE', ...: 'conditions': (condition,), ...: 'fulfillments': (fulfillment,), ...: }, ...: 'version': 1, ...: }
Generate the id, by hashing the encoded json formatted string representation of the transaction:
- In [0]: json_str_tx = json.dumps(
...: handcrafted_car_creation_tx, ...: sort_keys=True, ...: separators=(',', ':'), ...: ensure_ascii=False, ...: )
In [0]: car_creation_txid = sha3_256(json_str_tx.encode()).hexdigest()
In [0]: handcrafted_car_creation_tx['id'] = car_creation_txid
Let's make sure our txid is the same as the one provided by the driver:
In [0]: handcrafted_car_creation_tx['id'] == car_creation_tx['id']
Sign the transaction:
- In [0]: message = json.dumps(
...: handcrafted_car_creation_tx, ...: sort_keys=True, ...: separators=(',', ':'), ...: ensure_ascii=False, ...: )
In [0]: alice_ed25519.sign(message.encode(), Ed25519SigningKey(alice.signing_key))
In [0]: fulfillment_uri = alice_ed25519.serialize_uri()
In [0]: handcrafted_car_creation_tx['transaction']['fulfillments'][0]['fulfillment'] = fulfillment_uri
Compare our signed CREATE transaction with the driver's:
- In [0]: (json.dumps(handcrafted_car_creation_tx, sort_keys=True) ==
...: json.dumps(signed_car_creation_tx, sort_keys=True))
The transfer:
In [0]: alice_ed25519 = Ed25519Fulfillment(public_key=alice.verifying_key)
In [0]: bob_ed25519 = Ed25519Fulfillment(public_key=bob.verifying_key)
In [0]: carol_ed25519 = Ed25519Fulfillment(public_key=carol.verifying_key)
In [0]: unsigned_fulfillments_dict = carol_ed25519.to_dict()
In [0]: condition_uri = carol_ed25519.condition.serialize_uri()
- In [0]: condition = {
...: 'amount': 1, ...: 'cid': 0, ...: 'condition': { ...: 'details': unsigned_fulfillments_dict, ...: 'uri': condition_uri, ...: }, ...: 'owners_after': (carol.verifying_key,), ...: }
The yet to be fulfilled fulfillments:
- In [0]: fulfillment = {
...: 'fid': 0, ...: 'fulfillment': None, ...: 'input': { ...: 'txid': handcrafted_car_creation_tx['id'], ...: 'cid': 0, ...: }, ...: 'owners_before': (alice.verifying_key, bob.verifying_key), ...: }
Craft the payload:
- In [0]: handcrafted_car_transfer_tx = {
...: 'transaction': { ...: 'asset': {'id': car_asset_id}, ...: 'metadata': None, ...: 'operation': 'TRANSFER', ...: 'conditions': (condition,), ...: 'fulfillments': (fulfillment,), ...: }, ...: 'version': 1, ...: }
Generate the id, by hashing the encoded json formatted string representation of the transaction:
- In [0]: json_str_tx = json.dumps(
...: handcrafted_car_transfer_tx, ...: sort_keys=True, ...: separators=(',', ':'), ...: ensure_ascii=False, ...: )
In [0]: car_transfer_txid = sha3_256(json_str_tx.encode()).hexdigest()
In [0]: handcrafted_car_transfer_tx['id'] = car_transfer_txid
Let's make sure our txid is the same as the one provided by the driver:
In [0]: handcrafted_car_transfer_tx['id'] == car_transfer_tx['id']
Sign the transaction:
- In [0]: message = json.dumps(
...: handcrafted_car_transfer_tx, ...: sort_keys=True, ...: separators=(',', ':'), ...: ensure_ascii=False, ...: )
In [0]: alice_sk = Ed25519SigningKey(alice.signing_key)
In [0]: bob_sk = Ed25519SigningKey(bob.signing_key)
In [0]: threshold_sha256 = ThresholdSha256Fulfillment(threshold=2)
In [0]: threshold_sha256.add_subfulfillment(alice_ed25519)
In [0]: threshold_sha256.add_subfulfillment(bob_ed25519)
In [102]: alice_condition = threshold_sha256.get_subcondition_from_vk(alice.verifying_key)[0]
In [103]: bob_condition = threshold_sha256.get_subcondition_from_vk(bob.verifying_key)[0]
In [106]: alice_condition.sign(message.encode(), private_key=alice_sk)
In [107]: bob_condition.sign(message.encode(), private_key=bob_sk)
In [0]: fulfillment_uri = threshold_sha256.serialize_uri()
In [0]: handcrafted_car_transfer_tx['transaction']['fulfillments'][0]['fulfillment'] = fulfillment_uri
Compare our signed TRANSFER transaction with the driver's:
- In [0]: (json.dumps(handcrafted_car_transfer_tx, sort_keys=True) ==
...: json.dumps(signed_car_transfer_tx, sort_keys=True))
import json
import sha3
import cryptoconditions
from bigchaindb_driver.crypto import generate_keypair
car_asset = {
'data': {
'car': {
'vin': '5YJRE11B781000196',
},
},
'divisible': False,
'refillable': False,
'updatable': False,
'id': '5YJRE11B781000196',
}
alice, bob = generate_keypair(), generate_keypair()
# CRYPTO-CONDITIONS: instantiate an Ed25519 crypto-condition for alice
alice_ed25519 = cryptoconditions.Ed25519Fulfillment(public_key=alice.verifying_key)
# CRYPTO-CONDITIONS: instantiate an Ed25519 crypto-condition for bob
bob_ed25519 = cryptoconditions.Ed25519Fulfillment(public_key=bob.verifying_key)
# CRYPTO-CONDITIONS: instantiate a threshold SHA 256 crypto-condition
threshold_sha256 = cryptoconditions.ThresholdSha256Fulfillment(threshold=2)
# CRYPTO-CONDITIONS: add alice ed25519 to the threshold SHA 256 condition
threshold_sha256.add_subfulfillment(alice_ed25519)
# CRYPTO-CONDITIONS: add bob ed25519 to the threshold SHA 256 condition
threshold_sha256.add_subfulfillment(bob_ed25519)
# CRYPTO-CONDITIONS: get the unsigned fulfillment dictionary (details)
unsigned_subfulfillments_dict = threshold_sha256.to_dict()
# CRYPTO-CONDITIONS: generate the condition uri
condition_uri = threshold_sha256.condition.serialize_uri()
condition = {
'amount': 1,
'cid': 0,
'condition': {
'details': unsigned_subfulfillments_dict,
'uri': threshold_sha256.condition_uri,
},
'owners_after': (alice.verifying_key, bob.verifying_key),
}
# The yet to be fulfilled fulfillment:
fulfillment = {
'fid': 0,
'fulfillment': None,
'input': None,
'owners_before': (alice.verifying_key,),
}
# Craft the payload:
handcrafted_car_creation_tx = {
'transaction': {
'asset': car_asset,
'metadata': None,
'operation': 'CREATE',
'conditions': (condition,),
'fulfillments': (fulfillment,),
},
'version': 1,
}
# JSON: serialize the id-less transaction to a json formatted string
# Generate the id, by hashing the encoded json formatted string representation of
# the transaction:
json_str_tx = json.dumps(
handcrafted_car_creation_tx,
sort_keys=True,
separators=(',', ':'),
ensure_ascii=False,
)
# SHA3: hash the serialized id-less transaction to generate the id
car_creation_txid = sha3.sha3_256(json_str_tx.encode()).hexdigest()
# add the id
handcrafted_car_creation_tx['id'] = car_creation_txid
# JSON: serialize the transaction-with-id to a json formatted string
message = json.dumps(
handcrafted_car_creation_tx,
sort_keys=True,
separators=(',', ':'),
ensure_ascii=False,
)
# CRYPTO-CONDITIONS: sign the serialized transaction-with-id
alice_ed25519.sign(message.encode(),
cryptoconditions.crypto.Ed25519SigningKey(alice.signing_key))
# CRYPTO-CONDITIONS: generate the fulfillment uri
fulfillment_uri = alice_ed25519.serialize_uri()
# add the fulfillment uri (signature)
handcrafted_car_creation_tx['transaction']['fulfillments'][0]['fulfillment'] = fulfillment_uri
Sending it over to a BigchainDB node:
from bigchaindb_driver import BigchainDB
bdb = BigchainDB('http://bdb-server:9984/api/v1')
returned_car_creation_tx = bdb.transactions.send(handcrafted_car_creation_tx)
Wait for some nano seconds, and check the status:
>>> bdb.transactions.status(returned_car_creation_tx['id'])
{'status': 'valid'}
carol = generate_keypair()
alice_ed25519 = cryptoconditions.Ed25519Fulfillment(public_key=alice.verifying_key)
bob_ed25519 = cryptoconditions.Ed25519Fulfillment(public_key=bob.verifying_key)
carol_ed25519 = cryptoconditions.Ed25519Fulfillment(public_key=carol.verifying_key)
unsigned_fulfillments_dict = carol_ed25519.to_dict()
condition_uri = carol_ed25519.condition.serialize_uri()
condition = {
'amount': 1,
'cid': 0,
'condition': {
'details': unsigned_fulfillments_dict,
'uri': condition_uri,
},
'owners_after': (carol.verifying_key,),
}
# The yet to be fulfilled fulfillments:
fulfillment = {
'fid': 0,
'fulfillment': None,
'input': {
'txid': handcrafted_car_creation_tx['id'],
'cid': 0,
},
'owners_before': (alice.verifying_key, bob.verifying_key),
}
# Craft the payload:
handcrafted_car_transfer_tx = {
'transaction': {
'asset': {'id': car_asset['id']},
'metadata': None,
'operation': 'TRANSFER',
'conditions': (condition,),
'fulfillments': (fulfillment,),
},
'version': 1,
}
# Generate the id, by hashing the encoded json formatted string
# representation of the transaction:
json_str_tx = json.dumps(
handcrafted_car_transfer_tx,
sort_keys=True,
separators=(',', ':'),
ensure_ascii=False,
)
car_transfer_txid = sha3.sha3_256(json_str_tx.encode()).hexdigest()
handcrafted_car_transfer_tx['id'] = car_transfer_txid
# Sign the transaction:
message = json.dumps(
handcrafted_car_transfer_tx,
sort_keys=True,
separators=(',', ':'),
ensure_ascii=False,
)
alice_sk = cryptoconditions.crypto.Ed25519SigningKey(alice.signing_key)
bob_sk = cryptoconditions.crypto.Ed25519SigningKey(bob.signing_key)
threshold_sha256 = cryptoconditions.ThresholdSha256Fulfillment(threshold=2)
threshold_sha256.add_subfulfillment(alice_ed25519)
threshold_sha256.add_subfulfillment(bob_ed25519)
alice_condition = threshold_sha256.get_subcondition_from_vk(alice.verifying_key)[0]
bob_condition = threshold_sha256.get_subcondition_from_vk(bob.verifying_key)[0]
alice_condition.sign(message.encode(), private_key=alice_sk)
bob_condition.sign(message.encode(), private_key=bob_sk)
fulfillment_uri = threshold_sha256.serialize_uri()
handcrafted_car_transfer_tx['transaction']['fulfillments'][0]['fulfillment'] = fulfillment_uri
Sending it over to a BigchainDB node:
bdb = BigchainDB('http://bdb-server:9984/api/v1')
returned_car_transfer_tx = bdb.transactions.send(handcrafted_car_transfer_tx)
Wait for some nano seconds, and check the status:
>>> bdb.transactions.status(returned_car_transfer_tx['id'])
{'status': 'valid'}