Skip to content

Commit

Permalink
fix: finalize transition to ES modules (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
ukstv authored Jan 10, 2022
1 parent d35d537 commit 2de6ac9
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 33 deletions.
18 changes: 0 additions & 18 deletions .eslintrc.js

This file was deleted.

18 changes: 18 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"env": {
"node": true,
"jest/globals": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": ["@typescript-eslint", "jest"],
"rules": {}
}
3 changes: 0 additions & 3 deletions babel.config.js

This file was deleted.

3 changes: 3 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["@babel/preset-env", { "targets": { "node": "current" } }], "@babel/preset-typescript"]
}
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "did-jwt",
"version": "5.12.1",
"description": "Library for Signing and Verifying JWTs that use DIDs as issuers and JWEs that use DIDs as recipients",
"type": "module",
"source": "src/index.ts",
"main": "./lib/index.js",
"main": "./lib/index.cjs",
"module": "./lib/index.module.js",
"unpkg": "./lib/index.umd.js",
"types": "./lib/index.d.ts",
Expand All @@ -13,11 +14,17 @@
"dist",
"src"
],
"exports": {
".": {
"require": "./lib/index.cjs",
"import": "./lib/index.module.js"
}
},
"scripts": {
"test": "jest",
"test:ci": "jest --coverage && codecov",
"build:js": "microbundle --compress=false",
"build:browser": "webpack --config webpack.config.js",
"build:browser": "webpack --config webpack.config.cjs",
"build": "yarn build:js && yarn test && yarn build:browser",
"build:docs": "echo 'PLEASE UPDATE REFERENCE DOCS MANUALLY'",
"format": "prettier --write \"src/**/*.ts\"",
Expand Down
4 changes: 2 additions & 2 deletions src/Digest.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { hash } from '@stablelib/sha256'
import * as u8a from 'uint8arrays'
import { keccak_256 } from 'js-sha3' // eslint-disable-line
import sha3 from 'js-sha3'

export function sha256(payload: string | Uint8Array): Uint8Array {
const data = typeof payload === 'string' ? u8a.fromString(payload) : payload
return hash(data)
}

export function keccak(data: Uint8Array): Uint8Array {
return new Uint8Array(keccak_256.arrayBuffer(data))
return new Uint8Array(sha3.keccak_256.arrayBuffer(data))
}

export function toEthereumAddress(hexPublicKey: string): string {
Expand Down
5 changes: 3 additions & 2 deletions src/VerifierAlgorithm.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ec as EC, SignatureInput } from 'elliptic'
import type { SignatureInput } from 'elliptic'
import elliptic from 'elliptic'
import { sha256, toEthereumAddress } from './Digest'
import { verify } from '@stablelib/ed25519'
import type { VerificationMethod } from 'did-resolver'
import { bases } from 'multiformats/basics'
import { hexToBytes, base58ToBytes, base64ToBytes, bytesToHex, EcdsaSignature, stringToBytes } from './util'
import { verifyBlockchainAccountId } from './blockchains'

const secp256k1 = new EC('secp256k1')
const secp256k1 = new elliptic.ec('secp256k1')

// converts a JOSE signature to it's components
export function toSignatureObject(signature: string, recoverable = false): EcdsaSignature {
Expand Down
4 changes: 3 additions & 1 deletion src/blockchains/cosmos.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ec as EC } from 'elliptic'
import elliptic from 'elliptic'
import { bech32 } from 'bech32'
import * as u8a from 'uint8arrays'
import { sha256 } from '../Digest'
import { Ripemd160 } from './utils/ripemd160'

const EC = elliptic.ec

export const publicKeyToAddress = (publicKey: string, prefix: string): string => {
const ec = new EC('secp256k1')
const compressedPublicKey = ec.keyFromPublic(publicKey, 'hex').getPublic().encode('hex', true)
Expand Down
8 changes: 4 additions & 4 deletions src/signers/ES256KSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { parseKey, leftpad } from '../util'
import { toJose } from '../util'
import { Signer } from '../JWT'
import { sha256 } from '../Digest'
import elliptic from 'elliptic'

import { ec as EC, ec } from 'elliptic'
const secp256k1: EC = new EC('secp256k1')
const secp256k1 = new elliptic.ec('secp256k1')

/**
* Creates a configured signer function for signing data using the ES256K (secp256k1 + sha256) algorithm.
Expand All @@ -26,10 +26,10 @@ export function ES256KSigner(privateKey: string | Uint8Array, recoverable = fals
if (privateKeyBytes.length !== 32) {
throw new Error(`bad_key: Invalid private key format. Expecting 32 bytes, but got ${privateKeyBytes.length}`)
}
const keyPair: ec.KeyPair = secp256k1.keyFromPrivate(privateKeyBytes)
const keyPair: elliptic.ec.KeyPair = secp256k1.keyFromPrivate(privateKeyBytes)

return async (data: string | Uint8Array): Promise<string> => {
const { r, s, recoveryParam }: EC.Signature = keyPair.sign(sha256(data))
const { r, s, recoveryParam }: elliptic.ec.Signature = keyPair.sign(sha256(data))
return toJose(
{
r: leftpad(r.toString('hex')),
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js → webpack.config.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path')

module.exports = {
entry: './lib/index.js',
entry: './lib/index.cjs',
mode: 'production',
resolve: {
fallback: {
Expand Down

0 comments on commit 2de6ac9

Please sign in to comment.