Skip to content

Commit

Permalink
Support compiling for ESM and CJS + fix tests (#7)
Browse files Browse the repository at this point in the history
* Compile for both ESM and CJS.

* Fix tests

* Prettier fmt whole project
  • Loading branch information
hwrdtm authored Apr 24, 2023
1 parent 7ddff38 commit 99fe7d0
Show file tree
Hide file tree
Showing 8 changed files with 843 additions and 57 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ recap.addAttenuation('https://example.com/my/resource', 'crud', 'read');
recap.add_to_siwe_messate(siwe);
```

## Compilation

In order to generate the ECMAScript, CommonJS and TypeScript declaration files, run `yarn compile`.

## Disclaimer

This package is still ALPHA software, bugs may exist and APIs may change in future. This library for has not yet undergone a formal security audit. We welcome continued feedback on the usability, architecture, and security of this implementation.
23 changes: 0 additions & 23 deletions jest.config.js

This file was deleted.

57 changes: 48 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,73 @@
"name": "siwe-recap",
"version": "0.0.1-alpha.0",
"description": "A Typescript implementation of EIP-5573 utilities",
"main": "dist/index.js",
"types": "dist/siwe.d.ts",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"license": "MIT",
"author": "Spruce Systems Inc.",
"scripts": {
"build": "tsc",
"test": "jest",
"build:cjs": "esbuild --bundle --target=es2019 --format=cjs --outfile=dist/index.cjs src/index.ts",
"build:esm": "esbuild --bundle --target=es2019 --format=esm --outfile=dist/index.mjs src/index.ts",
"build:types": "dts-bundle-generator --out-file=dist/index.d.ts src/index.ts",
"clean": "rm -rf dist",
"compile": "run-s clean build:*",
"format": "prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\"",
"lint": "eslint --ext .js,.ts .",
"format": "prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\""
"test": "run-s test:*",
"test:lib": "cross-env NODE_OPTIONS=--experimental-vm-modules jest"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"jest": {
"preset": "ts-jest/presets/default-esm",
"globals": {
"ts-jest": {
"useESM": true
}
},
"verbose": true,
"testTimeout": 30000,
"testMatch": [
"**/*.test.[jt]s?(x)"
],
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
}
},
"devDependencies": {
"@types/jest": "^29.5.0",
"@typescript-eslint/eslint-plugin": "^5.23.0",
"@typescript-eslint/parser": "^5.23.0",
"cross-env": "^7.0.3",
"dts-bundle-generator": "^8.0.1",
"esbuild": "^0.17.17",
"eslint": "^8.38.0",
"eslint-config-prettier": "^8.5.0",
"ethers": "^5.5.1",
"jest": "^29.5.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.8.7",
"ts-jest": "^29.1.0",
"typescript": "^5.0.2",
"ethers": "^5.5.1"
"typescript": "^5.0.2"
},
"dependencies": {
"multiformats": "^11.0.2",
"canonicalize": "^2.0.0",
"multiformats": "^11.0.2",
"siwe": "^2.0.5"
},
"peerDependencies": {
"ethers": "^5.5.1"
}
},
"type": "module"
}
9 changes: 5 additions & 4 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { SiweMessage } from 'siwe';
import { Recap } from './index';
import { validAbString, isSorted, validString } from './utils';

const jsonCap = require('../test/serialized_cap.json');
const valid = require('../test/valid.json');
const invalid = require('../test/invalid.json');
import valid from '../test/valid.json' assert { type: 'json' };
import invalid from '../test/invalid.json' assert { type: 'json' };

describe('Recap Handling', () => {
test('should build a recap', () => {
Expand Down Expand Up @@ -68,7 +67,9 @@ describe('Recap Handling', () => {
expect(decoded.proofs).toEqual(proofs);
}
// @ts-ignore
for (const { message } of Object.values(invalid)) {
for (const { message } of Object.values(invalid).map(({ message }) => ({
message: new SiweMessage(message),
}))) {
expect(() => Recap.extract_and_verify(message)).toThrow();
}
});
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
} from './utils';
import { SiweMessage } from 'siwe';

export * from './utils';

export { AttObj, PlainJSON, CID };
const urnRecapPrefix = 'urn:recap:';

Expand Down Expand Up @@ -58,6 +60,9 @@ export class Recap {
}
}

// Strip trailing statement at the end.
statement = statement.slice(0, -1);

return statement;
}

Expand Down
38 changes: 35 additions & 3 deletions test/invalid.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
{
"withoutCaps": {
"message": "example.com wants you to sign in with your Ethereum account:\n0x0000000000000000000000000000000000000000\n\n\nURI: did:key:example\nVersion: 1\nChain ID: 1\nNonce: mynonce1\nIssued At: 2022-06-21T12:00:00.000Z"
"message": {
"domain": "example.com",
"address": "0x0000000000000000000000000000000000000000",
"uri": "did:key:example",
"version": "1",
"chainId": 1,
"nonce": "mynonce1",
"issuedAt": "2022-06-21T12:00:00.000Z"
}
},
"withStatementNoCaps": {
"message": "example.com wants you to sign in with your Ethereum account:\n0x0000000000000000000000000000000000000000\n\nSome custom statement.\n\nURI: did:key:example\nVersion: 1\nChain ID: 1\nNonce: mynonce1\nIssued At: 2022-06-21T12:00:00.000Z"
"message": {
"domain": "example.com",
"address": "0x0000000000000000000000000000000000000000",
"statement": "Some custom statement.",
"uri": "did:key:example",
"version": "1",
"chainId": 1,
"nonce": "mynonce1",
"issuedAt": "2022-06-21T12:00:00.000Z"
}
},
"interleavedResources": {
"message": "example.com wants you to sign in with your Ethereum account:\n0x0000000000000000000000000000000000000000\n\nI further authorize the stated URI to perform the following actions on my behalf: (1) \"credential\": \"present\" for \"credential:*\". (2) \"kv\": \"get\", \"list\", \"metadata\" for \"kepler:ens:example.eth://default/\".\n\nURI: did:key:example\nVersion: 1\nChain ID: 1\nNonce: mynonce1\nIssued At: 2022-06-21T12:00:00.000Z\nResources:\n- http://example.com\n- urn:recap:eyJhdHQiOnsiY3JlZGVudGlhbDoqIjp7ImNyZWRlbnRpYWwvcHJlc2VudCI6W119LCJrZXBsZXI6ZW5zOmV4YW1wbGUuZXRoOi8vZGVmYXVsdC8iOnsia3YvZ2V0IjpbXSwia3YvbGlzdCI6W10sImt2L21ldGFkYXRhIjpbXX19LCJwcmYiOltdfQ\n- ftp://example.com\n- ssh://[email protected]"
"message": {
"domain": "example.com",
"address": "0x0000000000000000000000000000000000000000",
"statement": "I further authorize the stated URI to perform the following actions on my behalf: (1) \"credential\": \"present\" for \"credential:*\". (2) \"kv\": \"get\", \"list\", \"metadata\" for \"kepler:ens:example.eth://default/\".",
"uri": "did:key:example",
"version": "1",
"chainId": 1,
"nonce": "mynonce1",
"issuedAt": "2022-06-21T12:00:00.000Z",
"resourtes": [
"http://example.com",
"urn:recap:eyJhdHQiOnsiY3JlZGVudGlhbDoqIjp7ImNyZWRlbnRpYWwvcHJlc2VudCI6W119LCJrZXBsZXI6ZW5zOmV4YW1wbGUuZXRoOi8vZGVmYXVsdC8iOnsia3YvZ2V0IjpbXSwia3YvbGlzdCI6W10sImt2L21ldGFkYXRhIjpbXX19LCJwcmYiOltdfQ",
"ftp://example.com",
"ssh://[email protected]"
]
}
}
}
25 changes: 12 additions & 13 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{
"include": ["src/**/*.ts"],
"exclude": ["node_modules/", "dist", "src/**/*.test.ts"],
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"baseUrl": "./src",
"paths": {
"multiformats": ["../node_modules/multiformats/dist"]
},
"declaration": true,
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"include": ["src/*"],
"exclude": ["src/**/*.test.ts"]
"noUnusedLocals": true,
"target": "ES2015",
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"esModuleInterop": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"baseUrl": ".",
"outDir": "dist"
}
}
Loading

0 comments on commit 99fe7d0

Please sign in to comment.