Skip to content

Commit

Permalink
factoring
Browse files Browse the repository at this point in the history
  • Loading branch information
OR13 committed Aug 11, 2024
1 parent 6e6ab70 commit af88a8c
Show file tree
Hide file tree
Showing 20 changed files with 108 additions and 77 deletions.
4 changes: 4 additions & 0 deletions scripts/graph.diagnostic.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

# Graph

npm run -s transmute -- graph assist ./tests/fixtures/issuer-claims.json --credential-type application/vc --output ./tests/fixtures/issuer-claims.graph.json
3 changes: 0 additions & 3 deletions scripts/vcwg.diagnostic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,3 @@ npm run -s transmute -- vcwg verify-credential ./tests/fixtures/public.sig.jwk.j
npm run -s transmute -- vcwg issue-presentation ./tests/fixtures/private.sig.jwk.json ./tests/fixtures/issuer-claims.cbor --credential-type application/vc-ld+cose --presentation-type application/vp-ld+cose --output ./tests/fixtures/holder-claims.cbor
npm run -s transmute -- vcwg verify-presentation ./tests/fixtures/public.sig.jwk.json ./tests/fixtures/holder-claims.cbor --presentation-type application/vp-ld+cose --output ./tests/fixtures/holder-claims.cbor.verified.json

# Graph

npm run -s transmute -- vcwg graph ./tests/fixtures/issuer-claims.json --credential-type application/vc --output ./tests/fixtures/issuer-claims.graph.json
3 changes: 2 additions & 1 deletion src/action/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import * as jose from '../jose'
import * as cose from '../cose'
import * as scitt from '../scitt'
import * as vcwg from '../vcwg'
import * as graph from '../graph'

const commands = { jose, cose, scitt, vcwg }
const commands = { jose, cose, scitt, vcwg, graph }

export const handler = async (args: Arguments) => {
const [command] = args.positionals
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
62 changes: 62 additions & 0 deletions src/graph/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import fs from 'fs'

import { Arguments } from "../types"
import { setOutput, debug } from '@actions/core'
import { env } from '../action'
import { jsongraph } from './graph/jsongraph'
import { query, injection } from './graph/gql'
import { driver, push } from './graph/driver'

export const handler = async function ({ positionals, values }: Arguments) {
positionals = positionals.slice(1)
const operation = positionals.shift()
switch (operation) {
case 'assist': {
const graphType = values['graph-type'] || 'application/vnd.jgf+json'
const output = values.output
const contentType: any = values['credential-type'] || values['presentation-type']
const verbose = values.verbose || false
const [pathToContent] = positionals
const content = new Uint8Array(fs.readFileSync(pathToContent))
const graph = await jsongraph.graph(content, contentType)
let graphText = JSON.stringify(graph, null, 2)
if (verbose) {
const message = `🕸️ ${graphType}`
debug(message)
}
if (graphType === 'application/gql') {
const components = await query(graph)
const dangerousQuery = await injection(components)
graphText = dangerousQuery
if (values.push) {
const d = await driver()
const session = d.session()
await push(session, components)
await d.close()
}
}
if (output) {
fs.writeFileSync(output, JSON.stringify(graphText, null, 2))
}
if (env.github()) {
if (graphType === 'application/gql') {
setOutput('gql', graphText)
}
if (graphType === 'application/vnd.jgf+json') {
setOutput('json', graph)
}
} else {
if (!output) {
console.log(graphText)
}
}
break
}
default: {
const message = `😕 Unknown Command`
console.error(message)
throw new Error(message)
}
}

}
1 change: 1 addition & 0 deletions src/graph/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './handler'
56 changes: 3 additions & 53 deletions src/vcwg/handler.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import fs from 'fs'
import * as jose from 'jose'
import { Arguments } from "../types"

import * as cose from '@transmute/cose'
import * as vc from '@transmute/verifiable-credentials'

import { Arguments } from "../types"
import yaml from 'yaml'

import { setSecret, setOutput, debug } from '@actions/core'
import * as cose from '@transmute/cose'

import { env } from '../action'

import * as vc from '@transmute/verifiable-credentials'

import { jsongraph } from './graph/jsongraph'
import { query, injection } from './graph/gql'
import { driver, push } from './graph/driver'

export const handler = async function ({ positionals, values }: Arguments) {
positionals = positionals.slice(1)
const operation = positionals.shift()
Expand Down Expand Up @@ -325,48 +317,6 @@ export const handler = async function ({ positionals, values }: Arguments) {
}
break
}
case 'graph': {
const graphType = values['graph-type'] || 'application/vnd.jgf+json'
const output = values.output
const contentType: any = values['credential-type'] || values['presentation-type']
const verbose = values.verbose || false
const [pathToContent] = positionals
const content = new Uint8Array(fs.readFileSync(pathToContent))
const graph = await jsongraph.graph(content, contentType)
let graphText = JSON.stringify(graph, null, 2)
if (verbose) {
const message = `🕸️ ${graphType}`
debug(message)
}
if (graphType === 'application/gql') {
const components = await query(graph)
const dangerousQuery = await injection(components)
graphText = dangerousQuery
if (values.push) {
const d = await driver()
const session = d.session()
await push(session, components)
await d.close()
}
}
if (output) {
fs.writeFileSync(output, JSON.stringify(graphText, null, 2))
}
if (env.github()) {
if (graphType === 'application/gql') {
setOutput('gql', graphText)
}
if (graphType === 'application/vnd.jgf+json') {
setOutput('json', graph)
}
} else {
if (!output) {
console.log(graphText)
}
}
break
}

default: {
const message = `😕 Unknown Command`
console.error(message)
Expand Down
36 changes: 36 additions & 0 deletions tests/graph.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as core from '@actions/core'

import { facade } from '../src'

let debug: jest.SpiedFunction<typeof core.debug>
let output: jest.SpiedFunction<typeof core.setOutput>
let secret: jest.SpiedFunction<typeof core.setSecret>

beforeEach(() => {
process.env.GITHUB_ACTION = 'jest-mock'
jest.clearAllMocks()
debug = jest.spyOn(core, 'debug').mockImplementation()
output = jest.spyOn(core, 'setOutput').mockImplementation()
secret = jest.spyOn(core, 'setSecret').mockImplementation()
})

// Graph

it('graph json', async () => {
await facade(`graph assist ./tests/fixtures/issuer-claims.json --verbose --credential-type application/vc --graph-type application/vnd.jgf+json`)
expect(debug).toHaveBeenCalledTimes(1)
expect(output).toHaveBeenCalledTimes(1)
})

it('graph gql', async () => {
await facade(`graph assist ./tests/fixtures/issuer-claims.json --verbose --credential-type application/vc --graph-type application/gql`)
expect(debug).toHaveBeenCalledTimes(1)
expect(output).toHaveBeenCalledTimes(1)
})

it.skip('graph gql neo4j', async () => {
await facade(`graph assist ./tests/fixtures/issuer-claims.json --verbose --credential-type application/vc --graph-type application/gql --push`)
expect(debug).toHaveBeenCalledTimes(1)
expect(output).toHaveBeenCalledTimes(1)
})

20 changes: 0 additions & 20 deletions tests/vcwg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,3 @@ it('verify-presentation', async () => {
expect(output).toHaveBeenCalledTimes(1)
})

// Graph

it('graph json', async () => {
await facade(`vcwg graph ./tests/fixtures/issuer-claims.json --verbose --credential-type application/vc --graph-type application/vnd.jgf+json`)
expect(debug).toHaveBeenCalledTimes(1)
expect(output).toHaveBeenCalledTimes(1)
})

it('graph gql', async () => {
await facade(`vcwg graph ./tests/fixtures/issuer-claims.json --verbose --credential-type application/vc --graph-type application/gql`)
expect(debug).toHaveBeenCalledTimes(1)
expect(output).toHaveBeenCalledTimes(1)
})

it.skip('graph gql neo4j', async () => {
await facade(`vcwg graph ./tests/fixtures/issuer-claims.json --verbose --credential-type application/vc --graph-type application/gql --push`)
expect(debug).toHaveBeenCalledTimes(1)
expect(output).toHaveBeenCalledTimes(1)
})

0 comments on commit af88a8c

Please sign in to comment.