-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (107 loc) · 3.86 KB
/
index.js
File metadata and controls
127 lines (107 loc) · 3.86 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* Copyright 2017 Tierion
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const chpSchema = require('chainpoint-proof-json-schema')
const mpack = require('msgpack-lite')
const pako = require('pako')
let isValidHex = function(hex) {
var hexRegex = /^[0-9A-Fa-f]{2,}$/
var hasHexChars = hexRegex.test(hex)
var hasEvenLen = hex.length % 2 === 0
if (hasHexChars && hasEvenLen) return true
return false
}
let objectToBinary = (proofObj, cb) => {
if (!proofObj) return cb('No proof Object or JSON string arg provided')
// Handle a JSON String arg
if (typeof proofObj === 'string') {
try {
proofObj = JSON.parse(proofObj)
} catch (err) {
return cb('Invalid JSON string proof provided')
}
}
// A well-formed, schema compliant Chainpoint proof?
let validateResult = chpSchema.validate(proofObj)
if (!validateResult.valid) return cb('Chainpoint v4 schema validation error')
let deflatedProof = pako.deflate(mpack.encode(proofObj))
return cb(null, Buffer.from(deflatedProof))
}
let objectToBinarySync = proofObj => {
if (!proofObj) throw new Error('No proof Object or JSON string arg provided')
// Handle a JSON String arg
if (typeof proofObj === 'string') {
try {
proofObj = JSON.parse(proofObj)
} catch (err) {
throw new Error('Invalid JSON string proof provided')
}
}
// A well-formed, schema compliant Chainpoint proof?
let validateResult = chpSchema.validate(proofObj)
if (!validateResult.valid) throw new Error('Chainpoint v4 schema validation error')
let deflatedProof = pako.deflate(mpack.encode(proofObj))
return Buffer.from(deflatedProof)
}
let objectToBase64 = (proofObj, cb) => {
objectToBinary(proofObj, (err, proofBinary) => {
if (err) return cb(err)
return cb(null, proofBinary.toString('base64'))
})
}
let objectToBase64Sync = proofObj => {
let proofBinary = objectToBinarySync(proofObj)
return proofBinary.toString('base64')
}
let binaryToObject = (proof, cb) => {
if (!proof) return cb('No binary proof arg provided')
try {
// Handle a Hexadecimal String arg in addition to a Buffer
if (!Buffer.isBuffer(proof)) {
if (isValidHex(proof)) {
proof = Buffer.from(proof, 'hex')
} else {
proof = Buffer.from(proof, 'base64')
}
}
let unpackedProof = mpack.decode(pako.inflate(proof))
if (!chpSchema.validate(unpackedProof).valid) return cb('Chainpoint v4 schema validation error')
return cb(null, unpackedProof)
} catch (e) {
return cb('Could not parse Chainpoint v4 binary')
}
}
let binaryToObjectSync = proof => {
if (!proof) throw new Error('No binary proof arg provided')
try {
// Handle a Hexadecimal String arg in addition to a Buffer
if (!Buffer.isBuffer(proof)) {
if (isValidHex(proof)) {
proof = Buffer.from(proof, 'hex')
} else {
proof = Buffer.from(proof, 'base64')
}
}
let unpackedProof = mpack.decode(pako.inflate(proof))
if (!chpSchema.validate(unpackedProof).valid) throw new Error('Chainpoint v4 schema validation error')
return unpackedProof
} catch (e) {
throw new Error('Could not parse Chainpoint v4 binary')
}
}
module.exports = {
objectToBinary: objectToBinary,
objectToBase64: objectToBase64,
binaryToObject: binaryToObject,
objectToBinarySync: objectToBinarySync,
objectToBase64Sync: objectToBase64Sync,
binaryToObjectSync: binaryToObjectSync
}