Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
Escrow Conditions
"""
# retrieve the last transaction of testuser2
tx_retrieved_id = b.get_owned_ids(testuser2_pub).pop()
# Create escrow template with the execute and abort address
tx_escrow = b.create_transaction(testuser2_pub, [testuser2_pub, testuser1_pub], tx_retrieved_id, 'TRANSFER')
# Set expiry time (12 secs from now)
time_sleep = 12
time_expire = str(float(gen_timestamp()) + time_sleep)
# Create escrow and timeout condition
condition_escrow = cc.ThresholdSha256Fulfillment(threshold=1) # OR Gate
condition_timeout = cc.TimeoutFulfillment(expire_time=time_expire) # only valid if now() <= time_expire
condition_timeout_inverted = cc.InvertedThresholdSha256Fulfillment(threshold=1)
condition_timeout_inverted.add_subfulfillment(condition_timeout)
# Create execute branch
condition_execute = cc.ThresholdSha256Fulfillment(threshold=2) # AND gate
condition_execute.add_subfulfillment(cc.Ed25519Fulfillment(public_key=testuser1_pub)) # execute address
condition_execute.add_subfulfillment(condition_timeout) # federation checks on expiry
condition_escrow.add_subfulfillment(condition_execute)
# Create abort branch
condition_abort = cc.ThresholdSha256Fulfillment(threshold=2) # AND gate
condition_abort.add_subfulfillment(cc.Ed25519Fulfillment(public_key=testuser2_pub)) # abort address
condition_abort.add_subfulfillment(condition_timeout_inverted)
condition_escrow.add_subfulfillment(condition_abort)
# Update the condition in the newly created transaction
b.write_transaction(hashlock_fulfill_tx)
print(json.dumps(hashlock_fulfill_tx, sort_keys=True, indent=4, separators=(',', ':')))
"""
Timeout Conditions
"""
# Create transaction template
tx_timeout = b.create_transaction(b.me, None, None, 'CREATE')
# Set expiry time (12 secs from now)
time_sleep = 12
time_expire = str(float(gen_timestamp()) + time_sleep)
# only valid if the server time <= time_expire
condition_timeout = cc.TimeoutFulfillment(expire_time=time_expire)
# The conditions list is empty, so we need to append a new condition
tx_timeout['transaction']['conditions'].append({
'condition': {
'details': condition_timeout.to_dict(),
'uri': condition_timeout.condition.serialize_uri()
},
'cid': 0,
'owners_after': None
})
# conditions have been updated, so hash needs updating
tx_timeout['id'] = util.get_hash_data(tx_timeout)
# sign transaction
tx_timeout_signed = b.sign_transaction(tx_timeout, b.me_private)
asset = bigchain.get_transaction(asset_id['txid'])
payload = asset['transaction']['data']['payload'].copy()
if ilp_header:
payload.update({'ilp_header': ilp_header})
# Create escrow template with the execute and abort address
asset_escrow = bigchain.create_transaction(source, [source, to], asset_id, 'TRANSFER',
payload=payload)
if not expires_at:
# Set expiry time (100 secs from now)
time_sleep = 100
expires_at = float(bigchaindb.util.timestamp()) + time_sleep
# Create escrow and timeout condition
condition_escrow = cc.ThresholdSha256Fulfillment(threshold=1) # OR Gate
condition_timeout = cc.TimeoutFulfillment(expire_time=str(expires_at)) # only valid if now() <= time_expire
condition_timeout_inverted = cc.InvertedThresholdSha256Fulfillment(threshold=1)
condition_timeout_inverted.add_subfulfillment(condition_timeout)
# Create execute branch
execution_threshold = 3 if execution_condition else 2
condition_execute = cc.ThresholdSha256Fulfillment(threshold=execution_threshold) # AND gate
condition_execute.add_subfulfillment(cc.Ed25519Fulfillment(public_key=to)) # execute address
condition_execute.add_subfulfillment(condition_timeout) # federation checks on expiry
if execution_condition:
condition_execute.add_subcondition_uri(execution_condition)
condition_escrow.add_subfulfillment(condition_execute)
# Create abort branch
condition_abort = cc.ThresholdSha256Fulfillment(threshold=2) # AND gate
condition_abort.add_subfulfillment(cc.Ed25519Fulfillment(public_key=source)) # abort address
condition_abort.add_subfulfillment(condition_timeout_inverted)
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)