Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# some checks
assert b.validate_transaction(tx_escrow_signed) == tx_escrow_signed
assert b.is_valid_transaction(tx_escrow_signed) == tx_escrow_signed
print(json.dumps(tx_escrow_signed, sort_keys=True, indent=4, separators=(',', ':')))
b.write_transaction(tx_escrow_signed)
sleep(8)
# Retrieve the last transaction of thresholduser1_pub
tx_escrow_id = {'txid': tx_escrow_signed['id'], 'cid': 0}
# Create a base template for output transaction
tx_escrow_execute = b.create_transaction([testuser2_pub, testuser1_pub], testuser1_pub, tx_escrow_id, 'TRANSFER')
# Parse the threshold cryptocondition
escrow_fulfillment = cc.Fulfillment.from_dict(
tx_escrow['transaction']['conditions'][0]['condition']['details'])
subfulfillment_testuser1 = escrow_fulfillment.get_subcondition_from_vk(testuser1_pub)[0]
subfulfillment_testuser2 = escrow_fulfillment.get_subcondition_from_vk(testuser2_pub)[0]
subfulfillment_timeout = escrow_fulfillment.subconditions[0]['body'].subconditions[1]['body']
subfulfillment_timeout_inverted = escrow_fulfillment.subconditions[1]['body'].subconditions[1]['body']
# Get the fulfillment message to sign
tx_escrow_execute_fulfillment_message = \
util.get_fulfillment_message(tx_escrow_execute,
tx_escrow_execute['transaction']['fulfillments'][0],
serialized=True)
escrow_fulfillment.subconditions = []
# fulfill execute branch
# sign transaction
tx_timeout_signed = b.sign_transaction(tx_timeout, b.me_private)
b.write_transaction(tx_timeout_signed)
print(json.dumps(tx_timeout, sort_keys=True, indent=4, separators=(',', ':')))
sleep(8)
# Retrieve the transaction id of tx_timeout
tx_timeout_id = {'txid': tx_timeout['id'], 'cid': 0}
# Create a template to transfer the tx_timeout
tx_timeout_transfer = b.create_transaction(None, testuser1_pub, tx_timeout_id, 'TRANSFER')
# Parse the threshold cryptocondition
timeout_fulfillment = cc.Fulfillment.from_dict(
tx_timeout['transaction']['conditions'][0]['condition']['details'])
tx_timeout_transfer['transaction']['fulfillments'][0]['fulfillment'] = timeout_fulfillment.serialize_uri()
# no need to sign transaction, like with hashlocks
for i in range(time_sleep - 4):
tx_timeout_valid = b.is_valid_transaction(tx_timeout_transfer) == tx_timeout_transfer
seconds_to_timeout = int(float(time_expire) - float(gen_timestamp()))
print('tx_timeout valid: {} ({}s to timeout)'.format(tx_timeout_valid, seconds_to_timeout))
sleep(1)
"""
Escrow Conditions
"""
# retrieve the last transaction of testuser2
tx_retrieved_id = b.get_owned_ids(testuser2_pub).pop()
# check if the transaction is already in the bigchain
tx_threshold_retrieved = b.get_transaction(threshold_tx_signed['id'])
print(json.dumps(tx_threshold_retrieved, sort_keys=True, indent=4, separators=(',', ':')))
thresholduser4_priv, thresholduser4_pub = crypto.generate_key_pair()
# retrieve the last transaction of thresholduser1_pub
tx_retrieved_id = b.get_owned_ids(thresholduser1_pub).pop()
# create a base template for a 2-input/1-output transaction
threshold_tx_transfer = b.create_transaction([thresholduser1_pub, thresholduser2_pub, thresholduser3_pub],
thresholduser4_pub, tx_retrieved_id, 'TRANSFER')
# parse the threshold cryptocondition
threshold_fulfillment = cc.Fulfillment.from_dict(threshold_tx['transaction']['conditions'][0]['condition']['details'])
subfulfillment1 = threshold_fulfillment.get_subcondition_from_vk(thresholduser1_pub)[0]
subfulfillment2 = threshold_fulfillment.get_subcondition_from_vk(thresholduser2_pub)[0]
subfulfillment3 = threshold_fulfillment.get_subcondition_from_vk(thresholduser3_pub)[0]
# get the fulfillment message to sign
threshold_tx_fulfillment_message = util.get_fulfillment_message(threshold_tx_transfer,
threshold_tx_transfer['transaction']['fulfillments'][0],
serialized=True)
# clear the subconditions of the threshold fulfillment, they will be added again after signing
threshold_fulfillment.subconditions = []
# sign and add the subconditions until threshold of 2 is reached
subfulfillment1.sign(threshold_tx_fulfillment_message, crypto.PrivateKey(thresholduser1_priv))
if 'hops' not in ilp_header:
ilp_header['hops'] = []
ilp_header['hops'].append({
'ledger': current_ledger_id,
'txid': tx['id']
})
destination_ledger_id = ilp_header['ledger']
ledger = get_bigchain(ledger_id=destination_ledger_id)
source = self.accounts[destination_ledger_id]['vk']
to = ilp_header['account']
asset_id = ledger.get_owned_ids(source).pop()
sk = self.accounts[destination_ledger_id]['sk']
condition = cc.Fulfillment.from_dict(tx['transaction']['conditions'][0]['condition']['details'])
timelocks, _ = get_subcondition_indices_from_type(condition, cc.TimeoutFulfillment.TYPE_ID)
expires_at = timelocks[0].expire_time.decode()
hashlocks, _ = get_subcondition_indices_from_type(condition, cc.PreimageSha256Fulfillment.TYPE_ID)
execution_condition = hashlocks[0].serialize_uri()
escrow_asset(bigchain=ledger,
source=source,
to=to,
asset_id=asset_id,
sk=sk,
expires_at=expires_at,
ilp_header=ilp_header,
execution_condition=execution_condition)
def fulfill_escrow_asset(bigchain, source, to, asset_id, sk, execution_fulfillment=None):
asset = bigchain.get_transaction(asset_id['txid'])
asset_owners = asset['transaction']['conditions'][asset_id['cid']]['new_owners']
other_owner = [owner for owner in asset_owners if not owner == source][0]
# Create a base template for fulfill transaction
asset_escrow_fulfill = bigchain.create_transaction(asset_owners, to, asset_id, 'TRANSFER',
payload=asset['transaction']['data']['payload'])
# Parse the threshold cryptocondition
escrow_fulfillment = cc.Fulfillment.from_dict(
asset['transaction']['conditions'][0]['condition']['details'])
# Get the fulfillment message to sign
tx_escrow_execute_fulfillment_message = \
bigchaindb.util.get_fulfillment_message(asset_escrow_fulfill,
asset_escrow_fulfill['transaction']['fulfillments'][0],
serialized=True)
# get the indices path for the source that wants to fulfill
_, indices = get_subcondition_indices_from_vk(escrow_fulfillment, source)
subfulfillment_source = escrow_fulfillment
for index in indices:
subfulfillment_source = subfulfillment_source.subconditions[index]['body']
# sign the fulfillment
subfulfillment_source.sign(tx_escrow_execute_fulfillment_message, bigchaindb.crypto.SigningKey(sk))