Skip to content

Commit

Permalink
Prettier fmt the whole project (#6)
Browse files Browse the repository at this point in the history
* Prettier fmt

* Use local prettier config
  • Loading branch information
hwrdtm authored Apr 24, 2023
1 parent 887950b commit 7ddff38
Show file tree
Hide file tree
Showing 8 changed files with 368 additions and 280 deletions.
File renamed without changes.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ A Typescript implementation of [ERC-5573 ReCap](https://eips.ethereum.org/EIPS/e

ReCap can be installed via `npm`:

``` sh
```sh
npm -i siwe-recap
```

`yarn`:

``` sh
```sh
yarn add siwe-recap
```

or `package.json` entry:

``` json
```json
{
"dependencies": {
"siwe-recap": "0.0.1-alpha.0"
Expand All @@ -30,13 +30,15 @@ or `package.json` entry:

ReCaps are designed to be used in conjunction with SIWE. They can be initialized and built as follows:

``` typescript
```typescript
import { Recap } from 'siwe-recap';

const recap = new Recap();

recap.addAttenuation('https://example.com/my/resource', 'crud', 'read');
recap.addAttenuation('https://example.com/my/resource', 'crud', 'update', { maxTimes: 5 });
recap.addAttenuation('https://example.com/my/resource', 'crud', 'update', {
maxTimes: 5,
});
recap.addProof('bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea');

console.log(recap.attenuations);
Expand All @@ -50,7 +52,7 @@ console.log(recap.attenuations);

They can also be applied to or extracted from SIWE messages:

``` typescript
```typescript
import { SiweMessage } from 'siwe';
import { Recap } from 'siwe-recap';

Expand All @@ -60,7 +62,7 @@ const siwe = new SiweMessage( ... );
// the recap listed in `siwe`s resource list
const recap = Recap.extract_and_verify(siwe);

//
//
recap.addAttenuation('https://example.com/my/resource', 'crud', 'read');

// the new change will be merged into the existing recap in `siwe`
Expand Down
13 changes: 8 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { pathsToModuleNameMapper } = require('ts-jest')
const { pathsToModuleNameMapper } = require('ts-jest');
// In the following statement, replace `./tsconfig` with the path to your `tsconfig` file
// which contains the path mapping (ie the `compilerOptions.paths` option):
const { compilerOptions } = require('./tsconfig')
const { compilerOptions } = require('./tsconfig');

/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
Expand All @@ -11,10 +11,13 @@ module.exports = {
modulePathIgnorePatterns: ['<rootDir>/dist/'],
modulePaths: [compilerOptions.baseUrl],
moduleNameMapper: Object.assign(
pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }),
pathsToModuleNameMapper(compilerOptions.paths, {
prefix: '<rootDir>/',
}),
{
'^multiformats(.*)$': '<rootDir>/node_modules/multiformats/dist/index.min.js',
'^multiformats(.*)$':
'<rootDir>/node_modules/multiformats/dist/index.min.js',
'^ethers(.*)$': '<rootDir>/node_modules/ethers/lib/index.js',
}
)
),
};
181 changes: 96 additions & 85 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,99 +9,110 @@ const valid = require('../test/valid.json');
const invalid = require('../test/invalid.json');

describe('Recap Handling', () => {
test('should build a recap', () => {
const recap = new Recap();
test('should build a recap', () => {
const recap = new Recap();

expect(recap.proofs).toEqual([]);
expect(recap.proofs).toEqual([]);

recap.addAttenuation('https://example.com', 'crud', 'read');
expect(recap.attenuations).toEqual({
'https://example.com': { 'crud/read': [{}] },
});
expect(recap.proofs).toEqual([]);
recap.addAttenuation('https://example.com', 'crud', 'read');
expect(recap.attenuations).toEqual({
'https://example.com': { 'crud/read': [{}] },
});
expect(recap.proofs).toEqual([]);

recap.addAttenuation('kepler:example://default/kv', 'kv', 'read');
expect(recap.attenuations).toEqual({
'https://example.com': { 'crud/read': [{}] },
'kepler:example://default/kv': { 'kv/read': [{}] }
});
expect(recap.proofs).toEqual([]);
recap.addAttenuation('kepler:example://default/kv', 'kv', 'read');
expect(recap.attenuations).toEqual({
'https://example.com': { 'crud/read': [{}] },
'kepler:example://default/kv': { 'kv/read': [{}] },
});
expect(recap.proofs).toEqual([]);

recap.addAttenuation('kepler:example://default/kv', 'kv', 'write', { max: 10 });
expect(recap.attenuations).toEqual({
'https://example.com': { 'crud/read': [{}] },
'kepler:example://default/kv': { 'kv/read': [{}], 'kv/write': [{ max: 10 }] }
});
expect(recap.proofs).toEqual([]);
recap.addAttenuation('kepler:example://default/kv', 'kv', 'write', {
max: 10,
});
expect(recap.attenuations).toEqual({
'https://example.com': { 'crud/read': [{}] },
'kepler:example://default/kv': {
'kv/read': [{}],
'kv/write': [{ max: 10 }],
},
});
expect(recap.proofs).toEqual([]);

const cidStr = 'bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea';
const cid = CID.parse(cidStr);
const cidStr =
'bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea';
const cid = CID.parse(cidStr);

recap.addProof(cidStr);
expect(recap.proofs).toEqual([cid]);
});
test('should decode properly', () => {
recap.addProof(cidStr);
expect(recap.proofs).toEqual([cid]);
});
test('should decode properly', () => {
// @ts-ignore
for (const { message, recap } of Object.values(valid).map(
// @ts-ignore
({ message, recap: { att, prf } }) => ({
// @ts-ignore
for (const { message, recap } of Object.values(valid).map(
// @ts-ignore
({ message, recap: { att, prf } }) => ({
// @ts-ignore
message: new SiweMessage(message),
// @ts-ignore
recap: { att, prf: prf.map(CID.decode) }
}))
) {
let decoded;
// @ts-ignore
expect(() => decoded = Recap.extract_and_verify(message)).not.toThrow();
// @ts-ignore
expect(decoded.attenuations).toEqual(recap.att);
// @ts-ignore
let proofs = recap.prf.map(CID.decode);
// @ts-ignore
expect(decoded.proofs).toEqual(proofs);
}
message: new SiweMessage(message),
// @ts-ignore
for (const { message } of Object.values(invalid)) {
expect(() => Recap.extract_and_verify(message)).toThrow();
}
})
})
recap: { att, prf: prf.map(CID.decode) },
})
)) {
let decoded;
// @ts-ignore
expect(() => (decoded = Recap.extract_and_verify(message))).not.toThrow();
// @ts-ignore
expect(decoded.attenuations).toEqual(recap.att);
// @ts-ignore
let proofs = recap.prf.map(CID.decode);
// @ts-ignore
expect(decoded.proofs).toEqual(proofs);
}
// @ts-ignore
for (const { message } of Object.values(invalid)) {
expect(() => Recap.extract_and_verify(message)).toThrow();
}
});
});

describe('Utils', () => {
const unordered = {
c: 1,
b: 2,
ca: 3,
bnested: {
c: [3, 2, 1],
b: 2
}
};
const ordered = {
b: 2,
bnested: {
b: 2,
c: [3, 2, 1]
},
c: 1,
ca: 3
};
const validStrings = ['crud', 'kepler', 'https-proto'];
const validAbilityStrings = ['crud/read', 'kepler/*', 'https/put'];
const invalidAbilityStrings = ['crud', 'crud/read/write', 'with a/space', 'with/a space'];
const unordered = {
c: 1,
b: 2,
ca: 3,
bnested: {
c: [3, 2, 1],
b: 2,
},
};
const ordered = {
b: 2,
bnested: {
b: 2,
c: [3, 2, 1],
},
c: 1,
ca: 3,
};
const validStrings = ['crud', 'kepler', 'https-proto'];
const validAbilityStrings = ['crud/read', 'kepler/*', 'https/put'];
const invalidAbilityStrings = [
'crud',
'crud/read/write',
'with a/space',
'with/a space',
];

test('should test for ordering', () => {
expect(isSorted(ordered)).toBeTruthy();
expect(isSorted(unordered)).toBeFalsy();
})
test('should test for valid strings', () => {
validStrings.forEach(str => expect(validString(str)).toBeTruthy());
validAbilityStrings.forEach((str) => {
expect(validAbString(str)).toBeTruthy();
})
invalidAbilityStrings.forEach((str) => {
expect(validAbString(str)).toBeFalsy();
})
})
})
test('should test for ordering', () => {
expect(isSorted(ordered)).toBeTruthy();
expect(isSorted(unordered)).toBeFalsy();
});
test('should test for valid strings', () => {
validStrings.forEach(str => expect(validString(str)).toBeTruthy());
validAbilityStrings.forEach(str => {
expect(validAbString(str)).toBeTruthy();
});
invalidAbilityStrings.forEach(str => {
expect(validAbString(str)).toBeFalsy();
});
});
});
Loading

0 comments on commit 7ddff38

Please sign in to comment.