Skip to content

Commit

Permalink
factored
Browse files Browse the repository at this point in the history
  • Loading branch information
OR13 committed Aug 11, 2024
1 parent af88a8c commit 2b84f6c
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,4 @@ jobs:
neo4j-user: ${{ secrets.NEO4J_USERNAME }}
neo4j-password: ${{ secrets.NEO4J_PASSWORD }}
transmute: |
vcwg graph ./tests/fixtures/issuer-claims.json --verbose --credential-type application/vc --graph-type application/gql --push
graph assist ./tests/fixtures/issuer-claims.json --verbose --credential-type application/vc --graph-type application/gql --push
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"build": "ncc build -m src/action/run.ts",
"transmute": "node dist/index.js",
"lint": "eslint ./src --fix",
"test": "dotenv -e .env jest",
"coverage": "dotenv -e .env jest --ci --coverage --runInBand",
"test": "jest",
"coverage": "jest --ci --coverage --runInBand",
"prerelease": "npm run build",
"release": "npm version patch && git add ./package.json",
"postrelease": "git push --tags",
Expand Down Expand Up @@ -51,8 +51,10 @@
"@actions/core": "^1.10.1",
"@transmute/cose": "^0.2.8",
"@transmute/edn": "^0.0.5",
"@transmute/sdk": "^0.0.0",
"@transmute/verifiable-credentials": "^0.3.3",
"axios": "^1.7.3",
"dotenv": "^16.4.5",
"jose": "^5.6.3",
"jsonld": "^8.3.2",
"neo4j-driver": "^5.23.0"
Expand Down
7 changes: 4 additions & 3 deletions scripts/build-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const crypto = require('crypto');
const contextsUrls = [
'https://www.w3.org/2018/credentials/v1',
'https://www.w3.org/ns/credentials/v2',
'https://www.w3.org/ns/credentials/examples/v2'
'https://www.w3.org/ns/credentials/examples/v2',
'https://w3id.org/traceability/v1'
];

(async () => {
Expand All @@ -20,7 +21,7 @@ const contextsUrls = [
.createHash('sha256')
.update(response.data)
.digest('hex')
fs.writeFileSync(`./src/vcwg/graph/contexts/${urlContentHash}.json`, response.data)
fs.writeFileSync(`./src/graph/graph/contexts/${urlContentHash}.json`, response.data)
const context = `import { default as c${urlContentHash} } from "./${urlContentHash}.json"`
index += `${context}\n`
contexts[contextUrl] = `c${urlContentHash}`
Expand All @@ -30,5 +31,5 @@ const contextsUrls = [
index += ` "${url}": ${urlContentHash},\n`
}
index += `}\n`
fs.writeFileSync(`./src/vcwg/graph/contexts/index.ts`, index)
fs.writeFileSync(`./src/graph/graph/contexts/index.ts`, index)
})()
4 changes: 4 additions & 0 deletions src/action/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type VcwgOptions = {
'credential-type'?: string
'presentation-type'?: string
push?: boolean
env?: string
}

export type ScittOptions = {
Expand Down Expand Up @@ -61,6 +62,9 @@ export const args = (prompt: string) => {
push: {
type: 'boolean' as "boolean",
},
env: {
type: 'string' as "string",
},
'credential-type': {
type: 'string' as "string",
},
Expand Down

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/graph/graph/contexts/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { default as cab4ddd9a531758807a79a5b450510d61ae8d147eab966cc9a200c07095b0cdcc } from "./ab4ddd9a531758807a79a5b450510d61ae8d147eab966cc9a200c07095b0cdcc.json"
import { default as c24a18c90e9856d526111f29376e302d970b2bd10182e33959995b0207d7043b9 } from "./24a18c90e9856d526111f29376e302d970b2bd10182e33959995b0207d7043b9.json"
import { default as c57393fbc69d6efb9b9b5dc9cb6b9880b0944360abfe2eaf459c9e58cf2279d7c } from "./57393fbc69d6efb9b9b5dc9cb6b9880b0944360abfe2eaf459c9e58cf2279d7c.json"
import { default as cd7cfce00f08fa1cbd4c58cf5ee60c6a0398aeae1f908621625e61477be878579 } from "./d7cfce00f08fa1cbd4c58cf5ee60c6a0398aeae1f908621625e61477be878579.json"
export const contexts = {
"https://www.w3.org/2018/credentials/v1": cab4ddd9a531758807a79a5b450510d61ae8d147eab966cc9a200c07095b0cdcc,
"https://www.w3.org/ns/credentials/v2": c24a18c90e9856d526111f29376e302d970b2bd10182e33959995b0207d7043b9,
"https://www.w3.org/ns/credentials/examples/v2": c57393fbc69d6efb9b9b5dc9cb6b9880b0944360abfe2eaf459c9e58cf2279d7c,
"https://w3id.org/traceability/v1": cd7cfce00f08fa1cbd4c58cf5ee60c6a0398aeae1f908621625e61477be878579,
}
8 changes: 4 additions & 4 deletions src/graph/graph/jsongraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ const fromPresentation = async (document: any) => {
const normalizeToTypeArray = Array.isArray(verifiableCredential.type) ? verifiableCredential.type : [verifiableCredential.type]
let credentialGraph = undefined as any;
if (normalizeToTypeArray.includes('EnvelopedVerifiableCredential')) {
if (verifiableCredential.id && verifiableCredential.id.startsWith('data:application/vc-ld+sd-jwt;')) {
const token = verifiableCredential.id.replace('data:application/vc-ld+sd-jwt;', '')
if (verifiableCredential.id && verifiableCredential.id.includes('+sd-jwt;')) {
const token = verifiableCredential.id.split('+sd-jwt;').pop()
const payload = jose.decodeJwt(token)
credentialGraph = await fromCredential(payload)
}
if (verifiableCredential.id && verifiableCredential.id.startsWith('data:application/vc-ld+jwt;')) {
const token = verifiableCredential.id.replace('data:application/vc-ld+jwt;', '')
if (verifiableCredential.id && verifiableCredential.id.includes('+jwt;')) {
const token = verifiableCredential.id.split('+jwt;').pop()
const payload = jose.decodeJwt(token)
credentialGraph = await fromCredential(payload)
}
Expand Down
115 changes: 91 additions & 24 deletions src/graph/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,114 @@ import { jsongraph } from './graph/jsongraph'
import { query, injection } from './graph/gql'
import { driver, push } from './graph/driver'

import dotenv from 'dotenv'

import VerifiableDataPlatform from '@transmute/sdk'
import { getPresentations } from './presentations'

export const handler = async function ({ positionals, values }: Arguments) {
positionals = positionals.slice(1)
const operation = positionals.shift()
const encoder = new TextEncoder()
switch (operation) {
case 'assist': {
const graphType = values['graph-type'] || 'application/vnd.jgf+json'
const output = values.output
const graphType = values['graph-type'] || 'application/vnd.jgf+json'
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))
const envFile = values.env
if (envFile) {
dotenv.config({ path: envFile })
}
if (env.github()) {
if (graphType === 'application/gql') {
setOutput('gql', graphText)
let graph
let graphText
if (!pathToContent) {
let allGraphText = ''
const allGraphs = [] as any[]
const api = await VerifiableDataPlatform.fromEnv({
CLIENT_ID: process.env.CLIENT_ID as string,
CLIENT_SECRET: process.env.CLIENT_SECRET as string,
API_BASE_URL: process.env.API_BASE_URL as string,
TOKEN_AUDIENCE: process.env.TOKEN_AUDIENCE as string,
})
const { items } = await getPresentations({ sent: true, received: true, api })
const d = await driver()
for (const item of items) {
try {
const content = encoder.encode(item.content)
graph = await jsongraph.graph(content, 'application/vp-ld+sd-jwt')
allGraphs.push(graph)
const components = await query(graph)
const dangerousQuery = await injection(components)
allGraphText += dangerousQuery + '\n'
if (verbose) {
const message = `\n${dangerousQuery}\n`
console.log(message)
}
const session = d.session()
await push(session, components)
} catch (e) {
if (verbose) {
const message = `⛔ ${item.id}`
console.error(message)
console.error(e)
}
}
}
if (graphType === 'application/vnd.jgf+json') {
setOutput('json', graph)
if (output) {
fs.writeFileSync(output, allGraphText)
}
if (env.github()) {
if (graphType === 'application/gql') {
setOutput('gql', allGraphText)
}
if (graphType === 'application/vnd.jgf+json') {
setOutput('json', allGraphs)
}
} else {
if (!output) {
console.log(allGraphText)
}
}


await d.close()

} else {
if (!output) {
console.log(graphText)
// single file
const content = new Uint8Array(fs.readFileSync(pathToContent))
graph = await jsongraph.graph(content, contentType)
graphText = JSON.stringify(graph, null, 2)
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, graphText)
}
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
Expand Down
41 changes: 41 additions & 0 deletions src/graph/presentations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

export const getPresentations = async ({ sent, received, api }: any) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const results: any = { items: [] }
if (received) {
const response = await api.presentations.getPresentationsSharedWithMe()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const data = response.data as any
if (response.data) {
results.page = data.page
results.count = data.count
results.items = [
...results.items,
...data.items.map((p) => {
return { id: p.id, content: p.verifiablePresentation }
}),
]
}
}
if (sent) {
const response = await api.presentations.getPresentationsSharedWithOthers()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const data = response.data as any
if (response.data) {
results.page = data.page
results.count = data.count
results.items = [
...results.items,
...data.items.map((p) => {
return { id: p.id, content: p.verifiablePresentation }
}),
]
}
// in case both are passed, these values don't make sense...
if (received) {
delete results.page
delete results.count
}
}
return results
}
11 changes: 8 additions & 3 deletions tests/graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,26 @@ beforeEach(() => {

// Graph

it('graph json', async () => {
it('graph assist 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 () => {
it.skip('graph assist 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 () => {
it.skip('graph assist 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)
})

it.skip('graph assist vdp', async () => {
await facade(`graph assist --verbose --credential-type application/vc --graph-type application/gql --push --env ./.env`)
expect(debug).toHaveBeenCalledTimes(1)
expect(output).toHaveBeenCalledTimes(1)
})

0 comments on commit 2b84f6c

Please sign in to comment.