Crypto-conditions code: make code more consistent

- Put code in tabs
- Generate an actually random preimage in Python
- Make code more consistent across languages
This commit is contained in:
Rome Reginelli
2022-05-09 17:14:20 -07:00
committed by GitHub
parent 71880b2ea8
commit 1a099eded7

View File

@@ -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. <!-- SPELLING_IGNORE: psk -->
Example JavaScript code for a random fulfillment and condition:
Example code for a random fulfillment and condition:
<!-- MULTICODE_BLOCK_START -->
_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())
```
<!-- MULTICODE_BLOCK_END -->
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.