From 1a099eded703630a2bf2f5c17d35cd5270473eb3 Mon Sep 17 00:00:00 2001 From: Rome Reginelli Date: Mon, 9 May 2022 17:14:20 -0700 Subject: [PATCH] Crypto-conditions code: make code more consistent - Put code in tabs - Generate an actually random preimage in Python - Make code more consistent across languages --- .../send-a-conditionally-held-escrow.md | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/content/tutorials/use-specialized-payment-types/use-escrows/send-a-conditionally-held-escrow.md b/content/tutorials/use-specialized-payment-types/use-escrows/send-a-conditionally-held-escrow.md index 855fc116d8..67a0f7fa8f 100644 --- a/content/tutorials/use-specialized-payment-types/use-escrows/send-a-conditionally-held-escrow.md +++ b/content/tutorials/use-specialized-payment-types/use-escrows/send-a-conditionally-held-escrow.md @@ -15,40 +15,46 @@ XRP Ledger escrows require PREIMAGE-SHA-256 [crypto-conditions][]. To calculate - Use a cryptographically secure source of randomness to generate at least 32 random bytes. - Follow Interledger Protocol's [PSK specification](https://github.com/interledger/rfcs/blob/master/deprecated/0016-pre-shared-key/0016-pre-shared-key.md) and use an HMAC-SHA-256 of the ILP packet as the fulfillment. -Example JavaScript code for a random fulfillment and condition: +Example code for a random fulfillment and condition: + + + +_JavaScript_ ```js const cc = require('five-bells-condition') const crypto = require('crypto') const preimageData = crypto.randomBytes(32) -const myFulfillment = new cc.PreimageSha256() -myFulfillment.setPreimage(preimageData) +const fulfillment = new cc.PreimageSha256() +fulfillment.setPreimage(preimageData) -const condition = myFulfillment.getConditionBinary().toString('hex').toUpperCase() +const condition = fulfillment.getConditionBinary().toString('hex').toUpperCase() console.log('Condition:', condition) -// (Random hexadecimal, 72 chars in length) -// keep secret until you want to finish executing the held payment: -const fulfillment = myFulfillment.serializeBinary().toString('hex').toUpperCase() -console.log('Fulfillment:', fulfillment) -// (Random hexadecimal, 78 chars in length) +// Keep secret until you want to finish the escrow +const fulfillment_hex = fulfillment.serializeBinary().toString('hex').toUpperCase() +console.log('Fulfillment:', fulfillment_hex) ``` -Example Python code for a random fulfillment and condition: -``` py +_Python_ + +```py +from os import urandom from cryptoconditions import PreimageSha256 -secret = b"Python is awesome" # modify this if you want to change the output -# scret must be bytes format +secret = urandom(32) -fufill = PreimageSha256(preimage=secret) +fulfillment = PreimageSha256(preimage=secret) -print("Condition", str.upper(fufill.condition_binary.hex())) +print("Condition", fulfillment.condition_binary.hex().upper()) -print("Fulfillment", str.upper(fufill.serialize_binary().hex())) +// Keep secret until you want to finish the escrow +print("Fulfillment", fulfillment.serialize_binary().hex().upper()) ``` + + Save the condition and the fulfillment for later. Be sure to keep the fulfillment secret until you want to finish executing the held payment. Anyone who knows the fulfillment can finish the escrow, releasing the held funds to their intended destination.