mirror of
https://github.com/bigchaindb/js-bigchaindb-driver.git
synced 2024-11-21 17:26:56 +01:00
fix: Crypto conditions parsers
This commit is contained in:
parent
90a2cb2608
commit
2a104eb86b
@ -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
|
||||
}
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user