Skip to content
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

add two possible solutions #79

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions json/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package json
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"testing"

core "github.com/iden3/go-iden3-core/v2"
"github.com/iden3/go-merkletree-sql/v2"
"github.com/iden3/go-schema-processor/v2/merklize"
"github.com/iden3/go-schema-processor/v2/processor"
tst "github.com/iden3/go-schema-processor/v2/testing"
Expand All @@ -17,6 +19,7 @@ import (
)

func TestParser_parseSlots(t *testing.T) {
fmt.Println(merkletree.HashZero.Hex())
defer tst.MockHTTPClient(t,
map[string]string{
"https://www.w3.org/2018/credentials/v1": "../merklize/testdata/httpresp/credentials-v1.jsonld",
Expand Down
247 changes: 247 additions & 0 deletions loaders/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
package loaders

// W3CCredential2018ContextURL is w3c context url
//
//nolint:golint,gosec
const W3CCredential2018ContextURL = "https://www.w3.org/2018/credentials/v1"

// W3CCredential2018ContextDocument is w3c context file
//
//nolint:golint,gosec
const W3CCredential2018ContextDocument string = `{
"@context": {
"@version": 1.1,
"@protected": true,

"id": "@id",
"type": "@type",

"VerifiableCredential": {
"@id": "https://www.w3.org/2018/credentials#VerifiableCredential",
"@context": {
"@version": 1.1,
"@protected": true,

"id": "@id",
"type": "@type",

"cred": "https://www.w3.org/2018/credentials#",
"sec": "https://w3id.org/security#",
"xsd": "http://www.w3.org/2001/XMLSchema#",

"credentialSchema": {
"@id": "cred:credentialSchema",
"@type": "@id",
"@context": {
"@version": 1.1,
"@protected": true,

"id": "@id",
"type": "@type",

"cred": "https://www.w3.org/2018/credentials#",

"JsonSchemaValidator2018": "cred:JsonSchemaValidator2018"
}
},
"credentialStatus": {"@id": "cred:credentialStatus", "@type": "@id"},
"credentialSubject": {"@id": "cred:credentialSubject", "@type": "@id"},
"evidence": {"@id": "cred:evidence", "@type": "@id"},
"expirationDate": {"@id": "cred:expirationDate", "@type": "xsd:dateTime"},
"holder": {"@id": "cred:holder", "@type": "@id"},
"issued": {"@id": "cred:issued", "@type": "xsd:dateTime"},
"issuer": {"@id": "cred:issuer", "@type": "@id"},
"issuanceDate": {"@id": "cred:issuanceDate", "@type": "xsd:dateTime"},
"proof": {"@id": "sec:proof", "@type": "@id", "@container": "@graph"},
"refreshService": {
"@id": "cred:refreshService",
"@type": "@id",
"@context": {
"@version": 1.1,
"@protected": true,

"id": "@id",
"type": "@type",

"cred": "https://www.w3.org/2018/credentials#",

"ManualRefreshService2018": "cred:ManualRefreshService2018"
}
},
"termsOfUse": {"@id": "cred:termsOfUse", "@type": "@id"},
"validFrom": {"@id": "cred:validFrom", "@type": "xsd:dateTime"},
"validUntil": {"@id": "cred:validUntil", "@type": "xsd:dateTime"}
}
},

"VerifiablePresentation": {
"@id": "https://www.w3.org/2018/credentials#VerifiablePresentation",
"@context": {
"@version": 1.1,
"@protected": true,

"id": "@id",
"type": "@type",

"cred": "https://www.w3.org/2018/credentials#",
"sec": "https://w3id.org/security#",

"holder": {"@id": "cred:holder", "@type": "@id"},
"proof": {"@id": "sec:proof", "@type": "@id", "@container": "@graph"},
"verifiableCredential": {"@id": "cred:verifiableCredential", "@type": "@id", "@container": "@graph"}
}
},

"EcdsaSecp256k1Signature2019": {
"@id": "https://w3id.org/security#EcdsaSecp256k1Signature2019",
"@context": {
"@version": 1.1,
"@protected": true,

"id": "@id",
"type": "@type",

"sec": "https://w3id.org/security#",
"xsd": "http://www.w3.org/2001/XMLSchema#",

"challenge": "sec:challenge",
"created": {"@id": "http://purl.org/dc/terms/created", "@type": "xsd:dateTime"},
"domain": "sec:domain",
"expires": {"@id": "sec:expiration", "@type": "xsd:dateTime"},
"jws": "sec:jws",
"nonce": "sec:nonce",
"proofPurpose": {
"@id": "sec:proofPurpose",
"@type": "@vocab",
"@context": {
"@version": 1.1,
"@protected": true,

"id": "@id",
"type": "@type",

"sec": "https://w3id.org/security#",

"assertionMethod": {"@id": "sec:assertionMethod", "@type": "@id", "@container": "@set"},
"authentication": {"@id": "sec:authenticationMethod", "@type": "@id", "@container": "@set"}
}
},
"proofValue": "sec:proofValue",
"verificationMethod": {"@id": "sec:verificationMethod", "@type": "@id"}
}
},

"EcdsaSecp256r1Signature2019": {
"@id": "https://w3id.org/security#EcdsaSecp256r1Signature2019",
"@context": {
"@version": 1.1,
"@protected": true,

"id": "@id",
"type": "@type",

"sec": "https://w3id.org/security#",
"xsd": "http://www.w3.org/2001/XMLSchema#",

"challenge": "sec:challenge",
"created": {"@id": "http://purl.org/dc/terms/created", "@type": "xsd:dateTime"},
"domain": "sec:domain",
"expires": {"@id": "sec:expiration", "@type": "xsd:dateTime"},
"jws": "sec:jws",
"nonce": "sec:nonce",
"proofPurpose": {
"@id": "sec:proofPurpose",
"@type": "@vocab",
"@context": {
"@version": 1.1,
"@protected": true,

"id": "@id",
"type": "@type",

"sec": "https://w3id.org/security#",

"assertionMethod": {"@id": "sec:assertionMethod", "@type": "@id", "@container": "@set"},
"authentication": {"@id": "sec:authenticationMethod", "@type": "@id", "@container": "@set"}
}
},
"proofValue": "sec:proofValue",
"verificationMethod": {"@id": "sec:verificationMethod", "@type": "@id"}
}
},

"Ed25519Signature2018": {
"@id": "https://w3id.org/security#Ed25519Signature2018",
"@context": {
"@version": 1.1,
"@protected": true,

"id": "@id",
"type": "@type",

"sec": "https://w3id.org/security#",
"xsd": "http://www.w3.org/2001/XMLSchema#",

"challenge": "sec:challenge",
"created": {"@id": "http://purl.org/dc/terms/created", "@type": "xsd:dateTime"},
"domain": "sec:domain",
"expires": {"@id": "sec:expiration", "@type": "xsd:dateTime"},
"jws": "sec:jws",
"nonce": "sec:nonce",
"proofPurpose": {
"@id": "sec:proofPurpose",
"@type": "@vocab",
"@context": {
"@version": 1.1,
"@protected": true,

"id": "@id",
"type": "@type",

"sec": "https://w3id.org/security#",

"assertionMethod": {"@id": "sec:assertionMethod", "@type": "@id", "@container": "@set"},
"authentication": {"@id": "sec:authenticationMethod", "@type": "@id", "@container": "@set"}
}
},
"proofValue": "sec:proofValue",
"verificationMethod": {"@id": "sec:verificationMethod", "@type": "@id"}
}
},

"RsaSignature2018": {
"@id": "https://w3id.org/security#RsaSignature2018",
"@context": {
"@version": 1.1,
"@protected": true,

"challenge": "sec:challenge",
"created": {"@id": "http://purl.org/dc/terms/created", "@type": "xsd:dateTime"},
"domain": "sec:domain",
"expires": {"@id": "sec:expiration", "@type": "xsd:dateTime"},
"jws": "sec:jws",
"nonce": "sec:nonce",
"proofPurpose": {
"@id": "sec:proofPurpose",
"@type": "@vocab",
"@context": {
"@version": 1.1,
"@protected": true,

"id": "@id",
"type": "@type",

"sec": "https://w3id.org/security#",

"assertionMethod": {"@id": "sec:assertionMethod", "@type": "@id", "@container": "@set"},
"authentication": {"@id": "sec:authenticationMethod", "@type": "@id", "@container": "@set"}
}
},
"proofValue": "sec:proofValue",
"verificationMethod": {"@id": "sec:verificationMethod", "@type": "@id"}
}
},

"proof": {"@id": "https://w3id.org/security#proof", "@type": "@id", "@container": "@graph"}
}
}`
26 changes: 26 additions & 0 deletions loaders/document_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type documentLoader struct {
httpLoader *ld.RFC7324CachingDocumentLoader
ipfsCli *shell.Shell
ipfsGW string

localCacheFn func(url string) (io.Reader, error)
}

// NewDocumentLoader creates a new document loader with a cache for http.
Expand All @@ -28,6 +30,25 @@ func NewDocumentLoader(ipfsCli *shell.Shell, ipfsGW string) ld.DocumentLoader {
func (d *documentLoader) LoadDocument(
u string) (doc *ld.RemoteDocument, err error) {

if d.localCacheFn != nil {
var r io.Reader
r, err = d.localCacheFn(u)
if err != nil {
return nil, err
}
if r != nil {
var docFromReader interface{}
docFromReader, err = ld.DocumentFromReader(r)
if err == nil {
return nil, err
}
return &ld.RemoteDocument{
DocumentURL: u,
Document: docFromReader,
ContextURL: u,
}, nil
}
}
const ipfsPrefix = "ipfs://"

switch {
Expand Down Expand Up @@ -65,6 +86,11 @@ func (d *documentLoader) LoadDocument(
}
}

func (d *documentLoader) SetLocalReader(
f func(url string) (io.Reader, error)) {
d.localCacheFn = f
}

func (d *documentLoader) loadDocumentFromIPFSNode(
ipfsURL string) (document any, err error) {

Expand Down
41 changes: 41 additions & 0 deletions loaders/w3c_loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package loaders

import (
"net/http"
"strings"

shell "github.com/ipfs/go-ipfs-api"
"github.com/piprate/json-gold/ld"
)

type W3CDocumentLoader struct {
documentLoader
}

// NewW3CDocumentLoader creates a new document loader with a predefined http schema
func NewW3CDocumentLoader(ipfsCli *shell.Shell, ipfsGW string, httpClient *http.Client) ld.DocumentLoader {
return &W3CDocumentLoader{
documentLoader: documentLoader{
httpLoader: ld.NewRFC7324CachingDocumentLoader(httpClient),
ipfsCli: ipfsCli,
ipfsGW: ipfsGW,
},
}
}

func (d *W3CDocumentLoader) LoadDocument(
u string) (doc *ld.RemoteDocument, err error) {

if u == W3CCredential2018ContextURL {
w3cDoc, errIn := ld.DocumentFromReader(strings.NewReader(W3CCredential2018ContextDocument))
if errIn != nil {
return nil, errIn
}
return &ld.RemoteDocument{
DocumentURL: u,
Document: w3cDoc,
ContextURL: u,
}, nil
}
return d.LoadDocument(u)
}
33 changes: 33 additions & 0 deletions loaders/w3c_loader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package loaders_test

import (
"net/http"
"testing"

"github.com/iden3/go-schema-processor/v2/loaders"
"github.com/stretchr/testify/require"
)

func TestW3CLoader_JSONUnmarshal(t *testing.T) {

w3cLoader := loaders.NewW3CDocumentLoader(nil, "https://ipfs.io", createDisabledHTTPClient())
doc, err := w3cLoader.LoadDocument(loaders.W3CCredential2018ContextURL)
require.NoError(t, err)

require.NotNil(t, (doc.Document.(map[string]interface{}))["@context"])
}

type DisableHTTPTransport struct{}

func (t *DisableHTTPTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// Do nothing and return a dummy response
return &http.Response{
StatusCode: http.StatusNotImplemented, // You can choose any status code
Body: nil,
}, nil
}
func createDisabledHTTPClient() *http.Client {
return &http.Client{
Transport: &DisableHTTPTransport{},
}
}
Loading