-
Notifications
You must be signed in to change notification settings - Fork 33
tle: add -m metadata flag
#94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bd65c6a
tle: add -m flag to show metadata; support INPUT parsing and timestam…
alienx5499 f767d60
fix: remove unused variables and function to satisfy staticcheck
alienx5499 968302d
refactor: use armor.NewReader directly instead of manual armor parsing
alienx5499 830c1e2
test: add TestMetadata to validate metadata extraction from TLE files
alienx5499 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "io" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "filippo.io/age/armor" | ||
| "gopkg.in/yaml.v3" | ||
|
|
||
| "github.com/drand/tlock/networks/http" | ||
| ) | ||
|
|
||
| type CiphertextMetadata struct { | ||
| Round uint64 `yaml:"round"` | ||
| ChainHash string `yaml:"chain_hash"` | ||
| Time time.Time `yaml:"time"` | ||
| } | ||
|
|
||
| // Metadata reads INPUT from src and, if it contains a tlock stanza, outputs YAML with round, chainhash and estimated time. | ||
| func Metadata(dst io.Writer, src io.Reader, network *http.Network) error { | ||
| rr := bufio.NewReader(src) | ||
|
|
||
| // Use armor.NewReader to handle armor decoding automatically | ||
| // Only support armored input for metadata extraction in this change. | ||
| armorReader := armor.NewReader(rr) | ||
|
|
||
| // Read from the de-armored content to find the tlock stanza | ||
| scanner := bufio.NewScanner(armorReader) | ||
| var round uint64 | ||
| var chainHash string | ||
| found := false | ||
|
|
||
| for scanner.Scan() { | ||
| line := strings.TrimSpace(scanner.Text()) | ||
| if strings.HasPrefix(line, "-> ") { | ||
| fields := strings.Fields(line) | ||
| if len(fields) >= 4 && fields[1] == "tlock" { | ||
| r, err := strconv.ParseUint(fields[2], 10, 64) | ||
| if err != nil { | ||
| return fmt.Errorf("parse round: %w", err) | ||
| } | ||
| round = r | ||
| chainHash = fields[3] | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if err := scanner.Err(); err != nil { | ||
| return fmt.Errorf("read armored content: %w", err) | ||
| } | ||
|
|
||
| if !found { | ||
| return fmt.Errorf("no tlock stanza found in armored age header") | ||
| } | ||
|
|
||
| // Estimate time for the given round | ||
| now := time.Now() | ||
| current := network.Current(now) | ||
| var low, high time.Time | ||
| if round <= current { | ||
| high = now | ||
| low = now.Add(-365 * 24 * time.Hour) | ||
| } else { | ||
| low = now | ||
| high = now.Add(365 * 24 * time.Hour) | ||
| } | ||
|
|
||
| t, err := roundToTimeBinarySearch(network, round, low, high) | ||
| if err != nil { | ||
| return fmt.Errorf("estimate time: %w", err) | ||
| } | ||
|
|
||
| out := CiphertextMetadata{Round: round, ChainHash: chainHash, Time: t} | ||
| b, err := yaml.Marshal(out) | ||
| if err != nil { | ||
| return fmt.Errorf("yaml marshal: %w", err) | ||
| } | ||
| if _, err := dst.Write(b); err != nil { | ||
| return fmt.Errorf("write: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // roundToTimeBinarySearch searches for a time whose round is the target. | ||
| func roundToTimeBinarySearch(network *http.Network, target uint64, low, high time.Time) (time.Time, error) { | ||
| // If bounds are inverted, fix. | ||
| if high.Before(low) { | ||
| low, high = high, low | ||
| } | ||
| // Binary search with tolerance of 1 round. | ||
| for i := 0; i < 64; i++ { | ||
| mid := low.Add(high.Sub(low) / 2) | ||
| r := network.RoundNumber(mid) | ||
| if r == target { | ||
| return mid, nil | ||
| } | ||
| if r < target { | ||
| low = mid.Add(time.Second) | ||
| } else { | ||
| high = mid.Add(-time.Second) | ||
| } | ||
| if !high.After(low) { | ||
| break | ||
| } | ||
| } | ||
| // Best effort: return low as approximation. | ||
| return low, nil | ||
| } | ||
|
|
||
| // parseArgs tries to extract round and chain from a tlock stanza arguments slice. | ||
| // (no other helpers) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.