|
1 | 1 | package opentimestamps |
2 | 2 |
|
3 | | -import ( |
4 | | - "encoding/binary" |
5 | | - "errors" |
6 | | - "io" |
| 3 | +// https://opentimestamps.org/ |
7 | 4 |
|
| 5 | +import ( |
8 | 6 | "github.com/wader/fq/format" |
9 | 7 | "github.com/wader/fq/pkg/decode" |
10 | 8 | "github.com/wader/fq/pkg/interp" |
11 | 9 | "github.com/wader/fq/pkg/scalar" |
| 10 | + "golang.org/x/exp/slices" |
12 | 11 | ) |
13 | 12 |
|
14 | 13 | func init() { |
15 | 14 | interp.RegisterFormat( |
16 | | - format.OpenTimestamps, |
| 15 | + format.Opentimestamps, |
17 | 16 | &decode.Format{ |
18 | | - Description: "OpenTimestamps file", |
19 | | - Dependencies: nil, |
20 | | - DecodeFn: decodeOTSFile, |
| 17 | + Description: "OpenTimestamps file", |
| 18 | + DecodeFn: decodeOTSFile, |
| 19 | + Groups: []*decode.Group{format.Probe}, |
21 | 20 | }) |
22 | 21 | } |
23 | 22 |
|
24 | | -func decodeVarInt(d *decode.D) uint64 { |
25 | | - var value uint64 = 0 |
26 | | - var shift uint64 = 0 |
27 | | - |
28 | | - for { |
29 | | - b := d.U8() |
30 | | - value |= (b & 0b01111111) << shift |
31 | | - shift += 7 |
32 | | - if b&0b10000000 == 0 { |
33 | | - break |
34 | | - } |
35 | | - } |
36 | | - |
37 | | - return value |
38 | | -} |
| 23 | +const ( |
| 24 | + continuationByte = 0xff |
| 25 | + attestationTag = 0x00 |
| 26 | + appendTag = 0xf0 |
| 27 | + prependTag = 0xf1 |
| 28 | + reverseTag = 0xf2 |
| 29 | + hexlifyTag = 0xf3 |
| 30 | + sha1Tag = 0x02 |
| 31 | + ripemd160Tag = 0x03 |
| 32 | + sha256Tag = 0x08 |
| 33 | + keccak256Tag = 0x67 |
| 34 | +) |
39 | 35 |
|
40 | 36 | var ( |
41 | | - headerMagic = []byte{0x00, 0x4f, 0x70, 0x65, 0x6e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x73, 0x00, 0x00, 0x50, 0x72, 0x6f, 0x6f, 0x66, 0x00, 0xbf, 0x89, 0xe2, 0xe8, 0x84, 0xe8, 0x92, 0x94} |
42 | | - pendingMagic = binary.BigEndian.Uint64([]byte{0x83, 0xdf, 0xe3, 0x0d, 0x2e, 0xf9, 0x0c, 0x8e}) |
43 | | - bitcoinMagic = binary.BigEndian.Uint64([]byte{0x05, 0x88, 0x96, 0x0d, 0x73, 0xd7, 0x19, 0x01}) |
| 37 | + headerMagic = []byte{0x00, 0x4f, 0x70, 0x65, 0x6e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x73, 0x00, 0x00, 0x50, 0x72, 0x6f, 0x6f, 0x66, 0x00, 0xbf, 0x89, 0xe2, 0xe8, 0x84, 0xe8, 0x92, 0x94} |
| 38 | + calendarMagic uint64 = 0x83_df_e3_0d_2e_f9_0c_8e |
| 39 | + bitcoinMagic uint64 = 0x05_88_96_0d_73_d7_19_01 |
| 40 | + |
| 41 | + binaryTags = []byte{appendTag, prependTag} |
44 | 42 | ) |
45 | 43 |
|
46 | 44 | var attestationMapper = scalar.UintMapSymStr{ |
47 | | - pendingMagic: "calendar", |
48 | | - bitcoinMagic: "bitcoin", |
| 45 | + calendarMagic: "calendar", |
| 46 | + bitcoinMagic: "bitcoin", |
49 | 47 | } |
50 | 48 |
|
51 | | -var opMapper = scalar.UintMapSymStr{ |
52 | | - 0xf0: "append", |
53 | | - 0xf1: "prepend", |
54 | | - 0xf2: "reverse", |
55 | | - 0xf3: "hexlify", |
56 | | - 0x02: "sha1", |
57 | | - 0x03: "ripemd160", |
58 | | - 0x08: "sha256", |
59 | | - 0x67: "keccak256", |
60 | | - 0x00: "attestation", |
61 | | - 0xff: "continuation_byte", |
| 49 | +var tagMapper = scalar.UintMapSymStr{ |
| 50 | + continuationByte: "continuation_byte", |
| 51 | + attestationTag: "attestation", |
| 52 | + appendTag: "append", |
| 53 | + prependTag: "prepend", |
| 54 | + reverseTag: "reverse", |
| 55 | + hexlifyTag: "hexlify", |
| 56 | + sha1Tag: "sha1", |
| 57 | + ripemd160Tag: "ripemd160", |
| 58 | + sha256Tag: "sha256", |
| 59 | + keccak256Tag: "keccak256", |
62 | 60 | } |
63 | 61 |
|
64 | 62 | func decodeOTSFile(d *decode.D) any { |
65 | 63 | d.Endian = decode.BigEndian |
66 | 64 |
|
67 | 65 | d.FieldRawLen("magic_bytes", int64(8*len(headerMagic)), d.AssertBitBuf(headerMagic)) |
68 | 66 | d.FieldUintFn("version", decodeVarInt) |
69 | | - tag := d.FieldU8("digest_hash_algorithm", opMapper, |
70 | | - scalar.UintDescription("algorithm used to hash the source file")) |
| 67 | + tag := d.FieldU8("digest_hash_algorithm", tagMapper) |
71 | 68 | if tag != 8 { |
72 | | - name := opMapper[tag] |
73 | | - d.Errorf("only sha256 supported, got %x: %s", tag, name) |
| 69 | + name := tagMapper[tag] |
| 70 | + d.Fatalf("only sha256 supported, got %x: %s", tag, name) |
74 | 71 | return nil |
75 | 72 | } |
76 | | - d.FieldRawLen("digest", 8*32, scalar.RawHex, |
77 | | - scalar.BitBufDescription("hash of the source file")) |
| 73 | + d.FieldRawLen("digest", 8*32, scalar.RawHex) |
78 | 74 |
|
79 | | - d.FieldArray("instructions", func(d *decode.D) { |
80 | | - for { |
81 | | - if b, err := d.TryPeekBytes(1); errors.Is(err, io.EOF) { |
82 | | - break |
83 | | - } else if b[0] == 0x00 { |
84 | | - d.FieldStruct("attestation", func(d *decode.D) { |
85 | | - d.FieldU8("attestation_tag", scalar.UintMapSymBool{0x00: true}) |
| 75 | + d.FieldArray("operations", func(d *decode.D) { |
| 76 | + for d.NotEnd() { |
| 77 | + d.FieldStruct("operation", func(d *decode.D) { |
| 78 | + tag := d.FieldU8("tag", tagMapper) |
| 79 | + if tag == attestationTag { |
86 | 80 | val := d.FieldU64BE("attestation_type", attestationMapper) |
87 | 81 | d.FieldUintFn("attestation_varbytes_size", decodeVarInt) |
88 | 82 | switch val { |
89 | 83 | case bitcoinMagic: |
90 | | - d.FieldUintFn("block", decodeVarInt, |
91 | | - scalar.UintDescription("bitcoin block height to check for the merkle root")) |
92 | | - case pendingMagic: |
| 84 | + d.FieldUintFn("block", decodeVarInt) |
| 85 | + case calendarMagic: |
93 | 86 | nurl := d.FieldUintFn("url_size", decodeVarInt) |
94 | | - d.FieldUTF8("url", int(nurl), |
95 | | - scalar.StrDescription("url of the calendar server to get the final proof")) |
| 87 | + d.FieldUTF8("url", int(nurl)) |
96 | 88 | default: |
97 | | - d.Errorf("unknown attestation tag %x", val) |
| 89 | + d.Fatalf("unknown attestation tag %x", val) |
98 | 90 | } |
99 | | - }) |
100 | | - } else if b[0] == 0xff { |
101 | | - d.FieldStruct("continuation_byte", func(d *decode.D) { |
102 | | - d.FieldU8("continuation_byte", scalar.UintMapSymBool{0xff: true}, |
103 | | - scalar.UintDescription("tells we should continue reading after the next attestation block")) |
104 | | - }) |
105 | | - } else { |
106 | | - d.FieldStruct("instruction", func(d *decode.D) { |
107 | | - tag := d.FieldU8("op", opMapper) |
108 | | - if name, ok := opMapper[tag]; ok { |
| 91 | + } else { |
| 92 | + if _, ok := tagMapper[tag]; ok { |
109 | 93 | // read var bytes if argument |
110 | | - if name == "append" || name == "prepend" { |
| 94 | + if slices.Contains(binaryTags, byte(tag)) { |
111 | 95 | n := d.FieldUintFn("argument_size", decodeVarInt) |
112 | 96 | d.FieldRawLen("argument", int64(8*n), scalar.RawHex) |
113 | 97 | } |
114 | 98 | } else { |
115 | | - d.Errorf("unknown operation tag %x", tag) |
| 99 | + d.Fatalf("unknown operation tag %x", tag) |
116 | 100 | } |
117 | | - }) |
118 | | - } |
| 101 | + } |
| 102 | + }) |
119 | 103 | } |
120 | 104 | }) |
121 | 105 |
|
122 | 106 | return nil |
123 | 107 | } |
| 108 | + |
| 109 | +func decodeVarInt(d *decode.D) uint64 { |
| 110 | + var value uint64 = 0 |
| 111 | + var shift uint64 = 0 |
| 112 | + |
| 113 | + for { |
| 114 | + b := d.U8() |
| 115 | + value |= (b & 0b01111111) << shift |
| 116 | + shift += 7 |
| 117 | + if b&0b10000000 == 0 { |
| 118 | + break |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return value |
| 123 | +} |
0 commit comments