Skip to content

Commit

Permalink
feat(package): update package json and lockfile, lint
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcook1186 committed Oct 17, 2024
1 parent 6c8beb8 commit 0823ef0
Show file tree
Hide file tree
Showing 9 changed files with 622 additions and 384 deletions.
628 changes: 434 additions & 194 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"dependencies": {
"@commitlint/cli": "^18.6.0",
"@commitlint/config-conventional": "^18.6.0",
"@ethereum-attestation-service/eas-sdk": "^2.3.0",
"@ethereum-attestation-service/eas-sdk": "^2.6.1",
"@grnsft/if-core": "^0.0.25",
"axios": "^1.7.2",
"csv-parse": "^5.5.6",
Expand Down
4 changes: 2 additions & 2 deletions src/if-attest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ SCHEMA_UID: '0x9f074eced91e2c6952fdf5734ec1d8cc05e1e1d07eaa442f07746b7d8a422c0e'
For an onchain attestation

```
npm run if-attest -- -manifest ./manifests/outputs/example.yaml --blockchain true
npm run if-attest -- -manifest ./manifests/outputs/example.yaml --blockchain true --level 3 --unit site-visits
```

For an offchain attestation

```
npm run if-attest -- --manifest ./manifests/outputs/example.yaml --blockchain false
npm run if-attest -- --manifest ./manifests/outputs/example.yaml --blockchain false --level 3 --unit site-visits
```
86 changes: 42 additions & 44 deletions src/if-attest/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
import { ArgumentConfig, ParseOptions } from 'ts-command-line-args';
import { STRINGS as COMMON_STRINGS } from '../../common/config';
import { IFAttestArgs } from '../types/process-args';
import {ArgumentConfig, ParseOptions} from 'ts-command-line-args';
import {STRINGS as COMMON_STRINGS} from '../../common/config';
import {IFAttestArgs} from '../types/process-args';

const { DISCLAIMER_MESSAGE } = COMMON_STRINGS;
const {DISCLAIMER_MESSAGE} = COMMON_STRINGS;

export const CONFIG = {
ARGS: {
manifest: {
type: String,
optional: false,
alias: 'm',
description: '[path to the manifest file]',
},
blockchain: {
type: Boolean,
optional: true,
alias: 'b',
description:
'[Boolean to toggle posting attestation to blockchain (true to post, false to save locally)]',
},
level: {
type: Number,
optional: true,
alias: 'l',
description:
'[Audit level being attested to - integers from 1-5]',
},
unit: {
type: String,
optional: true,
alias: 'u',
description:
'[The functional unit used to calculate SCI]',
},
} as ArgumentConfig<IFAttestArgs>,
HELP: {
helpArg: 'help',
headerContentSections: [
{ header: 'Impact Framework', content: 'IF-Attest Helpful keywords:' },
],
footerContentSections: [
{ header: 'Green Software Foundation', content: DISCLAIMER_MESSAGE },
],
} as ParseOptions<any>,
ARGS: {
manifest: {
type: String,
optional: false,
alias: 'm',
description: '[path to the manifest file]',
},
blockchain: {
type: Boolean,
optional: true,
alias: 'b',
description:
'[Boolean to toggle posting attestation to blockchain (true to post, false to save locally)]',
},
level: {
type: Number,
optional: true,
alias: 'l',
description: '[Audit level being attested to - integers from 1-5]',
},
unit: {
type: String,
optional: true,
alias: 'u',
description: '[The functional unit used to calculate SCI]',
},
} as ArgumentConfig<IFAttestArgs>,
HELP: {
helpArg: 'help',
headerContentSections: [
{header: 'Impact Framework', content: 'IF-Attest Helpful keywords:'},
],
footerContentSections: [
{header: 'Green Software Foundation', content: DISCLAIMER_MESSAGE},
],
} as ParseOptions<any>,
};

export const STRINGS = {
MANIFEST_IS_NOT_YAML: (path: string) =>
`The \`${path}\` is not in yaml format.`,
MANIFEST_IS_NOT_YAML: (path: string) =>
`The \`${path}\` is not in yaml format.`,
};
22 changes: 13 additions & 9 deletions src/if-attest/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#!/usr/bin/env node
/* eslint-disable no-process-exit */
import {
EAS,
} from '@ethereum-attestation-service/eas-sdk';
import { logger } from '../common/util/logger';
import { parseIfAttestArgs } from './util/args';
import {EAS} from '@ethereum-attestation-service/eas-sdk';
import {logger} from '../common/util/logger';
import {parseIfAttestArgs} from './util/args';
import * as fs from 'fs';
import {createSigningWallet} from './util/ethereum-utils';
import {addSignerInfoToAttestation, createOffchainAttestaton} from './util/offchain-attestation-utils';
import {sendAttestationTx, encodeSchema, getManifestInfo } from './util/attestation-utils';
import {
addSignerInfoToAttestation,
createOffchainAttestaton,
} from './util/offchain-attestation-utils';
import {
sendAttestationTx,
encodeSchema,
getManifestInfo,
} from './util/attestation-utils';

const EAS_CONTRACT_ADDRESS_SEPOLIA: string =
process.env.EAS_CONTRACT_ADDRESS_SEPOLIA ?? '';
Expand All @@ -26,7 +31,7 @@ const IfAttest = async () => {
const signer = createSigningWallet();
const eas = new EAS(EAS_CONTRACT_ADDRESS_SEPOLIA);

console.debug('creating signer object')
console.debug('creating signer object');
eas.connect(signer);

const manifestInfo = await getManifestInfo(
Expand Down Expand Up @@ -61,7 +66,6 @@ const IfAttest = async () => {
}
};


IfAttest().catch(error => {
if (error instanceof Error) {
logger.error(error);
Expand Down
8 changes: 4 additions & 4 deletions src/if-attest/types/process-args.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface IFAttestArgs {
manifest: string;
blockchain?: boolean;
level: number;
unit: string;
manifest: string;
blockchain?: boolean;
level: number;
unit: string;
}
173 changes: 85 additions & 88 deletions src/if-attest/util/attestation-utils.ts
Original file line number Diff line number Diff line change
@@ -1,122 +1,119 @@
import { ManifestInfo } from '../types/types';
import {ManifestInfo} from '../types/types';
import * as YAML from 'js-yaml';
import {
EAS,
SchemaEncoder,
} from '@ethereum-attestation-service/eas-sdk';
import { execPromise } from '../../common/util/helpers';
import { openYamlFileAsObject } from '../../common/util/yaml';
import { Manifest } from '../../common/types/manifest';
import { SCHEMA } from '../util/schema';
import { ethers, Wallet } from 'ethers';
import {EAS, SchemaEncoder} from '@ethereum-attestation-service/eas-sdk';
import {execPromise} from '../../common/util/helpers';
import {openYamlFileAsObject} from '../../common/util/yaml';
import {Manifest} from '../../common/types/manifest';
import {SCHEMA} from '../util/schema';
import {ethers, Wallet} from 'ethers';

const packageJson = require('../../../package.json');
const UID = process.env.SCHEMA_UID ?? '';

export const sendAttestationTx = async (
eas: EAS,
signer: Wallet,
encodedData: string
eas: EAS,
signer: Wallet,
encodedData: string
): Promise<string> => {
const tx = await eas.attest({
schema: UID,
data: {
recipient: signer.address, //can provide an ethereum address for the attested org if needed- here it's the signer address
expirationTime: BigInt(0),
revocable: true, // Be aware that if your schema is not revocable, this MUST be false
data: encodedData,
},
});
const attestationUID = await tx.wait();
return attestationUID;
const tx = await eas.attest({
schema: UID,
data: {
recipient: signer.address, //can provide an ethereum address for the attested org if needed- here it's the signer address
expirationTime: BigInt(0),
revocable: true, // Be aware that if your schema is not revocable, this MUST be false
data: encodedData,
},
});
const attestationUID = await tx.wait();
return attestationUID;
};

export const encodeSchema = (manifestInfo: ManifestInfo) => {
const schemaEncoder = new SchemaEncoder(SCHEMA);
const encodedData = schemaEncoder.encodeData([
{ name: 'start', value: manifestInfo.start, type: 'string' },
{ name: 'end', value: manifestInfo.end, type: 'string' },
{ name: 'hash', value: manifestInfo.hash, type: 'bytes32' },
{ name: 'if', value: manifestInfo.if, type: 'string' },
{ name: 'verified', value: manifestInfo.verified, type: 'bool' },
{ name: 'sci', value: manifestInfo.sci, type: 'uint64' },
{ name: 'energy', value: manifestInfo.energy, type: 'uint64' },
{ name: 'carbon', value: manifestInfo.carbon, type: 'uint64' },
{ name: 'level', value: manifestInfo.level, type: 'uint8' },
{ name: 'quality', value: manifestInfo.quality, type: 'uint8' },
{
name: 'functionalUnit',
value: manifestInfo.functionalUnit,
type: 'string',
},
]);
return encodedData;
const schemaEncoder = new SchemaEncoder(SCHEMA);
const encodedData = schemaEncoder.encodeData([
{name: 'start', value: manifestInfo.start, type: 'string'},
{name: 'end', value: manifestInfo.end, type: 'string'},
{name: 'hash', value: manifestInfo.hash, type: 'bytes32'},
{name: 'if', value: manifestInfo.if, type: 'string'},
{name: 'verified', value: manifestInfo.verified, type: 'bool'},
{name: 'sci', value: manifestInfo.sci, type: 'uint64'},
{name: 'energy', value: manifestInfo.energy, type: 'uint64'},
{name: 'carbon', value: manifestInfo.carbon, type: 'uint64'},
{name: 'level', value: manifestInfo.level, type: 'uint8'},
{name: 'quality', value: manifestInfo.quality, type: 'uint8'},
{
name: 'functionalUnit',
value: manifestInfo.functionalUnit,
type: 'string',
},
]);
return encodedData;
};

export const getManifestStart = (manifest: Manifest): string => {
const firstChildName = Object.keys(manifest.tree.children)[0] ?? 0;
const manifestStart =
manifest.tree.children[`${firstChildName}`].inputs[0].timestamp ?? 0;
return manifestStart;
const firstChildName = Object.keys(manifest.tree.children)[0] ?? 0;
const manifestStart =
manifest.tree.children[`${firstChildName}`].inputs[0].timestamp ?? 0;
return manifestStart;
};

export const getManifestEnd = (manifest: Manifest): string => {
const firstChildName = Object.keys(manifest.tree.children)[0];
const inputsLength =
manifest.tree.children[`${firstChildName}`].inputs.length ?? '';
const manifestEnd =
manifest.tree.children[`${firstChildName}`].inputs[inputsLength - 1]
.timestamp ?? '';
return manifestEnd;
const firstChildName = Object.keys(manifest.tree.children)[0];
const inputsLength =
manifest.tree.children[`${firstChildName}`].inputs.length ?? '';
const manifestEnd =
manifest.tree.children[`${firstChildName}`].inputs[inputsLength - 1]
.timestamp ?? '';
return manifestEnd;
};

export const getManifestInfo = async (
manifestPath: string,
level: number,
functionalUnit: string
manifestPath: string,
level: number,
functionalUnit: string
): Promise<ManifestInfo> => {
const manifest = await openYamlFileAsObject<Manifest>(manifestPath);
const manifest = await openYamlFileAsObject<Manifest>(manifestPath);

// const functionalUnitStub = file.initialize.plugins.sci['global-config']['functional-unit'] ?? '';
// const functionalUnitStub = file.initialize.plugins.sci['global-config']['functional-unit'] ?? '';

const info: ManifestInfo = {
start: getManifestStart(manifest),
end: getManifestEnd(manifest),
hash: GetManifestHash(manifest),
if: GetIfVersion(),
verified: await runIfCheck(manifestPath),
sci: manifest.tree.aggregated.sci ?? 0,
energy: manifest.tree.aggregated.energy ?? 0,
carbon: manifest.tree.aggregated.carbon ?? 0,
level: level,
quality: 1, // quality score not yet functional in IF,
functionalUnit: functionalUnit,
};
console.log(info)
return info;
const info: ManifestInfo = {
start: getManifestStart(manifest),
end: getManifestEnd(manifest),
hash: GetManifestHash(manifest),
if: GetIfVersion(),
verified: await runIfCheck(manifestPath),
sci: manifest.tree.aggregated.sci ?? 0,
energy: manifest.tree.aggregated.energy ?? 0,
carbon: manifest.tree.aggregated.carbon ?? 0,
level: level,
quality: 1, // quality score not yet functional in IF,
functionalUnit: functionalUnit,
};
console.log(info);
return info;
};

export const GetManifestHash = (manifest: Manifest): string => {
const manifestAsString = YAML.dump(manifest).toString();
const manifestAsBytes: Uint8Array = ethers.toUtf8Bytes(manifestAsString);
const manifestHash = ethers.keccak256(manifestAsBytes);
return manifestHash;
const manifestAsString = YAML.dump(manifest).toString();
const manifestAsBytes: Uint8Array = ethers.toUtf8Bytes(manifestAsString);
const manifestHash = ethers.keccak256(manifestAsBytes);
return manifestHash;
};

export const GetIfVersion = (): string => {
return packageJson.version;
return packageJson.version;
};

export const runIfCheck = async (manifestPath: string): Promise<boolean> => {
const response = await execPromise(`npm run if-check -- -m ${manifestPath}`, {
cwd: process.env.CURRENT_DIR || process.cwd(),
});
const response = await execPromise(`npm run if-check -- -m ${manifestPath}`, {
cwd: process.env.CURRENT_DIR || process.cwd(),
});

if (response.stdout.includes('if-check could not verify the manifest')) {
console.log('IF-CHECK: verification was unsuccessful. Files do not match');
return false;
}
console.log('IF-CHECK: verification was successful');
if (response.stdout.includes('if-check could not verify the manifest')) {
console.log('IF-CHECK: verification was unsuccessful. Files do not match');
return false;
}
console.log('IF-CHECK: verification was successful');

return true;
return true;
};
Loading

0 comments on commit 0823ef0

Please sign in to comment.