Skip to content

Commit

Permalink
fix: Crypto conditions parsers
Browse files Browse the repository at this point in the history
Signed-off-by: getlarge <[email protected]>
  • Loading branch information
getlarge committed Mar 10, 2021
1 parent af90b97 commit 71fe66c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
18 changes: 9 additions & 9 deletions src/utils/ccJsonLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions src/utils/ccJsonify.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export default function ccJsonify(fulfillment) {
}

const jsonBody = {
'details': {},
'uri': conditionUri,
details: {},
uri: conditionUri,
}

if (fulfillment.getTypeId() === 0) {
Expand All @@ -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,
}
}

Expand Down
11 changes: 7 additions & 4 deletions test/transaction/test_cryptoconditions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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')
))
Expand All @@ -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)
Expand Down

0 comments on commit 71fe66c

Please sign in to comment.