1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-11-22 01:36:56 +01:00

fix: Crypto conditions parsers

Signed-off-by: getlarge <ed@getlarge.eu>
This commit is contained in:
getlarge 2021-03-10 13:36:00 +01:00
parent af90b97460
commit 71fe66c1f5
No known key found for this signature in database
GPG Key ID: E4E13243600F9566
3 changed files with 23 additions and 20 deletions

View File

@ -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
}

View File

@ -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,
}
}

View File

@ -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)