mirror of
https://github.com/iden3/js-iden3-core.git
synced 2026-01-08 21:37:58 -05:00
Migrate to rollup. FIx sha256 browser dependency. Remove esbuild. Add esm browser build (#27)
This commit is contained in:
committed by
GitHub
parent
89623bbbaf
commit
58b361dd26
67
config/rollup.config.mjs
Normal file
67
config/rollup.config.mjs
Normal 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
|
||||
}
|
||||
]
|
||||
}
|
||||
];
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"extends": "../tsconfig",
|
||||
"compilerOptions": {
|
||||
"module": "ES2020",
|
||||
"outDir": "../dist/esm"
|
||||
}
|
||||
}
|
||||
49
index.html
49
index.html
@@ -1,25 +1,36 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible"
|
||||
content="IE=edge">
|
||||
<meta name="viewport"
|
||||
content="width=device-width, initial-scale=1.0">
|
||||
<script src="./dist/umd/index.js"></script>
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<script src="./dist/browser/umd/index.js"></script>
|
||||
<title>Test</title>
|
||||
</head>
|
||||
<script type="importmap">
|
||||
{
|
||||
"imports": {
|
||||
"@iden3/js-crypto": "./node_modules/@iden3/js-crypto/dist/browser/esm/index.js"
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
Test UMD 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>
|
||||
<body>
|
||||
Test UMD/ESM script work
|
||||
|
||||
<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>
|
||||
|
||||
3568
package-lock.json
generated
3568
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
48
package.json
48
package.json
@@ -1,25 +1,33 @@
|
||||
{
|
||||
"name": "@iden3/js-iden3-core",
|
||||
"version": "1.0.2",
|
||||
"version": "1.0.3",
|
||||
"description": "Low level API to create and manipulate iden3 Claims.",
|
||||
"main": "dist/cjs/index.js",
|
||||
"module": "dist/esm/index.js",
|
||||
"source": "./src/index.ts",
|
||||
"esm:esbuild": "dist/esm_esbuild/index.js",
|
||||
"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": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"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:esm:esbuild": "node ./scripts/esm.config.js",
|
||||
"build:esm": "tsc -p config/tsconfig.esm.json",
|
||||
"build:umd": "node ./scripts/umd.config.js",
|
||||
"build:tsc": "tsc --module commonjs",
|
||||
"test:coverage": "jest --coverage",
|
||||
"build": "npm run clean && npm run build:node && npm run build:browser",
|
||||
"build:node": "npm run build:tsc && npm run build:esm",
|
||||
"build:esm": "tsc --outDir dist/node/esm --declaration --declarationDir dist/types",
|
||||
"build:browser": "rollup -c config/rollup.config.mjs",
|
||||
"build:tsc": "tsc --module commonjs --outDir dist/node/cjs",
|
||||
"deps:check": "madge --warning --circular --extensions ts ./",
|
||||
"test": "jest",
|
||||
"tsc": "tsc",
|
||||
"test:watch": "jest --watch",
|
||||
"lint": "eslint --fix --ext .ts src/** tests/**",
|
||||
"format": "prettier --config .prettierrc './**/*.ts' --write"
|
||||
@@ -47,25 +55,23 @@
|
||||
"homepage": "https://github.com/iden3/js-iden3-core#readme",
|
||||
"devDependencies": {
|
||||
"@iden3/eslint-config": "https://github.com/iden3/eslint-config",
|
||||
"@esbuild-plugins/node-globals-polyfill": "^0.1.1",
|
||||
"@esbuild-plugins/node-modules-polyfill": "^0.1.4",
|
||||
"@rollup/plugin-commonjs": "^25.0.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",
|
||||
"@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",
|
||||
"madge": "^6.1.0",
|
||||
"prettier": "^2.7.1",
|
||||
"rimraf": "^3.0.2",
|
||||
"rollup": "^3.29.4",
|
||||
"ts-jest": "^29.0.3",
|
||||
"ts-loader": "^9.4.1",
|
||||
"ts-node": "^10.9.1",
|
||||
"typescript": "^4.7.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@iden3/js-crypto": "1.0.1",
|
||||
"base58-js": "^1.0.4",
|
||||
"cross-sha256": "^1.2.0"
|
||||
"peerDependencies": {
|
||||
"@iden3/js-crypto": "1.0.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -1,3 +0,0 @@
|
||||
const esbuild = require('esbuild');
|
||||
const baseConfig = require('./base.config');
|
||||
esbuild.build(baseConfig).catch(() => process.exit(1));
|
||||
@@ -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);
|
||||
});
|
||||
@@ -16,10 +16,11 @@ import {
|
||||
} from './did-helper';
|
||||
import { Parser } from './did-parser';
|
||||
import { IDID, Param } from './types';
|
||||
import { sha256 } from 'cross-sha256';
|
||||
|
||||
import { sha256 } from '@iden3/js-crypto';
|
||||
import { encoder } from '../utils';
|
||||
// DID Decentralized Identifiers (DIDs)
|
||||
// https://w3c.github.io/did-core/#did-syntax
|
||||
|
||||
export class DID {
|
||||
method = '';
|
||||
id = '';
|
||||
@@ -199,7 +200,7 @@ export class DID {
|
||||
}
|
||||
|
||||
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 idSlice = hash.slice(hash.length - Constants.GENESIS_LENGTH);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Constants } from './constants';
|
||||
import { sha256 } from 'cross-sha256';
|
||||
import { checkBigIntInField, fromLittleEndian, toLittleEndian } from './utils';
|
||||
import { Hex } from '@iden3/js-crypto';
|
||||
import { checkBigIntInField, fromLittleEndian, toLittleEndian, encoder } from './utils';
|
||||
import { Hex, sha256 } from '@iden3/js-crypto';
|
||||
export class BytesHelper {
|
||||
static intToBytes(int: bigint): Uint8Array {
|
||||
return BytesHelper.intToNBytes(int, Constants.BYTES_LENGTH);
|
||||
@@ -43,13 +42,12 @@ export class BytesHelper {
|
||||
}
|
||||
|
||||
static hashBytes(str: string): Uint8Array {
|
||||
const hash = new sha256().update(str).digest();
|
||||
const hash = sha256(encoder.encode(str));
|
||||
return new Uint8Array(hash);
|
||||
}
|
||||
|
||||
static hexToBytes(str: string): Uint8Array {
|
||||
const buffer = Buffer.from(str, 'hex');
|
||||
return Uint8Array.from(buffer);
|
||||
return Hex.decodeString(str);
|
||||
}
|
||||
|
||||
static bytesToHex(bytes: Uint8Array) {
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { Constants } from './constants';
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const base58Js = require('base58-js');
|
||||
import { fromLittleEndian } from './utils';
|
||||
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
|
||||
// [ type | root_genesis | checksum ]
|
||||
@@ -30,7 +28,7 @@ export class Id {
|
||||
}
|
||||
|
||||
string(): string {
|
||||
return base58Js.binary_to_base58(this._bytes);
|
||||
return base58FromBytes(this._bytes);
|
||||
}
|
||||
|
||||
get bytes(): Uint8Array {
|
||||
@@ -81,7 +79,7 @@ export class Id {
|
||||
}
|
||||
|
||||
static fromString(s: string): Id {
|
||||
const bytes = base58Js.base58_to_binary(s);
|
||||
const bytes = base58ToBytes(s);
|
||||
return Id.fromBytes(bytes);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { poseidon } from '@iden3/js-crypto';
|
||||
import { Constants } from './constants';
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
export const encoder = new TextEncoder();
|
||||
|
||||
export function fromLittleEndian(bytes: Uint8Array): bigint {
|
||||
const n256 = BigInt(256);
|
||||
|
||||
@@ -2,22 +2,15 @@
|
||||
"compilerOptions": {
|
||||
"moduleResolution": "node",
|
||||
"target": "es2020",
|
||||
"module": "es6",
|
||||
"module": "ES2020",
|
||||
"lib": ["es2020", "dom"],
|
||||
"allowJs": true,
|
||||
"esModuleInterop": true,
|
||||
|
||||
"strict": true,
|
||||
"sourceMap": true,
|
||||
"skipLibCheck": true,
|
||||
"declaration": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"resolveJsonModule": true,
|
||||
"declarationDir": "dist/types",
|
||||
"outDir": "dist/cjs",
|
||||
"typeRoots": [
|
||||
"node_modules/@types"
|
||||
]
|
||||
"typeRoots": ["node_modules/@types"]
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
|
||||
Reference in New Issue
Block a user