-
Notifications
You must be signed in to change notification settings - Fork 92
/
ccJsonify.js
65 lines (54 loc) · 2.02 KB
/
ccJsonify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright BigchainDB GmbH and BigchainDB contributors
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
// Code is Apache-2.0 and docs are CC-BY-4.0
import base58 from 'bs58'
/**
* Serializes a crypto-condition class (Condition or Fulfillment) into a BigchainDB-compatible JSON
* @param {cc.Fulfillment} fulfillment base58 encoded Ed25519 public key for recipient of the Transaction
* @returns {Object} Ed25519 Condition (that will need to wrapped in an Output)
*/
export default function ccJsonify(fulfillment) {
let conditionUri
if ('getConditionUri' in fulfillment) {
conditionUri = fulfillment.getConditionUri()
} else if ('serializeUri' in fulfillment) {
conditionUri = fulfillment.serializeUri()
}
const jsonBody = {
details: {},
uri: conditionUri,
}
if (fulfillment.getTypeId() === 0) {
jsonBody.details.type_id = 0
jsonBody.details.bitmask = 3
if ('preimage' in fulfillment) {
jsonBody.details.preimage = fulfillment.preimage.toString()
jsonBody.details.type = 'fulfillment'
}
}
if (fulfillment.getTypeId() === 2) {
return {
details: {
type: 'threshold-sha-256',
threshold: fulfillment.threshold,
subconditions: fulfillment.subconditions.map((subcondition) => {
const subconditionJson = ccJsonify(subcondition.body)
return subconditionJson.details
})
},
uri: conditionUri,
}
}
if (fulfillment.getTypeId() === 4) {
jsonBody.details.type = 'ed25519-sha-256'
if ('publicKey' in fulfillment) {
jsonBody.details.public_key = base58.encode(fulfillment.publicKey)
}
}
if ('hash' in fulfillment) {
jsonBody.details.hash = base58.encode(fulfillment.hash)
jsonBody.details.max_fulfillment_length = fulfillment.maxFulfillmentLength
jsonBody.details.type = 'condition'
}
return jsonBody
}