Skip to content
Permalink
Browse files

Add command line flag to add upload digest (#51)

* Add command line flag to support submitting digests directly to dcrtime via command line param

* Update command line description

* Return error if -digest and -file flags are used simultaneously
  • Loading branch information...
sefbkn authored and marcopeereboom committed May 30, 2019
1 parent 56572d4 commit d6f4a5134c7ca78c3c2b2a849ad9c74de064656e
Showing with 30 additions and 1 deletion.
  1. +30 −1 cmd/dcrtime/dcrtime.go
@@ -37,6 +37,7 @@ var (
host = flag.String("h", "", "Timestamping host")
trial = flag.Bool("t", false, "Trial run, don't contact server")
verbose = flag.Bool("v", false, "Verbose")
digest = flag.String("digest", "", "Submit a raw 256 bit digest to anchor")
)

// normalizeAddress returns addr with the passed default port appended if
@@ -67,7 +68,7 @@ func isFile(filename string) bool {
return fi.Mode().IsRegular()
}

// isTimestamp determines if a string is a valid SHA256 digest.
// isDigest determines if a string is a valid SHA256 digest.
func isDigest(digest string) bool {
return v1.RegexpSHA256.MatchString(digest)
}
@@ -371,9 +372,31 @@ func upload(digests []string, exists map[string]string) error {
return nil
}

func hasDigestFlag() bool {
return digest != nil && *digest != ""
}

func uploadDigest(digest string) error {
return upload([]string{digest}, make(map[string]string))
}

// Ensures that there are no conflicting flags
func ensureFlagCompatibility() error {
if *fileOnly && hasDigestFlag() {
return fmt.Errorf("-digest and -file flags cannot be used simultaneously")
}

return nil
}

func _main() error {
flag.Parse()

flagError := ensureFlagCompatibility()
if flagError != nil {
return flagError
}

if *host == "" {
if *testnet {
*host = v1.DefaultTestnetTimeHost
@@ -396,6 +419,12 @@ func _main() error {
}
*host = u.String()

// Allow submitting a pre-calculated 256 bit digest from the command line, rather than
// needing to hash a payload.
if hasDigestFlag() {
return uploadDigest(*digest)
}

// We attempt to open files first; if that doesn't work we treat the
// args as digests or timestamps. Digests and timestamps are sent to
// the server for lookup. Use fileOnly to override this behavior.

0 comments on commit d6f4a51

Please sign in to comment.
You can’t perform that action at this time.