From d18c5e636973a9690d891ab5ebf2457f0617c05b Mon Sep 17 00:00:00 2001 From: makeworld Date: Wed, 3 Jul 2024 16:57:00 -0400 Subject: [PATCH] get: add --attestation flag --- aa/aa.go | 35 ++++++++++++++++++----------------- get/get.go | 30 +++++++++++++++++++++++++----- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/aa/aa.go b/aa/aa.go index 3d7e9e9..20c4374 100644 --- a/aa/aa.go +++ b/aa/aa.go @@ -32,28 +32,29 @@ type GetAttOpts struct { // Attestation as stored in the database in DAG-CBOR. // See https://github.com/starlinglab/authenticated-attributes/blob/main/docs/database.md#schema // -// This does not encode into the same CBOR it was decoded from, but that's okay -// as encoding this struct should be not required anywhere. +// This may not encode into the same CBOR it was decoded from, but that's okay +// as that should not be required anywhere. type AttEntry struct { Signature struct { - PubKey [32]byte - Sig [64]byte - Msg CborCID - } + PubKey []byte `json:"pubKey"` + Sig []byte `json:"sig"` + Msg CborCID `json:"msg"` + } `json:"signature"` Timestamp struct { OTS struct { - Proof []byte - Upgraded bool - Msg CborCID - } - } + Proof []byte `json:"proof"` + Upgraded bool `json:"upgraded"` + Msg CborCID `json:"msg"` + } `json:"ots"` + } `json:"timestamp"` Attestation struct { - CID CborCID - Value any - Encrypted bool - Timestamp time.Time - } - Version string + CID CborCID `json:"CID"` + Value any `json:"value"` + Attribute string `json:"attribute"` + Encrypted bool `json:"encrypted"` + Timestamp time.Time `json:"timestamp"` + } `json:"attestation"` + Version string `json:"version"` } // Attributes for uploading. diff --git a/get/get.go b/get/get.go index b4acb9a..4d32215 100644 --- a/get/get.go +++ b/get/get.go @@ -13,10 +13,11 @@ import ( ) var ( - attr string - getAll bool - isEncrypted bool - encKeyPath string + attr string + getAll bool + isEncrypted bool + encKeyPath string + showAttestation bool ) func Run(args []string) error { @@ -25,6 +26,7 @@ func Run(args []string) error { fs.BoolVar(&getAll, "all", false, "get all attributes instead of just one") fs.BoolVar(&isEncrypted, "encrypted", false, "value to get is encrypted") fs.StringVar(&encKeyPath, "key", "", "(optional) manual path to encryption key file, implies --encrypted") + fs.BoolVar(&showAttestation, "attestation", false, "show attestation information, not just value. Note values are not decrypted for this output.") err := fs.Parse(args) if err != nil { @@ -37,6 +39,9 @@ func Run(args []string) error { fs.PrintDefaults() return fmt.Errorf("\nprovide attribute name with --attr") } + if getAll && showAttestation { + return fmt.Errorf("can't use --all and --attestation together") + } if fs.NArg() != 1 { return fmt.Errorf("provide a single CID to work with") } @@ -82,7 +87,12 @@ func Run(args []string) error { os.Stdout.Write(b) fmt.Fprintln(os.Stderr, "\n\nThis is not an exact canonical representation.") } else { - ae, err := aa.GetAttestation(cid, attr, aa.GetAttOpts{EncKey: encKey}) + leaveEnc := false + if showAttestation { + leaveEnc = true + } + + ae, err := aa.GetAttestation(cid, attr, aa.GetAttOpts{EncKey: encKey, LeaveEncrypted: leaveEnc}) if err == aa.ErrNeedsKey { return fmt.Errorf("error attestation is encrypted, use --encrypted or --key") } @@ -90,6 +100,16 @@ func Run(args []string) error { return fmt.Errorf("error getting attestation: %w", err) } + if showAttestation { + b, err := json.MarshalIndent(ae, "", " ") + if err != nil { + return fmt.Errorf("error encoding value as JSON: %w", err) + } + os.Stdout.Write(b) + fmt.Fprintln(os.Stderr, "\n\nThis is not an exact canonical representation.") + return nil + } + kind := reflect.TypeOf(ae.Attestation.Value).Kind() if kind == reflect.Slice || kind == reflect.Struct || kind == reflect.Map || kind == reflect.Array {