Skip to content

Commit

Permalink
opentimestamps: abstract away file digest sizes and support sha1, rip…
Browse files Browse the repository at this point in the history
…emd160 and keccac256.
  • Loading branch information
fiatjaf committed Sep 26, 2023
1 parent 5e7c01a commit cc8e6f1
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 5 deletions.
15 changes: 12 additions & 3 deletions format/opentimestamps/ots.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,27 @@ var tagMapper = scalar.UintMapSymStr{
keccak256Tag: "keccak256",
}

var digestSizes = map[byte]int64{
sha1Tag: 20,
ripemd160Tag: 20,
sha256Tag: 32,
keccak256Tag: 32,
}

func decodeOTSFile(d *decode.D) any {
d.Endian = decode.BigEndian

d.FieldRawLen("magic_bytes", int64(8*len(headerMagic)), d.AssertBitBuf(headerMagic))
d.FieldUintFn("version", decodeVarInt)

tag := d.FieldU8("digest_hash_algorithm", tagMapper)
if tag != 8 {
digestSize, ok := digestSizes[byte(tag)]
if !ok {
name := tagMapper[tag]
d.Fatalf("only sha256 supported, got %x: %s", tag, name)
d.Fatalf("hash algorithm not supported, got %x: '%s'", tag, name)
return nil
}
d.FieldRawLen("digest", 8*32, scalar.RawHex)
d.FieldRawLen("digest", 8*digestSize, scalar.RawHex)

d.FieldArray("operations", func(d *decode.D) {
for d.NotEnd() {
Expand Down
116 changes: 116 additions & 0 deletions format/opentimestamps/testdata/sha1.fqtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
$ fq dv sha1.ots
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.{}: sha1.ots (opentimestamps) 0x0-0x1bc.7 (445)
0x000|00 4f 70 65 6e 54 69 6d 65 73 74 61 6d 70 73 00|.OpenTimestamps.| magic_bytes: raw bits (valid) 0x0-0x1e.7 (31)
0x010|00 50 72 6f 6f 66 00 bf 89 e2 e8 84 e8 92 94 |.Proof......... |
0x010| 01| .| version: 1 0x1f-0x1f.7 (1)
0x020|03 |. | digest_hash_algorithm: "ripemd160" (3) 0x20-0x20.7 (1)
0x020| 04 23 ce d4 91 4b c5 e4 7a ed a3 14 b5 99 3b| .#...K..z.....;| digest: "0423ced4914bc5e47aeda314b5993b7bd796fe00" (raw bits) 0x21-0x34.7 (20)
0x030|7b d7 96 fe 00 |{.... |
| | | operations[0:25]: 0x35-0x1bc.7 (392)
| | | [0]{}: operation 0x35-0x46.7 (18)
0x030| f0 | . | tag: "append" (240) 0x35-0x35.7 (1)
0x030| 10 | . | argument_size: 16 0x36-0x36.7 (1)
0x030| 63 cd 59 00 38 53 32 f9 b4| c.Y.8S2..| argument: "63cd5900385332f9b4d1a26fc8308d10" (raw bits) 0x37-0x46.7 (16)
0x040|d1 a2 6f c8 30 8d 10 |..o.0.. |
| | | [1]{}: operation 0x47-0x47.7 (1)
0x040| 08 | . | tag: "sha256" (8) 0x47-0x47.7 (1)
| | | [2]{}: operation 0x48-0x48.7 (1)
0x040| ff | . | tag: "continuation_byte" (255) 0x48-0x48.7 (1)
| | | [3]{}: operation 0x49-0x5a.7 (18)
0x040| f0 | . | tag: "append" (240) 0x49-0x49.7 (1)
0x040| 10 | . | argument_size: 16 0x4a-0x4a.7 (1)
0x040| ae 0c 08 b1 f7| .....| argument: "ae0c08b1f77675fbc50b1e8be47045f7" (raw bits) 0x4b-0x5a.7 (16)
0x050|76 75 fb c5 0b 1e 8b e4 70 45 f7 |vu......pE. |
| | | [4]{}: operation 0x5b-0x5b.7 (1)
0x050| 08 | . | tag: "sha256" (8) 0x5b-0x5b.7 (1)
| | | [5]{}: operation 0x5c-0x7d.7 (34)
0x050| f0 | . | tag: "append" (240) 0x5c-0x5c.7 (1)
0x050| 20 | | argument_size: 32 0x5d-0x5d.7 (1)
0x050| 16 92| ..| argument: "169236a03672969e9f21bc781c8b1615768714f5463d666..." (raw bits) 0x5e-0x7d.7 (32)
0x060|36 a0 36 72 96 9e 9f 21 bc 78 1c 8b 16 15 76 87|6.6r...!.x....v.|
0x070|14 f5 46 3d 66 6c 5f 33 a9 9a 94 76 dc 66 |..F=fl_3...v.f |
| | | [6]{}: operation 0x7e-0x7e.7 (1)
0x070| 08 | . | tag: "sha256" (8) 0x7e-0x7e.7 (1)
| | | [7]{}: operation 0x7f-0xa0.7 (34)
0x070| f0| .| tag: "append" (240) 0x7f-0x7f.7 (1)
0x080|20 | | argument_size: 32 0x80-0x80.7 (1)
0x080| 7c 88 ef 76 16 6c 18 47 37 d7 89 ee eb 09 2d| |..v.l.G7.....-| argument: "7c88ef76166c184737d789eeeb092da1b92c77ef311b5dd..." (raw bits) 0x81-0xa0.7 (32)
0x090|a1 b9 2c 77 ef 31 1b 5d d1 15 19 4f 75 1b 29 0e|..,w.1.]...Ou.).|
0x0a0|60 |` |
| | | [8]{}: operation 0xa1-0xa1.7 (1)
0x0a0| 08 | . | tag: "sha256" (8) 0xa1-0xa1.7 (1)
| | | [9]{}: operation 0xa2-0xa7.7 (6)
0x0a0| f1 | . | tag: "prepend" (241) 0xa2-0xa2.7 (1)
0x0a0| 04 | . | argument_size: 4 0xa3-0xa3.7 (1)
0x0a0| 5a aa 52 3d | Z.R= | argument: "5aaa523d" (raw bits) 0xa4-0xa7.7 (4)
| | | [10]{}: operation 0xa8-0xb1.7 (10)
0x0a0| f0 | . | tag: "append" (240) 0xa8-0xa8.7 (1)
0x0a0| 08 | . | argument_size: 8 0xa9-0xa9.7 (1)
0x0a0| 46 64 e7 5b 69 5f| Fd.[i_| argument: "4664e75b695f322b" (raw bits) 0xaa-0xb1.7 (8)
0x0b0|32 2b |2+ |
| | | [11]{}: operation 0xb2-0xe9.7 (56)
0x0b0| 00 | . | tag: "attestation" (0) 0xb2-0xb2.7 (1)
0x0b0| 83 df e3 0d 2e f9 0c 8e | ........ | attestation_type: "calendar" (9502563384537189518) 0xb3-0xba.7 (8)
0x0b0| 2e | . | attestation_varbytes_size: 46 0xbb-0xbb.7 (1)
0x0b0| 2d | - | url_size: 45 0xbc-0xbc.7 (1)
0x0b0| 68 74 74| htt| url: "https://alice.btc.calendar.opentimestamps.org" 0xbd-0xe9.7 (45)
0x0c0|70 73 3a 2f 2f 61 6c 69 63 65 2e 62 74 63 2e 63|ps://alice.btc.c|
* |until 0xe9.7 (45) | |
| | | [12]{}: operation 0xea-0xea.7 (1)
0x0e0| ff | . | tag: "continuation_byte" (255) 0xea-0xea.7 (1)
| | | [13]{}: operation 0xeb-0xfc.7 (18)
0x0e0| f0 | . | tag: "append" (240) 0xeb-0xeb.7 (1)
0x0e0| 10 | . | argument_size: 16 0xec-0xec.7 (1)
0x0e0| 84 da bc| ...| argument: "84dabc8c98c925a3b4bbf6dce8329220" (raw bits) 0xed-0xfc.7 (16)
0x0f0|8c 98 c9 25 a3 b4 bb f6 dc e8 32 92 20 |...%......2. |
| | | [14]{}: operation 0xfd-0xfd.7 (1)
0x0f0| 08 | . | tag: "sha256" (8) 0xfd-0xfd.7 (1)
| | | [15]{}: operation 0xfe-0x103.7 (6)
0x0f0| f1 | . | tag: "prepend" (241) 0xfe-0xfe.7 (1)
0x0f0| 04| .| argument_size: 4 0xff-0xff.7 (1)
0x100|5a aa 52 3d |Z.R= | argument: "5aaa523d" (raw bits) 0x100-0x103.7 (4)
| | | [16]{}: operation 0x104-0x10d.7 (10)
0x100| f0 | . | tag: "append" (240) 0x104-0x104.7 (1)
0x100| 08 | . | argument_size: 8 0x105-0x105.7 (1)
0x100| 6e 33 5d 90 22 fb 8e 4d | n3]."..M | argument: "6e335d9022fb8e4d" (raw bits) 0x106-0x10d.7 (8)
| | | [17]{}: operation 0x10e-0x143.7 (54)
0x100| 00 | . | tag: "attestation" (0) 0x10e-0x10e.7 (1)
0x100| 83| .| attestation_type: "calendar" (9502563384537189518) 0x10f-0x116.7 (8)
0x110|df e3 0d 2e f9 0c 8e |....... |
0x110| 2c | , | attestation_varbytes_size: 44 0x117-0x117.7 (1)
0x110| 2b | + | url_size: 43 0x118-0x118.7 (1)
0x110| 68 74 74 70 73 3a 2f| https:/| url: "https://bob.btc.calendar.opentimestamps.org" 0x119-0x143.7 (43)
0x120|2f 62 6f 62 2e 62 74 63 2e 63 61 6c 65 6e 64 61|/bob.btc.calenda|
* |until 0x143.7 (43) | |
| | | [18]{}: operation 0x144-0x155.7 (18)
0x140| f0 | . | tag: "append" (240) 0x144-0x144.7 (1)
0x140| 10 | . | argument_size: 16 0x145-0x145.7 (1)
0x140| c4 07 38 54 5d 16 73 ff c7 f6| ..8T].s...| argument: "c40738545d1673ffc7f69abfa027e52f" (raw bits) 0x146-0x155.7 (16)
0x150|9a bf a0 27 e5 2f |...'./ |
| | | [19]{}: operation 0x156-0x156.7 (1)
0x150| 08 | . | tag: "sha256" (8) 0x156-0x156.7 (1)
| | | [20]{}: operation 0x157-0x178.7 (34)
0x150| f1 | . | tag: "prepend" (241) 0x157-0x157.7 (1)
0x150| 20 | | argument_size: 32 0x158-0x158.7 (1)
0x150| c9 af 2a f9 b5 ce 18| ..*....| argument: "c9af2af9b5ce1884303370f73bd151b1e25e0464660ce56..." (raw bits) 0x159-0x178.7 (32)
0x160|84 30 33 70 f7 3b d1 51 b1 e2 5e 04 64 66 0c e5|.03p.;.Q..^.df..|
0x170|6a 02 d8 b9 8f c0 e8 dc a4 |j........ |
| | | [21]{}: operation 0x179-0x179.7 (1)
0x170| 08 | . | tag: "sha256" (8) 0x179-0x179.7 (1)
| | | [22]{}: operation 0x17a-0x17f.7 (6)
0x170| f1 | . | tag: "prepend" (241) 0x17a-0x17a.7 (1)
0x170| 04 | . | argument_size: 4 0x17b-0x17b.7 (1)
0x170| 5a aa 52 3d| Z.R=| argument: "5aaa523d" (raw bits) 0x17c-0x17f.7 (4)
| | | [23]{}: operation 0x180-0x189.7 (10)
0x180|f0 |. | tag: "append" (240) 0x180-0x180.7 (1)
0x180| 08 | . | argument_size: 8 0x181-0x181.7 (1)
0x180| d6 c3 57 cd f9 1e 4b 16 | ..W...K. | argument: "d6c357cdf91e4b16" (raw bits) 0x182-0x189.7 (8)
| | | [24]{}: operation 0x18a-0x1bc.7 (51)
0x180| 00 | . | tag: "attestation" (0) 0x18a-0x18a.7 (1)
0x180| 83 df e3 0d 2e| .....| attestation_type: "calendar" (9502563384537189518) 0x18b-0x192.7 (8)
0x190|f9 0c 8e |... |
0x190| 29 | ) | attestation_varbytes_size: 41 0x193-0x193.7 (1)
0x190| 28 | ( | url_size: 40 0x194-0x194.7 (1)
0x190| 68 74 74 70 73 3a 2f 2f 66 69 6e| https://fin| url: "https://finney.calendar.eternitywall.com" 0x195-0x1bc.7 (40)
0x1a0|6e 65 79 2e 63 61 6c 65 6e 64 61 72 2e 65 74 65|ney.calendar.ete|
0x1b0|72 6e 69 74 79 77 61 6c 6c 2e 63 6f 6d| |rnitywall.com| |
Binary file added format/opentimestamps/testdata/sha1.ots
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
$ fq dv 039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb82.ots
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.{}: 039058c6f2c0cb492c533b0a4d14ef77cc0f78abccced5287d84a1a2011cfb82.ots (opentimestamps) 0x0-0x25f.7 (608)
$ fq dv simple.ots
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.{}: simple.ots (opentimestamps) 0x0-0x25f.7 (608)
0x000|00 4f 70 65 6e 54 69 6d 65 73 74 61 6d 70 73 00|.OpenTimestamps.| magic_bytes: raw bits (valid) 0x0-0x1e.7 (31)
0x010|00 50 72 6f 6f 66 00 bf 89 e2 e8 84 e8 92 94 |.Proof......... |
0x010| 01| .| version: 1 0x1f-0x1f.7 (1)
Expand Down
File renamed without changes.

0 comments on commit cc8e6f1

Please sign in to comment.