Skip to content

Commit

Permalink
update test using real data (#216)
Browse files Browse the repository at this point in the history
* update test using real data

* updating

* error handling in marshal

Co-authored-by: Neal Roessler <nealr@nealr-macbookpro.local>
Co-authored-by: Gabe <gcohen@squareup.com>
  • Loading branch information
3 people committed Oct 12, 2022
1 parent 619481b commit d911e62
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 69 deletions.
124 changes: 55 additions & 69 deletions example/usecase/steel_thread/steel_thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
package main

import (
"embed"
"encoding/json"
"fmt"
"github.com/TBD54566975/ssi-sdk/credential"
"github.com/TBD54566975/ssi-sdk/credential/exchange"
"github.com/TBD54566975/ssi-sdk/credential/manifest"
"github.com/TBD54566975/ssi-sdk/crypto"
"github.com/TBD54566975/ssi-sdk/did"
"github.com/TBD54566975/ssi-sdk/example"
"github.com/google/uuid"
"github.com/lestrrat-go/jwx/jwk"
)

Expand All @@ -32,6 +33,11 @@ type Entity struct {
verifiableCredentials []credential.VerifiableCredential
}

var (
//go:embed testdata
exampleFS embed.FS
)

func (t *Entity) GenerateWallet() {
walletDIDPrivateKey, walletDIDKey, err := did.GenerateDIDKey(crypto.Ed25519)
example.HandleExampleError(err, "Failed to generate DID")
Expand Down Expand Up @@ -235,86 +241,66 @@ func main() {
aliceWalletEntity.FlexFullyValidatedCredentials()
}

func getFileBytes(filename string) []byte {
caBytes, err := exampleFS.ReadFile(filename)

if err != nil {
example.HandleExampleError(err, "can not open file")
}

return caBytes
}

func createCredentialApplication(cm manifest.CredentialManifest) manifest.CredentialApplication {
return manifest.CredentialApplication{
ID: uuid.New().String(),
SpecVersion: "https://identity.foundation/credential-manifest/spec/v1.0.0/",
ManifestID: cm.ID,
Format: &exchange.ClaimFormat{
JWT: &exchange.JWTType{Alg: []crypto.SignatureAlgorithm{crypto.EdDSA}},
},
PresentationSubmission: &exchange.PresentationSubmission{
ID: "psid",
DefinitionID: "definitionId",
DescriptorMap: []exchange.SubmissionDescriptor{
{
ID: "ps-id",
Format: "jwt",
Path: "path",
},
},
},
caBytes := getFileBytes("testdata/ca.json")

var credApp manifest.CredentialApplication
if err := json.Unmarshal(caBytes, &credApp); err != nil {
example.HandleExampleError(err, "problem unmarshalling credential application")
}

credApp.ManifestID = cm.ID

return credApp
}

func createVerifiableCredential(issuerDID string, walletDID string, descriptor manifest.OutputDescriptor) credential.VerifiableCredential {
knownIssuanceDate := "2020-01-01T19:23:24Z"
knownSubject := map[string]interface{}{
"id": string(walletDID),
"birthdate": "1975-01-01",
vcBytes := getFileBytes("testdata/vc.json")

var vc credential.VerifiableCredential
if err := json.Unmarshal(vcBytes, &vc); err != nil {
example.HandleExampleError(err, "problem unmarshalling verifiable credential")
}

vcBuilder := credential.NewVerifiableCredentialBuilder()
credSubject := vc.CredentialSubject
credSubject["id"] = walletDID

err := vcBuilder.SetIssuer(issuerDID)
example.HandleExampleError(err, "Failed to set issuer")
err = vcBuilder.SetIssuanceDate(knownIssuanceDate)
example.HandleExampleError(err, "Failed to set issuance date")
err = vcBuilder.SetCredentialSubject(knownSubject)
example.HandleExampleError(err, "Failed to set subject")
builder := credential.NewVerifiableCredentialBuilder()
builder.SetIssuer(issuerDID)
builder.SetCredentialSubject(credSubject)
builder.SetCredentialSchema(*vc.CredentialSchema)
builder.SetIssuanceDate(vc.IssuanceDate)
builder.SetCredentialStatus(vc.CredentialStatus)
builder.SetEvidence(vc.Evidence)
builder.SetExpirationDate(vc.ExpirationDate)

vc, err := vcBuilder.Build()
example.HandleExampleError(err, "Failed to make verifiable credential")
example.HandleExampleError(vc.IsValid(), "Verifiable credential is not valid")
builderVC, err := builder.Build()
if err != nil {
example.HandleExampleError(err, "could not build verifiable credential")
}

return *vc
return *builderVC
}

func createCredentialManifest(issuer string) manifest.CredentialManifest {
return manifest.CredentialManifest{
ID: "WA-DL-CLASS-A",
SpecVersion: "https://identity.foundation/credential-manifest/spec/v1.0.0/",
Issuer: manifest.Issuer{
ID: issuer,
},
PresentationDefinition: &exchange.PresentationDefinition{
ID: "pres-def-id",
InputDescriptors: []exchange.InputDescriptor{
{
ID: "test-id",
Constraints: &exchange.Constraints{
Fields: []exchange.Field{
{
Path: []string{".vc.id"},
},
},
},
},
},
},
OutputDescriptors: []manifest.OutputDescriptor{
{
ID: "id1",
Schema: "https://test.com/schema",
Name: "good ID",
Description: "it's all good",
},
{
ID: "id2",
Schema: "https://test.com/schema",
Name: "good ID",
Description: "it's all good",
},
},
cmBytes := getFileBytes("testdata/cm.json")

var mfst manifest.CredentialManifest
if err := json.Unmarshal(cmBytes, &mfst); err != nil {
example.HandleExampleError(err, "problem unmarshalling credential manifest")
}

mfst.Issuer.ID = issuer

return mfst
}
26 changes: 26 additions & 0 deletions example/usecase/steel_thread/testdata/ca.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"id": "c0c6e312-ad44-4f18-935e-a6efaad91612",
"spec_version": "https://identity.foundation/credential-manifest/spec/v1.0.0/",
"manifest_id": "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d",
"format": {
"jwt_vc": {
"alg": [
"EdDSA"
]
}
},
"presentation_submission": {
"id": "00239c16-3e64-4438-8dda-641e963fa853",
"definition_id": "32f54163-7166-48f1-93d8-ff217bdb0653",
"descriptor_map": [
{
"id": "kyc1",
"format": "jwt_vc",
"path": "$.verifiableCredential[0]"
}
]
},
"verifiableCredential": [
"eyJraWQiOiJkaWQ6amFua3k6YWxpY2UjZGlkOmphbmt5OmFsaWNlIiwiYWxnIjoiRWREU0EifQ.eyJpc3MiOiJkaWQ6amFua3k6YWxpY2UiLCJzdWIiOiJkaWQ6amFua3k6YWxpY2UiLCJ2YyI6eyJAY29udGV4dCI6WyJodHRwczpcL1wvd3d3LnczLm9yZ1wvMjAxOFwvY3JlZGVudGlhbHNcL3YxIl0sInR5cGUiOlsiS3ljQ3JlZGVudGlhbCJdLCJpc3N1ZXIiOiJkaWQ6amFua3k6YWxpY2UiLCJpc3N1YW5jZURhdGUiOiIyMDIyLTA5LTI5VDAwOjAwOjAwWiIsImNyZWRlbnRpYWxTY2hlbWEiOnsiaWQiOiJodHRwczpcL1wvY29tcGxpYW5jZS1pcy1rZXdsLmNvbVwvanNvbi1zY2hlbWFzXC9reWMuanNvbiIsInR5cGUiOiJKc29uU2NoZW1hVmFsaWRhdG9yMjAxOCJ9LCJjcmVkZW50aWFsU3ViamVjdCI6eyJpZCI6ImRpZDpqYW5reTphbGljZSIsImdpdmVuTmFtZSI6IlJhbmR5IiwiYWRkaXRpb25hbE5hbWUiOiJOXC9BIiwiZmFtaWx5TmFtZSI6Ik1jSmFua3kiLCJiaXJ0aERhdGUiOiIxOTg4LTAzLTI4IiwicG9zdGFsQWRkcmVzcyI6eyJhZGRyZXNzQ291bnRyeSI6IlVTQSIsImFkZHJlc3NMb2NhbGl0eSI6IkF1c3RpbiIsImFkZHJlc3NSZWdpb24iOiJUWCIsInBvc3RhbENvZGUiOiI3ODcyNCIsInN0cmVldEFkZHJlc3MiOiIxMjMgSmFua3RvcGlhIEF2ZS4ifSwidGF4SUQiOiIxMjMtNDUtNjc4OSJ9fX0.OIga_ix7x4Tk4fEThfu_akXCVuI0aZ770CSSztSJ1NkBiGm4V_NOSQRk0EOo3heh6F6LiK9g9sa_527rNH_wCw"
]
}
104 changes: 104 additions & 0 deletions example/usecase/steel_thread/testdata/cm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"id": "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d",
"issuer": {
"id": "did:example:134"
},
"spec_version": "https://identity.foundation/credential-manifest/spec/v1.0.0/",
"output_descriptors": [
{
"id": "kyc_credential",
"schema": "https://compliance-is-kewl.com/json-schemas/kyc.json"
}
],
"presentation_definition": {
"id": "32f54163-7166-48f1-93d8-ff217bdb0653",
"name": "KYC Requirements",
"purpose": "TBD. i donno rn",
"format": {
"jwt": {
"alg": [
"EdDSA"
]
}
},
"input_descriptors": [
{
"id": "kyc1",
"name": "Personal Info",
"constraints": {
"subject_is_issuer": "required",
"fields": [
{
"id": "kycSchema",
"path": [
"$.vc.credentialSchema.id"
],
"filter": {
"type": "string",
"const": "https://compliance-is-kewl.com/json-schemas/kyc.json"
}
},
{
"id": "givenName",
"path": [
"$.vc.credentialSubject.givenName"
],
"filter": {
"type": "string",
"pattern": "[a-zA-Z \\-\\.].+"
}
},
{
"id": "additionalName",
"path": [
"$.vc.credentialSubject.additionalName"
],
"filter": {
"type": "string",
"pattern": "[a-zA-Z \\-\\.].+"
}
},
{
"id": "familyName",
"path": [
"$.vc.credentialSubject.familyName"
],
"filter": {
"type": "string",
"pattern": "[a-zA-Z \\-\\.].+"
}
},
{
"id": "birthDate",
"path": [
"$.vc.credentialSubject.birthDate"
],
"filter": {
"type": "string",
"format": "date"
}
},
{
"id": "postalAddress",
"path": [
"$.vc.credentialSubject.postalAddress"
],
"filter": {
"type": "string"
}
},
{
"id": "taxID",
"path": [
"$.vc.credentialSubject.taxID"
],
"filter": {
"type": "string"
}
}
]
}
}
]
}
}
30 changes: 30 additions & 0 deletions example/usecase/steel_thread/testdata/vc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{

"id" : "vcid",
"@context": ["https://www.w3.org/2018/credentials/v1"],
"type": ["KycCredential"],
"issuer": "did:janky:alice",
"issuanceDate": "2022-09-29T00:00:00Z",
"credentialSchema": {
"id": "https://compliance-is-kewl.com/json-schemas/kyc.json",
"type": "JsonSchemaValidator2018"
},
"credentialSubject": {
"id": "did:janky:alice",
"givenName": "Randy",
"additionalName": "N/A",
"familyName": "McJanky",
"birthDate": "1988-03-28",
"postalAddress": {
"addressCountry": "USA",
"addressLocality": "Austin",
"addressRegion": "TX",
"postalCode": "78724",
"streetAddress": "7405 Janktopia Ave."
},
"taxID": "123-45-6789"
},

"iss": "did:janky:alice",
"sub": "did:janky:alice"
}

0 comments on commit d911e62

Please sign in to comment.