Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workflow tooling to the cli #158

Merged
merged 7 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Empty file.
797 changes: 797 additions & 0 deletions packages/cli/data/workflows/workflow.instance-0.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packages/cli/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ require('yargs')
// data
.command(...commands.data.createDataCommand)

// neo
.command(...commands.neo.importWorkflowInstanceComand)

.help().argv;
1,084 changes: 1,017 additions & 67 deletions packages/cli/package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"node": ">=14"
},
"scripts": {
"pretransmute": "npm run build",
"transmute": "node main.js",
"start": "tsdx watch",
"build": "tsdx build",
Expand Down Expand Up @@ -73,9 +72,14 @@
"@transmute/vc.js": "^0.7.0-unstable.42",
"axios": "^0.24.0",
"bip39": "^3.0.4",
"bpmn-engine": "^13.0.0",
"bpmn-js": "^8.9.1",
"factory.ts": "^0.5.1",
"faker": "5.5.3",
"graphology": "^0.23.2",
"hdkey": "^2.0.1",
"neo4j-driver": "^4.4.2",
"uuid": "^8.3.2",
"yargs": "^16.2.0"
}
}
25 changes: 2 additions & 23 deletions packages/cli/src/commands/data/createData.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@
import {
generateOrganization,
generateProduct,
generateDevice,
generateCredential,
generatePresentation,
} from './generate';

import { handleCommandResponse } from '../../util';

const issuerTypeGenerators: any = {
Organization: generateOrganization,
Device: generateDevice,
};

const typeGenerators: any = {
...issuerTypeGenerators,
Product: generateProduct,
};

// for all base types create credential types from them.
Object.keys(typeGenerators).forEach((k: string) => {
typeGenerators['Certified' + k] = generateCredential;
});
import { typeGenerators } from './generate/typeGenerators';

typeGenerators.VerifiablePresentation = generatePresentation;
import { issuerTypeGenerators } from './generate/simpleTypeGenerators';

export const createDataCommand = [
'data create',
Expand Down
74 changes: 63 additions & 11 deletions packages/cli/src/commands/data/generate/generateCredential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,62 @@ import { generateKey } from '../../key/generate';
import { sha256 } from '../../../util';
import { createCredential } from '../../credential';

export const generateCredential = async (argv: any, typeGenerators: any) => {
const subjectType = argv.type.split('Certified').pop();
const subject = await typeGenerators[subjectType](
{ ...argv, seed: argv.subjectSeed }, // make sure subject generator uses subject seed
typeGenerators
);
export const makeCredential = (
type: string,
issuer: any,
subject: any,
workflow?: any
) => {
const credential: any = {
'@context': [
'https://www.w3.org/2018/credentials/v1',
'https://w3id.org/security/suites/jws-2020/v1',
{ '@vocab': 'https://ontology.example/vocab/#' },
],
id: 'urn:uuid:' + faker.random.alphaNumeric(8),
type: ['VerifiableCredential', type],
issuer: issuer,
issuanceDate: new Date().toISOString(),
credentialSubject: subject,
};
if (workflow) {
credential.workflow = workflow;
}
return credential;
};

export const makeSignedCredential = async (
credential: any,
issuerSeed: number
) => {
const issuerKeys = await generateKey({
type: 'ed25519',
// unsafe expansion of integer to bytes 32
// for testing purposes only.
seed: sha256(Buffer.from(issuerSeed.toString())).toString('hex'),
});
return createCredential(credential, issuerKeys[0], 'vc');
};

const issuer = await typeGenerators[argv.issuerType](
{ ...argv, seed: argv.issuerSeed }, // make sure issuer generator uses issuer seed
export const issueCredential = async (
type: string,
subject: any,
issuerSeed: string,
issuerType: string,
typeGenerators: any
) => {
const issuer = await typeGenerators[issuerType](
{ type: issuerType, seed: issuerSeed }, // make sure issuer generator uses issuer seed
typeGenerators
);

const credential = {
'@context': [
'https://www.w3.org/2018/credentials/v1',
'https://w3id.org/security/suites/jws-2020/v1',
{ '@vocab': 'https://ontology.example/vocab/#' },
],
id: 'urn:uuid:' + faker.random.alphaNumeric(8),
type: ['VerifiableCredential', argv.type],
type: ['VerifiableCredential', type],
issuer: issuer,
issuanceDate: new Date().toISOString(),
credentialSubject: subject,
Expand All @@ -31,8 +67,24 @@ export const generateCredential = async (argv: any, typeGenerators: any) => {
type: 'ed25519',
// unsafe expansion of integer to bytes 32
// for testing purposes only.
seed: sha256(Buffer.from(argv.issuerSeed.toString())).toString('hex'),
seed: sha256(Buffer.from(issuerSeed.toString())).toString('hex'),
});
const data = await createCredential(credential, issuerKeys[0], 'vc');
return data;
};

export const generateCredential = async (argv: any, typeGenerators: any) => {
const subjectType = argv.type.split('Certified').pop();
const subject = await typeGenerators[subjectType](
{ ...argv, seed: argv.subjectSeed }, // make sure subject generator uses subject seed
typeGenerators
);

return issueCredential(
argv.type,
subject,
argv.issuerSeed,
argv.issuerType,
typeGenerators
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import faker from 'faker';
import { generateKey } from '../../key/generateKey';
import { sha256 } from '../../../util';

export const generateOrganization = async (argv: any) => {
export const generateOrganization = async (argv: { seed: any }) => {
const seed = argv.seed;
if (seed) {
faker.seed(seed);
Expand Down
21 changes: 21 additions & 0 deletions packages/cli/src/commands/data/generate/generatePerson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import faker from 'faker';
import { generateKey } from '../../key/generateKey';
import { sha256 } from '../../../util';

export const generatePerson = async (argv: { seed: number }) => {
const { seed } = argv;
if (seed) {
faker.seed(seed);
}
const keys = await generateKey({
type: 'ed25519',
seed: sha256(Buffer.from(seed.toString())).toString('hex'),
});
return {
id: keys[0].controller,
type: 'Person',
name: faker.name.firstName(),
email: faker.internet.email(),
phone: faker.phone.phoneNumber(),
};
};
66 changes: 50 additions & 16 deletions packages/cli/src/commands/data/generate/generatePresentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,53 @@ import { generateKey } from '../../key/generateKey';
import { sha256, getAllCredentialsInDirectory } from '../../../util';
import { createPresentation } from '../../presentation';

export const makePresentation = (
holder: any,
credentials: any[],
workflow?: any
) => {
const presentation: any = {
'@context': [
'https://www.w3.org/2018/credentials/v1',
'https://w3id.org/security/suites/jws-2020/v1',
{ '@vocab': 'https://ontology.example/vocab/#' },
],
id: 'urn:uuid:' + faker.random.alphaNumeric(8),
type: ['VerifiablePresentation'],
holder: holder,
verifiableCredential: credentials,
};
if (workflow) {
presentation.workflow = workflow;
}
if (presentation.verifiableCredential.length === 0) {
delete presentation.verifiableCredential;
}
return presentation;
};

export const makeSignedPresentationFromSeed = async (
presentation: any,
holderSeed: number,
domain: string,
challenge: string
) => {
const holderKeys = await generateKey({
type: 'ed25519',
seed: sha256(Buffer.from(holderSeed.toString())).toString('hex'),
});
return makeSignedPresentation(presentation, holderKeys[0], domain, challenge);
};

const makeSignedPresentation = (
presentation: any,
key: any,
domain: string,
challenge: string
) => {
return createPresentation(presentation, key, 'vp', domain, challenge);
};

export const generatePresentation = async (argv: any, typeGenerators: any) => {
if (argv.debug) {
console.log(argv);
Expand All @@ -11,33 +58,20 @@ export const generatePresentation = async (argv: any, typeGenerators: any) => {
if (argv.holderSeed) {
faker.seed(argv.holderSeed);
}
const holderKeys = await generateKey({
type: 'ed25519',
seed: sha256(Buffer.from(argv.holderSeed.toString())).toString('hex'),
});

const credentials = getAllCredentialsInDirectory(argv.credentialsDirectory);

const holder = await typeGenerators[argv.holderType](argv, typeGenerators);

const presentation: any = {
'@context': [
'https://www.w3.org/2018/credentials/v1',
'https://w3id.org/security/suites/jws-2020/v1',
{ '@vocab': 'https://ontology.example/vocab/#' },
],
type: ['VerifiablePresentation'],
holder: holder,
};
const presentation: any = makePresentation(holder, credentials);

if (credentials && credentials.length) {
presentation.verifiableCredential = credentials;
}

return createPresentation(
return makeSignedPresentationFromSeed(
presentation,
holderKeys[0],
'vp',
argv.holderSeed,
argv.domain,
argv.challenge
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { run } from '../../workflow/engine/run';

import ecommerce from './workflows/scenarios/e-commerce/e-commerce.scenario.json';

export const generateWorkflowInstance = async (argv: any) => {
const { input } = argv;
let workflowVariables;
let xml;
switch (input) {
case 'https://w3id.org/traceability/#e-commerce': {
workflowVariables = ecommerce.variables;
xml = ecommerce.xml;
break;
}
default: {
throw new Error(
'Unsupported scenario. Try using "https://w3id.org/traceability/#e-commerce"'
);
}
}

if (argv.variables) {
try {
workflowVariables = {
...workflowVariables,
...JSON.parse(argv.variables),
};
} catch (e) {
console.warn('Unable to parse variables, defaulting to empty object.');
}
}
const services = {};
const instance = await run('workflow-run', xml, services, workflowVariables);

return { inst: instance };
};
3 changes: 3 additions & 0 deletions packages/cli/src/commands/data/generate/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export * from './generatePerson';
export * from './generateOrganization';
export * from './generateProduct';
export * from './generateDevice';

export * from './typeGenerators';

// vc
export * from './generateCredential';
// vp
Expand Down
24 changes: 24 additions & 0 deletions packages/cli/src/commands/data/generate/simpleTypeGenerators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { generateOrganization } from './generateOrganization';
import { generatePerson } from './generatePerson';
import { generateProduct } from './generateProduct';
import { generateDevice } from './generateDevice';
import { generateCredential } from './generateCredential';
import { generatePresentation } from './generatePresentation';

export const issuerTypeGenerators: any = {
Organization: generateOrganization,
Person: generatePerson,
Device: generateDevice,
};

export const simpleTypeGenerators: any = {
...issuerTypeGenerators,
Product: generateProduct,
};

// for all base types create credential types from them.
Object.keys(simpleTypeGenerators).forEach((k: string) => {
simpleTypeGenerators['Certified' + k] = generateCredential;
});

simpleTypeGenerators.VerifiablePresentation = generatePresentation;
8 changes: 8 additions & 0 deletions packages/cli/src/commands/data/generate/typeGenerators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { simpleTypeGenerators } from './simpleTypeGenerators';

import { generateWorkflowInstance } from './generateWorkflowInstance';
export const typeGenerators: any = {
...simpleTypeGenerators,
};

typeGenerators.WorkflowInstance = generateWorkflowInstance;