Crypto Conditions
Implementation of crypto-conditions in JavaScript for Node.js and the browser
Specification
Editor's Draft: draft-thomas-crypto-conditions-02
This specification is only a draft at this stage and has not been submitted.
Table of Contents
- Crypto Conditions
- Specification
- Table of Contents
- API Documentation
- Usage
- Validate a Condition
- Validate a Fulfillment (No Message)
- Get Condition from Fulfillment And Validate
- Create a PREIMAGE-SHA-256 Condition (Hashlock)
- Create a PREIMAGE-SHA-256 Fullfillment (Hashlock)
- Parse a Fulfillment
- Create an ED25519 Condition
- Fulfill an ED25519 Condition
- Verify a Fulfillment (with Message)
- Create a THRESHOLD-SHA-256 Condition
- Create a THRESHOLD-SHA-256 Fulfillment
- Create a PREFIX-SHA-256 Condition
- Create a PREFIX-SHA-256 Fulfillment
- Create an RSA-SHA-256 Condition
- Create an RSA-SHA-256 Fulfillment
- Advanced: Parse a Condition
- Advanced: Parse and Reserialize a THRESHOLD-SHA-256 Fulfillment
- Advanced: Manually Create a Condition
API Documentation
Usage
Validate a Condition
const cc =// Check a condition for validityconst condition = 'ni:///sha-256;47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU?fpt=preimage-sha-256&cost=0'const validationResult = cc// validationResult === true
This will ensure that the requested type, features and fulfillment length are all accepted by the current implementation.
Validate a Fulfillment (No Message)
const cc =const condition = 'ni:///sha-256;47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU?fpt=preimage-sha-256&cost=0'const fulfillment = 'oAKAAA'const validationResult = cc// validationResult === true
This validates the fulfillment and ensures that it matches the given condition.
Get Condition from Fulfillment And Validate
const cc =const fulfillment = 'oAKAAA'const condition = cc// You could now look up this condition in your database etc.const validationResult = cc// validationResult === true
Create a PREIMAGE-SHA-256 Condition (Hashlock)
const cc =const myFulfillment =myFulfillmentconsole// prints 'ni:///sha-256;47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU?fpt=preimage-sha-256&cost=0'
Create a PREIMAGE-SHA-256 Fullfillment (Hashlock)
const cc =const myFulfillment =myFulfillmentconsole// prints 'oAKAAA'
Parse a Fulfillment
const cc =const parsedFulfillment = cc// parsedFulfillment instanceof cc.PreimageSha256 === true// Note: Merely parsing a fulfillment DOES NOT validate it.// Validate a fulfillmentparsedFulfillment
Create an ED25519 Condition
const cc =const ed25519Fulfillment =ed25519Fulfillmentconsole// prints 'ni:///sha-256;U1YhFdW0lOI-SVF3PbDP4t_lVefj_-tB5P11yvfBaoE?fpt=ed25519-sha-256&cost=131072'
Fulfill an ED25519 Condition
const cc =const edPrivateKey = '833fe62409237b9d62ec77587520911e9a759cec1d19755b7da901b96dca3d42' 'hex'const ed25519Fulfillment =// ed25519Fulfillment.setPublicKey(new Buffer('...'))// ed25519Fulfillment.setSignature(new Buffer('...'))// -- or --ed25519Fulfillmentconsole// prints 'ni:///sha-256;U1YhFdW0lOI-SVF3PbDP4t_lVefj_-tB5P11yvfBaoE?fpt=ed25519-sha-256&cost=131072'console// prints 'pGSAIOwXK5OtXlY79JMscOEkUDTDVGfvLv1NZOv4GWg0Z-K_gUC2IpH62UMvjymLnEpIldvik_b_2hpo2t8Mze9fR6DHISpf6jzal6P0wD6p8uisHOyGpR1FISer26CdG28zHAcK'
Verify a Fulfillment (with Message)
const cc =const fulfillment = 'pGSAIOwXK5OtXlY79JMscOEkUDTDVGfvLv1NZOv4GWg0Z-K_gUC2IpH62UMvjymLnEpIldvik_b_2hpo2t8Mze9fR6DHISpf6jzal6P0wD6p8uisHOyGpR1FISer26CdG28zHAcK'const condition = 'ni:///sha-256;U1YhFdW0lOI-SVF3PbDP4t_lVefj_-tB5P11yvfBaoE?fpt=ed25519-sha-256&cost=131072'const message = 'Hello World! Conditions are here!'const result = cc// result === true
Create a THRESHOLD-SHA-256 Condition
const cc =const thresholdFulfillment =thresholdFulfillmentthresholdFulfillmentthresholdFulfillment // defaults to subconditions.lengthconsole// prints 'ni:///sha-256;l-wuy18t5Ic2GfCbVb9yAiTJ_gJbN2x34fk3eHOz5kY?fpt=threshold-sha-256&cost=133120&subtypes=ed25519-sha-256,preimage-sha-256'
Create a THRESHOLD-SHA-256 Fulfillment
const cc =const thresholdFulfillment =thresholdFulfillmentthresholdFulfillmentthresholdFulfillment // defaults to subconditions.lengthconsole// prints 'ni:///sha-256;l-wuy18t5Ic2GfCbVb9yAiTJ_gJbN2x34fk3eHOz5kY?fpt=threshold-sha-256&cost=133120&subtypes=ed25519-sha-256,preimage-sha-256'const thresholdFulfillmentUri = thresholdFulfillment// Note: If there are more than enough fulfilled subconditions, shorter// fulfillments will be chosen over longer ones.// thresholdFulfillmentUri.length === 68console// prints 'ojGgBKACgAChKaQngCBTViEV1bSU4j5JUXc9sM_i3-VV5-P_60Hk_XXK98FqgYEDAgAA'
Create a PREFIX-SHA-256 Condition
const cc =const prefix =prefixprefixprefixconsole// prints 'ni:///sha-256;3Q87-ZwAaOH3KKkRD-wAuTiA3g7T8idCir2Gie6hkoI?fpt=prefix-sha-256&cost=197637&subtypes=ed25519-sha-256'
Create a PREFIX-SHA-256 Fulfillment
const cc =const prefix =prefixprefixprefixconst fulfillmentUri = prefixconsole// prints 'oXuADUhlbGxvIFdvcmxkISCBAkAAomakZIAg7Bcrk61eVjv0kyxw4SRQNMNUZ-8u_U1k6_gZaDRn4r-BQLYikfrZQy-PKYucSkiV2-KT9v_aGmja3wzN719HoMchKl_qPNqXo_TAPqny6Kwc7IalHUUhJ6vboJ0bbzMcBwo'const conditionUri = prefixconst message = 'Conditions are here!'cc
Create an RSA-SHA-256 Condition
const cc =const rsaFulfillment =rsaFulfillmentconsole// prints 'ni:///sha-256;j2luKLUjz-Ilu0jdDQO-Eg5Srmu06lEs4dsZHHVxcdc?fpt=rsa-sha-256&cost=16384'
Create an RSA-SHA-256 Fulfillment
const cc =const exampleMessage = 'Hello World! Conditions are here!'const privateKey ='-----BEGIN RSA PRIVATE KEY-----\n' +'MIICXAIBAAKBgQCzDnqTh4O6v4NoUP9J4U+H4/ktXEbjP+yj5PCyI1hYCxF2WZX0\n' +'uO6n+0cSwuHjFvf3dalT0jIhahadmmTdwAcSCkALN/KvwHe2L+ME3nTeahGexAdr\n' +'UpxPYJawuq1PUz3wFzubgi/YXWX6S++pLY9ST2nLygE2vYDQlcFprsDglQIDAQAB\n' +'AoGAB7Rjyd1W6b475U027vLm/S3uFumVk0m44QSE5uVmc8NmKPWJ4lHi0w+Y61G/\n' +'booaeWdytcyho5ZxCq8OEAynQSkJiBNtzBg+xCGcO6GPOf+dFBYZFQsXiG/EbwrA\n' +'pT0cv+AqiGzLIAh2WtNI6cr5/ZEMScNhMcQ4AZ1kRyUdpIECQQDbRtFz0dSMMvS/\n' +'1KtDZxej9HqC5xOEuCDEZuLvk4bW4mC02OP/H/VV5qqclz0LIvMWK6TDtoFRpkvD\n' +'UYiYoc85AkEA0QtH1zQlGGlliLcWoPeqjkbtf3ocmYy2exBSCwnOf87xV//k9pNC\n' +'7jmoIzRgKVef8kQR/mXWszo3WbWMt0aAPQJBAMtoRD/GM/7h/fw9Uamy5lEnJsZr\n' +'iMWi8HKAZp+LIJgRY1gfolA12yWWVknwWaYNA6ZbUfpjQE73jmxfI/FCmLECQBmF\n' +'WAr06cZ2L5gmShPyyJbAIASdItq4LBsQHgQM+XHvENXeftR/m/87eMR7g3XopbVN\n' +'DClTw4d0Bwfjuz8w0z0CQFG7RmgPqsTEGfojpRgLZnec87R6XhuUY5ZoGgpnx7r9\n' +'/zGekAwjBZDKpc+H0jC14JjMzRRKeWVEpDU3k2cfBH0=\n' +'-----END RSA PRIVATE KEY-----\n'const rsaFulfillment =// rsaFulfillment.setPublicModulus(new Buffer('...'))// rsaFulfillment.setSignature(new Buffer('...'))// -- or --rsaFulfillmentconsole// prints '355'// Verify RSA-SHA256 conditionconst rsaFulfillmentUri = rsaFulfillmentconst rsaConditionUri = rsaFulfillmentcc
Advanced: Parse a Condition
const cc =// Parse a conditionconst condition = 'ni:///sha-256;47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU?fpt=preimage-sha-256&cost=0'const parsedCondition = ccconsole// prints 'Condition'// Compile to a conditionconsole// prints condition
Advanced: Parse and Reserialize a THRESHOLD-SHA-256 Fulfillment
const cc =const thresholdFulfillmentUri = 'oi-gBKACgAChJ6AlgCB_g7Flf_H8U7ktwYFIodZd_C1LH6PWdyhK3dIAEm2QaYEBDA'const reparsedFulfillment = ccconst reserializedFulfillment = reparsedFulfillmentconsole// prints thresholdFulfillmentUri
Advanced: Manually Create a Condition
const cc =const myCondition =myConditionmyConditionmyConditionmyConditionconsole// prints 'ni:///sha-256;47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU?fpt=preimage-sha-256&cost=0'