-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
269 lines (252 loc) · 9.73 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* 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 crypto = require('crypto')
const sha3512 = require('js-sha3').sha3_512
const sha3384 = require('js-sha3').sha3_384
const sha3256 = require('js-sha3').sha3_256
const sha3224 = require('js-sha3').sha3_224
const chpSchema = require('chainpoint-proof-json-schema')
const chpBinary = require('chainpoint-binary')
function parse(chainpointObject) {
// if the supplied is a Buffer, Hex, or Base64 string, convert to JS object
if (typeof chainpointObject === 'string' || Buffer.isBuffer(chainpointObject))
chainpointObject = chpBinary.binaryToObjectSync(chainpointObject)
let schemaCheck = chpSchema.validate(chainpointObject)
if (!schemaCheck.valid) throw new Error(schemaCheck.errors)
// initialize the result object
let result = {}
// identify this result set with the basic information on the hash
result.hash = chainpointObject.hash
result.proof_id = chainpointObject.proof_id
result.hash_received = chainpointObject.hash_received
// acquire all anchor points and calcaulte expected values for all branches, recursively
result.branches = parseBranches(chainpointObject.hash, chainpointObject.branches)
return result
}
function parseBranches(startHash, branchArray) {
var branches = []
var currentHashValue = Buffer.from(startHash, 'hex')
// iterate through all branches in the current branch array
for (var b = 0; b < branchArray.length; b++) {
// initialize anchors array for this branch
let anchors = []
// iterate through all operations in the operations array for this branch
let currentbranchOps = branchArray[b].ops
for (var o = 0; o < currentbranchOps.length; o++) {
if (currentbranchOps[o].r) {
// hex data gets treated as hex, otherwise it is converted to bytes assuming a ut8 encoded string
let concatValue = isHex(currentbranchOps[o].r)
? Buffer.from(currentbranchOps[o].r, 'hex')
: Buffer.from(currentbranchOps[o].r, 'utf8')
currentHashValue = Buffer.concat([currentHashValue, concatValue])
} else if (currentbranchOps[o].l) {
// hex data gets treated as hex, otherwise it is converted to bytes assuming a ut8 encoded string
let concatValue = isHex(currentbranchOps[o].l)
? Buffer.from(currentbranchOps[o].l, 'hex')
: Buffer.from(currentbranchOps[o].l, 'utf8')
currentHashValue = Buffer.concat([concatValue, currentHashValue])
} else if (currentbranchOps[o].op) {
switch (currentbranchOps[o].op) {
case 'sha-224':
currentHashValue = crypto
.createHash('sha224')
.update(currentHashValue)
.digest()
break
case 'sha-256':
currentHashValue = crypto
.createHash('sha256')
.update(currentHashValue)
.digest()
break
case 'sha-384':
currentHashValue = crypto
.createHash('sha384')
.update(currentHashValue)
.digest()
break
case 'sha-512':
currentHashValue = crypto
.createHash('sha512')
.update(currentHashValue)
.digest()
break
case 'sha3-224':
currentHashValue = Buffer.from(sha3224.array(currentHashValue))
break
case 'sha3-256':
currentHashValue = Buffer.from(sha3256.array(currentHashValue))
break
case 'sha3-384':
currentHashValue = Buffer.from(sha3384.array(currentHashValue))
break
case 'sha3-512':
currentHashValue = Buffer.from(sha3512.array(currentHashValue))
break
case 'sha-256-x2':
currentHashValue = crypto
.createHash('sha256')
.update(currentHashValue)
.digest()
currentHashValue = crypto
.createHash('sha256')
.update(currentHashValue)
.digest()
break
}
} else if (currentbranchOps[o].anchors) {
anchors = anchors.concat(parseAnchors(currentHashValue, currentbranchOps[o].anchors))
}
}
let branchObj = {
label: branchArray[b].label || undefined,
anchors: anchors
}
if (branchArray[b].branches)
branchObj.branches = parseBranches(currentHashValue.toString('hex'), branchArray[b].branches)
// if this branch is a standard Chaipoint BTC anchor branch,
// output the OP_RETURN value and the BTC transaction id
if (branchObj.label === 'btc_anchor_branch') {
let btcAnchorInfo = getBtcAnchorInfo(startHash, currentbranchOps)
branchObj.opReturnValue = btcAnchorInfo.opReturnValue
branchObj.btcTxId = btcAnchorInfo.btcTxId
branchObj.rawTx = btcAnchorInfo.rawTx
}
branches.push(branchObj)
}
return branches
}
function parseAnchors(currentHashValue, anchorsArray) {
var anchors = []
for (var x = 0; x < anchorsArray.length; x++) {
let expectedValue = currentHashValue.toString('hex')
// BTC merkle root values is in little endian byte order
// All hashes and calculations in a Chainpoint proof are in big endian byte order
// If we are determining the expected value for a BTC anchor, the expected value
// result byte order must be reversed to match the BTC merkle root byte order
// before making any comparisons
if (['btc', 'tbtc'].includes(anchorsArray[x].type))
expectedValue = expectedValue
.match(/.{2}/g)
.reverse()
.join('')
anchors.push({
type: anchorsArray[x].type,
anchor_id: anchorsArray[x].anchor_id,
uris: anchorsArray[x].uris || undefined,
expected_value: expectedValue
})
}
return anchors
}
function getBtcAnchorInfo(startHash, ops) {
// This calculation depends on the branch using the standard format
// for btc_anchor_branch type branches created by Chainpoint services
let currentHashValue = Buffer.from(startHash, 'hex')
let has256x2 = false
let isFirst256x2 = false
let rawTx = ''
let opResultTable = ops.map(op => {
if (op.r) {
// hex data gets treated as hex, otherwise it is converted to bytes assuming a ut8 encoded string
let concatValue = isHex(op.r) ? Buffer.from(op.r, 'hex') : Buffer.from(op.r, 'utf8')
currentHashValue = Buffer.concat([currentHashValue, concatValue])
return { opResult: currentHashValue, op: op, isFirst256x2: isFirst256x2 }
} else if (op.l) {
// hex data gets treated as hex, otherwise it is converted to bytes assuming a ut8 encoded string
let concatValue = isHex(op.l) ? Buffer.from(op.l, 'hex') : Buffer.from(op.l, 'utf8')
currentHashValue = Buffer.concat([concatValue, currentHashValue])
return { opResult: currentHashValue, op: op, isFirst256x2: isFirst256x2 }
} else if (op.op) {
switch (op.op) {
case 'sha-224':
currentHashValue = crypto
.createHash('sha224')
.update(currentHashValue)
.digest()
break
case 'sha-256':
currentHashValue = crypto
.createHash('sha256')
.update(currentHashValue)
.digest()
break
case 'sha-384':
currentHashValue = crypto
.createHash('sha384')
.update(currentHashValue)
.digest()
break
case 'sha-512':
currentHashValue = crypto
.createHash('sha512')
.update(currentHashValue)
.digest()
break
case 'sha3-224':
currentHashValue = Buffer.from(sha3224.array(currentHashValue))
break
case 'sha3-256':
currentHashValue = Buffer.from(sha3256.array(currentHashValue))
break
case 'sha3-384':
currentHashValue = Buffer.from(sha3384.array(currentHashValue))
break
case 'sha3-512':
currentHashValue = Buffer.from(sha3512.array(currentHashValue))
break
case 'sha-256-x2':
// if this is the first double sha256, then the currentHashValue is the rawTx
// on the public Bitcoin blockchain
if (!has256x2) rawTx = currentHashValue
currentHashValue = crypto
.createHash('sha256')
.update(currentHashValue)
.digest()
currentHashValue = crypto
.createHash('sha256')
.update(currentHashValue)
.digest()
if (!has256x2) {
isFirst256x2 = true
has256x2 = true
} else {
isFirst256x2 = false
}
break
}
return { opResult: currentHashValue, op: op, isFirst256x2: isFirst256x2 }
}
})
let btcTxIdOpIndex = opResultTable.findIndex(result => {
return result.isFirst256x2
})
let opReturnOpIndex = btcTxIdOpIndex - 3
return {
opReturnValue: opResultTable[opReturnOpIndex].opResult.toString('hex'),
btcTxId: opResultTable[btcTxIdOpIndex].opResult
.toString('hex')
.match(/.{2}/g)
.reverse()
.join(''),
rawTx: rawTx.toString('hex')
}
}
function isHex(value) {
var hexRegex = /^[0-9A-Fa-f]{2,}$/
var result = hexRegex.test(value)
if (result) result = !(value.length % 2)
return result
}
module.exports = {
parse: parse
}