-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
288 lines (275 loc) · 6.94 KB
/
parse.go
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package chainpoint
import (
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"strings"
"golang.org/x/crypto/sha3"
)
type (
// ParsedChainpoint ...
ParsedChainpoint struct {
Hash string
ProofID string
HashReceived Time
Branches []ParsedBranch
}
// ParsedBranch ...
ParsedBranch struct {
Label string
Branches []ParsedBranch
Anchors []ParsedAnchor
OpReturnValue string
BTCTxID string
}
// ParsedAnchor ...
ParsedAnchor struct {
Type string
AnchorID string
URIs []string
ExpectedValue string
}
)
// Parse ...
func Parse(chp *Chainpoint) (*ParsedChainpoint, error) {
branches, err := parseBranches(chp.Hash, chp.Branches)
if err != nil {
return nil, err
}
pchp := &ParsedChainpoint{
Hash: chp.Hash,
ProofID: chp.ProofID,
HashReceived: chp.HashReceived,
Branches: branches,
}
return pchp, nil
}
// parseBranches acquires all anchor points and calcaulte expected values for
// all branches, recursively.
func parseBranches(hash string, branches []Branch) ([]ParsedBranch, error) {
curHash, err := hex.DecodeString(hash)
if err != nil {
return nil, err
}
parsedBranched := []ParsedBranch{}
for _, b := range branches {
anchors := []ParsedAnchor{}
for _, op := range b.Ops {
if op.R != "" {
// hex data gets treated as hex, otherwise it is converted to bytes assuming a ut8 encoded string
var val []byte
if isHex(op.R) {
val, _ = hex.DecodeString(op.R)
} else {
val = []byte(op.R)
}
curHash = concat(curHash, val)
} else if op.L != "" {
// hex data gets treated as hex, otherwise it is converted to bytes assuming a ut8 encoded string
var val []byte
if isHex(op.L) {
val, _ = hex.DecodeString(op.L)
} else {
val = []byte(op.L)
}
curHash = concat(val, curHash)
} else if op.Op != OpUnknown {
switch op.Op {
case OpSHA224:
h := sha256.Sum224(curHash)
curHash = h[:]
break
case OpSHA256:
h := sha256.Sum256(curHash)
curHash = h[:]
break
case OpSHA384:
h := sha512.Sum384(curHash)
curHash = h[:]
break
case OpSHA512:
h := sha512.Sum512(curHash)
curHash = h[:]
break
case OpSHA3224:
h := sha3.Sum224(curHash)
curHash = h[:]
break
case OpSHA3256:
h := sha3.Sum256(curHash)
curHash = h[:]
break
case OpSHA3384:
h := sha3.Sum384(curHash)
curHash = h[:]
break
case OpSHA3512:
h := sha3.Sum512(curHash)
curHash = h[:]
break
case OpSHA256x2:
h := sha256.Sum256(curHash)
h = sha256.Sum256(h[:])
curHash = h[:]
break
default:
return nil, fmt.Errorf("unknown op: %s", op.Op)
}
} else if len(op.Anchors) > 0 {
anchors = append(anchors, parseAnchors(hex.EncodeToString(curHash), op.Anchors)...)
}
}
branch := ParsedBranch{
Label: b.Label,
Anchors: anchors,
}
if len(b.Branches) > 0 {
branch.Branches, _ = parseBranches(hex.EncodeToString(curHash), b.Branches)
}
// if this branch is a standard Chaipoint BTC anchor branch,
// output the OP_RETURN value and the BTC transaction id
if b.Label == "btc_anchor_branch" {
opReturnValue, btcTxID, err := btcAnchorInfo(hash, b.Ops)
if err != nil {
return nil, err
}
branch.OpReturnValue = opReturnValue
branch.BTCTxID = btcTxID
}
parsedBranched = append(parsedBranched, branch)
}
return parsedBranched, nil
}
// parseAnchors ...
func parseAnchors(curHash string, anchorsArray []Anchor) []ParsedAnchor {
anchors := []ParsedAnchor{}
for _, a := range anchorsArray {
expectedValue := curHash
// 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
switch a.Type {
case AnchorBTC:
r := strings.Builder{}
l := len(expectedValue)
for i := 0; i < l; i += 2 {
r.WriteByte(expectedValue[l-i-2])
r.WriteByte(expectedValue[l-i-1])
}
expectedValue = r.String()
case AnchorTBTC:
}
anchors = append(anchors, ParsedAnchor{
Type: a.Type,
AnchorID: a.AnchorID,
URIs: a.URIs,
ExpectedValue: expectedValue,
})
}
return anchors
}
func btcAnchorInfo(startHash string, ops []Operation) (string, string, error) {
// This calculation depends on the branch using the standard format
// for btc_anchor_branch type branches created by Chainpoint services
curHash, err := hex.DecodeString(startHash)
if err != nil {
return "", "", err
}
type opResult struct {
OpResult []byte
Op Operation
}
opRes := []opResult{}
btcTxIDOpIndex := -1
for _, op := range ops {
if op.R != "" {
// hex data gets treated as hex, otherwise it is converted to bytes assuming a ut8 encoded string
var val []byte
if isHex(op.R) {
val, _ = hex.DecodeString(op.R)
} else {
val = []byte(op.R)
}
curHash = concat(val, curHash)
opRes = append(opRes, opResult{OpResult: curHash, Op: op})
} else if op.L != "" {
// hex data gets treated as hex, otherwise it is converted to bytes assuming a ut8 encoded string
var val []byte
if isHex(op.L) {
val, _ = hex.DecodeString(op.L)
} else {
val = []byte(op.L)
}
curHash = concat(curHash, val)
opRes = append(opRes, opResult{OpResult: curHash, Op: op})
} else if op.Op != OpUnknown {
switch op.Op {
case OpSHA224:
h := sha256.Sum224(curHash)
curHash = h[:]
break
case OpSHA256:
h := sha256.Sum256(curHash)
curHash = h[:]
break
case OpSHA384:
h := sha512.Sum384(curHash)
curHash = h[:]
break
case OpSHA512:
h := sha512.Sum512(curHash)
curHash = h[:]
break
case OpSHA3224:
h := sha3.Sum224(curHash)
curHash = h[:]
break
case OpSHA3256:
h := sha3.Sum256(curHash)
curHash = h[:]
break
case OpSHA3384:
h := sha3.Sum384(curHash)
curHash = h[:]
break
case OpSHA3512:
h := sha3.Sum512(curHash)
curHash = h[:]
break
case OpSHA256x2:
h := sha256.Sum256(curHash)
h = sha256.Sum256(h[:])
curHash = h[:]
if btcTxIDOpIndex == -1 {
btcTxIDOpIndex = len(opRes)
}
break
default:
return "", "", fmt.Errorf("unknown op: %s", op.Op)
}
opRes = append(opRes, opResult{OpResult: curHash, Op: op})
}
}
opReturnOpIndex := btcTxIDOpIndex - 3
opReturnValue := hex.EncodeToString(opRes[opReturnOpIndex].OpResult)
btcTxID := reverseHex(hex.EncodeToString(opRes[btcTxIDOpIndex].OpResult))
return opReturnValue, btcTxID, nil
}
func reverseHex(s string) string {
r := strings.Builder{}
l := len(s)
for i := 0; i < l; i += 2 {
r.WriteByte(s[l-i-2])
r.WriteByte(s[l-i-1])
}
return r.String()
}
func concat(a, b []byte) []byte {
r := make([]byte, len(a)+len(b))
bp := copy(r, a)
copy(r[bp:], b)
return r
}