Skip to content

Commit

Permalink
Refactoring so error causality is easier to follow (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
andresuribe87 authored Aug 31, 2023
1 parent 154e116 commit 1b69542
Show file tree
Hide file tree
Showing 25 changed files with 112 additions and 106 deletions.
6 changes: 3 additions & 3 deletions credential/exchange/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,19 @@ func VerifyPresentationRequest(verifier any, pt PresentationRequestType, request
func VerifyJWTPresentationRequest(verifier jwx.Verifier, request []byte) (*PresentationDefinition, error) {
_, parsed, err := verifier.VerifyAndParse(string(request))
if err != nil {
return nil, errors.Wrap(err, "could not verify and parse jwt presentation request")
return nil, errors.Wrap(err, "verifying and parse jwt presentation request")
}
presDefGeneric, ok := parsed.Get(PresentationDefinitionKey)
if !ok {
return nil, fmt.Errorf("presentation definition key<%s> not found in token", PresentationDefinitionKey)
}
presDefBytes, err := json.Marshal(presDefGeneric)
if err != nil {
return nil, errors.Wrap(err, "could not marshal token into bytes for presentation definition")
return nil, errors.Wrap(err, "marshalling token into bytes for presentation definition")
}
var def PresentationDefinition
if err := json.Unmarshal(presDefBytes, &def); err != nil {
return nil, errors.Wrap(err, "could not unmarshal token into presentation definition")
return nil, errors.Wrap(err, "unmarshalling token into presentation definition")
}
return &def, nil
}
Expand Down
24 changes: 12 additions & 12 deletions credential/exchange/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
func IsValidPresentationDefinition(definition PresentationDefinition) error {
jsonBytes, err := json.Marshal(definition)
if err != nil {
return errors.Wrap(err, "could not marshal presentation definition to JSON")
return errors.Wrap(err, "marshaling presentation definition to JSON")
}
s, err := schema.LoadSchema(schema.PresentationDefinitionSchema)
if err != nil {
return errors.Wrap(err, "could not get presentation definition schema")
return errors.Wrap(err, "getting presentation definition schema")
}
if err = schema.IsValidAgainstJSONSchema(string(jsonBytes), s); err != nil {
return errors.Wrap(err, "presentation definition not valid against schema")
Expand All @@ -26,11 +26,11 @@ func IsValidPresentationDefinition(definition PresentationDefinition) error {
func IsValidPresentationDefinitionEnvelope(definition PresentationDefinitionEnvelope) error {
jsonBytes, err := json.Marshal(definition)
if err != nil {
return errors.Wrap(err, "could not marshal presentation definition to JSON")
return errors.Wrap(err, "marshaling presentation definition to JSON")
}
s, err := schema.LoadSchema(schema.PresentationDefinitionEnvelopeSchema)
if err != nil {
return errors.Wrap(err, "could not get presentation definition schema")
return errors.Wrap(err, "getting presentation definition schema")
}
if err = schema.IsValidAgainstJSONSchema(string(jsonBytes), s); err != nil {
return errors.Wrap(err, "presentation definition not valid against schema")
Expand All @@ -47,11 +47,11 @@ func IsValidPresentationSubmission(submission PresentationSubmission) error {
}
jsonBytes, err := json.Marshal(submissionWrapper)
if err != nil {
return errors.Wrap(err, "could not marshal presentation submission to JSON")
return errors.Wrap(err, "marshaling presentation submission to JSON")
}
s, err := schema.LoadSchema(schema.PresentationSubmissionSchema)
if err != nil {
return errors.Wrap(err, "could not get presentation submission schema")
return errors.Wrap(err, "getting presentation submission schema")
}
if err = schema.IsValidAgainstJSONSchema(string(jsonBytes), s); err != nil {
return errors.Wrap(err, "submission declaration not valid against schema")
Expand All @@ -63,11 +63,11 @@ func IsValidPresentationSubmission(submission PresentationSubmission) error {
func IsValidDefinitionClaimFormatDesignation(format ClaimFormat) error {
jsonBytes, err := json.Marshal(format)
if err != nil {
return errors.Wrap(err, "could not marshal claim format to JSON")
return errors.Wrap(err, "marshaling claim format to JSON")
}
s, err := schema.LoadSchema(schema.PresentationClaimFormatDesignationsSchema)
if err != nil {
return errors.Wrap(err, "could not get claim format schema")
return errors.Wrap(err, "getting claim format schema")
}
if err = schema.IsValidAgainstJSONSchema(string(jsonBytes), s); err != nil {
return errors.Wrap(err, "format declaration not valid against schema")
Expand All @@ -79,11 +79,11 @@ func IsValidDefinitionClaimFormatDesignation(format ClaimFormat) error {
func IsValidSubmissionRequirement(requirement SubmissionRequirement) error {
jsonBytes, err := json.Marshal(requirement)
if err != nil {
return errors.Wrap(err, "could not marshal submission requirement to JSON")
return errors.Wrap(err, "marshaling submission requirement to JSON")
}
s, err := schema.LoadSchema(schema.SubmissionRequirementSchema)
if err != nil {
return errors.Wrap(err, "could not get submission requirement schema")
return errors.Wrap(err, "getting submission requirement schema")
}
if err = schema.IsValidAgainstJSONSchema(string(jsonBytes), s); err != nil {
return errors.Wrap(err, "submission requirement not valid against schema")
Expand All @@ -100,11 +100,11 @@ func AreValidSubmissionRequirements(requirements []SubmissionRequirement) error
}
jsonBytes, err := json.Marshal(requirementsWrapper)
if err != nil {
return errors.Wrap(err, "could not marshal submission requirements to JSON")
return errors.Wrap(err, "marshaling submission requirements to JSON")
}
s, err := schema.LoadSchema(schema.SubmissionRequirementsSchema)
if err != nil {
return errors.Wrap(err, "could not get submission requirements schema")
return errors.Wrap(err, "getting submission requirements schema")
}
if err = schema.IsValidAgainstJSONSchema(string(jsonBytes), s); err != nil {
return errors.Wrap(err, "submission requirements not valid against schema")
Expand Down
4 changes: 2 additions & 2 deletions credential/exchange/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func BuildPresentationSubmission(signer any, requester string, def PresentationD
}
normalizedClaims, err := normalizePresentationClaims(claims)
if err != nil {
return nil, errors.Wrap(err, "failed to normalize some presentation claims")
return nil, errors.Wrap(err, "normalizing some presentation claims")
}
if len(normalizedClaims) == 0 {
return nil, errors.New("no claims remain after normalization; cannot continue processing")
Expand Down Expand Up @@ -296,7 +296,7 @@ func BuildPresentationSubmissionVP(submitter string, def PresentationDefinition,
// in the case where we've seen the claim, we need to check as to not add a nil claim value
if claim.claim != nil {
if err := builder.AddVerifiableCredentials(claim.claim); err != nil {
return nil, errors.Wrap(err, "could not add claim to verifiable presentation")
return nil, errors.Wrap(err, "adding claim to verifiable presentation")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion credential/exchange/verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func VerifyPresentationSubmissionVP(def PresentationDefinition, vp credential.Ve
// turn the vp into JSON so we can use the paths from the submission descriptor to resolve each claim
vpJSON, err := util.ToJSONMap(vp)
if err != nil {
return nil, errors.Wrap(err, "could not turn VP into JSON representation")
return nil, errors.Wrap(err, "turning VP into JSON representation")
}

// store results for each input descriptor
Expand Down
6 changes: 6 additions & 0 deletions credential/integrity/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,12 @@ func TestVerifyJWTPresentation(t *testing.T) {
assert.True(tt, verified)
})

t.Run("mytests", func(t *testing.T) {
_, didKey, err := key.GenerateDIDKey(crypto.Ed25519)
assert.NoError(t, err)
assert.Contains(t, didKey.String(), "did:key:z6Mk")
})

t.Run("valid presentation, bad credential", func(tt *testing.T) {
resolver, err := resolution.NewResolver([]resolution.Resolver{key.Resolver{}}...)
assert.NoError(tt, err)
Expand Down
16 changes: 8 additions & 8 deletions credential/manifest/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
func IsValidCredentialManifest(manifest CredentialManifest) error {
jsonBytes, err := json.Marshal(manifest)
if err != nil {
return errors.Wrap(err, "could not marshal manifest to JSON")
return errors.Wrap(err, "marshaling manifest to JSON")
}
s, err := schema.LoadSchema(schema.CredentialManifestSchema)
if err != nil {
return errors.Wrap(err, "could not get credential manifest schema")
return errors.Wrap(err, "getting credential manifest schema")
}
if err = schema.IsValidAgainstJSONSchema(string(jsonBytes), s); err != nil {
return errors.Wrap(err, "credential manifest not valid against schema")
Expand All @@ -26,11 +26,11 @@ func IsValidCredentialManifest(manifest CredentialManifest) error {
func IsValidCredentialApplication(application CredentialApplication) error {
jsonBytes, err := json.Marshal(application)
if err != nil {
return errors.Wrap(err, "could not marshal application to JSON")
return errors.Wrap(err, "marshaling application to JSON")
}
s, err := schema.LoadSchema(schema.CredentialApplicationSchema)
if err != nil {
return errors.Wrap(err, "could not get credential application schema")
return errors.Wrap(err, "getting credential application schema")
}
if err = schema.IsValidAgainstJSONSchema(string(jsonBytes), s); err != nil {
return errors.Wrap(err, "credential application not valid against schema")
Expand All @@ -42,11 +42,11 @@ func IsValidCredentialApplication(application CredentialApplication) error {
func IsValidCredentialResponse(response CredentialResponse) error {
jsonBytes, err := json.Marshal(response)
if err != nil {
return errors.Wrap(err, "could not marshal response to JSON")
return errors.Wrap(err, "marshaling response to JSON")
}
s, err := schema.LoadSchema(schema.CredentialResponseSchema)
if err != nil {
return errors.Wrap(err, "could not get credential response schema")
return errors.Wrap(err, "getting credential response schema")
}
if err = schema.IsValidAgainstJSONSchema(string(jsonBytes), s); err != nil {
return errors.Wrap(err, "credential response not valid against schema")
Expand All @@ -63,11 +63,11 @@ func AreValidOutputDescriptors(descriptors []OutputDescriptor) error {
}
jsonBytes, err := json.Marshal(descriptorsWrapper)
if err != nil {
return errors.Wrap(err, "could not marshal output descriptors to JSON")
return errors.Wrap(err, "marshaling output descriptors to JSON")
}
s, err := schema.LoadSchema(schema.OutputDescriptorsSchema)
if err != nil {
return errors.Wrap(err, "could not get output descriptors schema")
return errors.Wrap(err, "getting output descriptors schema")
}
if err = schema.IsValidAgainstJSONSchema(string(jsonBytes), s); err != nil {
return errors.Wrap(err, "output descriptors not valid against schema")
Expand Down
2 changes: 1 addition & 1 deletion credential/parsing/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func VCJWTJSONToVC(vcJWTJSON []byte) (jws.Headers, jwt.Token, *credential.Verifi
// get headers
headers, err := jwx.GetJWSHeaders(vcJWTJSON)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "could not get JWT headers")
return nil, nil, nil, errors.Wrap(err, "getting JWT headers")
}

cred, err := integrity.ParseVerifiableCredentialFromToken(token)
Expand Down
12 changes: 6 additions & 6 deletions credential/rendering/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
func IsValidEntityStyle(esd EntityStyleDescriptor) error {
jsonBytes, err := json.Marshal(esd)
if err != nil {
return errors.Wrap(err, "could not marshal entity style descriptor")
return errors.Wrap(err, "marshaling entity style descriptor")
}
s, err := schema.LoadSchema(schema.EntityStylesSchema)
if err != nil {
return errors.Wrap(err, "could not get entity styles schema")
return errors.Wrap(err, "getting entity styles schema")
}
if err = schema.IsValidAgainstJSONSchema(string(jsonBytes), s); err != nil {
return errors.Wrap(err, "entity style not valid against schema")
Expand All @@ -26,11 +26,11 @@ func IsValidEntityStyle(esd EntityStyleDescriptor) error {
func IsValidDisplayMappingObject(dmo DisplayMappingObject) error {
jsonBytes, err := json.Marshal(dmo)
if err != nil {
return errors.Wrap(err, "could not marshal display mapping object")
return errors.Wrap(err, "marshaling display mapping object")
}
s, err := schema.LoadSchema(schema.DisplayMappingObjectSchema)
if err != nil {
return errors.Wrap(err, "could not get display mapping object schema")
return errors.Wrap(err, "getting display mapping object schema")
}
if err = schema.IsValidAgainstJSONSchema(string(jsonBytes), s); err != nil {
return errors.Wrap(err, "display mapping object not valid against schema")
Expand All @@ -42,11 +42,11 @@ func IsValidDisplayMappingObject(dmo DisplayMappingObject) error {
func IsValidLabeledDisplayMappingObject(ldmo LabeledDisplayMappingObject) error {
jsonBytes, err := json.Marshal(ldmo)
if err != nil {
return errors.Wrap(err, "could not marshal labeled display mapping object")
return errors.Wrap(err, "marshaling labeled display mapping object")
}
s, err := schema.LoadSchema(schema.LabeledDisplayMappingObjectSchema)
if err != nil {
return errors.Wrap(err, "could not get labeled display mapping object schema")
return errors.Wrap(err, "getting labeled display mapping object schema")
}
if err = schema.IsValidAgainstJSONSchema(string(jsonBytes), s); err != nil {
return errors.Wrap(err, "labeled display mapping object not valid against schema")
Expand Down
8 changes: 4 additions & 4 deletions credential/schema/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ func (ra *RemoteAccess) GetVCJSONSchema(ctx context.Context, t VCJSONSchemaType,
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, errors.Wrap(err, "error creating request")
return nil, errors.Wrap(err, "creating request")
}
resp, err := ra.Do(req)
if err != nil {
return nil, errors.Wrap(err, "error getting schema")
return nil, errors.Wrap(err, "getting schema")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("error getting schema, status code: %d", resp.StatusCode)
return nil, errors.Errorf("getting schema, status code: %d", resp.StatusCode)
}

var schema VCJSONSchema
if err = json.NewDecoder(resp.Body).Decode(&schema); err != nil {
return nil, errors.Wrap(err, "error decoding schema")
return nil, errors.Wrap(err, "decoding schema")
}
return schema, nil
}
28 changes: 14 additions & 14 deletions credential/status/statuslist2021.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ type StatusList2021Credential struct {
func GenerateStatusList2021Credential(id string, issuer string, purpose StatusPurpose, issuedCredentials []credential.VerifiableCredential) (*credential.VerifiableCredential, error) {
statusListIndices, err := prepareCredentialsForStatusList(purpose, issuedCredentials)
if err != nil {
return nil, errors.Wrap(err, "could not generate status list credential")
return nil, errors.Wrap(err, "generating status list credential")
}

bitString, err := bitstringGeneration(statusListIndices)
if err != nil {
return nil, errors.Wrap(err, "could not generate bitstring for status list credential")
return nil, errors.Wrap(err, "generating bitstring for status list credential")
}

rlc := StatusList2021Credential{
Expand All @@ -89,15 +89,15 @@ func GenerateStatusList2021Credential(id string, issuer string, purpose StatusPu
}
rlcJSON, err := util.ToJSONMap(rlc)
if err != nil {
return nil, errors.Wrap(err, "could not turn RLC to JSON")
return nil, errors.Wrap(err, "turning RLC to JSON")
}
if err = builder.SetCredentialSubject(rlcJSON); err != nil {
return nil, errors.Wrap(err, errMsgFragment+"subject")
}

statusListCredential, err := builder.Build()
if err != nil {
return nil, errors.Wrap(err, "could not build status list credential")
return nil, errors.Wrap(err, "building status list credential")
}
return statusListCredential, nil
}
Expand Down Expand Up @@ -151,11 +151,11 @@ func prepareCredentialsForStatusList(purpose StatusPurpose, credentials []creden
func getStatusEntry(maybeCredentialStatus any) (*StatusList2021Entry, error) {
statusBytes, err := json.Marshal(maybeCredentialStatus)
if err != nil {
return nil, errors.Wrap(err, "could not marshal credential status property")
return nil, errors.Wrap(err, "marshaling credential status property")
}
var statusEntry StatusList2021Entry
if err = json.Unmarshal(statusBytes, &statusEntry); err != nil {
return nil, errors.Wrap(err, "could not unmarshal credential status property")
return nil, errors.Wrap(err, "unmarshaling credential status property")
}
return &statusEntry, util.IsValidStruct(statusEntry)
}
Expand Down Expand Up @@ -185,19 +185,19 @@ func bitstringGeneration(statusListCredentialIndices []string) (string, error) {

bitstringBinary, err := b.MarshalBinary()
if err != nil {
return "", errors.Wrap(err, "could not generate bitstring binary representation")
return "", errors.Wrap(err, "generating bitstring binary representation")
}

// 3. Generate a compressed bitstring by using the GZIP compression algorithm [RFC1952] on the bitstring and then
// base64-encoding [RFC4648] the result.
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
if _, err = zw.Write(bitstringBinary); err != nil {
return "", errors.Wrap(err, "could not compress status list bitstring using GZIP")
return "", errors.Wrap(err, "compressing status list bitstring using GZIP")
}

if err = zw.Close(); err != nil {
return "", errors.Wrap(err, "could not close gzip writer")
return "", errors.Wrap(err, "closing gzip writer")
}

base64Bitstring := base64.StdEncoding.EncodeToString(buf.Bytes())
Expand All @@ -214,27 +214,27 @@ func bitstringExpansion(compressedBitstring string) ([]string, error) {
// bitstring and then expanding the output using the GZIP decompression algorithm [RFC1952].
decoded, err := base64.StdEncoding.DecodeString(compressedBitstring)
if err != nil {
return nil, errors.Wrap(err, "could not decode compressed bitstring")
return nil, errors.Wrap(err, "decoding compressed bitstring")
}

bitstringReader := bytes.NewReader(decoded)
zr, err := gzip.NewReader(bitstringReader)
if err != nil {
return nil, errors.Wrap(err, "could not unzip status list bitstring using GZIP")
return nil, errors.Wrap(err, "unzipping status list bitstring using GZIP")
}

unzipped, err := io.ReadAll(zr)
if err != nil {
return nil, errors.Wrap(err, "could not expand status list bitstring using GZIP")
return nil, errors.Wrap(err, "expanding status list bitstring using GZIP")
}

if err := zr.Close(); err != nil {
return nil, errors.Wrap(err, "could not close gzip reader")
return nil, errors.Wrap(err, "closing gzip reader")
}

b := bitset.New(uint(len(unzipped)))
if err := b.UnmarshalBinary(unzipped); err != nil {
return nil, errors.Wrap(err, "could not unmarshal binary bitstring")
return nil, errors.Wrap(err, "unmarshaling binary bitstring")
}

// find set bits to reconstruct the status list indices
Expand Down
2 changes: 1 addition & 1 deletion crypto/jwx/jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func jwkFromRSAPrivateKey(key rsa.PrivateKey) (*PublicKeyJWK, *PrivateKeyJWK, er
}
rsaJWKBytes, err := json.Marshal(rsaJWKGeneric)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to marshal rsa jwk")
return nil, nil, errors.Wrap(err, "marshalling rsa jwk")
}
var publicKeyJWK PublicKeyJWK
if err = json.Unmarshal(rsaJWKBytes, &publicKeyJWK); err != nil {
Expand Down
Loading

0 comments on commit 1b69542

Please sign in to comment.