Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
feat: convert to typescript (#222)
Browse files Browse the repository at this point in the history
Converts this module to typescript with ESM-only publishing.

Part of the wider effor to move libp2p to the bright new future of proper types and ESM: libp2p/js-libp2p#1021
  • Loading branch information
achingbrain authored Jan 4, 2022
1 parent 508c790 commit 7875906
Show file tree
Hide file tree
Showing 98 changed files with 1,885 additions and 4,408 deletions.
44 changes: 28 additions & 16 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ jobs:
- run: npm run lint
- run: npm run build
- run: npm run dep-check
- uses: ipfs/aegir/actions/bundle-size@master
name: size
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
test-node:
needs: check
runs-on: ${{ matrix.os }}
Expand All @@ -30,37 +26,53 @@ jobs:
fail-fast: true
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npx nyc --reporter=lcov aegir test -t node -- --bail
- run: npm run build
- run: npm run test:node -- --bail --cov
- uses: codecov/codecov-action@v1
test-chrome:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
- run: npm install
- run: npm run test -- -t browser -t webworker --bail
- run: npm run build
- run: npm run test:browser -- -t browser -t webworker --bail
test-firefox:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
- run: npm install
- run: npm run test -- -t browser -t webworker --bail -- --browsers FirefoxHeadless
- run: npm run build
- run: npm run test:browser -- -t browser -t webworker --bail -- --browser firefox
test-electron-main:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
- run: npm install
- run: npx xvfb-maybe aegir test -t electron-main --bail
test-electron-renderer:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx xvfb-maybe aegir test -t electron-renderer --bail
- run: npm run build
- run: npx xvfb-maybe npm run test:electron --bail
# test-electron-renderer:
# needs: check
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - uses: actions/setup-node@v2
# with:
# node-version: 16
# - run: npm install
# - run: npx xvfb-maybe aegir test -t electron-renderer --bail
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-console */
'use strict'
// @ts-expect-error types are missing
const forge = require('node-forge/lib/forge')

/*
* Make sure that every Ed25519 implementation can use keys generated
Expand All @@ -23,7 +24,6 @@ const native = require('ed25519')
const noble = require('@noble/ed25519')
const { subtle } = require('crypto').webcrypto
require('node-forge/lib/ed25519')
const forge = require('node-forge/lib/forge')
const stable = require('@stablelib/ed25519')
const supercopWasm = require('supercop.wasm')

Expand Down
5 changes: 2 additions & 3 deletions benchmarks/ed25519/index.js → benchmarks/ed25519/index.cjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/* eslint-disable no-console */
'use strict'

// @ts-expect-error types are missing
const forge = require('node-forge/lib/forge')
const Benchmark = require('benchmark')
const randomBytes = require('iso-random-stream/src/random')
const native = require('ed25519')
const noble = require('@noble/ed25519')
const { subtle } = require('crypto').webcrypto
require('node-forge/lib/ed25519')
const forge = require('node-forge/lib/forge')
const stable = require('@stablelib/ed25519')
const supercopWasm = require('supercop.wasm')
const ed25519WasmPro = require('ed25519-wasm-pro')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable no-console */
'use strict'
const crypto = require('../src')

const Benchmark = require('benchmark')
const crypto = require('../src')

const suite = new Benchmark.Suite('ephemeral-keys')

Expand Down
4 changes: 1 addition & 3 deletions benchmarks/key-stretcher.js → benchmarks/key-stretcher.cjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/* eslint-disable no-console */
'use strict'
const crypto = require('../src')

const Benchmark = require('benchmark')

const crypto = require('../src')

const suite = new Benchmark.Suite('key-stretcher')

const keys = []
Expand Down
3 changes: 1 addition & 2 deletions benchmarks/rsa.js → benchmarks/rsa.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable no-console */
'use strict'
const crypto = require('../src')

const Benchmark = require('benchmark')
const crypto = require('../src')

const suite = new Benchmark.Suite('rsa')

Expand Down
95 changes: 56 additions & 39 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,62 @@
{
"name": "libp2p-crypto",
"name": "@libp2p/crypto",
"version": "0.21.0",
"description": "Crypto primitives for libp2p",
"main": "src/index.js",
"types": "src/index.d.ts",
"leadMaintainer": "Jacob Heun <[email protected]>",
"browser": {
"./src/aes/ciphers.js": "./src/aes/ciphers-browser.js",
"./src/ciphers/aes-gcm.js": "./src/ciphers/aes-gcm.browser.js",
"./src/hmac/index.js": "./src/hmac/index-browser.js",
"./src/keys/ecdh.js": "./src/keys/ecdh-browser.js",
"./src/keys/rsa.js": "./src/keys/rsa-browser.js"
"type": "module",
"types": "./dist/src/index.d.ts",
"exports": {
".": {
"import": "./dist/src/index.js"
},
"./aes": {
"import": "./dist/src/aes/index.js"
},
"./ciphers": {
"import": "./dist/src/ciphers/index.js"
},
"./hmac": {
"import": "./dist/src/hmac/index.js"
},
"./keys": {
"import": "./dist/src/keys/index.js"
}
},
"files": [
"src",
"dist"
"dist/src",
"!**/*.tsbuildinfo"
],
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
},
"ignorePatterns": [
"src/*.d.ts"
]
},
"scripts": {
"lint": "aegir lint",
"dep-check": "aegir dep-check package.json dep-check src/**/*.js test/**/*.js",
"build": "npm run build:proto && aegir build --no-types",
"build:proto": "pbjs -t static-module -w commonjs -r libp2p-crypto-keys --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/keys/keys.js ./src/keys/keys.proto",
"test": "aegir test",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser -t webworker",
"release": "aegir release",
"release-minor": "aegir release --type minor",
"release-major": "aegir release --type major",
"coverage": "aegir coverage --ignore src/keys/keys.proto.js",
"size": "aegir build --bundlesize --no-types",
"test:types": "npx tsc"
"pretest": "npm run build",
"test": "aegir test -f ./dist/test/**/*.js",
"test:browser": "aegir test -t browser -f ./dist/test/**/*.js",
"test:node": "aegir test -t node -f ./dist/test/**/*.js",
"test:electron": "aegir test -t electron-main -f ./dist/test/**/*.js",
"lint": "aegir ts -p check && aegir lint",
"release": "aegir release --docs",
"release-minor": "aegir release --target node --type minor --docs",
"release-major": "aegir release --type major --docs",
"build": "tsc",
"build:proto": "npm run build:proto:js && npm run build:proto:types",
"build:proto:js": "pbjs -t static-module -w es6 --es6 -r libp2p-crypto-keys --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/keys/keys.js ./src/keys/keys.proto",
"build:proto:types": "pbts -o src/keys/keys.d.ts src/keys/keys.js",
"dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js"
},
"browser": {
"./dist/src/aes/ciphers.js": "./dist/src/aes/ciphers-browser.js",
"./dist/src/ciphers/aes-gcm.js": "./dist/src/ciphers/aes-gcm.browser.js",
"./dist/src/hmac/index.js": "./dist/src/hmac/index-browser.js",
"./dist/src/keys/ecdh.js": "./dist/src/keys/ecdh-browser.js",
"./dist/src/keys/rsa.js": "./dist/src/keys/rsa-browser.js"
},
"keywords": [
"IPFS",
Expand All @@ -40,8 +67,8 @@
],
"license": "MIT",
"dependencies": {
"@noble/ed25519": "^1.3.0",
"@noble/secp256k1": "^1.3.0",
"@noble/ed25519": "^1.3.3",
"@noble/secp256k1": "^1.3.4",
"err-code": "^3.0.1",
"iso-random-stream": "^2.0.0",
"multiformats": "^9.4.5",
Expand All @@ -54,21 +81,11 @@
"aegir": "^36.0.2",
"benchmark": "^2.1.4",
"sinon": "^12.0.1",
"util": "^0.12.3"
},
"aegir": {
"build": {
"bundlesizeMax": "71kB"
}
"util": "^0.12.3",
"wherearewe": "^1.0.0"
},
"engines": {
"node": ">=12.0.0"
},
"eslintConfig": {
"extends": "ipfs",
"ignorePatterns": [
"src/*.d.ts"
]
"node": ">=15.0.0"
},
"repository": {
"type": "git",
Expand Down
17 changes: 0 additions & 17 deletions src/aes/cipher-mode.js

This file was deleted.

15 changes: 15 additions & 0 deletions src/aes/cipher-mode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import errcode from 'err-code'

const CIPHER_MODES = {
16: 'aes-128-ctr',
32: 'aes-256-ctr'
}

export function cipherMode (key: Uint8Array) {
if (key.length === 16 || key.length === 32) {
return CIPHER_MODES[key.length]
}

const modes = Object.entries(CIPHER_MODES).map(([k, v]) => `${k} (${v})`).join(' / ')
throw errcode(new Error(`Invalid key length ${key.length} bytes. Must be ${modes}`), 'ERR_INVALID_KEY_LENGTH')
}
29 changes: 0 additions & 29 deletions src/aes/ciphers-browser.js

This file was deleted.

28 changes: 28 additions & 0 deletions src/aes/ciphers-browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

import 'node-forge/lib/aes.js'
// @ts-expect-error types are missing
import forge from 'node-forge/lib/forge.js'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'

export function createCipheriv (mode: any, key: Uint8Array, iv: Uint8Array) {
const cipher2 = forge.cipher.createCipher('AES-CTR', uint8ArrayToString(key, 'ascii'))
cipher2.start({ iv: uint8ArrayToString(iv, 'ascii') })
return {
update: (data: Uint8Array) => {
cipher2.update(forge.util.createBuffer(uint8ArrayToString(data, 'ascii')))
return uint8ArrayFromString(cipher2.output.getBytes(), 'ascii')
}
}
}

export function createDecipheriv (mode: any, key: Uint8Array, iv: Uint8Array) {
const cipher2 = forge.cipher.createDecipher('AES-CTR', uint8ArrayToString(key, 'ascii'))
cipher2.start({ iv: uint8ArrayToString(iv, 'ascii') })
return {
update: (data: Uint8Array) => {
cipher2.update(forge.util.createBuffer(uint8ArrayToString(data, 'ascii')))
return uint8ArrayFromString(cipher2.output.getBytes(), 'ascii')
}
}
}
8 changes: 0 additions & 8 deletions src/aes/ciphers.js

This file was deleted.

4 changes: 4 additions & 0 deletions src/aes/ciphers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import crypto from 'crypto'

export const createCipheriv = crypto.createCipheriv
export const createDecipheriv = crypto.createDecipheriv
13 changes: 8 additions & 5 deletions src/aes/index.js → src/aes/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use strict'
import * as ciphers from './ciphers.js'
import { cipherMode } from './cipher-mode.js'

const ciphers = require('./ciphers')
const cipherMode = require('./cipher-mode')
export interface AESCipher {
encrypt: (data: Uint8Array) => Promise<Uint8Array>
decrypt: (data: Uint8Array) => Promise<Uint8Array>
}

exports.create = async function (key, iv) { // eslint-disable-line require-await
export async function create (key: Uint8Array, iv: Uint8Array) { // eslint-disable-line require-await
const mode = cipherMode(key)
const cipher = ciphers.createCipheriv(mode, key, iv)
const decipher = ciphers.createDecipheriv(mode, key, iv)

const res = {
const res: AESCipher = {
async encrypt (data) { // eslint-disable-line require-await
return cipher.update(data)
},
Expand Down
Loading

0 comments on commit 7875906

Please sign in to comment.