Skip to content

Commit

Permalink
embed and preload w3c cred (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
Allyson-English authored Nov 20, 2023
1 parent 1260c0c commit 7eb6a65
Show file tree
Hide file tree
Showing 3 changed files with 335 additions and 2 deletions.
29 changes: 28 additions & 1 deletion internal/services/issuer/verifiable_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"crypto/ecdsa"
"crypto/rand"
"crypto/sha256"
_ "embed" //nolint
"encoding/base64"
"encoding/json"
"fmt"
"math/big"
"os"
"time"

"github.com/btcsuite/btcd/btcutil/base58"
Expand All @@ -28,6 +30,9 @@ import (
var secp256k1Prefix = []byte{0xe7, 0x01}
var period byte = '.'

//go:embed w3c_2018_credentials_v1.json
var w3c2018CredentialsV1 string

type Config struct {
PrivateKey []byte
ChainID *big.Int
Expand Down Expand Up @@ -60,8 +65,31 @@ func New(c Config, log *zerolog.Logger) (*Issuer, error) {

ldProc := ld.NewJsonLdProcessor()
options := ld.NewJsonLdOptions("")

rfcDocLoader := ld.NewRFC7324CachingDocumentLoader(nil)
dl := ld.NewCachingDocumentLoader(rfcDocLoader)

f, err := os.CreateTemp("", "")
if err != nil {
return nil, err
}
if _, err := f.Write([]byte(w3c2018CredentialsV1)); err != nil {
return nil, err
}
if err := f.Close(); err != nil {
return nil, err
}

err = dl.PreloadWithMapping(map[string]string{
"https://www.w3.org/2018/credentials/v1": f.Name(),
})
if err != nil {
return nil, err
}

options.Format = "application/n-quads"
options.Algorithm = ld.AlgorithmURDNA2015
options.DocumentLoader = dl

return &Issuer{
PrivateKey: privateKey,
Expand All @@ -85,7 +113,6 @@ func (i *Issuer) VIN(vin string, tokenID *big.Int, expirationDate time.Time) (id
credential := map[string]any{
"@context": []any{
"https://www.w3.org/2018/credentials/v1",
"https://schema.org/",
},
"id": "urn:uuid:" + id,
"type": []any{"VerifiableCredential", "Vehicle"},
Expand Down
1 change: 0 additions & 1 deletion internal/services/issuer/verifiable_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func TestCredentialTestSuite(t *testing.T) {
}

func (s *CredentialTestSuite) TestVerifiableCredential() {
s.T().Skip("Isolate this test from the network before putting it in CI.")
ctx := context.Background()
vin := "1G6AL1RY2K0111939"
tokenID := big.NewInt(3)
Expand Down
307 changes: 307 additions & 0 deletions internal/services/issuer/w3c_2018_credentials_v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
{
"@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"
}
}
}

0 comments on commit 7eb6a65

Please sign in to comment.