From d493945c5264aa5803eff15a123753105450b665 Mon Sep 17 00:00:00 2001 From: Abdulbois Date: Fri, 7 Oct 2022 16:25:27 +0500 Subject: [PATCH] refactor: change xeipuuv/gojsonschema to santhosh-tekuri/jsonschema package inside verifiable presentation. Refactor related unit tests Signed-off-by: Abdulbois --- cmd/aries-agent-mobile/go.mod | 5 +++-- cmd/aries-agent-mobile/go.sum | 2 ++ cmd/aries-agent-rest/go.mod | 7 ++++--- cmd/aries-agent-rest/go.sum | 2 ++ cmd/aries-js-worker/go.mod | 5 +++-- cmd/aries-js-worker/go.sum | 2 ++ go.mod | 1 + go.sum | 2 ++ pkg/doc/verifiable/presentation.go | 22 +++++++++++----------- pkg/doc/verifiable/presentation_test.go | 14 +++++++------- 10 files changed, 37 insertions(+), 25 deletions(-) diff --git a/cmd/aries-agent-mobile/go.mod b/cmd/aries-agent-mobile/go.mod index 8892e7e5df..da6ec0b95f 100644 --- a/cmd/aries-agent-mobile/go.mod +++ b/cmd/aries-agent-mobile/go.mod @@ -8,8 +8,8 @@ go 1.19 require ( github.com/google/uuid v1.3.0 - github.com/hyperledger/aries-framework-go v0.1.8-0.20220322085443-50e8f9bd208b - github.com/hyperledger/aries-framework-go/component/storageutil v0.0.0-20220322085443-50e8f9bd208b + github.com/hyperledger/aries-framework-go v0.1.9-0.20221007132211-6233891ce372 + github.com/hyperledger/aries-framework-go/component/storageutil v0.1.9-0.20221007132211-6233891ce372 github.com/hyperledger/aries-framework-go/spi v0.0.0-20220606124520-53422361c38c github.com/hyperledger/aries-framework-go/test/component v0.0.0-20220428211718-66cc046674a1 github.com/piprate/json-gold v0.4.1 @@ -49,6 +49,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pquerna/cachecontrol v0.1.0 // indirect github.com/rs/cors v1.7.0 // indirect + github.com/santhosh-tekuri/jsonschema v1.2.4 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/square/go-jose/v3 v3.0.0-20200630053402-0a67ce9b0693 // indirect github.com/teserakt-io/golang-ed25519 v0.0.0-20210104091850-3888c087a4c8 // indirect diff --git a/cmd/aries-agent-mobile/go.sum b/cmd/aries-agent-mobile/go.sum index aad427cd6c..bcea1cc658 100644 --- a/cmd/aries-agent-mobile/go.sum +++ b/cmd/aries-agent-mobile/go.sum @@ -270,6 +270,8 @@ github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc= +github.com/santhosh-tekuri/jsonschema v1.2.4 h1:hNhW8e7t+H1vgY+1QeEQpveR6D4+OwKPXCfD2aieJis= +github.com/santhosh-tekuri/jsonschema v1.2.4/go.mod h1:TEAUOeZSmIxTTuHatJzrvARHiuO9LYd+cIxzgEHCQI4= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/square/go-jose/v3 v3.0.0-20200630053402-0a67ce9b0693 h1:wD1IWQwAhdWclCwaf6DdzgCAe9Bfz1M+4AHRd7N786Y= diff --git a/cmd/aries-agent-rest/go.mod b/cmd/aries-agent-rest/go.mod index 420796c2eb..469b2af439 100644 --- a/cmd/aries-agent-rest/go.mod +++ b/cmd/aries-agent-rest/go.mod @@ -9,9 +9,9 @@ go 1.19 require ( github.com/cenkalti/backoff/v4 v4.1.2 github.com/gorilla/mux v1.7.3 - github.com/hyperledger/aries-framework-go v0.1.8-0.20220322085443-50e8f9bd208b - github.com/hyperledger/aries-framework-go/component/storage/leveldb v0.0.0-20220322085443-50e8f9bd208b - github.com/hyperledger/aries-framework-go/component/storageutil v0.0.0-20220322085443-50e8f9bd208b + github.com/hyperledger/aries-framework-go v0.1.9-0.20221007132211-6233891ce372 + github.com/hyperledger/aries-framework-go/component/storage/leveldb v0.1.9-0.20221007132211-6233891ce372 + github.com/hyperledger/aries-framework-go/component/storageutil v0.1.9-0.20221007132211-6233891ce372 github.com/hyperledger/aries-framework-go/spi v0.0.0-20220606124520-53422361c38c github.com/rs/cors v1.7.0 github.com/spf13/cobra v1.0.0 @@ -65,6 +65,7 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pquerna/cachecontrol v0.1.0 // indirect + github.com/santhosh-tekuri/jsonschema v1.2.4 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/square/go-jose/v3 v3.0.0-20200630053402-0a67ce9b0693 // indirect github.com/syndtr/goleveldb v1.0.0 // indirect diff --git a/cmd/aries-agent-rest/go.sum b/cmd/aries-agent-rest/go.sum index da042a5637..617e0c8700 100644 --- a/cmd/aries-agent-rest/go.sum +++ b/cmd/aries-agent-rest/go.sum @@ -632,6 +632,8 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc= github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= +github.com/santhosh-tekuri/jsonschema v1.2.4 h1:hNhW8e7t+H1vgY+1QeEQpveR6D4+OwKPXCfD2aieJis= +github.com/santhosh-tekuri/jsonschema v1.2.4/go.mod h1:TEAUOeZSmIxTTuHatJzrvARHiuO9LYd+cIxzgEHCQI4= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo= diff --git a/cmd/aries-js-worker/go.mod b/cmd/aries-js-worker/go.mod index 39040f471e..3809760045 100644 --- a/cmd/aries-js-worker/go.mod +++ b/cmd/aries-js-worker/go.mod @@ -8,8 +8,8 @@ go 1.19 require ( github.com/google/uuid v1.3.0 - github.com/hyperledger/aries-framework-go v0.1.8-0.20220322085443-50e8f9bd208b - github.com/hyperledger/aries-framework-go/component/storage/indexeddb v0.1.8-0.20220322085443-50e8f9bd208b + github.com/hyperledger/aries-framework-go v0.1.9-0.20221007132211-6233891ce372 + github.com/hyperledger/aries-framework-go/component/storage/indexeddb v0.1.9-0.20221007132211-6233891ce372 github.com/hyperledger/aries-framework-go/spi v0.0.0-20220606124520-53422361c38c github.com/mitchellh/mapstructure v1.3.0 github.com/stretchr/testify v1.7.2 @@ -48,6 +48,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pquerna/cachecontrol v0.1.0 // indirect github.com/rs/cors v1.7.0 // indirect + github.com/santhosh-tekuri/jsonschema v1.2.4 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/square/go-jose/v3 v3.0.0-20200630053402-0a67ce9b0693 // indirect github.com/teserakt-io/golang-ed25519 v0.0.0-20210104091850-3888c087a4c8 // indirect diff --git a/cmd/aries-js-worker/go.sum b/cmd/aries-js-worker/go.sum index 4ca69bbb54..48b3a49a1f 100644 --- a/cmd/aries-js-worker/go.sum +++ b/cmd/aries-js-worker/go.sum @@ -269,6 +269,8 @@ github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc= +github.com/santhosh-tekuri/jsonschema v1.2.4 h1:hNhW8e7t+H1vgY+1QeEQpveR6D4+OwKPXCfD2aieJis= +github.com/santhosh-tekuri/jsonschema v1.2.4/go.mod h1:TEAUOeZSmIxTTuHatJzrvARHiuO9LYd+cIxzgEHCQI4= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/square/go-jose/v3 v3.0.0-20200630053402-0a67ce9b0693 h1:wD1IWQwAhdWclCwaf6DdzgCAe9Bfz1M+4AHRd7N786Y= diff --git a/go.mod b/go.mod index 4454447ba6..16bfa1d8b4 100644 --- a/go.mod +++ b/go.mod @@ -30,6 +30,7 @@ require ( github.com/piprate/json-gold v0.4.1 github.com/pkg/errors v0.9.1 github.com/rs/cors v1.7.0 + github.com/santhosh-tekuri/jsonschema v1.2.4 github.com/square/go-jose/v3 v3.0.0-20200630053402-0a67ce9b0693 github.com/stretchr/testify v1.7.2 github.com/teserakt-io/golang-ed25519 v0.0.0-20210104091850-3888c087a4c8 diff --git a/go.sum b/go.sum index f125d1988e..7225c4571d 100644 --- a/go.sum +++ b/go.sum @@ -317,6 +317,8 @@ github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc= +github.com/santhosh-tekuri/jsonschema v1.2.4 h1:hNhW8e7t+H1vgY+1QeEQpveR6D4+OwKPXCfD2aieJis= +github.com/santhosh-tekuri/jsonschema v1.2.4/go.mod h1:TEAUOeZSmIxTTuHatJzrvARHiuO9LYd+cIxzgEHCQI4= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/square/go-jose/v3 v3.0.0-20200630053402-0a67ce9b0693 h1:wD1IWQwAhdWclCwaf6DdzgCAe9Bfz1M+4AHRd7N786Y= diff --git a/pkg/doc/verifiable/presentation.go b/pkg/doc/verifiable/presentation.go index 3e31298e34..092612baa6 100644 --- a/pkg/doc/verifiable/presentation.go +++ b/pkg/doc/verifiable/presentation.go @@ -6,12 +6,14 @@ SPDX-License-Identifier: Apache-2.0 package verifiable import ( + "bytes" "encoding/json" "errors" "fmt" + "strings" jsonld "github.com/piprate/json-gold/ld" - "github.com/xeipuuv/gojsonschema" + "github.com/santhosh-tekuri/jsonschema" "github.com/hyperledger/aries-framework-go/pkg/doc/jose" docjsonld "github.com/hyperledger/aries-framework-go/pkg/doc/jsonld" @@ -157,7 +159,12 @@ const basePresentationSchema = ` ` //nolint:gochecknoglobals -var basePresentationSchemaLoader = gojsonschema.NewStringLoader(basePresentationSchema) +var ( + schemaName = "presentationSchema" + schemaCompiler = jsonschema.NewCompiler() + _ = schemaCompiler.AddResource(schemaName, strings.NewReader(basePresentationSchema)) + presentationSchema = schemaCompiler.MustCompile(schemaName) +) // MarshalledCredential defines marshalled Verifiable Credential enclosed into Presentation. // MarshalledCredential can be passed to verifiable.ParseCredential(). @@ -568,16 +575,9 @@ func validateVPJSONLD(vpBytes []byte, opts *presentationOpts) error { } func validateVPJSONSchema(data []byte) error { - loader := gojsonschema.NewStringLoader(string(data)) - - result, err := gojsonschema.Validate(basePresentationSchemaLoader, loader) + err := presentationSchema.Validate(bytes.NewReader(data)) if err != nil { - return fmt.Errorf("validation of verifiable credential: %w", err) - } - - if !result.Valid() { - errMsg := describeSchemaValidationError(result, "verifiable presentation") - return errors.New(errMsg) + return fmt.Errorf("verifiable presentation is not valid: %w", err) } return nil diff --git a/pkg/doc/verifiable/presentation_test.go b/pkg/doc/verifiable/presentation_test.go index 17043a0832..afd4050ab3 100644 --- a/pkg/doc/verifiable/presentation_test.go +++ b/pkg/doc/verifiable/presentation_test.go @@ -304,7 +304,7 @@ func TestValidateVP_Context(t *testing.T) { require.NoError(t, err) vp, err := newTestPresentation(t, bytes) require.Error(t, err) - require.Contains(t, err.Error(), "@context is required") + require.Contains(t, err.Error(), "missing properties: \"@context\"") require.Nil(t, vp) }) @@ -319,7 +319,7 @@ func TestValidateVP_Context(t *testing.T) { require.NoError(t, err) vp, err := newTestPresentation(t, bytes) require.Error(t, err) - require.Contains(t, err.Error(), "does not match: \"https://www.w3.org/2018/credentials/v1\"") + require.Contains(t, err.Error(), "must be \"https://www.w3.org/2018/credentials/v1\"") require.Nil(t, vp) }) @@ -342,7 +342,7 @@ func TestValidateVP_Context(t *testing.T) { require.NoError(t, err) vp, err := newTestPresentation(t, bytes) require.Error(t, err) - require.Contains(t, err.Error(), "does not match: \"https://www.w3.org/2018/credentials/v1\"") + require.Contains(t, err.Error(), "must be \"https://www.w3.org/2018/credentials/v1\"") require.Nil(t, vp) }) } @@ -356,7 +356,7 @@ func TestValidateVP_ID(t *testing.T) { require.NoError(t, err) vp, err := newTestPresentation(t, bytes) require.Error(t, err) - require.Contains(t, err.Error(), "id: Does not match format 'uri'") + require.Contains(t, err.Error(), "\"not valid presentation ID URL\" is not valid \"uri\"") require.Nil(t, vp) }) } @@ -402,7 +402,7 @@ func TestValidateVP_Type(t *testing.T) { require.NoError(t, err) vp, err := newTestPresentation(t, bytes) require.Error(t, err) - require.Contains(t, err.Error(), "type is required") + require.Contains(t, err.Error(), "missing properties: \"type\"") require.Nil(t, vp) }) @@ -414,7 +414,7 @@ func TestValidateVP_Type(t *testing.T) { require.NoError(t, err) vp, err := newTestPresentation(t, bytes) require.Error(t, err) - require.Contains(t, err.Error(), "Does not match pattern '^VerifiablePresentation$'") + require.Contains(t, err.Error(), "does not match pattern \"^VerifiablePresentation$\"") require.Nil(t, vp) }) } @@ -428,7 +428,7 @@ func TestValidateVP_Holder(t *testing.T) { require.NoError(t, err) vp, err := newTestPresentation(t, bytes) require.Error(t, err) - require.Contains(t, err.Error(), "holder: Does not match format 'uri'") + require.Contains(t, err.Error(), "not valid presentation Holder URL\" is not valid \"uri\"") require.Nil(t, vp) }) }