From 71fe66c1f51f50df0919236962ba8806aa14b5ef Mon Sep 17 00:00:00 2001 From: getlarge Date: Wed, 10 Mar 2021 13:36:00 +0100 Subject: [PATCH] fix: Crypto conditions parsers Signed-off-by: getlarge --- src/utils/ccJsonLoad.js | 18 +++++++++--------- src/utils/ccJsonify.js | 14 +++++++------- test/transaction/test_cryptoconditions.js | 11 +++++++---- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/utils/ccJsonLoad.js b/src/utils/ccJsonLoad.js index 7dc4ec4..a169394 100644 --- a/src/utils/ccJsonLoad.js +++ b/src/utils/ccJsonLoad.js @@ -2,9 +2,8 @@ // SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) // Code is Apache-2.0 and docs are CC-BY-4.0 -import { Buffer } from 'buffer' import base58 from 'bs58' -import cc from 'crypto-conditions' +import { Condition, Ed25519Sha256, ThresholdSha256 } from 'crypto-conditions' /** * Loads a crypto-condition class (Fulfillment or Condition) from a BigchainDB JSON object @@ -13,17 +12,18 @@ import cc from 'crypto-conditions' */ export default function ccJsonLoad(conditionJson) { if ('hash' in conditionJson) { - const condition = new cc.Condition() - condition.type = conditionJson.type_id - condition.bitmask = conditionJson.bitmask - condition.hash = Buffer.from(base58.decode(conditionJson.hash)) + const condition = new Condition() + condition.setTypeId(conditionJson.type_id) + condition.setSubtypes(conditionJson.bitmask) + condition.setHash(base58.decode(conditionJson.hash)) + // TODO: fix this, maxFulfillmentLength cannot be set in CryptoCondition lib condition.maxFulfillmentLength = parseInt(conditionJson.max_fulfillment_length, 10) return condition } else { let fulfillment if (conditionJson.type === 'threshold-sha-256') { - fulfillment = new cc.ThresholdSha256() + fulfillment = new ThresholdSha256() fulfillment.threshold = conditionJson.threshold conditionJson.subconditions.forEach((subconditionJson) => { const subcondition = ccJsonLoad(subconditionJson) @@ -36,8 +36,8 @@ export default function ccJsonLoad(conditionJson) { } if (conditionJson.type === 'ed25519-sha-256') { - fulfillment = new cc.Ed25519Sha256() - fulfillment.publicKey = Buffer.from(base58.decode(conditionJson.public_key)) + fulfillment = new Ed25519Sha256() + fulfillment.setPublicKey(base58.decode(conditionJson.public_key)) } return fulfillment } diff --git a/src/utils/ccJsonify.js b/src/utils/ccJsonify.js index 52f3ddb..9abb47b 100644 --- a/src/utils/ccJsonify.js +++ b/src/utils/ccJsonify.js @@ -19,8 +19,8 @@ export default function ccJsonify(fulfillment) { } const jsonBody = { - 'details': {}, - 'uri': conditionUri, + details: {}, + uri: conditionUri, } if (fulfillment.getTypeId() === 0) { @@ -35,15 +35,15 @@ export default function ccJsonify(fulfillment) { if (fulfillment.getTypeId() === 2) { return { - 'details': { - 'type': 'threshold-sha-256', - 'threshold': fulfillment.threshold, - 'subconditions': fulfillment.subconditions.map((subcondition) => { + details: { + type: 'threshold-sha-256', + threshold: fulfillment.threshold, + subconditions: fulfillment.subconditions.map((subcondition) => { const subconditionJson = ccJsonify(subcondition.body) return subconditionJson.details }) }, - 'uri': conditionUri, + uri: conditionUri, } } diff --git a/test/transaction/test_cryptoconditions.js b/test/transaction/test_cryptoconditions.js index d9a102e..4fedc29 100644 --- a/test/transaction/test_cryptoconditions.js +++ b/test/transaction/test_cryptoconditions.js @@ -2,8 +2,10 @@ // SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0) // Code is Apache-2.0 and docs are CC-BY-4.0 +import { createHash } from 'crypto' +import { validateFulfillment } from 'crypto-conditions' import test from 'ava' -import cc from 'crypto-conditions' +import base58 from 'bs58' import { Ed25519Keypair, Transaction, ccJsonLoad } from '../../src' import { delegatedSignTransaction } from '../constants' import sha256Hash from '../../src/sha256Hash' @@ -89,7 +91,7 @@ test('Fulfillment correctly formed', t => { .concat(txTransfer.inputs[0].fulfills.output_index) : msg const msgHash = sha256Hash(msgUniqueFulfillment) - t.truthy(cc.validateFulfillment( + t.truthy(validateFulfillment( txSigned.inputs[0].fulfillment, txCreate.outputs[0].condition.uri, Buffer.from(msgHash, 'hex') )) @@ -114,15 +116,16 @@ test('Delegated signature is correct', t => { }) test('CryptoConditions JSON load', t => { + const publicKey = '4zvwRjXUKGfvwnParsHAS3HuSVzV5cA4McphgmoCtajS' const cond = ccJsonLoad({ type: 'threshold-sha-256', threshold: 1, subconditions: [{ type: 'ed25519-sha-256', - public_key: 'a' + public_key: publicKey }, { - hash: 'a' + hash: base58.encode(createHash('sha256').update('a').digest()) }], }) t.truthy(cond.subconditions.length === 2)