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

feat: Expose as ESM #24

Merged
merged 4 commits into from Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 0 additions & 3 deletions .eslintrc.js

This file was deleted.

6 changes: 6 additions & 0 deletions .eslintrc.json
@@ -0,0 +1,6 @@
{
"extends": ["3box", "3box/jest", "3box/typescript"],
"parserOptions": {
"project": ["./tsconfig.json"]
}
}
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Use Node 12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- name: Use Node 12
- name: Use Node 16

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we maybe test with both 14 and 16?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup

uses: actions/setup-node@v1
with:
node-version: 12.x
node-version: 16.x

- name: Use cached node_modules
uses: actions/cache@v1
Expand Down
1 change: 1 addition & 0 deletions .prettierrc.json
@@ -0,0 +1 @@
"eslint-config-3box/prettier.config"
7 changes: 0 additions & 7 deletions babel.config.js

This file was deleted.

3 changes: 3 additions & 0 deletions babel.config.json
@@ -0,0 +1,3 @@
{
"presets": [["@babel/preset-env", { "targets": { "node": 16 } }], "@babel/preset-typescript"]
}
8 changes: 8 additions & 0 deletions jest.config.json
@@ -0,0 +1,8 @@
{
"testPathIgnorePatterns": [".*__fixtures__.*"],
"testEnvironment": "node",
"extensionsToTreatAsEsm": [".ts"],
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
}
}
9,814 changes: 4,861 additions & 4,953 deletions package-lock.json

Large diffs are not rendered by default.

47 changes: 23 additions & 24 deletions package.json
Expand Up @@ -2,9 +2,16 @@
"name": "dag-jose",
"version": "1.0.0",
"description": "Typescript implementation of the IPLD dag-jose format",
"main": "lib/index.js",
"main": "./lib/index.js",
"type": "module",
"types": "./lib/index.d.ts",
"exports": {
".": {
"import": "./lib/index.js"
}
},
"scripts": {
"test": "jest --coverage",
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest --coverage",
"build": "./node_modules/.bin/tsc -p tsconfig.json",
"prepublishOnly": "npm run build",
"prebuild": "npm run clean",
Expand Down Expand Up @@ -32,34 +39,26 @@
"@babel/preset-env": "^7.14.4",
"@babel/preset-typescript": "^7.13.0",
"@stablelib/x25519": "^1.0.1",
"@types/jest": "^26.0.23",
"@types/jest": "^27.4.1",
"@types/lodash.transform": "^4.6.6",
"@types/node": "^15.9.0",
"@typescript-eslint/eslint-plugin": "^4.26.0",
"@typescript-eslint/parser": "^4.26.0",
"babel-jest": "^27.0.2",
"blockcodec-to-ipld-format": "^1.0.0",
"did-jwt": "^5.5.2",
"enhanced-resolve": "^5.8.2",
"eslint": "^7.27.0",
"eslint-config-3box": "^0.2.0",
"eslint-plugin-jest": "^24.3.6",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
"babel-jest": "^27.5.1",
"blockcodec-to-ipld-format": "^2.0.0",
"did-jwt": "^6.0.0",
"eslint": "^8.12.0",
"eslint-config-3box": "^0.4.1",
"eslint-plugin-jest": "^26.1.4",
"fast-json-stable-stringify": "^2.1.0",
"ipld": "^0.30.0",
"ipld-in-memory": "^8.0.0",
"jest": "^27.0.3",
"jest": "^27.5.1",
"prettier": "^2.3.0",
"typescript": "^4.3.2"
},
"jest": {
"resolver": "./test/_export_maps_resolver",
"testPathIgnorePatterns": [
".*__fixtures__.*"
],
"testEnvironment": "node"
"typescript": "^4.3.2",
"uint8arrays": "^3.0.0"
},
"dependencies": {
"@ipld/dag-cbor": "^6.0.3",
"multiformats": "^9.0.2"
"@ipld/dag-cbor": "^7.0.1",
"multiformats": "^9.6.4"
}
}
1 change: 0 additions & 1 deletion prettier.config.js

This file was deleted.

14 changes: 4 additions & 10 deletions src/encryption.ts
@@ -1,4 +1,4 @@
import { fromBase64url, toBase64url } from './utils'
import { fromBase64url, toBase64url } from './utils.js'

interface JWERecipient {
encrypted_key?: string
Expand Down Expand Up @@ -30,7 +30,7 @@ export interface EncodedJWE {
unprotected?: Record<string, any>
}

function fromSplit(split: Array<string>): DagJWE {
export function fromSplit(split: Array<string>): DagJWE {
const [protectedHeader, encrypted_key, iv, ciphertext, tag] = split
const jwe: DagJWE = {
ciphertext,
Expand All @@ -49,7 +49,7 @@ function encodeRecipient(recipient: JWERecipient): EncodedRecipient {
return encRec
}

function encode(jwe: DagJWE): EncodedJWE {
export function encode(jwe: DagJWE): EncodedJWE {
const encJwe: EncodedJWE = {
ciphertext: fromBase64url(jwe.ciphertext),
protected: fromBase64url(jwe.protected),
Expand All @@ -69,7 +69,7 @@ function decodeRecipient(encoded: EncodedRecipient): JWERecipient {
return recipient
}

function decode(encoded: EncodedJWE): DagJWE {
export function decode(encoded: EncodedJWE): DagJWE {
const jwe: DagJWE = {
ciphertext: toBase64url(encoded.ciphertext),
protected: toBase64url(encoded.protected),
Expand All @@ -81,9 +81,3 @@ function decode(encoded: EncodedJWE): DagJWE {
if (encoded.unprotected) jwe.unprotected = encoded.unprotected
return jwe
}

export default {
fromSplit,
decode,
encode,
}
8 changes: 4 additions & 4 deletions src/index.ts
@@ -1,7 +1,7 @@
import signing, { DagJWS, EncodedJWS } from './signing'
export type { DagJWS } from './signing'
import encryption, { DagJWE, EncodedJWE } from './encryption'
export type { DagJWE } from './encryption'
import * as signing from './signing.js'
import type { DagJWS, EncodedJWS } from './signing.js'
import * as encryption from './encryption.js'
import type { DagJWE, EncodedJWE } from './encryption.js'
import * as cbor from '@ipld/dag-cbor'
import type { ByteView } from 'multiformats/codecs/interface'

Expand Down
17 changes: 5 additions & 12 deletions src/signing.ts
@@ -1,4 +1,4 @@
import { fromBase64url, toBase64url } from './utils'
import { fromBase64url, toBase64url } from './utils.js'
import { CID } from 'multiformats/cid'

interface JWSSignature {
Expand Down Expand Up @@ -32,7 +32,7 @@ export interface PublicKey {
publicKeyBase64?: string
}

function fromSplit(split: Array<string>): DagJWS {
export function fromSplit(split: Array<string>): DagJWS {
const [protectedHeader, payload, signature] = split
return {
payload,
Expand All @@ -50,18 +50,17 @@ function encodeSignature(signature: JWSSignature): EncodedSignature {
return encoded
}

function encode(jws: DagJWS): EncodedJWS {
export function encode(jws: DagJWS): EncodedJWS {
const payload = fromBase64url(jws.payload)
try {
CID.decode(payload)
} catch (e) {
throw new Error('Not a valid DagJWS')
}
const encodedJws: EncodedJWS = {
return {
payload,
signatures: jws.signatures.map(encodeSignature),
}
return encodedJws
}

function decodeSignature(encoded: EncodedSignature): JWSSignature {
Expand All @@ -73,17 +72,11 @@ function decodeSignature(encoded: EncodedSignature): JWSSignature {
return sign
}

function decode(encoded: EncodedJWS): DagJWS {
export function decode(encoded: EncodedJWS): DagJWS {
const decoded: DagJWS = {
payload: toBase64url(encoded.payload),
signatures: encoded.signatures.map(decodeSignature),
}
decoded.link = CID.decode(new Uint8Array(encoded.payload))
return decoded
}

export default {
fromSplit,
encode,
decode,
}
6 changes: 2 additions & 4 deletions test/__fixtures__/encryption.fixtures.ts
@@ -1,7 +1,7 @@
import { bytes } from 'multiformats'

/*eslint-disable */
const fixtures = {
export const fixtures = {
keys: [
bytes.fromHex('278a5de700e29faae8e40e366ec5012b5ec63d36ec77e8a2417154cc1d25383f'),
bytes.fromHex('62387646793457634876534678c5012b5ec63d36ec77e8a2417154cc1d25383f'),
Expand Down Expand Up @@ -140,6 +140,4 @@ const fixtures = {
multipleRecip: bytes.fromHex('a56269765818f3f3c92467c191b2a33f703edc72bf09f6538392160737746374616750803e4b5f3e3f87518fe1776ff52feef96970726f7465637465644f7b22656e63223a225843323050227d6a6369706865727465787458246dfdc48e4b46a6b77ba5443c9b3538ba47bcff8283c0de99f8f1c15fae56d0994da891bd6a726563697069656e747382a266686561646572a4626976782041674c795961746c4a6e4771586f39466159356173794e4d6a694b664d64745a63616c676f454344482d45532b58433230504b576365706ba36178782b45707255565a424b7a644b575766644a6437724a672d2d5f385a686b414e65356e7a686c653056704179676363727666583235353139636b7479634f4b506374616776365f7532354f747055425756587a76765064707053416d656e637279707465645f6b657958200c5cd81201eb63af7b2dcecc4f29f3bce66f00fc39646085e5c9b0d69ae414daa266686561646572a462697678206c7244625738456c63385330675437695f794a52684c4e516b576c41516a4a3363616c676f454344482d45532b58433230504b576365706ba36178782b7a426475443459577068372d4f5349703346674646325656417a55443778684766792d6a327061347a51416363727666583235353139636b7479634f4b5063746167764b334b396b593331505137476d50625f4741496f76516d656e637279707465645f6b65795820987d496475ef5e759153c355d2493edb6a7007b2e8a188a01026fa6636f77be0')
},
}
/*eslint-enable */

export default fixtures
/*eslint-enable */
6 changes: 2 additions & 4 deletions test/__fixtures__/signing.fixtures.ts
@@ -1,7 +1,7 @@
import { CID, bytes } from 'multiformats'

/*eslint-disable */
const fixtures = {
export const fixtures = {
keys: [{
priv: '278a5de700e29faae8e40e366ec5012b5ec63d36ec77e8a2417154cc1d25383f',
pub: { publicKeyHex: '03fdd57adec3d438ea237fe46b33ee1e016eda6b585c3e27ea66686c2ea5358479' }
Expand Down Expand Up @@ -68,6 +68,4 @@ const fixtures = {
multipleSig: bytes.fromHex('a2677061796c6f6164582401701220debd7adb3ce56544d22a6f6b93396f6980a8067c2cc134f0f7801b6331092b956a7369676e61747572657382a26970726f746563746564507b22616c67223a2245533235364b227d697369676e617475726558404a26065d6ed88be2b16e92252cd9aed25121adac95ef2a5a002e3d180710feaa53b2d656f3d333e82a7c5655045fea95b2062373ef7ed73bcb703625c4eb2bd6a26970726f746563746564507b22616c67223a2245533235364b227d697369676e6174757265584043c3dd4c4e40e4dddad24b4edb035d5329ae987952c4d17d4a2dfc22fcec31a4990badf2430f9b24da4a7fe51e2453c7edc0f363b8cb8361bfbe27a3a7b36a5e'),
},
}
/*eslint-enable */

export default fixtures
/*eslint-enable */
9 changes: 0 additions & 9 deletions test/_export_maps_resolver.js

This file was deleted.

8 changes: 4 additions & 4 deletions test/encryption.test.ts
@@ -1,8 +1,8 @@
/* eslint-env jest */

import encryption from '../src/encryption'
import { DagJWE } from '../src/index'
import fixtures from './__fixtures__/encryption.fixtures'
import * as encryption from '../src/encryption'
import type { DagJWE } from '../src/encryption.js'
import { fixtures } from './__fixtures__/encryption.fixtures'
import { CID } from 'multiformats/cid'
import {
xc20pDirEncrypter,
Expand All @@ -12,7 +12,7 @@ import {
createJWE,
decryptJWE,
Encrypter,
Decrypter
Decrypter,
} from 'did-jwt'
import { generateKeyPairFromSeed } from '@stablelib/x25519'

Expand Down
12 changes: 7 additions & 5 deletions test/index.test.ts
@@ -1,9 +1,9 @@
/* eslint-env jest */

import * as dagJose from '../src/index'
import { createDagJWS } from './signing.test'
import sFixtures from './__fixtures__/signing.fixtures'
import eFixtures from './__fixtures__/encryption.fixtures'
import * as dagJose from '../src/index.js'
import { createDagJWS } from './signing.test.js'
import { fixtures as sFixtures } from './__fixtures__/signing.fixtures'
import { fixtures as eFixtures } from './__fixtures__/encryption.fixtures'
import { convert as toLegacyIpld } from 'blockcodec-to-ipld-format'
import IPLD from 'ipld'
import ipldInMem from 'ipld-in-memory'
Expand Down Expand Up @@ -45,7 +45,9 @@ describe('dag-jose codec', () => {
const cidPayload = CID.parse('bagcqcera73rupyla6bauseyk75rslfys3st25spm75ykhvgusqvv2zfqtucq')
const dagJws = await createDagJWS(cidPayload, signer)
const cid = await ipld.put(dagJws, format.codec)
expect(cid.toString()).toEqual('bagcqcera5p4hvkei322lg3hp3dvrmndlojwcst3gvq2nhledmv4plt2ore2q')
expect(cid.toString()).toEqual(
'bagcqcera5p4hvkei322lg3hp3dvrmndlojwcst3gvq2nhledmv4plt2ore2q'
)
const data = await ipld.get(cid)
expect(data).toEqual(sFixtures.dagJws.oneSig[0])
})
Expand Down
11 changes: 6 additions & 5 deletions test/signing.test.ts
@@ -1,12 +1,13 @@
/* eslint-env jest */

import signing, { PublicKey} from '../src/signing'
import { DagJWS } from '../src/index'
import * as signing from '../src/signing.js'
import type { PublicKey, DagJWS } from '../src/signing.js'
import { toBase64url } from '../src/utils'
import fixtures from './__fixtures__/signing.fixtures'
import { fixtures } from './__fixtures__/signing.fixtures'
import { CID, bytes } from 'multiformats'
import { ES256KSigner, Signer, createJWS, verifyJWS } from 'did-jwt'
import stringify from 'fast-json-stable-stringify'
import { fromString } from 'uint8arrays'

export async function createDagJWS(
cid: CID,
Expand Down Expand Up @@ -38,8 +39,8 @@ describe('Signing support', () => {
let signer1, signer2

beforeAll(() => {
signer1 = ES256KSigner(fixtures.keys[0].priv)
signer2 = ES256KSigner(fixtures.keys[1].priv)
signer1 = ES256KSigner(fromString(fixtures.keys[0].priv, 'base16'))
signer2 = ES256KSigner(fromString(fixtures.keys[1].priv, 'base16'))
})

describe('fromSplit', () => {
Expand Down
8 changes: 5 additions & 3 deletions tsconfig.json
@@ -1,15 +1,17 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["dom", "es2017"],
"target": "es6",
"module": "ES2020",
"target": "ES2020",
"declaration": true,
"noImplicitAny": true,
"removeComments": true,
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"resolveJsonModule": false,
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true,
"outDir": "lib",
"rootDir": "src"
},
Expand Down