forked from bigchaindb/js-crypto-conditions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (68 loc) · 2.45 KB
/
index.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict'
const Condition = require('./src/lib/condition')
const Fulfillment = require('./src/lib/fulfillment')
const TypeRegistry = require('./src/lib/type-registry')
const PreimageSha256 = require('./src/types/preimage-sha256')
const PrefixSha256 = require('./src/types/prefix-sha256')
const ThresholdSha256 = require('./src/types/threshold-sha256')
const RsaSha256 = require('./src/types/rsa-sha256')
const Ed25519Sha256 = require('./src/types/ed25519-sha256')
const base64url = require('./src/util/base64url')
const EMPTY_BUFFER = Buffer.alloc(0)
const validateCondition = (serializedCondition) => {
// Parse condition, throw on error
const condition = Condition.fromUri(serializedCondition)
// Validate condition, throw on error
return condition.validate()
}
const validateFulfillment = (serializedFulfillment, serializedCondition, message) => {
if (typeof message === 'undefined') {
message = EMPTY_BUFFER
}
if (!Buffer.isBuffer(message)) {
throw new Error('Message must be provided as a Buffer')
}
// Parse fulfillment, throw on error
const fulfillment = Fulfillment.fromUri(serializedFulfillment)
// Compare condition URI, throw on error
const conditionUri = fulfillment.getConditionUri()
if (conditionUri !== serializedCondition) {
throw new Error('Fulfillment does not match condition (expected: ' +
serializedCondition + ', actual: ' + conditionUri + ')')
}
// Validate fulfillment, throw on error
return fulfillment.validate(message)
}
const fulfillmentToCondition = (serializedFulfillment) => {
// Parse fulfillment, throw on error
const fulfillment = Fulfillment.fromUri(serializedFulfillment)
return fulfillment.getConditionUri()
}
const fromJson = (json) => {
const fulfillment = Fulfillment.fromJson(json)
return fulfillment
}
TypeRegistry.registerType(PreimageSha256)
TypeRegistry.registerType(PrefixSha256)
TypeRegistry.registerType(ThresholdSha256)
TypeRegistry.registerType(RsaSha256)
TypeRegistry.registerType(Ed25519Sha256)
module.exports = {
Condition,
Fulfillment,
TypeRegistry,
PreimageSha256,
RsaSha256,
PrefixSha256,
ThresholdSha256,
Ed25519Sha256,
validateCondition,
validateFulfillment,
fulfillmentToCondition,
fromJson,
base64url,
fromConditionUri: Condition.fromUri.bind(Condition),
fromConditionBinary: Condition.fromBinary.bind(Condition),
fromFulfillmentUri: Fulfillment.fromUri.bind(Fulfillment),
fromFulfillmentBinary: Fulfillment.fromBinary.bind(Fulfillment)
}