diff --git a/.eslintrc b/.eslintrc index ab8ceb96..b2dc13c9 100644 --- a/.eslintrc +++ b/.eslintrc @@ -34,6 +34,15 @@ "class-methods-use-this": "off", "no-plusplus": "off", "implicit-arrow-linebreak": "off", - "object-curly-newline": "off" + "object-curly-newline": "off", + "import/extensions": [ + "error", + "ignorePackages", + { + "js": "always", + "json": "always" + } + ], + "operator-linebreak": ["warn", "after"] } } diff --git a/.husky/commit-msg b/.husky/commit-msg index 2f6c62d8..ac119ba5 100755 --- a/.husky/commit-msg +++ b/.husky/commit-msg @@ -1,3 +1,3 @@ #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" -HUSKY_GIT_PARAMS=$1 node scripts/verify-commit-msg.js \ No newline at end of file +HUSKY_GIT_PARAMS=$1 node scripts/verify-commit-msg.cjs \ No newline at end of file diff --git a/build/utils.js b/build/utils.js index 308a14a0..2331b53a 100644 --- a/build/utils.js +++ b/build/utils.js @@ -1,10 +1,5 @@ -/** - * @file utils - * @author atom-yang - * @date 2019-06-28 - */ -const path = require('path'); +import path from 'path'; -module.exports.ROOT = path.resolve(__dirname, '..'); +export const ROOT = path.resolve(process.cwd(), '.'); -module.exports.OUTPUT_PATH = path.resolve(__dirname, '..', 'dist/'); +export const OUTPUT_PATH = path.resolve(process.cwd(), '.', 'dist/'); diff --git a/build/webpack.analyze.js b/build/webpack.analyze.cjs similarity index 65% rename from build/webpack.analyze.js rename to build/webpack.analyze.cjs index eafa3931..36dc263a 100644 --- a/build/webpack.analyze.js +++ b/build/webpack.analyze.cjs @@ -5,25 +5,22 @@ /* eslint-env node */ -const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer'); +const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer'); const DeadCodePlugin = require('webpack-deadcode-plugin'); const { merge } = require('webpack-merge'); -const nodeConfig = require('./webpack.node'); +const nodeConfig = require('./webpack.node.js'); const browserConfig = require('./webpack.browser'); const unusedAnalyzeConfig = { patterns: ['src/**/*.*'], globOptions: { - ignore: [ - '**/*.md', - 'node_modules/**/*' - ] + ignore: ['**/*.md', 'node_modules/**/*'] } }; module.exports = merge(process.env.RUNTIME_ENV === 'node' ? nodeConfig : browserConfig, { plugins: [ - new BundleAnalyzerPlugin({analyzerMode: 'static', generateStatsFile: true}), + new BundleAnalyzerPlugin({ analyzerMode: 'static', generateStatsFile: true }), new DeadCodePlugin(unusedAnalyzeConfig) ] }); diff --git a/build/webpack.browser.js b/build/webpack.browser.js index 12ca634c..44e4781b 100644 --- a/build/webpack.browser.js +++ b/build/webpack.browser.js @@ -3,11 +3,13 @@ * @author atom-yang */ -/* eslint-env node */ -const { merge } = require('webpack-merge'); -const webpack = require('webpack'); -const baseConfig = require('./webpack.common'); -const { OUTPUT_PATH } = require('./utils'); +import { merge } from 'webpack-merge'; +import webpack from 'webpack'; +import baseConfig from './webpack.common.js'; +import { OUTPUT_PATH } from './utils.js'; +import { createRequire } from 'module'; + +const require = createRequire(import.meta.url); const browserConfig = { mode: 'production', @@ -63,11 +65,10 @@ const browserConfig = { new webpack.ProvidePlugin({ Buffer: ['buffer', 'Buffer'] }), - // fix "process is not defined" error: new webpack.ProvidePlugin({ process: 'process/browser' }) ] }; -module.exports = merge(baseConfig, browserConfig); +export default merge(baseConfig, browserConfig); diff --git a/build/webpack.common.js b/build/webpack.common.js index e65cf977..d36b55e8 100644 --- a/build/webpack.common.js +++ b/build/webpack.common.js @@ -1,13 +1,11 @@ -/** - * @file common config - * @author atom-yang - */ - /* eslint-env node */ -const path = require('path'); -const webpack = require('webpack'); -const { ROOT } = require('./utils'); -const { version, name } = require(path.resolve(ROOT, './package.json')); +import path from 'path'; +import webpack from 'webpack'; +import { ROOT } from './utils.js'; +const pkg = await import(path.resolve(ROOT, './package.json'), { + assert: { type: 'json' } +}); +const { version, name } = pkg; const banner = `${name}.js v${version} \n(c) 2019-${new Date().getFullYear()} AElf \nReleased under MIT License`; @@ -43,4 +41,4 @@ const baseConfig = { } }; -module.exports = baseConfig; +export default baseConfig; diff --git a/build/webpack.esModule.js b/build/webpack.esModule.js new file mode 100644 index 00000000..1c342595 --- /dev/null +++ b/build/webpack.esModule.js @@ -0,0 +1,35 @@ +/** + * @file node config + * @author atom-yang + */ + +/* eslint-env node */ +import { merge } from 'webpack-merge'; +import baseConfig from './webpack.common.js'; +import { OUTPUT_PATH } from './utils.js'; + +const nodeConfig = { + mode: 'production', + output: { + path: OUTPUT_PATH, + filename: 'aelf.esm.js', + libraryTarget: 'module' + }, + experiments: { + outputModule: true + }, + resolve: { + alias: {}, + fallback: { + crypto: 'crypto-browserify', + stream: 'stream-browserify', + https: false, + http: false, + child_process: false, + fs: false, + url: false + } + } +}; + +export default merge(baseConfig, nodeConfig); diff --git a/build/webpack.node.js b/build/webpack.node.js index 7f0e3199..47395ce2 100644 --- a/build/webpack.node.js +++ b/build/webpack.node.js @@ -4,9 +4,9 @@ */ /* eslint-env node */ -const { merge } = require('webpack-merge'); -const baseConfig = require('./webpack.common'); -const { OUTPUT_PATH } = require('./utils'); +import { merge } from 'webpack-merge'; +import baseConfig from './webpack.common.js'; +import { OUTPUT_PATH } from './utils.js'; const nodeConfig = { mode: 'production', @@ -18,11 +18,6 @@ const nodeConfig = { }, libraryExport: 'default' }, - resolve: { - alias: { - // 'scryptsy$': '../scrypt-polyfill.js', - } - }, target: 'node', optimization: { removeEmptyChunks: true, @@ -33,4 +28,4 @@ const nodeConfig = { } }; -module.exports = merge(baseConfig, nodeConfig); +export default merge(baseConfig, nodeConfig); diff --git a/jest-report.xml b/jest-report.xml index ea7aa9af..8e793cdb 100644 --- a/jest-report.xml +++ b/jest-report.xml @@ -1,25 +1,25 @@ - - - + + + - + - + - + - + - + - + @@ -27,39 +27,39 @@ - + - + - + - + - - + + - + - + - + - + - + - + - + @@ -71,27 +71,27 @@ - + - + - + - + - + - + - + @@ -101,425 +101,425 @@ - + - - + + - + - + + + + + + + + + + + + + + + + + - + + + + + - + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - + + - + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - - - - - - - - + + - + - - - - - + - + - + - + - + - + - - - - + + - + - - - + - + - - - + - + - + - + + + - + - + + + - + - - + + - + - - - - - - - + - - + + - - + + - - + + - + - - + + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + \ No newline at end of file diff --git a/jest.browser.config.js b/jest.browser.config.cjs similarity index 100% rename from jest.browser.config.js rename to jest.browser.config.cjs diff --git a/jest.node.config.js b/jest.node.config.cjs similarity index 100% rename from jest.node.config.js rename to jest.node.config.cjs diff --git a/package.json b/package.json index 9b980bcb..9023247d 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,24 @@ { "name": "aelf-sdk", - "version": "3.4.16", + "version": "3.4.17", "description": "aelf-sdk js library", + "type": "module", "main": "dist/aelf.cjs.js", + "module": "dist/aelf.esm.js", "browser": "dist/aelf.umd.js", "unpkg": "dist/aelf.umd.js", "jsdelivr": "dist/aelf.umd.js", + "exports": { + ".": { + "import": "./dist/aelf.esm.js", + "require": "./dist/aelf.cjs.js" + } + }, "scripts": { - "build": "npm run clean && npm run build:browser && npm run build:node && npm run copy-ts", + "build": "npm run clean && npm run build:browser && npm run build:node && npm run build:esm && npm run copy-ts", "build:browser": "cross-env RUNTIME_ENV=browser webpack --progress --color --config ./build/webpack.browser.js", "build:node": "cross-env RUNTIME_ENV=node webpack --progress --color --config ./build/webpack.node.js", + "build:esm": "cross-env --es-module-specifier-resolution=node webpack --progress --color --config ./build/webpack.esModule.js", "analyze:node": "npm run clean && cross-env RUNTIME_ENV=node webpack --config ./build/webpack.analyze.js", "analyze:browser": "npm run clean && cross-env RUNTIME_ENV=browser webpack --config ./build/webpack.analyze.js", "clean": "rimraf dist/*", @@ -19,12 +28,12 @@ "release": "standard-version -a", "pre-commit": "lint-staged", "test": "yarn test:browser && yarn test:node", - "test:browser": "jest --config=jest.browser.config.js", - "test:browser:watch": "jest --config=jest.browser.config.js --watch", - "test:node": "jest --config=jest.node.config.js", - "test:node:watch": "jest --config=jest.node.config.js --watch", - "test:coverage": "jest --config=jest.browser.config.js --coverage", - "test:coverageNode": "jest --config=jest.node.config.js --coverage", + "test:browser": "jest --config=jest.browser.config.cjs", + "test:browser:watch": "jest --config=jest.browser.config.cjs --watch", + "test:node": "jest --config=jest.node.config.cjs", + "test:node:watch": "jest --config=jest.node.config.cjs --watch", + "test:coverage": "jest --config=jest.browser.config.cjs --coverage", + "test:coverageNode": "jest --config=jest.node.config.cjs --coverage", "prepare": "husky install", "copy-ts": "cpx \"types/**/*.ts\" dist/" }, @@ -78,6 +87,7 @@ "scryptsy": "^2.1.0", "stream-browserify": "^3.0.0", "typescript": "^5.0.4", + "xhr2": "^0.2.1", "xmlhttprequest": "^1.8.0" }, "lint-staged": { diff --git a/scripts/verify-commit-msg.cjs b/scripts/verify-commit-msg.cjs new file mode 100644 index 00000000..45bb1946 --- /dev/null +++ b/scripts/verify-commit-msg.cjs @@ -0,0 +1,18 @@ +const chalk = require('chalk'); +const msgPath = process.env.HUSKY_GIT_PARAMS; +const msg = require('fs').readFileSync(msgPath, 'utf-8').trim(); + +const commitRE = + /^(revert: )?(feat|security|fix|polish|docs|style|refactor|perf|test|workflow|ci|chore|types|build)(\(.+\))?: .{1,50}/; + +if (!commitRE.test(msg)) { + console.error( + ` ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid commit message format.`)}\n\n` + + chalk.red(` Proper commit message format is required for automated changelog generation. Examples:\n\n`) + + ` ${chalk.green(`feat(wallet): add getContractAddress`)}\n` + + ` ${chalk.green(`fix(contract): handle contract conflict (close #28)`)}\n\n` + + chalk.red(` See .github/COMMIT_CONVENTION.md for more details.\n`) + + chalk.red(` You can also use ${chalk.cyan(`npm run commit`)} to interactively generate a commit message.\n`) + ); + process.exit(1); +} diff --git a/scripts/verify-commit-msg.js b/scripts/verify-commit-msg.js deleted file mode 100644 index a71ba7f0..00000000 --- a/scripts/verify-commit-msg.js +++ /dev/null @@ -1,17 +0,0 @@ -const chalk = require('chalk'); -const msgPath = process.env.HUSKY_GIT_PARAMS; -const msg = require('fs').readFileSync(msgPath, 'utf-8').trim(); - -const commitRE = /^(revert: )?(feat|security|fix|polish|docs|style|refactor|perf|test|workflow|ci|chore|types|build)(\(.+\))?: .{1,50}/; - -if (!commitRE.test(msg)) { - console.error( - ` ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid commit message format.`)}\n\n` + - chalk.red(` Proper commit message format is required for automated changelog generation. Examples:\n\n`) + - ` ${chalk.green(`feat(wallet): add getContractAddress`)}\n` + - ` ${chalk.green(`fix(contract): handle contract conflict (close #28)`)}\n\n` + - chalk.red(` See .github/COMMIT_CONVENTION.md for more details.\n`) + - chalk.red(` You can also use ${chalk.cyan(`npm run commit`)} to interactively generate a commit message.\n`) - ); - process.exit(1); -} diff --git a/src/chain/chainMethod.js b/src/chain/chainMethod.js index 229dc8e5..0df5f99c 100644 --- a/src/chain/chainMethod.js +++ b/src/chain/chainMethod.js @@ -2,17 +2,10 @@ * @file method on chain * @author atom-yang */ -import { isFunction, noop, isBoolean } from '../util/utils'; +import { isFunction, noop, isBoolean } from '../util/utils.js'; export default class ChainMethod { - constructor({ - name, - call, - method = 'GET', - params = [], - inputFormatter = [], - outputFormatter = null, - }) { + constructor({ name, call, method = 'GET', params = [], inputFormatter = [], outputFormatter = null }) { this.name = name; this.call = call; this.requestMethod = method; @@ -39,9 +32,7 @@ export default class ChainMethod { } formatOutput(result) { - return this.outputFormatter && result - ? this.outputFormatter(result) - : result; + return this.outputFormatter && result ? this.outputFormatter(result) : result; } extractArgumentsIntoObject(args) { @@ -53,7 +44,7 @@ export default class ChainMethod { requestMethod: this.requestMethod, isSync: false, callback: noop, - params: {}, + params: {} }; this.formatInput(args).forEach((arg, index) => { if (index > this.params.length - 1) { diff --git a/src/chain/index.js b/src/chain/index.js index 0664275c..5d9fa1c5 100644 --- a/src/chain/index.js +++ b/src/chain/index.js @@ -2,12 +2,12 @@ * @file chain * @author atom-yang */ -import { isBoolean, isFunction, noop, setPath } from '../util/utils'; -import { CHAIN_METHODS } from '../common/constants'; -import ChainMethod from './chainMethod'; -import * as merkleTree from '../util/merkleTree'; +import { isBoolean, isFunction, noop, setPath } from '../util/utils.js'; +import { CHAIN_METHODS } from '../common/constants.js'; +import ChainMethod from './chainMethod.js'; +import * as merkleTree from '../util/merkleTree.js'; -import ContractFactory from '../contract'; +import ContractFactory from '../contract/index.js'; export default class Chain { constructor(requestManager) { diff --git a/src/common/constants.js b/src/common/constants.js index 77b89871..0f75979d 100644 --- a/src/common/constants.js +++ b/src/common/constants.js @@ -2,7 +2,7 @@ * @file AElf-sdk constants * @author atom-yang */ -import { inputAddressFormatter, outputFileDescriptorSetFormatter } from '../util/formatters'; +import { inputAddressFormatter, outputFileDescriptorSetFormatter } from '../util/formatters.js'; /** * unsigned 256 int diff --git a/src/contract/contractMethod.js b/src/contract/contractMethod.js index fd5d930e..a4b7fe30 100644 --- a/src/contract/contractMethod.js +++ b/src/contract/contractMethod.js @@ -2,16 +2,16 @@ * @file contract method * @author atom-yang */ -import { getTransaction, Transaction } from '../util/proto'; +import { getTransaction, Transaction } from '../util/proto.js'; import { transformArrayToMap, transformMapToArray, transform, INPUT_TRANSFORMERS, OUTPUT_TRANSFORMERS -} from '../util/transform'; -import { isBoolean, isFunction, isNumber, noop, uint8ArrayToHex, unpackSpecifiedTypeData } from '../util/utils'; -import wallet from '../wallet'; +} from '../util/transform.js'; +import { isBoolean, isFunction, isNumber, noop, uint8ArrayToHex, unpackSpecifiedTypeData } from '../util/utils.js'; +import wallet from '../wallet/index.js'; export default class ContractMethod { constructor(chain, method, contractAddress, walletInstance, option) { diff --git a/src/contract/contractMultiTransaction.js b/src/contract/contractMultiTransaction.js index d60e0b74..9a512366 100644 --- a/src/contract/contractMultiTransaction.js +++ b/src/contract/contractMultiTransaction.js @@ -2,11 +2,11 @@ * @file contract method * @author atom-yang */ -import HttpProvider from '../util/httpProvider'; -import { getTransactionAndChainId, TransactionAndChainId, MultiTransaction } from '../util/proto'; -import { transformMapToArray, transform, INPUT_TRANSFORMERS } from '../util/transform'; -import { isBoolean, isFunction, isObject, noop, uint8ArrayToHex, validateMulti } from '../util/utils'; -import wallet from '../wallet'; +import HttpProvider from '../util/httpProvider.js'; +import { getTransactionAndChainId, TransactionAndChainId, MultiTransaction } from '../util/proto.js'; +import { transformMapToArray, transform, INPUT_TRANSFORMERS } from '../util/transform.js'; +import { isBoolean, isFunction, isObject, noop, uint8ArrayToHex, validateMulti } from '../util/utils.js'; +import wallet from '../wallet/index.js'; export default class ContractMultiTransaction { constructor(contract, walletInstance, option) { diff --git a/src/contract/index.js b/src/contract/index.js index 2487a9a7..e08c1690 100644 --- a/src/contract/index.js +++ b/src/contract/index.js @@ -4,10 +4,10 @@ */ // eslint-disable-next-line max-classes-per-file import * as protobuf from '@aelfqueen/protobufjs'; -import ContractMethod from './contractMethod'; -import { noop } from '../util/utils'; -import { deserializeLog } from '../util/proto'; -import ContractMultiTransaction from './contractMultiTransaction'; +import ContractMethod from './contractMethod.js'; +import { noop } from '../util/utils.js'; +import { deserializeLog } from '../util/proto.js'; +import ContractMultiTransaction from './contractMultiTransaction.js'; const getServicesFromFileDescriptors = descriptors => { const root = protobuf.Root.fromDescriptor(descriptors, 'proto3').resolveAll(); diff --git a/src/index.js b/src/index.js index 32c43be5..152b0f92 100644 --- a/src/index.js +++ b/src/index.js @@ -2,17 +2,17 @@ * @file AElf-sdk index export * @author atom-yang */ -import * as protobuf from '@aelfqueen/protobufjs/light'; -import * as bloom from './util/bloom'; -import Chain from './chain'; -import RequestManager from './util/requestManage'; -import HttpProvider from './util/httpProvider'; -import wallet from './wallet'; -import * as utils from './util/utils'; -import * as proto from './util/proto'; -import * as transform from './util/transform'; -import Settings from './util/settings'; -import sha256 from './util/sha256'; +import * as protobuf from '@aelfqueen/protobufjs/light.js'; +import * as bloom from './util/bloom.js'; +import Chain from './chain/index.js'; +import RequestManager from './util/requestManage.js'; +import HttpProvider from './util/httpProvider.js'; +import wallet from './wallet/index.js'; +import * as utils from './util/utils.js'; +import * as proto from './util/proto.js'; +import * as transform from './util/transform.js'; +import Settings from './util/settings.js'; +import sha256 from './util/sha256.js'; /* eslint-disable no-underscore-dangle */ export default class AElf { diff --git a/src/util/bloom.js b/src/util/bloom.js index 80637507..10f849d3 100644 --- a/src/util/bloom.js +++ b/src/util/bloom.js @@ -3,11 +3,8 @@ * @author joshstevens19 * @link https://github.com/joshstevens19/ethereum-bloom-filters */ -import sha256 from './sha256'; -import { - Address, - getAddressObjectFromRep -} from './proto'; +import sha256 from './sha256.js'; +import { Address, getAddressObjectFromRep } from './proto.js'; /** * @deprecated Use the new Bloom instead */ @@ -19,10 +16,7 @@ function isBloom(bloom) { // return false; // } - if ( - /^(0x)?[0-9a-f]{512}$/.test(bloom) - || /^(0x)?[0-9A-F]{512}$/.test(bloom) - ) { + if (/^(0x)?[0-9a-f]{512}$/.test(bloom) || /^(0x)?[0-9A-F]{512}$/.test(bloom)) { return true; } return false; @@ -62,14 +56,10 @@ export function isInBloom(bloom, hash) { } for (let i = 0; i < 12; i += 4) { // calculate bit position in bloom filter that must be active - const bitpos = ((parseInt(hash.substr(i, 2), 16) << 8) - + parseInt(hash.substr(i + 2, 2), 16)) - & 2047; + const bitpos = ((parseInt(hash.substr(i, 2), 16) << 8) + parseInt(hash.substr(i + 2, 2), 16)) & 2047; // test if bitpos in bloom is active - const code = codePointToInt( - bloom.charCodeAt(bloom.length - 1 - Math.floor(bitpos / 4)), - ); + const code = codePointToInt(bloom.charCodeAt(bloom.length - 1 - Math.floor(bitpos / 4))); const offset = 1 << bitpos % 4; if ((code & offset) !== offset) { diff --git a/src/util/formatters.js b/src/util/formatters.js index d73fce40..ba0555be 100644 --- a/src/util/formatters.js +++ b/src/util/formatters.js @@ -4,9 +4,9 @@ * @author Fabian Vogelsteller * @date 2015 */ -import descriptor from '@aelfqueen/protobufjs/ext/descriptor'; +import descriptor from '@aelfqueen/protobufjs/ext/descriptor/index.js'; import bs58 from 'bs58'; -import { base58 } from './utils'; +import { base58 } from './utils.js'; const getByteCountByAddress = base58Str => { // convert a Base58 string to a binary array and get its byte count diff --git a/src/util/httpProvider.js b/src/util/httpProvider.js index 0b539560..16762716 100644 --- a/src/util/httpProvider.js +++ b/src/util/httpProvider.js @@ -3,6 +3,8 @@ * @author atom-yang */ import { stringify } from 'query-string'; +import NodeFetch, { Headers } from 'node-fetch'; +import { XMLHttpRequest as XHR } from 'xmlhttprequest'; const defaultHeaders = { Accept: 'text/plain;v=1.0', @@ -28,11 +30,8 @@ if (process.env.RUNTIME_ENV === 'browser') { } } else { // For node use xmlhttprequest - // eslint-disable-next-line global-require - RequestLibraryXMLOnly = require('xmlhttprequest').XMLHttpRequest; - // eslint-disable-next-line global-require - const NodeFetch = require('node-fetch'); - RequestLibrary = NodeFetch.default; + RequestLibraryXMLOnly = XHR; + RequestLibrary = NodeFetch; NodeHeaders = NodeFetch.Headers; isFetch = true; } diff --git a/src/util/keccak.js b/src/util/keccak.js index bd6ec839..f10dcdbb 100644 --- a/src/util/keccak.js +++ b/src/util/keccak.js @@ -1,4 +1,4 @@ -const createKeccakHash = require('keccak'); +import createKeccakHash from 'keccak'; const keccak = bits => str => { let msg; diff --git a/src/util/keyStore.js b/src/util/keyStore.js index 8df4ec6a..b245b147 100644 --- a/src/util/keyStore.js +++ b/src/util/keyStore.js @@ -5,8 +5,8 @@ import scrypt from 'scryptsy'; import { createCipheriv, createDecipheriv } from 'browserify-cipher'; import randomBytes from 'randombytes'; -import { keccak256 } from './keccak'; -import { KEY_STORE_ERRORS } from '../common/constants'; +import { keccak256 } from './keccak.js'; +import { KEY_STORE_ERRORS } from '../common/constants.js'; const AES_MODES = { 'aes-128-ecb': { diff --git a/src/util/merkleTree.js b/src/util/merkleTree.js index 68809916..2bcd42ad 100644 --- a/src/util/merkleTree.js +++ b/src/util/merkleTree.js @@ -1,4 +1,4 @@ -import sha256 from './sha256'; +import sha256 from './sha256.js'; const fromTwoBuffers = data => { if (data.length !== 2) throw new TypeError('Wrong data size.'); @@ -18,7 +18,9 @@ const generateMerkleTree = data => { return null; } - if (data.length % 2 === 1) { data.push(data[data.length - 1]); } + if (data.length % 2 === 1) { + data.push(data[data.length - 1]); + } let nodeToAdd = data.length / 2; let newAdded = 0; let i = 0; diff --git a/src/util/proto.js b/src/util/proto.js index 28cd9486..3d8e4cba 100644 --- a/src/util/proto.js +++ b/src/util/proto.js @@ -3,8 +3,8 @@ * @author atom-yang */ import * as protobuf from '@aelfqueen/protobufjs'; -import * as utils from './utils'; -import { transform, OUTPUT_TRANSFORMERS, transformArrayToMap } from './transform'; +import * as utils from './utils.js'; +import { transform, OUTPUT_TRANSFORMERS, transformArrayToMap } from './transform.js'; import coreDescriptor from '../../proto/transaction_fee.proto.json'; import VirtualTransactionDescriptor from '../../proto/virtual_transaction.proto.json'; diff --git a/src/util/transform.js b/src/util/transform.js index 8811d0f5..b70193f1 100644 --- a/src/util/transform.js +++ b/src/util/transform.js @@ -2,8 +2,8 @@ * @file transform protobuf * @author atom-yang */ -import { base58, decodeAddressRep } from './utils'; -import { inputAddressFormatter } from './formatters'; +import { base58, decodeAddressRep } from './utils.js'; +import { inputAddressFormatter } from './formatters.js'; const isWrappedBytes = (resolvedType, name) => { if (!resolvedType.name || resolvedType.name !== name) { @@ -33,11 +33,7 @@ export function transform(inputType, origin, transformers = []) { } // eslint-disable-next-line no-restricted-syntax Object.keys(inputType.fields).forEach(field => { - const { - rule, - name, - resolvedType - } = inputType.fields[field]; + const { rule, name, resolvedType } = inputType.fields[field]; if (resolvedType) { if (rule && rule === 'repeated') { let value = origin[name]; @@ -53,8 +49,10 @@ export function transform(inputType, origin, transformers = []) { } else { result = { ...result, - [name]: origin[name] !== null && origin[name] !== undefined - ? transform(resolvedType, origin[name], transformers) : origin[name] + [name]: + origin[name] !== null && origin[name] !== undefined + ? transform(resolvedType, origin[name], transformers) + : origin[name] }; } } @@ -75,19 +73,13 @@ export function transformMapToArray(inputType, origin) { // if (isAddress(inputType) || isHash(inputType)) { // return origin; // } - const { - fields, - options = {} - } = inputType; + const { fields, options = {} } = inputType; if (fieldsLength === 2 && fields.value && fields.key && options.map_entry === true) { return Object.keys(origin).map(key => ({ key, value: origin[key] })); } // eslint-disable-next-line no-restricted-syntax Object.keys(inputType.fields).forEach(field => { - const { - name, - resolvedType - } = inputType.fields[field]; + const { name, resolvedType } = inputType.fields[field]; if (resolvedType) { if (origin[name] && Array.isArray(origin[name])) { let value = origin[name]; @@ -110,10 +102,7 @@ export function transformMapToArray(inputType, origin) { export function transformArrayToMap(inputType, origin) { const fieldsLength = (inputType.fieldsArray || []).length; let result = origin; - if ( - fieldsLength === 0 - || (fieldsLength === 1 && !inputType.fieldsArray[0].resolvedType) - ) { + if (fieldsLength === 0 || (fieldsLength === 1 && !inputType.fieldsArray[0].resolvedType)) { return origin; } // Params which satisfy address or hash format satisfy above first. @@ -121,16 +110,11 @@ export function transformArrayToMap(inputType, origin) { // return origin; // } const { fields, options = {} } = inputType; - if ( - fieldsLength === 2 - && fields.value - && fields.key - && options.map_entry === true - ) { + if (fieldsLength === 2 && fields.value && fields.key && options.map_entry === true) { return origin.reduce( (acc, v) => ({ ...acc, - [v.key]: v.value, + [v.key]: v.value }), {} ); @@ -140,40 +124,36 @@ export function transformArrayToMap(inputType, origin) { const { name, resolvedType } = fields[field]; if (resolvedType && origin !== null && origin !== undefined) { if (origin[name] && Array.isArray(origin[name])) { - const { - fieldsArray = [], - fields: resolvedFields, - options: resolvedOptions = {}, - } = resolvedType; + const { fieldsArray = [], fields: resolvedFields, options: resolvedOptions = {} } = resolvedType; // eslint-disable-next-line max-len if ( - fieldsArray.length === 2 - && resolvedFields.value - && resolvedFields.key - && resolvedOptions.map_entry === true + fieldsArray.length === 2 && + resolvedFields.value && + resolvedFields.key && + resolvedOptions.map_entry === true ) { result = { ...result, [name]: origin[name].reduce( (acc, v) => ({ ...acc, - [v.key]: v.value, + [v.key]: v.value }), {} - ), + ) }; } else { let value = origin[name]; value = value.map(item => transformArrayToMap(resolvedType, item)); result = { ...result, - [name]: value, + [name]: value }; } } else { result = { ...result, - [name]: transformArrayToMap(resolvedType, origin[name]), + [name]: transformArrayToMap(resolvedType, origin[name]) }; } } @@ -215,7 +195,7 @@ export const INPUT_TRANSFORMERS = [ } return result; } - }, + } ]; export function encodeAddress(str) { @@ -247,5 +227,5 @@ export const OUTPUT_TRANSFORMERS = [ } return result; } - }, + } ]; diff --git a/src/util/utils.js b/src/util/utils.js index 9e253a7d..11b29fcd 100644 --- a/src/util/utils.js +++ b/src/util/utils.js @@ -5,10 +5,10 @@ import BigNumber from 'bignumber.js'; import bs58 from 'bs58'; -import { UNIT_MAP, UNSIGNED_256_INT } from '../common/constants'; -import { Transaction } from './proto'; -import { OUTPUT_TRANSFORMERS, encodeAddress, transform, transformArrayToMap } from './transform'; -import sha256 from './sha256'; +import { UNIT_MAP, UNSIGNED_256_INT } from '../common/constants.js'; +import { Transaction } from './proto.js'; +import { OUTPUT_TRANSFORMERS, encodeAddress, transform, transformArrayToMap } from './transform.js'; +import sha256 from './sha256.js'; export const base58 = { encode(data, encoding = 'hex') { diff --git a/src/wallet/index.js b/src/wallet/index.js index 82e124e7..344b87ec 100644 --- a/src/wallet/index.js +++ b/src/wallet/index.js @@ -5,13 +5,13 @@ import elliptic from 'elliptic'; import * as bip39 from 'bip39'; import hdkey from 'hdkey'; -import AES from 'crypto-js/aes'; -import encUTF8 from 'crypto-js/enc-utf8'; +import AES from 'crypto-js/aes.js'; +import encUTF8 from 'crypto-js/enc-utf8.js'; import BN from 'bn.js'; -import sha256 from '../util/sha256'; -import * as keyStore from '../util/keyStore'; -import { encodeAddressRep, padLeft } from '../util/utils'; -import { Transaction } from '../util/proto'; +import sha256 from '../util/sha256.js'; +import * as keyStore from '../util/keyStore.js'; +import { encodeAddressRep, padLeft } from '../util/utils.js'; +import { Transaction } from '../util/proto.js'; // eslint-disable-next-line new-cap const ellipticEc = new elliptic.ec('secp256k1'); diff --git a/types/wallet/index.d.ts b/types/wallet/index.d.ts index 55620ceb..e98a7aee 100644 --- a/types/wallet/index.d.ts +++ b/types/wallet/index.d.ts @@ -1,4 +1,4 @@ -import HDNode = require('hdkey'); +import HDNode from 'hdkey'; import * as Bip39 from 'bip39'; import { ec, curve } from 'elliptic'; import { TRawTransaction, TAddress } from '../util/proto'; @@ -34,10 +34,7 @@ interface IWallet { createNewWallet(BIP44Path?: BIP44Path): IWalletInfo; getWalletByMnemonic(mnemonic: Mnemonic, BIP44Path: BIP44Path): IWalletInfo; getWalletByPrivateKey(privateKey: PrivateKey): IWalletInfo; - signTransaction( - rawTxn: TRawTransaction, - keyPair: ec.KeyPair - ): SignTransaction; + signTransaction(rawTxn: TRawTransaction, keyPair: ec.KeyPair): SignTransaction; sign(hexString: string, keyPair: ec.KeyPair): Buffer; } export declare class Wallet implements IWallet { @@ -52,10 +49,7 @@ export declare class Wallet implements IWallet { createNewWallet(BIP44Path?: BIP44Path): IWalletInfo; getWalletByMnemonic(mnemonic: Mnemonic, BIP44Path: BIP44Path): IWalletInfo; getWalletByPrivateKey(privateKey: PrivateKey): IWalletInfo; - signTransaction( - rawTxn: TRawTransaction, - keyPair: ec.KeyPair - ): SignTransaction; + signTransaction(rawTxn: TRawTransaction, keyPair: ec.KeyPair): SignTransaction; sign(hexString: string, keyPair: ec.KeyPair): Buffer; } export default Wallet; diff --git a/yarn.lock b/yarn.lock index c429ea8e..b0e795e5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10350,6 +10350,11 @@ ws@^8.11.0: resolved "https://registry.npmjs.org/ws/-/ws-8.17.1.tgz#9293da530bb548febc95371d90f9c878727d919b" integrity sha512-6XQFvXTkbfUOZOKKILFG1PDK2NDQs4azKQl26T0YS5CxqWLgXajbPZ+h4gZekJyRqFU8pvnbAbbs/3TgRPy+GQ== +xhr2@^0.2.1: + version "0.2.1" + resolved "https://registry.npmjs.org/xhr2/-/xhr2-0.2.1.tgz#4e73adc4f9cfec9cbd2157f73efdce3a5f108a93" + integrity sha512-sID0rrVCqkVNUn8t6xuv9+6FViXjUVXq8H5rWOH2rz9fDNQEd4g0EA2XlcEdJXRz5BMEn4O1pJFdT+z4YHhoWw== + xml-name-validator@^3.0.0: version "3.0.0" resolved "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-3.0.0.tgz#6ae73e06de4d8c6e47f9fb181f78d648ad457c6a"