2017-06-12 16:57:29 +02:00
|
|
|
import base58 from 'bs58'
|
2017-05-01 18:44:49 +02:00
|
|
|
|
|
|
|
/**
|
2017-05-03 01:02:50 +02:00
|
|
|
* @public
|
|
|
|
* Serializes a crypto-condition class (Condition or Fulfillment) into a BigchainDB-compatible JSON
|
2017-06-12 17:19:09 +02:00
|
|
|
* @param {cc.Fulfillment} fulfillment base58 encoded Ed25519 public key for recipient of the Transaction
|
2017-05-01 18:44:49 +02:00
|
|
|
* @returns {object} Ed25519 Condition (that will need to wrapped in an Output)
|
|
|
|
*/
|
|
|
|
export default function ccJsonify(fulfillment) {
|
2017-06-12 16:57:29 +02:00
|
|
|
let conditionUri
|
2017-05-01 18:44:49 +02:00
|
|
|
|
2017-06-12 16:57:29 +02:00
|
|
|
if ('getConditionUri' in fulfillment) {
|
|
|
|
conditionUri = fulfillment.getConditionUri()
|
|
|
|
} else if ('serializeUri' in fulfillment) {
|
|
|
|
conditionUri = fulfillment.serializeUri()
|
|
|
|
}
|
2017-05-01 18:44:49 +02:00
|
|
|
|
2017-06-12 16:57:29 +02:00
|
|
|
const jsonBody = {
|
2017-05-01 18:44:49 +02:00
|
|
|
'details': {},
|
|
|
|
'uri': conditionUri,
|
2017-06-12 16:57:29 +02:00
|
|
|
}
|
2017-05-01 18:44:49 +02:00
|
|
|
|
|
|
|
if (fulfillment.getTypeId() === 0) {
|
2017-06-12 16:57:29 +02:00
|
|
|
jsonBody.details.type_id = 0
|
|
|
|
jsonBody.details.bitmask = 3
|
2017-05-01 18:44:49 +02:00
|
|
|
|
|
|
|
if ('preimage' in fulfillment) {
|
2017-06-12 16:57:29 +02:00
|
|
|
jsonBody.details.preimage = fulfillment.preimage.toString()
|
|
|
|
jsonBody.details.type = 'fulfillment'
|
2017-05-01 18:44:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-12 16:57:29 +02:00
|
|
|
if (fulfillment.getTypeId() === 2) {
|
2017-05-01 18:44:49 +02:00
|
|
|
return {
|
|
|
|
'details': {
|
2017-06-20 11:35:25 +02:00
|
|
|
'type': 'threshold-sha-256',
|
2017-05-01 18:44:49 +02:00
|
|
|
'threshold': fulfillment.threshold,
|
2017-06-28 01:25:07 +02:00
|
|
|
'subconditions': fulfillment.subconditions.map((subcondition) => {
|
2017-06-12 16:57:29 +02:00
|
|
|
const subconditionJson = ccJsonify(subcondition.body)
|
|
|
|
return subconditionJson.details
|
2017-05-01 18:44:49 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
'uri': conditionUri,
|
2017-06-12 16:57:29 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-01 18:44:49 +02:00
|
|
|
|
|
|
|
if (fulfillment.getTypeId() === 4) {
|
2017-06-20 11:35:25 +02:00
|
|
|
jsonBody.details.type = 'ed25519-sha-256'
|
2017-05-01 18:44:49 +02:00
|
|
|
|
|
|
|
if ('publicKey' in fulfillment) {
|
2017-06-12 16:57:29 +02:00
|
|
|
jsonBody.details.public_key = base58.encode(fulfillment.publicKey)
|
2017-05-01 18:44:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('hash' in fulfillment) {
|
2017-06-12 16:57:29 +02:00
|
|
|
jsonBody.details.hash = base58.encode(fulfillment.hash)
|
|
|
|
jsonBody.details.max_fulfillment_length = fulfillment.maxFulfillmentLength
|
|
|
|
jsonBody.details.type = 'condition'
|
2017-05-01 18:44:49 +02:00
|
|
|
}
|
|
|
|
|
2017-06-12 16:57:29 +02:00
|
|
|
return jsonBody
|
2017-05-01 18:44:49 +02:00
|
|
|
}
|