hypercore-storage 0.0.41 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1069 @@
1
+ // needed here for compat, copied from old hypercore, do not change this
2
+
3
+ const c = require('compact-encoding')
4
+ const b4a = require('b4a')
5
+
6
+ const EMPTY = b4a.alloc(0)
7
+ const DEFAULT_NAMESPACE = b4a.from('4144eea531e483d54e0c14f4ca68e0644f355343ff6fcb0f005200e12cd747cb', 'hex')
8
+
9
+ const hashes = {
10
+ preencode (state, m) {
11
+ state.end++ // small uint
12
+ },
13
+ encode (state, m) {
14
+ if (m === 'blake2b') {
15
+ c.uint.encode(state, 0)
16
+ return
17
+ }
18
+
19
+ throw new Error('Unknown hash: ' + m)
20
+ },
21
+ decode (state) {
22
+ const n = c.uint.decode(state)
23
+ if (n === 0) return 'blake2b'
24
+ throw new Error('Unknown hash id: ' + n)
25
+ }
26
+ }
27
+
28
+ const signatures = {
29
+ preencode (state, m) {
30
+ state.end++ // small uint
31
+ },
32
+ encode (state, m) {
33
+ if (m === 'ed25519') {
34
+ c.uint.encode(state, 0)
35
+ return
36
+ }
37
+
38
+ throw new Error('Unknown signature: ' + m)
39
+ },
40
+ decode (state) {
41
+ const n = c.uint.decode(state)
42
+ if (n === 0) return 'ed25519'
43
+ throw new Error('Unknown signature id: ' + n)
44
+ }
45
+ }
46
+
47
+ const signer = {
48
+ preencode (state, m) {
49
+ signatures.preencode(state, m.signature)
50
+ c.fixed32.preencode(state, m.namespace)
51
+ c.fixed32.preencode(state, m.publicKey)
52
+ },
53
+ encode (state, m) {
54
+ signatures.encode(state, m.signature)
55
+ c.fixed32.encode(state, m.namespace)
56
+ c.fixed32.encode(state, m.publicKey)
57
+ },
58
+ decode (state) {
59
+ return {
60
+ signature: signatures.decode(state),
61
+ namespace: c.fixed32.decode(state),
62
+ publicKey: c.fixed32.decode(state)
63
+ }
64
+ }
65
+ }
66
+
67
+ const signerArray = c.array(signer)
68
+
69
+ const prologue = {
70
+ preencode (state, p) {
71
+ c.fixed32.preencode(state, p.hash)
72
+ c.uint.preencode(state, p.length)
73
+ },
74
+ encode (state, p) {
75
+ c.fixed32.encode(state, p.hash)
76
+ c.uint.encode(state, p.length)
77
+ },
78
+ decode (state) {
79
+ return {
80
+ hash: c.fixed32.decode(state),
81
+ length: c.uint.decode(state)
82
+ }
83
+ }
84
+ }
85
+
86
+ const manifestv0 = {
87
+ preencode (state, m) {
88
+ hashes.preencode(state, m.hash)
89
+ state.end++ // type
90
+
91
+ if (m.prologue && m.signers.length === 0) {
92
+ c.fixed32.preencode(state, m.prologue.hash)
93
+ return
94
+ }
95
+
96
+ if (m.quorum === 1 && m.signers.length === 1 && !m.allowPatch) {
97
+ signer.preencode(state, m.signers[0])
98
+ } else {
99
+ state.end++ // flags
100
+ c.uint.preencode(state, m.quorum)
101
+ signerArray.preencode(state, m.signers)
102
+ }
103
+ },
104
+ encode (state, m) {
105
+ hashes.encode(state, m.hash)
106
+
107
+ if (m.prologue && m.signers.length === 0) {
108
+ c.uint.encode(state, 0)
109
+ c.fixed32.encode(state, m.prologue.hash)
110
+ return
111
+ }
112
+
113
+ if (m.quorum === 1 && m.signers.length === 1 && !m.allowPatch) {
114
+ c.uint.encode(state, 1)
115
+ signer.encode(state, m.signers[0])
116
+ } else {
117
+ c.uint.encode(state, 2)
118
+ c.uint.encode(state, m.allowPatch ? 1 : 0)
119
+ c.uint.encode(state, m.quorum)
120
+ signerArray.encode(state, m.signers)
121
+ }
122
+ },
123
+ decode (state) {
124
+ const hash = hashes.decode(state)
125
+ const type = c.uint.decode(state)
126
+
127
+ if (type > 2) throw new Error('Unknown type: ' + type)
128
+
129
+ if (type === 0) {
130
+ return {
131
+ version: 0,
132
+ hash,
133
+ allowPatch: false,
134
+ quorum: 0,
135
+ signers: [],
136
+ prologue: {
137
+ hash: c.fixed32.decode(state),
138
+ length: 0
139
+ }
140
+ }
141
+ }
142
+
143
+ if (type === 1) {
144
+ return {
145
+ version: 0,
146
+ hash,
147
+ allowPatch: false,
148
+ quorum: 1,
149
+ signers: [signer.decode(state)],
150
+ prologue: null
151
+ }
152
+ }
153
+
154
+ const flags = c.uint.decode(state)
155
+
156
+ return {
157
+ version: 0,
158
+ hash,
159
+ allowPatch: (flags & 1) !== 0,
160
+ quorum: c.uint.decode(state),
161
+ signers: signerArray.decode(state),
162
+ prologue: null
163
+ }
164
+ }
165
+ }
166
+
167
+ const manifest = exports.manifest = {
168
+ preencode (state, m) {
169
+ state.end++ // version
170
+ if (m.version === 0) return manifestv0.preencode(state, m)
171
+
172
+ state.end++ // flags
173
+ hashes.preencode(state, m.hash)
174
+
175
+ c.uint.preencode(state, m.quorum)
176
+ signerArray.preencode(state, m.signers)
177
+ if (m.prologue) prologue.preencode(state, m.prologue)
178
+ },
179
+ encode (state, m) {
180
+ c.uint.encode(state, m.version)
181
+ if (m.version === 0) return manifestv0.encode(state, m)
182
+
183
+ c.uint.encode(state, (m.allowPatch ? 1 : 0) | (m.prologue ? 2 : 0))
184
+ hashes.encode(state, m.hash)
185
+
186
+ c.uint.encode(state, m.quorum)
187
+ signerArray.encode(state, m.signers)
188
+ if (m.prologue) prologue.encode(state, m.prologue)
189
+ },
190
+ decode (state) {
191
+ const v = c.uint.decode(state)
192
+ if (v === 0) return manifestv0.decode(state)
193
+ if (v !== 1) throw new Error('Unknown version: ' + v)
194
+
195
+ const flags = c.uint.decode(state)
196
+ const hash = hashes.decode(state)
197
+ const quorum = c.uint.decode(state)
198
+ const signers = signerArray.decode(state)
199
+
200
+ return {
201
+ version: 1,
202
+ hash,
203
+ allowPatch: (flags & 1) !== 0,
204
+ quorum,
205
+ signers,
206
+ prologue: (flags & 2) === 0 ? null : prologue.decode(state)
207
+ }
208
+ }
209
+ }
210
+
211
+ const node = {
212
+ preencode (state, n) {
213
+ c.uint.preencode(state, n.index)
214
+ c.uint.preencode(state, n.size)
215
+ c.fixed32.preencode(state, n.hash)
216
+ },
217
+ encode (state, n) {
218
+ c.uint.encode(state, n.index)
219
+ c.uint.encode(state, n.size)
220
+ c.fixed32.encode(state, n.hash)
221
+ },
222
+ decode (state) {
223
+ return {
224
+ index: c.uint.decode(state),
225
+ size: c.uint.decode(state),
226
+ hash: c.fixed32.decode(state)
227
+ }
228
+ }
229
+ }
230
+
231
+ const nodeArray = c.array(node)
232
+
233
+ const wire = exports.wire = {}
234
+
235
+ wire.handshake = {
236
+ preencode (state, m) {
237
+ c.uint.preencode(state, 1)
238
+ c.fixed32.preencode(state, m.capability)
239
+ },
240
+ encode (state, m) {
241
+ c.uint.encode(state, m.seeks ? 1 : 0)
242
+ c.fixed32.encode(state, m.capability)
243
+ },
244
+ decode (state) {
245
+ const flags = c.uint.decode(state)
246
+ return {
247
+ seeks: (flags & 1) !== 0,
248
+ capability: c.fixed32.decode(state)
249
+ }
250
+ }
251
+ }
252
+
253
+ const requestBlock = {
254
+ preencode (state, b) {
255
+ c.uint.preencode(state, b.index)
256
+ c.uint.preencode(state, b.nodes)
257
+ },
258
+ encode (state, b) {
259
+ c.uint.encode(state, b.index)
260
+ c.uint.encode(state, b.nodes)
261
+ },
262
+ decode (state) {
263
+ return {
264
+ index: c.uint.decode(state),
265
+ nodes: c.uint.decode(state)
266
+ }
267
+ }
268
+ }
269
+
270
+ const requestSeek = {
271
+ preencode (state, s) {
272
+ c.uint.preencode(state, s.bytes)
273
+ c.uint.preencode(state, s.padding)
274
+ },
275
+ encode (state, s) {
276
+ c.uint.encode(state, s.bytes)
277
+ c.uint.encode(state, s.padding)
278
+ },
279
+ decode (state) {
280
+ return {
281
+ bytes: c.uint.decode(state),
282
+ padding: c.uint.decode(state)
283
+ }
284
+ }
285
+ }
286
+
287
+ const requestUpgrade = {
288
+ preencode (state, u) {
289
+ c.uint.preencode(state, u.start)
290
+ c.uint.preencode(state, u.length)
291
+ },
292
+ encode (state, u) {
293
+ c.uint.encode(state, u.start)
294
+ c.uint.encode(state, u.length)
295
+ },
296
+ decode (state) {
297
+ return {
298
+ start: c.uint.decode(state),
299
+ length: c.uint.decode(state)
300
+ }
301
+ }
302
+ }
303
+
304
+ wire.request = {
305
+ preencode (state, m) {
306
+ state.end++ // flags
307
+ c.uint.preencode(state, m.id)
308
+ c.uint.preencode(state, m.fork)
309
+
310
+ if (m.block) requestBlock.preencode(state, m.block)
311
+ if (m.hash) requestBlock.preencode(state, m.hash)
312
+ if (m.seek) requestSeek.preencode(state, m.seek)
313
+ if (m.upgrade) requestUpgrade.preencode(state, m.upgrade)
314
+ if (m.priority) c.uint.preencode(state, m.priority)
315
+ },
316
+ encode (state, m) {
317
+ const flags = (m.block ? 1 : 0) | (m.hash ? 2 : 0) | (m.seek ? 4 : 0) | (m.upgrade ? 8 : 0) | (m.manifest ? 16 : 0) | (m.priority ? 32 : 0)
318
+
319
+ c.uint.encode(state, flags)
320
+ c.uint.encode(state, m.id)
321
+ c.uint.encode(state, m.fork)
322
+
323
+ if (m.block) requestBlock.encode(state, m.block)
324
+ if (m.hash) requestBlock.encode(state, m.hash)
325
+ if (m.seek) requestSeek.encode(state, m.seek)
326
+ if (m.upgrade) requestUpgrade.encode(state, m.upgrade)
327
+ if (m.priority) c.uint.encode(state, m.priority)
328
+ },
329
+ decode (state) {
330
+ const flags = c.uint.decode(state)
331
+
332
+ return {
333
+ id: c.uint.decode(state),
334
+ fork: c.uint.decode(state),
335
+ block: flags & 1 ? requestBlock.decode(state) : null,
336
+ hash: flags & 2 ? requestBlock.decode(state) : null,
337
+ seek: flags & 4 ? requestSeek.decode(state) : null,
338
+ upgrade: flags & 8 ? requestUpgrade.decode(state) : null,
339
+ manifest: (flags & 16) !== 0,
340
+ priority: flags & 32 ? c.uint.decode(state) : 0
341
+ }
342
+ }
343
+ }
344
+
345
+ wire.cancel = {
346
+ preencode (state, m) {
347
+ c.uint.preencode(state, m.request)
348
+ },
349
+ encode (state, m) {
350
+ c.uint.encode(state, m.request)
351
+ },
352
+ decode (state, m) {
353
+ return {
354
+ request: c.uint.decode(state)
355
+ }
356
+ }
357
+ }
358
+
359
+ const dataUpgrade = {
360
+ preencode (state, u) {
361
+ c.uint.preencode(state, u.start)
362
+ c.uint.preencode(state, u.length)
363
+ nodeArray.preencode(state, u.nodes)
364
+ nodeArray.preencode(state, u.additionalNodes)
365
+ c.buffer.preencode(state, u.signature)
366
+ },
367
+ encode (state, u) {
368
+ c.uint.encode(state, u.start)
369
+ c.uint.encode(state, u.length)
370
+ nodeArray.encode(state, u.nodes)
371
+ nodeArray.encode(state, u.additionalNodes)
372
+ c.buffer.encode(state, u.signature)
373
+ },
374
+ decode (state) {
375
+ return {
376
+ start: c.uint.decode(state),
377
+ length: c.uint.decode(state),
378
+ nodes: nodeArray.decode(state),
379
+ additionalNodes: nodeArray.decode(state),
380
+ signature: c.buffer.decode(state)
381
+ }
382
+ }
383
+ }
384
+
385
+ const dataSeek = {
386
+ preencode (state, s) {
387
+ c.uint.preencode(state, s.bytes)
388
+ nodeArray.preencode(state, s.nodes)
389
+ },
390
+ encode (state, s) {
391
+ c.uint.encode(state, s.bytes)
392
+ nodeArray.encode(state, s.nodes)
393
+ },
394
+ decode (state) {
395
+ return {
396
+ bytes: c.uint.decode(state),
397
+ nodes: nodeArray.decode(state)
398
+ }
399
+ }
400
+ }
401
+
402
+ const dataBlock = {
403
+ preencode (state, b) {
404
+ c.uint.preencode(state, b.index)
405
+ c.buffer.preencode(state, b.value)
406
+ nodeArray.preencode(state, b.nodes)
407
+ },
408
+ encode (state, b) {
409
+ c.uint.encode(state, b.index)
410
+ c.buffer.encode(state, b.value)
411
+ nodeArray.encode(state, b.nodes)
412
+ },
413
+ decode (state) {
414
+ return {
415
+ index: c.uint.decode(state),
416
+ value: c.buffer.decode(state) || EMPTY,
417
+ nodes: nodeArray.decode(state)
418
+ }
419
+ }
420
+ }
421
+
422
+ const dataHash = {
423
+ preencode (state, b) {
424
+ c.uint.preencode(state, b.index)
425
+ nodeArray.preencode(state, b.nodes)
426
+ },
427
+ encode (state, b) {
428
+ c.uint.encode(state, b.index)
429
+ nodeArray.encode(state, b.nodes)
430
+ },
431
+ decode (state) {
432
+ return {
433
+ index: c.uint.decode(state),
434
+ nodes: nodeArray.decode(state)
435
+ }
436
+ }
437
+ }
438
+
439
+ wire.data = {
440
+ preencode (state, m) {
441
+ state.end++ // flags
442
+ c.uint.preencode(state, m.request)
443
+ c.uint.preencode(state, m.fork)
444
+
445
+ if (m.block) dataBlock.preencode(state, m.block)
446
+ if (m.hash) dataHash.preencode(state, m.hash)
447
+ if (m.seek) dataSeek.preencode(state, m.seek)
448
+ if (m.upgrade) dataUpgrade.preencode(state, m.upgrade)
449
+ if (m.manifest) manifest.preencode(state, m.manifest)
450
+ },
451
+ encode (state, m) {
452
+ const flags = (m.block ? 1 : 0) | (m.hash ? 2 : 0) | (m.seek ? 4 : 0) | (m.upgrade ? 8 : 0) | (m.manifest ? 16 : 0)
453
+
454
+ c.uint.encode(state, flags)
455
+ c.uint.encode(state, m.request)
456
+ c.uint.encode(state, m.fork)
457
+
458
+ if (m.block) dataBlock.encode(state, m.block)
459
+ if (m.hash) dataHash.encode(state, m.hash)
460
+ if (m.seek) dataSeek.encode(state, m.seek)
461
+ if (m.upgrade) dataUpgrade.encode(state, m.upgrade)
462
+ if (m.manifest) manifest.encode(state, m.manifest)
463
+ },
464
+ decode (state) {
465
+ const flags = c.uint.decode(state)
466
+
467
+ return {
468
+ request: c.uint.decode(state),
469
+ fork: c.uint.decode(state),
470
+ block: flags & 1 ? dataBlock.decode(state) : null,
471
+ hash: flags & 2 ? dataHash.decode(state) : null,
472
+ seek: flags & 4 ? dataSeek.decode(state) : null,
473
+ upgrade: flags & 8 ? dataUpgrade.decode(state) : null,
474
+ manifest: flags & 16 ? manifest.decode(state) : null
475
+ }
476
+ }
477
+ }
478
+
479
+ wire.noData = {
480
+ preencode (state, m) {
481
+ c.uint.preencode(state, m.request)
482
+ },
483
+ encode (state, m) {
484
+ c.uint.encode(state, m.request)
485
+ },
486
+ decode (state, m) {
487
+ return {
488
+ request: c.uint.decode(state)
489
+ }
490
+ }
491
+ }
492
+
493
+ wire.want = {
494
+ preencode (state, m) {
495
+ c.uint.preencode(state, m.start)
496
+ c.uint.preencode(state, m.length)
497
+ },
498
+ encode (state, m) {
499
+ c.uint.encode(state, m.start)
500
+ c.uint.encode(state, m.length)
501
+ },
502
+ decode (state) {
503
+ return {
504
+ start: c.uint.decode(state),
505
+ length: c.uint.decode(state)
506
+ }
507
+ }
508
+ }
509
+
510
+ wire.unwant = {
511
+ preencode (state, m) {
512
+ c.uint.preencode(state, m.start)
513
+ c.uint.preencode(state, m.length)
514
+ },
515
+ encode (state, m) {
516
+ c.uint.encode(state, m.start)
517
+ c.uint.encode(state, m.length)
518
+ },
519
+ decode (state, m) {
520
+ return {
521
+ start: c.uint.decode(state),
522
+ length: c.uint.decode(state)
523
+ }
524
+ }
525
+ }
526
+
527
+ wire.range = {
528
+ preencode (state, m) {
529
+ state.end++ // flags
530
+ c.uint.preencode(state, m.start)
531
+ if (m.length !== 1) c.uint.preencode(state, m.length)
532
+ },
533
+ encode (state, m) {
534
+ c.uint.encode(state, (m.drop ? 1 : 0) | (m.length === 1 ? 2 : 0))
535
+ c.uint.encode(state, m.start)
536
+ if (m.length !== 1) c.uint.encode(state, m.length)
537
+ },
538
+ decode (state) {
539
+ const flags = c.uint.decode(state)
540
+
541
+ return {
542
+ drop: (flags & 1) !== 0,
543
+ start: c.uint.decode(state),
544
+ length: (flags & 2) !== 0 ? 1 : c.uint.decode(state)
545
+ }
546
+ }
547
+ }
548
+
549
+ wire.bitfield = {
550
+ preencode (state, m) {
551
+ c.uint.preencode(state, m.start)
552
+ c.uint32array.preencode(state, m.bitfield)
553
+ },
554
+ encode (state, m) {
555
+ c.uint.encode(state, m.start)
556
+ c.uint32array.encode(state, m.bitfield)
557
+ },
558
+ decode (state, m) {
559
+ return {
560
+ start: c.uint.decode(state),
561
+ bitfield: c.uint32array.decode(state)
562
+ }
563
+ }
564
+ }
565
+
566
+ wire.sync = {
567
+ preencode (state, m) {
568
+ state.end++ // flags
569
+ c.uint.preencode(state, m.fork)
570
+ c.uint.preencode(state, m.length)
571
+ c.uint.preencode(state, m.remoteLength)
572
+ },
573
+ encode (state, m) {
574
+ c.uint.encode(state, (m.canUpgrade ? 1 : 0) | (m.uploading ? 2 : 0) | (m.downloading ? 4 : 0) | (m.hasManifest ? 8 : 0))
575
+ c.uint.encode(state, m.fork)
576
+ c.uint.encode(state, m.length)
577
+ c.uint.encode(state, m.remoteLength)
578
+ },
579
+ decode (state) {
580
+ const flags = c.uint.decode(state)
581
+
582
+ return {
583
+ fork: c.uint.decode(state),
584
+ length: c.uint.decode(state),
585
+ remoteLength: c.uint.decode(state),
586
+ canUpgrade: (flags & 1) !== 0,
587
+ uploading: (flags & 2) !== 0,
588
+ downloading: (flags & 4) !== 0,
589
+ hasManifest: (flags & 8) !== 0
590
+ }
591
+ }
592
+ }
593
+
594
+ wire.reorgHint = {
595
+ preencode (state, m) {
596
+ c.uint.preencode(state, m.from)
597
+ c.uint.preencode(state, m.to)
598
+ c.uint.preencode(state, m.ancestors)
599
+ },
600
+ encode (state, m) {
601
+ c.uint.encode(state, m.from)
602
+ c.uint.encode(state, m.to)
603
+ c.uint.encode(state, m.ancestors)
604
+ },
605
+ decode (state) {
606
+ return {
607
+ from: c.uint.encode(state),
608
+ to: c.uint.encode(state),
609
+ ancestors: c.uint.encode(state)
610
+ }
611
+ }
612
+ }
613
+
614
+ wire.extension = {
615
+ preencode (state, m) {
616
+ c.string.preencode(state, m.name)
617
+ c.raw.preencode(state, m.message)
618
+ },
619
+ encode (state, m) {
620
+ c.string.encode(state, m.name)
621
+ c.raw.encode(state, m.message)
622
+ },
623
+ decode (state) {
624
+ return {
625
+ name: c.string.decode(state),
626
+ message: c.raw.decode(state)
627
+ }
628
+ }
629
+ }
630
+
631
+ const keyValue = {
632
+ preencode (state, p) {
633
+ c.string.preencode(state, p.key)
634
+ c.buffer.preencode(state, p.value)
635
+ },
636
+ encode (state, p) {
637
+ c.string.encode(state, p.key)
638
+ c.buffer.encode(state, p.value)
639
+ },
640
+ decode (state) {
641
+ return {
642
+ key: c.string.decode(state),
643
+ value: c.buffer.decode(state)
644
+ }
645
+ }
646
+ }
647
+
648
+ const treeUpgrade = {
649
+ preencode (state, u) {
650
+ c.uint.preencode(state, u.fork)
651
+ c.uint.preencode(state, u.ancestors)
652
+ c.uint.preencode(state, u.length)
653
+ c.buffer.preencode(state, u.signature)
654
+ },
655
+ encode (state, u) {
656
+ c.uint.encode(state, u.fork)
657
+ c.uint.encode(state, u.ancestors)
658
+ c.uint.encode(state, u.length)
659
+ c.buffer.encode(state, u.signature)
660
+ },
661
+ decode (state) {
662
+ return {
663
+ fork: c.uint.decode(state),
664
+ ancestors: c.uint.decode(state),
665
+ length: c.uint.decode(state),
666
+ signature: c.buffer.decode(state)
667
+ }
668
+ }
669
+ }
670
+
671
+ const bitfieldUpdate = { // TODO: can maybe be folded into a HAVE later on with the most recent spec
672
+ preencode (state, b) {
673
+ state.end++ // flags
674
+ c.uint.preencode(state, b.start)
675
+ c.uint.preencode(state, b.length)
676
+ },
677
+ encode (state, b) {
678
+ state.buffer[state.start++] = b.drop ? 1 : 0
679
+ c.uint.encode(state, b.start)
680
+ c.uint.encode(state, b.length)
681
+ },
682
+ decode (state) {
683
+ const flags = c.uint.decode(state)
684
+ return {
685
+ drop: (flags & 1) !== 0,
686
+ start: c.uint.decode(state),
687
+ length: c.uint.decode(state)
688
+ }
689
+ }
690
+ }
691
+
692
+ const oplog = exports.oplog = {}
693
+
694
+ oplog.entry = {
695
+ preencode (state, m) {
696
+ state.end++ // flags
697
+ if (m.userData) keyValue.preencode(state, m.userData)
698
+ if (m.treeNodes) nodeArray.preencode(state, m.treeNodes)
699
+ if (m.treeUpgrade) treeUpgrade.preencode(state, m.treeUpgrade)
700
+ if (m.bitfield) bitfieldUpdate.preencode(state, m.bitfield)
701
+ },
702
+ encode (state, m) {
703
+ const s = state.start++
704
+ let flags = 0
705
+
706
+ if (m.userData) {
707
+ flags |= 1
708
+ keyValue.encode(state, m.userData)
709
+ }
710
+ if (m.treeNodes) {
711
+ flags |= 2
712
+ nodeArray.encode(state, m.treeNodes)
713
+ }
714
+ if (m.treeUpgrade) {
715
+ flags |= 4
716
+ treeUpgrade.encode(state, m.treeUpgrade)
717
+ }
718
+ if (m.bitfield) {
719
+ flags |= 8
720
+ bitfieldUpdate.encode(state, m.bitfield)
721
+ }
722
+
723
+ state.buffer[s] = flags
724
+ },
725
+ decode (state) {
726
+ const flags = c.uint.decode(state)
727
+ return {
728
+ userData: (flags & 1) !== 0 ? keyValue.decode(state) : null,
729
+ treeNodes: (flags & 2) !== 0 ? nodeArray.decode(state) : null,
730
+ treeUpgrade: (flags & 4) !== 0 ? treeUpgrade.decode(state) : null,
731
+ bitfield: (flags & 8) !== 0 ? bitfieldUpdate.decode(state) : null
732
+ }
733
+ }
734
+ }
735
+
736
+ const keyPair = {
737
+ preencode (state, kp) {
738
+ c.buffer.preencode(state, kp.publicKey)
739
+ c.buffer.preencode(state, kp.secretKey)
740
+ },
741
+ encode (state, kp) {
742
+ c.buffer.encode(state, kp.publicKey)
743
+ c.buffer.encode(state, kp.secretKey)
744
+ },
745
+ decode (state) {
746
+ return {
747
+ publicKey: c.buffer.decode(state),
748
+ secretKey: c.buffer.decode(state)
749
+ }
750
+ }
751
+ }
752
+
753
+ const reorgHint = {
754
+ preencode (state, r) {
755
+ c.uint.preencode(state, r.from)
756
+ c.uint.preencode(state, r.to)
757
+ c.uint.preencode(state, r.ancestors)
758
+ },
759
+ encode (state, r) {
760
+ c.uint.encode(state, r.from)
761
+ c.uint.encode(state, r.to)
762
+ c.uint.encode(state, r.ancestors)
763
+ },
764
+ decode (state) {
765
+ return {
766
+ from: c.uint.decode(state),
767
+ to: c.uint.decode(state),
768
+ ancestors: c.uint.decode(state)
769
+ }
770
+ }
771
+ }
772
+
773
+ const reorgHintArray = c.array(reorgHint)
774
+
775
+ const hints = {
776
+ preencode (state, h) {
777
+ reorgHintArray.preencode(state, h.reorgs)
778
+ c.uint.preencode(state, h.contiguousLength)
779
+ },
780
+ encode (state, h) {
781
+ reorgHintArray.encode(state, h.reorgs)
782
+ c.uint.encode(state, h.contiguousLength)
783
+ },
784
+ decode (state) {
785
+ return {
786
+ reorgs: reorgHintArray.decode(state),
787
+ contiguousLength: state.start < state.end ? c.uint.decode(state) : 0
788
+ }
789
+ }
790
+ }
791
+
792
+ const treeHeader = {
793
+ preencode (state, t) {
794
+ c.uint.preencode(state, t.fork)
795
+ c.uint.preencode(state, t.length)
796
+ c.buffer.preencode(state, t.rootHash)
797
+ c.buffer.preencode(state, t.signature)
798
+ },
799
+ encode (state, t) {
800
+ c.uint.encode(state, t.fork)
801
+ c.uint.encode(state, t.length)
802
+ c.buffer.encode(state, t.rootHash)
803
+ c.buffer.encode(state, t.signature)
804
+ },
805
+ decode (state) {
806
+ return {
807
+ fork: c.uint.decode(state),
808
+ length: c.uint.decode(state),
809
+ rootHash: c.buffer.decode(state),
810
+ signature: c.buffer.decode(state)
811
+ }
812
+ }
813
+ }
814
+
815
+ const types = {
816
+ preencode (state, t) {
817
+ c.string.preencode(state, t.tree)
818
+ c.string.preencode(state, t.bitfield)
819
+ c.string.preencode(state, t.signer)
820
+ },
821
+ encode (state, t) {
822
+ c.string.encode(state, t.tree)
823
+ c.string.encode(state, t.bitfield)
824
+ c.string.encode(state, t.signer)
825
+ },
826
+ decode (state) {
827
+ return {
828
+ tree: c.string.decode(state),
829
+ bitfield: c.string.decode(state),
830
+ signer: c.string.decode(state)
831
+ }
832
+ }
833
+ }
834
+
835
+ const externalHeader = {
836
+ preencode (state, m) {
837
+ c.uint.preencode(state, m.start)
838
+ c.uint.preencode(state, m.length)
839
+ },
840
+ encode (state, m) {
841
+ c.uint.encode(state, m.start)
842
+ c.uint.encode(state, m.length)
843
+ },
844
+ decode (state) {
845
+ return {
846
+ start: c.uint.decode(state),
847
+ length: c.uint.decode(state)
848
+ }
849
+ }
850
+ }
851
+
852
+ const keyValueArray = c.array(keyValue)
853
+
854
+ oplog.header = {
855
+ preencode (state, h) {
856
+ state.end += 2 // version + flags
857
+ if (h.external) {
858
+ externalHeader.preencode(state, h.external)
859
+ return
860
+ }
861
+ c.fixed32.preencode(state, h.key)
862
+ if (h.manifest) manifest.preencode(state, h.manifest)
863
+ if (h.keyPair) keyPair.preencode(state, h.keyPair)
864
+ keyValueArray.preencode(state, h.userData)
865
+ treeHeader.preencode(state, h.tree)
866
+ hints.preencode(state, h.hints)
867
+ },
868
+ encode (state, h) {
869
+ c.uint.encode(state, 1)
870
+ if (h.external) {
871
+ c.uint.encode(state, 1) // ONLY set the first big for clarity
872
+ externalHeader.encode(state, h.external)
873
+ return
874
+ }
875
+ c.uint.encode(state, (h.manifest ? 2 : 0) | (h.keyPair ? 4 : 0))
876
+ c.fixed32.encode(state, h.key)
877
+ if (h.manifest) manifest.encode(state, h.manifest)
878
+ if (h.keyPair) keyPair.encode(state, h.keyPair)
879
+ keyValueArray.encode(state, h.userData)
880
+ treeHeader.encode(state, h.tree)
881
+ hints.encode(state, h.hints)
882
+ },
883
+ decode (state) {
884
+ const version = c.uint.decode(state)
885
+
886
+ if (version > 1) {
887
+ throw new Error('Invalid header version. Expected <= 1, got ' + version)
888
+ }
889
+
890
+ if (version === 0) {
891
+ const old = {
892
+ types: types.decode(state),
893
+ userData: keyValueArray.decode(state),
894
+ tree: treeHeader.decode(state),
895
+ signer: keyPair.decode(state),
896
+ hints: hints.decode(state)
897
+ }
898
+
899
+ return {
900
+ external: null,
901
+ key: old.signer.publicKey,
902
+ manifest: {
903
+ version: 0,
904
+ hash: old.types.tree,
905
+ allowPatch: false,
906
+ quorum: 1,
907
+ signers: [{
908
+ signature: old.types.signer,
909
+ namespace: DEFAULT_NAMESPACE,
910
+ publicKey: old.signer.publicKey
911
+ }],
912
+ prologue: null
913
+ },
914
+ keyPair: old.signer.secretKey ? old.signer : null,
915
+ userData: old.userData,
916
+ tree: old.tree,
917
+ hints: old.hints
918
+ }
919
+ }
920
+
921
+ const flags = c.uint.decode(state)
922
+
923
+ if (flags & 1) {
924
+ return {
925
+ external: externalHeader.decode(state),
926
+ key: null,
927
+ manifest: null,
928
+ keyPair: null,
929
+ userData: null,
930
+ tree: null,
931
+ hints: null
932
+ }
933
+ }
934
+
935
+ return {
936
+ external: null,
937
+ key: c.fixed32.decode(state),
938
+ manifest: (flags & 2) !== 0 ? manifest.decode(state) : null,
939
+ keyPair: (flags & 4) !== 0 ? keyPair.decode(state) : null,
940
+ userData: keyValueArray.decode(state),
941
+ tree: treeHeader.decode(state),
942
+ hints: hints.decode(state)
943
+ }
944
+ }
945
+ }
946
+
947
+ const uintArray = c.array(c.uint)
948
+
949
+ const multisigInput = {
950
+ preencode (state, inp) {
951
+ c.uint.preencode(state, inp.signer)
952
+ c.fixed64.preencode(state, inp.signature)
953
+ c.uint.preencode(state, inp.patch)
954
+ },
955
+ encode (state, inp) {
956
+ c.uint.encode(state, inp.signer)
957
+ c.fixed64.encode(state, inp.signature)
958
+ c.uint.encode(state, inp.patch)
959
+ },
960
+ decode (state) {
961
+ return {
962
+ signer: c.uint.decode(state),
963
+ signature: c.fixed64.decode(state),
964
+ patch: c.uint.decode(state)
965
+ }
966
+ }
967
+ }
968
+
969
+ const patchEncodingv0 = {
970
+ preencode (state, n) {
971
+ c.uint.preencode(state, n.start)
972
+ c.uint.preencode(state, n.length)
973
+ uintArray.preencode(state, n.nodes)
974
+ },
975
+ encode (state, n) {
976
+ c.uint.encode(state, n.start)
977
+ c.uint.encode(state, n.length)
978
+ uintArray.encode(state, n.nodes)
979
+ },
980
+ decode (state) {
981
+ return {
982
+ start: c.uint.decode(state),
983
+ length: c.uint.decode(state),
984
+ nodes: uintArray.decode(state)
985
+ }
986
+ }
987
+ }
988
+
989
+ const multisigInputv0 = {
990
+ preencode (state, n) {
991
+ state.end++
992
+ c.uint.preencode(state, n.signer)
993
+ c.fixed64.preencode(state, n.signature)
994
+ if (n.patch) patchEncodingv0.preencode(state, n.patch)
995
+ },
996
+ encode (state, n) {
997
+ c.uint.encode(state, n.patch ? 1 : 0)
998
+ c.uint.encode(state, n.signer)
999
+ c.fixed64.encode(state, n.signature)
1000
+ if (n.patch) patchEncodingv0.encode(state, n.patch)
1001
+ },
1002
+ decode (state) {
1003
+ const flags = c.uint.decode(state)
1004
+ return {
1005
+ signer: c.uint.decode(state),
1006
+ signature: c.fixed64.decode(state),
1007
+ patch: (flags & 1) ? patchEncodingv0.decode(state) : null
1008
+ }
1009
+ }
1010
+ }
1011
+
1012
+ const multisigInputArrayv0 = c.array(multisigInputv0)
1013
+ const multisigInputArray = c.array(multisigInput)
1014
+
1015
+ const compactNode = {
1016
+ preencode (state, n) {
1017
+ c.uint.preencode(state, n.index)
1018
+ c.uint.preencode(state, n.size)
1019
+ c.fixed32.preencode(state, n.hash)
1020
+ },
1021
+ encode (state, n) {
1022
+ c.uint.encode(state, n.index)
1023
+ c.uint.encode(state, n.size)
1024
+ c.fixed32.encode(state, n.hash)
1025
+ },
1026
+ decode (state) {
1027
+ return {
1028
+ index: c.uint.decode(state),
1029
+ size: c.uint.decode(state),
1030
+ hash: c.fixed32.decode(state)
1031
+ }
1032
+ }
1033
+ }
1034
+
1035
+ const compactNodeArray = c.array(compactNode)
1036
+
1037
+ exports.multiSignaturev0 = {
1038
+ preencode (state, s) {
1039
+ multisigInputArrayv0.preencode(state, s.proofs)
1040
+ compactNodeArray.preencode(state, s.patch)
1041
+ },
1042
+ encode (state, s) {
1043
+ multisigInputArrayv0.encode(state, s.proofs)
1044
+ compactNodeArray.encode(state, s.patch)
1045
+ },
1046
+ decode (state) {
1047
+ return {
1048
+ proofs: multisigInputArrayv0.decode(state),
1049
+ patch: compactNodeArray.decode(state)
1050
+ }
1051
+ }
1052
+ }
1053
+
1054
+ exports.multiSignature = {
1055
+ preencode (state, s) {
1056
+ multisigInputArray.preencode(state, s.proofs)
1057
+ compactNodeArray.preencode(state, s.patch)
1058
+ },
1059
+ encode (state, s) {
1060
+ multisigInputArray.encode(state, s.proofs)
1061
+ compactNodeArray.encode(state, s.patch)
1062
+ },
1063
+ decode (state) {
1064
+ return {
1065
+ proofs: multisigInputArray.decode(state),
1066
+ patch: compactNodeArray.decode(state)
1067
+ }
1068
+ }
1069
+ }