mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
docs for get_hash_data and get_fulfillment_message
added serialized=False arg to get_fulfillment_message
This commit is contained in:
@@ -308,7 +308,17 @@ def verify_signature(signed_transaction):
|
||||
return True
|
||||
|
||||
|
||||
def get_fulfillment_message(transaction, fulfillment):
|
||||
def get_fulfillment_message(transaction, fulfillment, serialized=False):
|
||||
"""Get the fulfillment message for signing a specific fulfillment in a transaction
|
||||
|
||||
Args:
|
||||
transaction (dict): a transaction
|
||||
fulfillment (dict): a specific fulfillment (for a condition index) within the transaction
|
||||
serialized (bool): False returns a dict, True returns a serialized string
|
||||
|
||||
Returns:
|
||||
str|dict: fulfillment message
|
||||
"""
|
||||
b = bigchaindb.Bigchain()
|
||||
|
||||
common_data = {
|
||||
@@ -331,10 +341,20 @@ def get_fulfillment_message(transaction, fulfillment):
|
||||
previous_tx = b.get_transaction(fulfillment['input']['txid'])
|
||||
conditions = sorted(previous_tx['transaction']['conditions'], key=lambda d: d['cid'])
|
||||
fulfillment_message['condition'] = conditions[fulfillment['input']['cid']]
|
||||
if serialized:
|
||||
return serialize(fulfillment_message)
|
||||
return fulfillment_message
|
||||
|
||||
|
||||
def get_hash_data(transaction):
|
||||
""" Get the hashed data that (should) correspond to the `transaction['id']`
|
||||
|
||||
Args:
|
||||
transaction (dict): the transaction to be hashed
|
||||
|
||||
Returns:
|
||||
str: the hash of the transaction
|
||||
"""
|
||||
tx = copy.deepcopy(transaction)
|
||||
if 'transaction' in tx:
|
||||
tx = tx['transaction']
|
||||
|
||||
@@ -368,14 +368,21 @@ subfulfillment2 = threshold_fulfillment.subconditions[1]['body']
|
||||
|
||||
# get the fulfillment message to sign
|
||||
threshold_tx_fulfillment_message = util.get_fulfillment_message(threshold_tx_transfer,
|
||||
threshold_tx_transfer['transaction']['fulfillments'][0])
|
||||
threshold_tx_transfer['transaction']['fulfillments'][0],
|
||||
serialized=True)
|
||||
|
||||
# sign the subconditions
|
||||
subfulfillment1.sign(util.serialize(threshold_tx_fulfillment_message), crypto.SigningKey(thresholduser1_priv))
|
||||
subfulfillment2.sign(util.serialize(threshold_tx_fulfillment_message), crypto.SigningKey(thresholduser2_priv))
|
||||
subfulfillment1.sign(threshold_tx_fulfillment_message, crypto.SigningKey(thresholduser1_priv))
|
||||
subfulfillment2.sign(threshold_tx_fulfillment_message, crypto.SigningKey(thresholduser2_priv))
|
||||
|
||||
# update the fulfillment
|
||||
threshold_tx_transfer['transaction']['fulfillments'][0]['fulfillment'] = threshold_fulfillment.serialize_uri()
|
||||
|
||||
# optional validation checks
|
||||
assert threshold_fulfillment.validate(threshold_tx_fulfillment_message) == True
|
||||
assert b.verify_signature(threshold_tx_transfer) == True
|
||||
assert b.validate_transaction(threshold_tx_transfer) == True
|
||||
|
||||
b.write_transaction(threshold_tx_transfer)
|
||||
|
||||
{
|
||||
|
||||
@@ -858,9 +858,9 @@ class TestCryptoconditions(object):
|
||||
def test_override_fulfillment_create(self, b, user_vk):
|
||||
tx = b.create_transaction(b.me, user_vk, None, 'CREATE')
|
||||
original_fulfillment = tx['transaction']['fulfillments'][0]
|
||||
fulfillment_message = util.get_fulfillment_message(tx, original_fulfillment)
|
||||
fulfillment_message = util.get_fulfillment_message(tx, original_fulfillment, serialized=True)
|
||||
fulfillment = Ed25519Fulfillment(public_key=b.me)
|
||||
fulfillment.sign(util.serialize(fulfillment_message), crypto.SigningKey(b.me_private))
|
||||
fulfillment.sign(fulfillment_message, crypto.SigningKey(b.me_private))
|
||||
|
||||
tx['transaction']['fulfillments'][0]['fulfillment'] = fulfillment.serialize_uri()
|
||||
|
||||
@@ -875,9 +875,9 @@ class TestCryptoconditions(object):
|
||||
tx = b.create_transaction(user_vk, other_vk, prev_tx_id, 'TRANSFER')
|
||||
|
||||
original_fulfillment = tx['transaction']['fulfillments'][0]
|
||||
fulfillment_message = util.get_fulfillment_message(tx, original_fulfillment)
|
||||
fulfillment_message = util.get_fulfillment_message(tx, original_fulfillment, serialized=True)
|
||||
fulfillment = Ed25519Fulfillment(public_key=user_vk)
|
||||
fulfillment.sign(util.serialize(fulfillment_message), crypto.SigningKey(user_sk))
|
||||
fulfillment.sign(fulfillment_message, crypto.SigningKey(user_sk))
|
||||
|
||||
tx['transaction']['fulfillments'][0]['fulfillment'] = fulfillment.serialize_uri()
|
||||
|
||||
@@ -897,9 +897,9 @@ class TestCryptoconditions(object):
|
||||
}
|
||||
|
||||
first_tx_fulfillment = first_tx['transaction']['fulfillments'][0]
|
||||
first_tx_fulfillment_message = util.get_fulfillment_message(first_tx, first_tx_fulfillment)
|
||||
first_tx_fulfillment_message = util.get_fulfillment_message(first_tx, first_tx_fulfillment, serialized=True)
|
||||
first_tx_fulfillment = Ed25519Fulfillment(public_key=user_vk)
|
||||
first_tx_fulfillment.sign(util.serialize(first_tx_fulfillment_message), crypto.SigningKey(user_sk))
|
||||
first_tx_fulfillment.sign(first_tx_fulfillment_message, crypto.SigningKey(user_sk))
|
||||
first_tx['transaction']['fulfillments'][0]['fulfillment'] = first_tx_fulfillment.serialize_uri()
|
||||
|
||||
assert b.validate_transaction(first_tx)
|
||||
@@ -916,9 +916,9 @@ class TestCryptoconditions(object):
|
||||
next_tx = b.create_transaction(other_vk, user_vk, next_input_tx, 'TRANSFER')
|
||||
|
||||
next_tx_fulfillment = next_tx['transaction']['fulfillments'][0]
|
||||
next_tx_fulfillment_message = util.get_fulfillment_message(next_tx, next_tx_fulfillment)
|
||||
next_tx_fulfillment_message = util.get_fulfillment_message(next_tx, next_tx_fulfillment, serialized=True)
|
||||
next_tx_fulfillment = Ed25519Fulfillment(public_key=other_vk)
|
||||
next_tx_fulfillment.sign(util.serialize(next_tx_fulfillment_message), crypto.SigningKey(other_sk))
|
||||
next_tx_fulfillment.sign(next_tx_fulfillment_message, crypto.SigningKey(other_sk))
|
||||
next_tx['transaction']['fulfillments'][0]['fulfillment'] = next_tx_fulfillment.serialize_uri()
|
||||
|
||||
assert b.validate_transaction(next_tx)
|
||||
@@ -959,13 +959,13 @@ class TestCryptoconditions(object):
|
||||
next_tx = b.create_transaction([other1_vk, other2_vk], user_vk, next_input_tx, 'TRANSFER')
|
||||
|
||||
next_tx_fulfillment = next_tx['transaction']['fulfillments'][0]
|
||||
next_tx_fulfillment_message = util.get_fulfillment_message(next_tx, next_tx_fulfillment)
|
||||
next_tx_fulfillment_message = util.get_fulfillment_message(next_tx, next_tx_fulfillment, serialized=True)
|
||||
next_tx_fulfillment = ThresholdSha256Fulfillment(threshold=2)
|
||||
next_tx_subfulfillment1 = Ed25519Fulfillment(public_key=other1_vk)
|
||||
next_tx_subfulfillment1.sign(util.serialize(next_tx_fulfillment_message), crypto.SigningKey(other1_sk))
|
||||
next_tx_subfulfillment1.sign(next_tx_fulfillment_message, crypto.SigningKey(other1_sk))
|
||||
next_tx_fulfillment.add_subfulfillment(next_tx_subfulfillment1)
|
||||
next_tx_subfulfillment2 = Ed25519Fulfillment(public_key=other2_vk)
|
||||
next_tx_subfulfillment2.sign(util.serialize(next_tx_fulfillment_message), crypto.SigningKey(other2_sk))
|
||||
next_tx_subfulfillment2.sign(next_tx_fulfillment_message, crypto.SigningKey(other2_sk))
|
||||
next_tx_fulfillment.add_subfulfillment(next_tx_subfulfillment2)
|
||||
next_tx['transaction']['fulfillments'][0]['fulfillment'] = next_tx_fulfillment.serialize_uri()
|
||||
|
||||
@@ -1007,15 +1007,15 @@ class TestCryptoconditions(object):
|
||||
next_tx = b.create_transaction([other1_vk, other2_vk], user_vk, next_input_tx, 'TRANSFER')
|
||||
|
||||
next_tx_fulfillment = next_tx['transaction']['fulfillments'][0]
|
||||
next_tx_fulfillment_message = util.get_fulfillment_message(next_tx, next_tx_fulfillment)
|
||||
next_tx_fulfillment_message = util.get_fulfillment_message(next_tx, next_tx_fulfillment, serialized=True)
|
||||
next_tx_fulfillment = ThresholdSha256Fulfillment(threshold=2)
|
||||
next_tx_subfulfillment1 = Ed25519Fulfillment(public_key=other1_vk)
|
||||
next_tx_subfulfillment1.sign(util.serialize(next_tx_fulfillment_message), crypto.SigningKey(other1_sk))
|
||||
next_tx_subfulfillment1.sign(next_tx_fulfillment_message, crypto.SigningKey(other1_sk))
|
||||
next_tx_fulfillment.add_subfulfillment(next_tx_subfulfillment1)
|
||||
|
||||
# Wrong signing happens here
|
||||
next_tx_subfulfillment2 = Ed25519Fulfillment(public_key=other1_vk)
|
||||
next_tx_subfulfillment2.sign(util.serialize(next_tx_fulfillment_message), crypto.SigningKey(other1_sk))
|
||||
next_tx_subfulfillment2.sign(next_tx_fulfillment_message, crypto.SigningKey(other1_sk))
|
||||
next_tx_fulfillment.add_subfulfillment(next_tx_subfulfillment2)
|
||||
next_tx['transaction']['fulfillments'][0]['fulfillment'] = next_tx_fulfillment.serialize_uri()
|
||||
|
||||
|
||||
@@ -113,17 +113,20 @@ subfulfillment2 = threshold_fulfillment.subconditions[1]['body']
|
||||
|
||||
# get the fulfillment message to sign
|
||||
threshold_tx_fulfillment_message = util.get_fulfillment_message(threshold_tx_transfer,
|
||||
threshold_tx_transfer['transaction']['fulfillments'][0])
|
||||
threshold_tx_transfer['transaction']['fulfillments'][0],
|
||||
serialized=True)
|
||||
|
||||
# sign the subconditions
|
||||
subfulfillment1.sign(util.serialize(threshold_tx_fulfillment_message), crypto.SigningKey(thresholduser1_priv))
|
||||
subfulfillment2.sign(util.serialize(threshold_tx_fulfillment_message), crypto.SigningKey(thresholduser2_priv))
|
||||
subfulfillment1.sign(threshold_tx_fulfillment_message, crypto.SigningKey(thresholduser1_priv))
|
||||
subfulfillment2.sign(threshold_tx_fulfillment_message, crypto.SigningKey(thresholduser2_priv))
|
||||
|
||||
assert threshold_fulfillment.validate(threshold_tx_fulfillment_message) == True
|
||||
|
||||
threshold_tx_transfer['transaction']['fulfillments'][0]['fulfillment'] = threshold_fulfillment.serialize_uri()
|
||||
|
||||
b.verify_signature(threshold_tx_transfer)
|
||||
assert b.verify_signature(threshold_tx_transfer) == True
|
||||
|
||||
b.validate_transaction(threshold_tx_transfer)
|
||||
assert b.validate_transaction(threshold_tx_transfer) == True
|
||||
|
||||
b.write_transaction(threshold_tx_transfer)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user