Skip to content

Commit dc8f80e

Browse files
Merge pull request #1 from TBD54566975/fun-with-js-files
structure for test runners
2 parents da9de52 + 6a0e285 commit dc8f80e

40 files changed

+2241
-568
lines changed

.eslintrc.cjs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module.exports = {
2+
'env': {
3+
'node': true,
4+
},
5+
'extends': [
6+
'google',
7+
'plugin:react/recommended',
8+
],
9+
'overrides': [
10+
{
11+
'env': {
12+
'node': true,
13+
},
14+
'files': [
15+
'.eslintrc.{js,cjs}',
16+
],
17+
'parserOptions': {
18+
'sourceType': 'script',
19+
},
20+
},
21+
],
22+
'parserOptions': {
23+
'ecmaVersion': 'latest',
24+
'sourceType': 'module',
25+
},
26+
'plugins': [
27+
'react',
28+
],
29+
'rules': {
30+
'no-invalid-this': 'off',
31+
'max-len': [
32+
'error',
33+
{
34+
'code': 100,
35+
'tabWidth': 2,
36+
'ignoreComments': true, // "comments": 80
37+
'ignoreUrls': true,
38+
'ignoreStrings': true,
39+
'ignoreTemplateLiterals': true,
40+
},
41+
],
42+
},
43+
};

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# IDE
22
.idea
33

4+
# Reports
5+
reports/*.html
6+
7+
# Output
8+
tests/output/*/*/*.json
9+
410
# Logs
511
logs
612
*.log

implementations/docker-compose.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: "3.9"
2+
services:
3+
tbd:
4+
image: tbd/vc-json-schema-test-suite
5+
build:
6+
context: ./tbd
7+
dockerfile: Dockerfile
8+
volumes:
9+
- ../tests:/tests

implementations/index.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
1-
import fs from 'fs';
2-
import path from 'path';
1+
import {createRequire} from 'node:module';
32

4-
const jsonsInDir = fs.readdirSync('./').filter(file => path.extname(file) === '.json');
3+
const require = createRequire(import.meta.url);
4+
const requireDir = require('require-dir');
5+
const dir = requireDir('./');
56

6-
const implementations = jsonsInDir.map(file => {
7-
const fileData = fs.readFileSync(path.join('./', file));
8-
return JSON.parse(fileData.toString());
9-
});
7+
export const implementations = Object.values(dir);
8+
9+
export const JsonSchemaVersions = {
10+
202012: '2020-12',
11+
201909: '2019-09',
12+
Draft7: 'Draft-7',
13+
};
14+
15+
export const VcJsonSchemaTypes = {
16+
JsonSchema: 'JsonSchema',
17+
JsonSchemaCredential: 'JsonSchemaCredential',
18+
};
19+
20+
export const implementationsWhichSupportVersionAndType = ({
21+
impls = implementations,
22+
version,
23+
type,
24+
}) => {
25+
const matchingImpls = [];
26+
for (const i of impls) {
27+
if (Object.keys(i.specs).includes(version) && i.specs[version].includes(type)) {
28+
matchingImpls.push(i);
29+
}
30+
}
31+
return matchingImpls;
32+
};

implementations/sample.json renamed to implementations/tbd.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "test impl",
2+
"name": "tbd",
33
"specs": {
44
"2020-12": [
55
"JsonSchema",

implementations/tbd/Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM golang:1.21-alpine
2+
3+
# Set destination for COPY
4+
WORKDIR /app
5+
6+
# Download Go modules
7+
COPY go.mod .
8+
COPY go.sum .
9+
RUN go mod download
10+
11+
# Copy the source code
12+
COPY *.go ./
13+
14+
# Build executable
15+
RUN go build -tags jwx_es256k -o /tbd .
16+
17+
ENTRYPOINT ["/tbd"]

implementations/tbd/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# TBD's VC JSON Schema Implementation
2+
3+
## Local
4+
### Building
5+
6+
Assuming you have [go](https://go.dev/) installed...
7+
8+
```bash
9+
go build ./...
10+
```
11+
12+
Which will produce a binary called `vc-json-schema-test-suite`
13+
14+
### Running
15+
16+
Select format, schema, credential, and output files and run as follows:
17+
18+
```bash
19+
./vc-json-schema-test-suite validate --format JsonSchema --schema ../../tests/input/test-1-schema.json --credential ../../tests/input/test-1-credential.json --output ../../tests/output/tests-1-output.json```
20+
```
21+
22+
## Docker
23+
### Building
24+
25+
### Running

implementations/tbd/cli.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
)
8+
9+
const (
10+
JSONSchemaType = "JsonSchema"
11+
JSONSchemaCredentialType = "JsonSchemaCredential"
12+
)
13+
14+
func main() {
15+
if len(os.Args) < 3 {
16+
panic("must supply [format, schema, credential, output] arguments")
17+
}
18+
19+
var schema, format, credential, output string
20+
21+
createCmd := flag.NewFlagSet("validate", flag.ExitOnError)
22+
createCmd.StringVar(&format, "format", "", "schema format")
23+
createCmd.StringVar(&schema, "schema", "", "schema schema file")
24+
createCmd.StringVar(&credential, "credential", "", "credential schema file")
25+
createCmd.StringVar(&output, "output", "", "output file")
26+
27+
switch os.Args[1] {
28+
case "validate":
29+
if err := createCmd.Parse(os.Args[2:]); err != nil {
30+
fmt.Printf("error running create: %s\n", err.Error())
31+
os.Exit(1)
32+
}
33+
fmt.Printf("flags parsed: format=%s, schema=%s, credential=%s, output=%s\n", format, schema, credential, output)
34+
validateFlags(format, schema, credential, output)
35+
if err := ValidateCredentialAgainstSchema(format, schema, credential, output); err != nil {
36+
fmt.Printf("error validating using schema %s: %s\n", schema, err.Error())
37+
os.Exit(1)
38+
}
39+
fmt.Println("credential validated; output written to file")
40+
default:
41+
fmt.Println("expected 'validate' command")
42+
os.Exit(1)
43+
}
44+
}
45+
46+
func validateFlags(format, schema, credential, output string) {
47+
if format == "" {
48+
fmt.Println("no format specified")
49+
os.Exit(1)
50+
}
51+
if format != JSONSchemaType && format != JSONSchemaCredentialType {
52+
fmt.Println("invalid format specified")
53+
os.Exit(1)
54+
}
55+
if schema == "" {
56+
fmt.Println("no schema file specified")
57+
os.Exit(1)
58+
}
59+
if credential == "" {
60+
fmt.Println("no credential file specified")
61+
os.Exit(1)
62+
}
63+
if output == "" {
64+
fmt.Println("no output file specified")
65+
}
66+
}

implementations/tbd/file.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
7+
"github.com/TBD54566975/ssi-sdk/credential"
8+
"github.com/TBD54566975/ssi-sdk/credential/schema"
9+
"github.com/pkg/errors"
10+
)
11+
12+
func getCredentialFromFile(filePath string) (*credential.VerifiableCredential, error) {
13+
bytes, err := os.ReadFile(filePath)
14+
if err != nil {
15+
return nil, errors.Wrapf(err, "could not read credential from file: %s", filePath)
16+
}
17+
var cred credential.VerifiableCredential
18+
if err = json.Unmarshal(bytes, &cred); err != nil {
19+
return nil, errors.Wrap(err, "could not unmarshal credential")
20+
}
21+
return &cred, nil
22+
}
23+
24+
func getSchemaFromFile(format, filePath string) (*schema.JSONSchema, error) {
25+
var s schema.JSONSchema
26+
if format == JSONSchemaCredentialType {
27+
schemaCred, err := getCredentialFromFile(filePath)
28+
if err != nil {
29+
return nil, errors.Wrap(err, "could not get schema credential from file")
30+
}
31+
jsonSchema, ok := schemaCred.CredentialSubject[credential.VerifiableCredentialJSONSchemaProperty]
32+
if !ok {
33+
return nil, errors.New("credential does not contain json schema")
34+
}
35+
s = jsonSchema.(map[string]any)
36+
} else {
37+
bytes, err := os.ReadFile(filePath)
38+
if err != nil {
39+
return nil, errors.Wrapf(err, "could not read vp from file: %s", filePath)
40+
}
41+
if err = json.Unmarshal(bytes, &s); err != nil {
42+
return nil, errors.Wrap(err, "could not unmarshal schema")
43+
}
44+
}
45+
return &s, nil
46+
}
47+
48+
func writeValidationResult(result ValidationResult, filePath string) error {
49+
data, err := json.MarshalIndent(validationResult{Result: result}, "", " ")
50+
if err != nil {
51+
return err
52+
}
53+
return writeOutputToFile(data, filePath)
54+
}
55+
56+
type ValidationResult string
57+
58+
const (
59+
Success ValidationResult = "success"
60+
Failure ValidationResult = "failure"
61+
Indeterminate ValidationResult = "indeterminate"
62+
)
63+
64+
type validationResult struct {
65+
Result ValidationResult `json:"result"`
66+
}
67+
68+
func writeOutputToFile(data []byte, filePath string) error {
69+
if err := os.WriteFile(filePath, data, 0755); err != nil {
70+
return errors.Wrapf(err, "could not write %d bytes to file: %s", len(data), filePath)
71+
}
72+
return nil
73+
}

implementations/tbd/go.mod

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module github.com/TBD54566975/vc-json-schema-test-suite
2+
3+
go 1.21
4+
5+
require (
6+
github.com/TBD54566975/ssi-sdk v0.0.4-alpha.0.20230818212001-6e1043316e75
7+
github.com/pkg/errors v0.9.1
8+
)
9+
10+
require (
11+
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
12+
github.com/cloudflare/circl v1.3.3 // indirect
13+
github.com/davecgh/go-spew v1.1.1 // indirect
14+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
15+
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
16+
github.com/go-playground/locales v0.14.1 // indirect
17+
github.com/go-playground/universal-translator v0.18.1 // indirect
18+
github.com/go-playground/validator/v10 v10.15.0 // indirect
19+
github.com/goccy/go-json v0.10.2 // indirect
20+
github.com/google/uuid v1.3.0 // indirect
21+
github.com/hyperledger/aries-framework-go v0.3.2 // indirect
22+
github.com/hyperledger/aries-framework-go/component/kmscrypto v0.0.0-20230427134832-0c9969493bd3 // indirect
23+
github.com/hyperledger/aries-framework-go/component/log v0.0.0-20230427134832-0c9969493bd3 // indirect
24+
github.com/hyperledger/aries-framework-go/component/models v0.0.0-20230501135648-a9a7ad029347 // indirect
25+
github.com/hyperledger/aries-framework-go/spi v0.0.0-20230427134832-0c9969493bd3 // indirect
26+
github.com/kilic/bls12-381 v0.1.1-0.20210503002446-7b7597926c69 // indirect
27+
github.com/leodido/go-urn v1.2.4 // indirect
28+
github.com/lestrrat-go/blackmagic v1.0.1 // indirect
29+
github.com/lestrrat-go/httpcc v1.0.1 // indirect
30+
github.com/lestrrat-go/httprc v1.0.4 // indirect
31+
github.com/lestrrat-go/iter v1.0.2 // indirect
32+
github.com/lestrrat-go/jwx/v2 v2.0.12 // indirect
33+
github.com/lestrrat-go/option v1.0.1 // indirect
34+
github.com/mr-tron/base58 v1.2.0 // indirect
35+
github.com/multiformats/go-base32 v0.1.0 // indirect
36+
github.com/multiformats/go-base36 v0.1.0 // indirect
37+
github.com/multiformats/go-multibase v0.2.0 // indirect
38+
github.com/multiformats/go-multicodec v0.9.0 // indirect
39+
github.com/multiformats/go-varint v0.0.7 // indirect
40+
github.com/piprate/json-gold v0.5.0 // indirect
41+
github.com/pmezard/go-difflib v1.0.0 // indirect
42+
github.com/pquerna/cachecontrol v0.1.0 // indirect
43+
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 // indirect
44+
github.com/segmentio/asm v1.2.0 // indirect
45+
github.com/sirupsen/logrus v1.9.3 // indirect
46+
github.com/stretchr/testify v1.8.4 // indirect
47+
golang.org/x/crypto v0.12.0 // indirect
48+
golang.org/x/net v0.10.0 // indirect
49+
golang.org/x/sys v0.11.0 // indirect
50+
golang.org/x/text v0.12.0 // indirect
51+
gopkg.in/yaml.v3 v3.0.1 // indirect
52+
)

0 commit comments

Comments
 (0)