changed structure of transactions

This commit is contained in:
Rodolphe Marques 2016-04-05 14:36:23 +02:00
parent 194bf8c6bd
commit dd3bc54c62

View File

@ -76,7 +76,7 @@ def timestamp():
return "{0:.6f}".format(time.mktime(dt.timetuple()) + dt.microsecond / 1e6) return "{0:.6f}".format(time.mktime(dt.timetuple()) + dt.microsecond / 1e6)
def create_tx(current_owner, new_owner, inputs, operation, payload=None): def create_tx(current_owners, new_owners, inputs, operation, payload=None):
"""Create a new transaction """Create a new transaction
A transaction in the bigchain is a transfer of a digital asset between two entities represented A transaction in the bigchain is a transfer of a digital asset between two entities represented
@ -104,6 +104,42 @@ def create_tx(current_owner, new_owner, inputs, operation, payload=None):
Raises: Raises:
TypeError: if the optional ``payload`` argument is not a ``dict``. TypeError: if the optional ``payload`` argument is not a ``dict``.
Reference:
{
"id": "<sha3 hash>",
"version": "transaction version number",
"transaction": {
"fulfillments": [
{
"current_owners": ["list of <pub-keys>"],
"input": {
"txid": "<sha3 hash>",
"cid": "condition index"
},
"fulfillment": "fulfillement of condition cid",
"fid": "fulfillment index"
}
],
"conditions": [
{
"new_owners": ["list of <pub-keys>"],
"condition": "condition to be met",
"cid": "condition index (1-to-1 mapping with fid)"
}
],
"operation": "<string>",
"timestamp": "<timestamp from client>",
"data": {
"hash": "<SHA3-256 hash hexdigest of payload>",
"payload": {
"title": "The Winds of Plast",
"creator": "Johnathan Plunkett",
"IPFS_key": "QmfQ5QAjvg4GtA3wg3adpnDJug8ktA1BxurVqBD8rtgVjP"
}
}
},
}
""" """
# handle payload # handle payload
@ -124,13 +160,38 @@ def create_tx(current_owner, new_owner, inputs, operation, payload=None):
'payload': payload 'payload': payload
} }
if inputs == []: # handle inputs
inputs = None fulfillments = []
# transfer
if inputs:
for fid, inp in enumerate(inputs):
fulfillments.append({
'current_owners': current_owners,
'input': inp,
'fulfillment': None,
'fid': fid
})
# create
else:
fulfillments.append({
'current_owners': current_owners,
'input': None,
'fulfillment': None,
'fid': 0
})
# handle outputs
conditions = []
for fulfillment in fulfillments:
conditions.append({
'new_owners': new_owners,
'condition': None,
'cid': fulfillment['fid']
})
tx = { tx = {
'current_owner': current_owner, 'fulfillments': fulfillments,
'new_owner': new_owner, 'conditions': conditions,
'inputs': inputs,
'operation': operation, 'operation': operation,
'timestamp': timestamp(), 'timestamp': timestamp(),
'data': data 'data': data
@ -143,12 +204,14 @@ def create_tx(current_owner, new_owner, inputs, operation, payload=None):
# create the transaction # create the transaction
transaction = { transaction = {
'id': tx_hash, 'id': tx_hash,
'version': 1,
'transaction': tx 'transaction': tx
} }
return transaction return transaction
#TODO: Change sign_tx to populate the fulfillments
def sign_tx(transaction, private_key): def sign_tx(transaction, private_key):
"""Sign a transaction """Sign a transaction
@ -159,14 +222,43 @@ def sign_tx(transaction, private_key):
private_key (str): base58 encoded private key to create a signature of the transaction. private_key (str): base58 encoded private key to create a signature of the transaction.
Returns: Returns:
dict: transaction with the `signature` field included. dict: transaction with the `fulfillment` fields populated.
""" """
b = bigchaindb.Bigchain()
private_key = PrivateKey(private_key) private_key = PrivateKey(private_key)
signature = private_key.sign(serialize(transaction))
signed_transaction = transaction.copy() common_data = {
signed_transaction.update({'signature': signature}) 'operation': transaction['transaction']['operation'],
return signed_transaction 'timestamp': transaction['transaction']['timestamp'],
'data': transaction['transaction']['data'],
'version': transaction['version'],
'id': transaction['id']
}
for fulfillment in transaction['transaction']['fulfillments']:
fulfillment_message = common_data.copy()
if transaction['transaction']['operation'] == 'CREATE':
fulfillment_message.update({
'input': None,
'condition': None
})
else:
# get previous condition
previous_tx = b.get_transaction(fulfillment['input']['txid'])
conditions = sorted(previous_tx['transaction']['conditions'], key=lambda d: d['cid'])
# update the fulfillment message
fulfillment_message.update({
'input': fulfillment['input'],
'condition': conditions[fulfillment['cid']]
})
# sign the fulfillment message
fulfillment_message_signature = private_key.sign(serialize(fulfillment_message))
fulfillment.update({'fulfillment': fulfillment_message_signature})
return transaction
def create_and_sign_tx(private_key, current_owner, new_owner, tx_input, operation='TRANSFER', payload=None): def create_and_sign_tx(private_key, current_owner, new_owner, tx_input, operation='TRANSFER', payload=None):