Migrate to rollup. FIx sha256 browser dependency. Remove esbuild. Add esm browser build (#27)

This commit is contained in:
Dimasik Kolezhniuk
2023-10-15 11:15:42 +02:00
committed by GitHub
parent 89623bbbaf
commit 58b361dd26
13 changed files with 2559 additions and 1255 deletions

67
config/rollup.config.mjs Normal file
View File

@@ -0,0 +1,67 @@
import commonJS from '@rollup/plugin-commonjs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
import typescript from '@rollup/plugin-typescript';
import tsConfig from '../tsconfig.json' assert { type: 'json' };
import packageJson from '../package.json' assert { type: 'json' };
const external = Object.keys(packageJson.peerDependencies || {});
const config = {
input: 'src/index.ts',
external,
output: [
{
format: 'es',
file: packageJson.exports['.'].browser,
sourcemap: true
}
],
treeshake: {
preset: 'smallest'
}
};
export default [
// esm browser
{
...config,
plugins: [
typescript({
compilerOptions: {
...tsConfig.compilerOptions
}
}),
commonJS(),
nodeResolve({
browser: true
}),
terser()
]
},
// umd browser
{
...config,
external: [],
plugins: [
typescript({
compilerOptions: {
...tsConfig.compilerOptions
}
}),
commonJS(),
nodeResolve({
browser: true
}),
terser()
],
output: [
{
format: 'iife',
file: packageJson.exports['.'].umd,
name: 'Iden3Core',
sourcemap: true
}
]
}
];

View File

@@ -1,7 +0,0 @@
{
"extends": "../tsconfig",
"compilerOptions": {
"module": "ES2020",
"outDir": "../dist/esm"
}
}

View File

@@ -1,25 +1,36 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head>
<head> <meta charset="UTF-8" />
<meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="X-UA-Compatible" <meta name="viewport" content="width=device-width, initial-scale=1.0" />
content="IE=edge"> <script src="./dist/browser/umd/index.js"></script>
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<script src="./dist/umd/index.js"></script>
<title>Test</title> <title>Test</title>
</head> <script type="importmap">
{
"imports": {
"@iden3/js-crypto": "./node_modules/@iden3/js-crypto/dist/browser/esm/index.js"
}
}
</script>
</head>
<body> <body>
Test UMD script work Test UMD/ESM script work
</body>
<script>
const claim = Iden3Core.Claim.newClaim(new Iden3Core.SchemaHash(), Iden3Core.ClaimOptions.withFlagUpdatable(true));
const { index, value } = claim.rawSlots();
console.log(index, value, claim);
console.log(claim.hIndex());
console.assert(claim.value.length === 4);
</script>
<script type="module">
import * as esm from './dist/browser/esm/index.js';
function test(module) {
const { Claim, SchemaHash, ClaimOptions, BytesHelper } = module;
const claim = Claim.newClaim(new SchemaHash(), ClaimOptions.withFlagUpdatable(true));
const { index, value } = claim.rawSlots();
console.log(index, value, claim);
console.log(claim.hIndex());
console.assert(claim.value.length === 4);
console.log(BytesHelper.hashBytes('test'));
}
test(esm);
test(Iden3Core);
</script>
</body>
</html> </html>

3568
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,25 +1,33 @@
{ {
"name": "@iden3/js-iden3-core", "name": "@iden3/js-iden3-core",
"version": "1.0.2", "version": "1.0.3",
"description": "Low level API to create and manipulate iden3 Claims.", "description": "Low level API to create and manipulate iden3 Claims.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"source": "./src/index.ts", "source": "./src/index.ts",
"esm:esbuild": "dist/esm_esbuild/index.js",
"typings": "dist/types/index.d.ts", "typings": "dist/types/index.d.ts",
"main": "dist/node/cjs/index.js",
"module": "dist/node/esm/index.js",
"exports": {
".": {
"node": {
"import": "./dist/node/esm/index.js",
"require": "./dist/node/cjs/index.js"
},
"browser": "./dist/browser/esm/index.js",
"umd": "./dist/browser/umd/index.js"
}
},
"files": [ "files": [
"dist" "dist"
], ],
"scripts": { "scripts": {
"clean": "rimraf ./dist", "clean": "rimraf ./dist",
"build": "npm run clean && npm run build:esm && npm run build:umd && npm run build:esm:esbuild && npm run build:tsc", "build": "npm run clean && npm run build:node && npm run build:browser",
"build:esm:esbuild": "node ./scripts/esm.config.js", "build:node": "npm run build:tsc && npm run build:esm",
"build:esm": "tsc -p config/tsconfig.esm.json", "build:esm": "tsc --outDir dist/node/esm --declaration --declarationDir dist/types",
"build:umd": "node ./scripts/umd.config.js", "build:browser": "rollup -c config/rollup.config.mjs",
"build:tsc": "tsc --module commonjs", "build:tsc": "tsc --module commonjs --outDir dist/node/cjs",
"test:coverage": "jest --coverage", "deps:check": "madge --warning --circular --extensions ts ./",
"test": "jest", "test": "jest",
"tsc": "tsc",
"test:watch": "jest --watch", "test:watch": "jest --watch",
"lint": "eslint --fix --ext .ts src/** tests/**", "lint": "eslint --fix --ext .ts src/** tests/**",
"format": "prettier --config .prettierrc './**/*.ts' --write" "format": "prettier --config .prettierrc './**/*.ts' --write"
@@ -47,25 +55,23 @@
"homepage": "https://github.com/iden3/js-iden3-core#readme", "homepage": "https://github.com/iden3/js-iden3-core#readme",
"devDependencies": { "devDependencies": {
"@iden3/eslint-config": "https://github.com/iden3/eslint-config", "@iden3/eslint-config": "https://github.com/iden3/eslint-config",
"@esbuild-plugins/node-globals-polyfill": "^0.1.1", "@rollup/plugin-commonjs": "^25.0.4",
"@esbuild-plugins/node-modules-polyfill": "^0.1.4", "@rollup/plugin-node-resolve": "^15.2.1",
"@rollup/plugin-terser": "^0.4.3",
"@rollup/plugin-typescript": "^11.1.4",
"@types/jest": "^29.2.0", "@types/jest": "^29.2.0",
"@typescript-eslint/eslint-plugin": "^5.41.0", "@typescript-eslint/eslint-plugin": "^5.41.0",
"esbuild": "^0.15.15",
"esbuild-node-externals": "^1.5.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.2.2", "jest": "^29.2.2",
"madge": "^6.1.0",
"prettier": "^2.7.1", "prettier": "^2.7.1",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"rollup": "^3.29.4",
"ts-jest": "^29.0.3", "ts-jest": "^29.0.3",
"ts-loader": "^9.4.1", "ts-loader": "^9.4.1",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",
"typescript": "^4.7.4" "typescript": "^4.7.4"
}, },
"dependencies": { "peerDependencies": {
"@iden3/js-crypto": "1.0.1", "@iden3/js-crypto": "1.0.2"
"base58-js": "^1.0.4",
"cross-sha256": "^1.2.0"
} }
} }

View File

@@ -1,19 +0,0 @@
const NodeModulesPolyfills = require('@esbuild-plugins/node-modules-polyfill').default;
const NodeGlobalPolyfills = require('@esbuild-plugins/node-globals-polyfill').default;
const pkg = require('../package.json');
const esmConfig = {
plugins: [
NodeGlobalPolyfills({
process: true,
buffer: true
}),
NodeModulesPolyfills()
],
entryPoints: ['src/index.ts'],
bundle: true,
sourcemap: true,
target: 'es2020',
outfile: pkg['esm:esbuild'],
format: 'esm',
};
module.exports = esmConfig;

View File

@@ -1,3 +0,0 @@
const esbuild = require('esbuild');
const baseConfig = require('./base.config');
esbuild.build(baseConfig).catch(() => process.exit(1));

View File

@@ -1,16 +0,0 @@
const esbuild = require('esbuild');
const baseConfig = require('./base.config');
const pkg = require('../package.json');
const name = 'Iden3Core';
esbuild.build({
...baseConfig,
minify: true,
format: 'iife',
outfile: pkg.main.replace('cjs', 'umd'),
globalName: name
}).catch((err) => {
console.error(err);
return process.exit(1);
});

View File

@@ -16,10 +16,11 @@ import {
} from './did-helper'; } from './did-helper';
import { Parser } from './did-parser'; import { Parser } from './did-parser';
import { IDID, Param } from './types'; import { IDID, Param } from './types';
import { sha256 } from 'cross-sha256'; import { sha256 } from '@iden3/js-crypto';
import { encoder } from '../utils';
// DID Decentralized Identifiers (DIDs) // DID Decentralized Identifiers (DIDs)
// https://w3c.github.io/did-core/#did-syntax // https://w3c.github.io/did-core/#did-syntax
export class DID { export class DID {
method = ''; method = '';
id = ''; id = '';
@@ -199,7 +200,7 @@ export class DID {
} }
static idFromUnsupportedDID(did: DID): Id { static idFromUnsupportedDID(did: DID): Id {
const hash = Uint8Array.from(new sha256().update(did.string()).digest()); const hash = sha256(encoder.encode(did.string()));
const genesis = new Uint8Array(27); const genesis = new Uint8Array(27);
const idSlice = hash.slice(hash.length - Constants.GENESIS_LENGTH); const idSlice = hash.slice(hash.length - Constants.GENESIS_LENGTH);

View File

@@ -1,7 +1,6 @@
import { Constants } from './constants'; import { Constants } from './constants';
import { sha256 } from 'cross-sha256'; import { checkBigIntInField, fromLittleEndian, toLittleEndian, encoder } from './utils';
import { checkBigIntInField, fromLittleEndian, toLittleEndian } from './utils'; import { Hex, sha256 } from '@iden3/js-crypto';
import { Hex } from '@iden3/js-crypto';
export class BytesHelper { export class BytesHelper {
static intToBytes(int: bigint): Uint8Array { static intToBytes(int: bigint): Uint8Array {
return BytesHelper.intToNBytes(int, Constants.BYTES_LENGTH); return BytesHelper.intToNBytes(int, Constants.BYTES_LENGTH);
@@ -43,13 +42,12 @@ export class BytesHelper {
} }
static hashBytes(str: string): Uint8Array { static hashBytes(str: string): Uint8Array {
const hash = new sha256().update(str).digest(); const hash = sha256(encoder.encode(str));
return new Uint8Array(hash); return new Uint8Array(hash);
} }
static hexToBytes(str: string): Uint8Array { static hexToBytes(str: string): Uint8Array {
const buffer = Buffer.from(str, 'hex'); return Hex.decodeString(str);
return Uint8Array.from(buffer);
} }
static bytesToHex(bytes: Uint8Array) { static bytesToHex(bytes: Uint8Array) {

View File

@@ -1,9 +1,7 @@
import { Constants } from './constants'; import { Constants } from './constants';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const base58Js = require('base58-js');
import { fromLittleEndian } from './utils'; import { fromLittleEndian } from './utils';
import { BytesHelper, ElemBytes } from './elemBytes'; import { BytesHelper, ElemBytes } from './elemBytes';
import { poseidon } from '@iden3/js-crypto'; import { poseidon, base58ToBytes, base58FromBytes } from '@iden3/js-crypto';
// ID is a byte array with // ID is a byte array with
// [ type | root_genesis | checksum ] // [ type | root_genesis | checksum ]
@@ -30,7 +28,7 @@ export class Id {
} }
string(): string { string(): string {
return base58Js.binary_to_base58(this._bytes); return base58FromBytes(this._bytes);
} }
get bytes(): Uint8Array { get bytes(): Uint8Array {
@@ -81,7 +79,7 @@ export class Id {
} }
static fromString(s: string): Id { static fromString(s: string): Id {
const bytes = base58Js.base58_to_binary(s); const bytes = base58ToBytes(s);
return Id.fromBytes(bytes); return Id.fromBytes(bytes);
} }

View File

@@ -1,6 +1,7 @@
import { poseidon } from '@iden3/js-crypto'; import { poseidon } from '@iden3/js-crypto';
import { Constants } from './constants'; import { Constants } from './constants';
// eslint-disable-next-line @typescript-eslint/no-var-requires // eslint-disable-next-line @typescript-eslint/no-var-requires
export const encoder = new TextEncoder();
export function fromLittleEndian(bytes: Uint8Array): bigint { export function fromLittleEndian(bytes: Uint8Array): bigint {
const n256 = BigInt(256); const n256 = BigInt(256);

View File

@@ -2,22 +2,15 @@
"compilerOptions": { "compilerOptions": {
"moduleResolution": "node", "moduleResolution": "node",
"target": "es2020", "target": "es2020",
"module": "es6", "module": "ES2020",
"lib": ["es2020", "dom"], "lib": ["es2020", "dom"],
"allowJs": true, "allowJs": true,
"esModuleInterop": true, "esModuleInterop": true,
"strict": true, "strict": true,
"sourceMap": true,
"skipLibCheck": true, "skipLibCheck": true,
"declaration": true,
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"resolveJsonModule": true, "resolveJsonModule": true,
"declarationDir": "dist/types", "typeRoots": ["node_modules/@types"]
"outDir": "dist/cjs",
"typeRoots": [
"node_modules/@types"
]
}, },
"include": [ "include": [
"src" "src"