-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclean.go
56 lines (46 loc) · 1.23 KB
/
clean.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
package main
import (
"context"
"fmt"
"os"
"github.com/nbd-wtf/opentimestamps"
"github.com/urfave/cli/v3"
)
var clean = &cli.Command{
Name: "clean",
Description: `modifies a file to leave only the oldest Bitcoin attestation`,
Action: func(ctx context.Context, c *cli.Command) error {
file := c.Args().First()
b, err := os.ReadFile(file)
if err != nil {
return err
}
ts, err := opentimestamps.ReadFromFile(b)
if err != nil {
return err
}
var oldest opentimestamps.Sequence
for _, seq := range ts.GetBitcoinAttestedSequences() {
if len(oldest) == 0 || seq.GetAttestation().BitcoinBlockHeight < oldest.GetAttestation().BitcoinBlockHeight {
oldest = seq
}
}
if len(oldest) < 0 {
return fmt.Errorf("no bitcoin attested sequences found")
}
if len(ts.Sequences) == 1 {
fmt.Fprintf(os.Stderr, "already clear")
return nil
}
ts.Sequences = []opentimestamps.Sequence{oldest}
if err := os.Rename(file, file+".full"); err != nil {
return err
}
fmt.Fprintf(os.Stderr, "renamed '%s' to '%s'\n", file, file+".full")
if err := os.WriteFile(file, ts.SerializeToFile(), 0644); err != nil {
return err
}
fmt.Fprintf(os.Stderr, "saved new file '%s'\n", file)
return nil
},
}