Skip to content

Commit

Permalink
chore: add husky and fix eslint error
Browse files Browse the repository at this point in the history
  • Loading branch information
way2ex committed Sep 25, 2023
1 parent 9b5a0e4 commit e4cbaae
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 95 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = {
files: ['./test/**'],
rules: {
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
},
],
Expand All @@ -35,5 +36,6 @@ module.exports = {
rules: {
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'warn',
'@typescript-eslint/no-explicit-any': 'off',
},
};
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run lint
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"build:all": "npm run clean:all && npm run build:esm && npm run build:cjs && npm run build:dist",
"copy-protocol:cjs": "cp -R ./src/protocol ./lib/commonjs/",
"copy-protocol:esm": "cp -R ./src/protocol ./lib/esm/",
"prepare": "npm run build",
"prepare": "husky install && npm run build",
"build": "npm run clean && webpack --config webpack.config.js --progress --color",
"build:dev": "NODE_ENV=development npm run build",
"clean:all": "rimraf dist lib",
Expand All @@ -46,7 +46,9 @@
"test:browser": "npm run-script newaccount && node scripts/test-browser.js && npx karma start --single-run --browsers ChromeHeadless",
"coverage": "npm run-script test:browser && npm run-script test",
"btest": "npm run build:dev && npm run test",
"format-all": "prettier --write ./src"
"format-all": "prettier --write ./src",
"lint": "eslint src",
"lint:fix": "eslint src --fix"
},
"husky": {
"hooks": {
Expand Down
1 change: 1 addition & 0 deletions src/lib/contract/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export class Contract {
// }

hasProperty(property: number | string | symbol) {
// eslint-disable-next-line no-prototype-builtins
return this.hasOwnProperty(property) || (this as unknown as IContract).__proto__.hasOwnProperty(property);
}

Expand Down
18 changes: 5 additions & 13 deletions src/lib/contract/method.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-control-regex */
import utils from '../../utils/index.js';
import { encodeParamsV2ByABI, decodeParamsV2ByABI } from '../../utils/abi.js';
import { TronWeb } from '../../tronweb.js';
Expand Down Expand Up @@ -273,8 +274,7 @@ export class Method {
};
if (broadcast.message) err.message = this.tronWeb.toUtf8(broadcast.message);
const error = new Error(err.message);
// @ts-ignore
error.error = broadcast.code;
(error as any).error = broadcast.code;
throw error;
}

Expand All @@ -284,10 +284,8 @@ export class Method {

const checkResult: (index: number) => any = async (index) => {
if (index === (options.pollTimes || 20)) {
const error = new Error('Cannot find result in solidity node');
// @ts-ignore
const error: any = new Error('Cannot find result in solidity node');
error.error = 'Cannot find result in solidity node';
// @ts-ignore
error.transaction = signedTransaction;
throw error;
}
Expand All @@ -300,23 +298,17 @@ export class Method {
}

if (output.result && output.result === 'FAILED') {
const error = new Error(this.tronWeb.toUtf8(output.resMessage));
// @ts-ignore
const error: any = new Error(this.tronWeb.toUtf8(output.resMessage));
error.error = this.tronWeb.toUtf8(output.resMessage);
// @ts-ignore
error.transaction = signedTransaction;
// @ts-ignore
error.output = output;
throw error;
}

if (!utils.hasProperty(output, 'contractResult')) {
const error = new Error('Failed to execute: ' + JSON.stringify(output, null, 2));
// @ts-ignore
const error: any = new Error('Failed to execute: ' + JSON.stringify(output, null, 2));
error.error = 'Failed to execute: ' + JSON.stringify(output, null, 2);
// @ts-ignore
error.transaction = signedTransaction;
// @ts-ignore
error.output = output;
throw error;
}
Expand Down
17 changes: 9 additions & 8 deletions src/lib/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ export class Event {
}

async getEventsByContractAddress(contractAddress: string, options: GetEventResultOptions = {}) {
let {
const newOptions = Object.assign(
{
limit: 20,
},
options
);
const {
eventName,
blockNumber,
onlyUnconfirmed,
Expand All @@ -103,13 +109,8 @@ export class Event {
maxBlockTimestamp,
orderBy,
fingerprint,
limit,
} = Object.assign(
{
limit: 20,
},
options
);
} = newOptions;
let { limit } = newOptions;

if (!this.tronWeb.eventServer) {
throw new Error('No event server configured');
Expand Down
15 changes: 8 additions & 7 deletions src/lib/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Plugin {
requires: '0.0.0',
components: {},
};
let result: {
const result: {
libs: any[];
plugged: any[];
skipped: any[];
Expand All @@ -59,8 +59,8 @@ export class Plugin {
if (semver.satisfies(TronWeb.version, pluginInterface.requires)) {
if (pluginInterface.fullClass) {
// plug the entire class at the same level of tronWeb.trx
let className = plugin.constructor.name;
let classInstanceName = className.substring(0, 1).toLowerCase() + className.substring(1);
const className = plugin.constructor.name;
const classInstanceName = className.substring(0, 1).toLowerCase() + className.substring(1);
if (className !== classInstanceName) {
Object.assign(TronWeb, {
[className]: Plugin,
Expand All @@ -72,13 +72,14 @@ export class Plugin {
}
} else {
// plug methods into a class, like trx
for (let component in pluginInterface.components) {
for (const component in pluginInterface.components) {
// eslint-disable-next-line no-prototype-builtins
if (!this.tronWeb.hasOwnProperty(component)) {
continue;
}
let methods = pluginInterface.components[component];
let pluginNoOverride = (this.tronWeb as any)[component].pluginNoOverride || [];
for (let method in methods) {
const methods = pluginInterface.components[component];
const pluginNoOverride = (this.tronWeb as any)[component].pluginNoOverride || [];
for (const method in methods) {
if (
method === 'constructor' ||
((this.tronWeb as any)[component][method] &&
Expand Down
12 changes: 5 additions & 7 deletions src/lib/trx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,8 @@ export class Trx {
]);

return [
// @ts-ignore
...from.map((tx) => ((tx.direction = 'from'), tx)),
// @ts-ignore
...to.map((tx) => ((tx.direction = 'to'), tx)),
...from.map((tx) => (((tx as any).direction = 'from'), tx)),
...to.map((tx) => (((tx as any).direction = 'to'), tx)),
].sort((a, b) => {
return b.raw_data.timestamp - a.raw_data.timestamp;
});
Expand Down Expand Up @@ -671,7 +669,7 @@ export class Trx {
return utils.crypto._signTypedData(domain, types, value, privateKey);
}

async multiSign(transaction: Transaction, privateKey = this.tronWeb.defaultPrivateKey, permissionId: number = 0) {
async multiSign(transaction: Transaction, privateKey = this.tronWeb.defaultPrivateKey, permissionId = 0) {
if (!utils.isObject(transaction) || !transaction.raw_data || !transaction.raw_data.contract) {
throw new Error('Invalid transaction provided');
}
Expand Down Expand Up @@ -863,8 +861,8 @@ export class Trx {
* @param callback
*/
async freezeBalance(
amount: number = 0,
duration: number = 3,
amount = 0,
duration = 3,
resource: Resource = 'BANDWIDTH',
options: AddressOptions = {},
receiverAddress?: string
Expand Down
3 changes: 2 additions & 1 deletion src/utils/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function generateAccount() {
};
}

export function generateRandom(password: string = '', path = TRON_BIP39_PATH_INDEX_0, wordlist?: Wordlist) {
export function generateRandom(password = '', path = TRON_BIP39_PATH_INDEX_0, wordlist?: Wordlist) {
const account = ethersHDNodeWallet.createRandom(password, path, wordlist);

const result = {
Expand All @@ -42,6 +42,7 @@ export function generateAccountWithMnemonic(
path: string = TRON_BIP39_PATH_INDEX_0,
wordlist: Wordlist | null = null
) {
// eslint-disable-next-line no-useless-escape
if (!String(path).match(/^m\/44\'\/195\'/)) {
throw new Error(INVALID_TRON_PATH_ERROR_MSG);
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/base58.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function encode58(buffer: BytesLike | string) {
for (i = 0; i < buffer.length; i++) {
for (j = 0; j < digits.length; j++) digits[j] <<= 8;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
digits[0] += buffer[i];
let carry = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class Base64 {
let enc4: number;
let i = 0;

// eslint-disable-next-line no-useless-escape
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');

while (i < input.length) {
Expand Down Expand Up @@ -107,6 +108,7 @@ export class Base64 {
let enc4: number;
let i = 0;

// eslint-disable-next-line no-useless-escape
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');

while (i < input.length) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function hextoString(hex: string) {
let out = '';

for (let i = 0; i < arr.length / 2; i++) {
let tmp = `0x${arr[i * 2]}${arr[i * 2 + 1]}`;
const tmp = `0x${arr[i * 2]}${arr[i * 2 + 1]}`;
out += String.fromCharCode(parseInt(tmp));
}

Expand Down
16 changes: 7 additions & 9 deletions src/utils/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function bin2String(array: BytesLike) {
return bytesToString(array);
}

export function arrayEquals(array1: any[], array2: any[], strict: boolean = false) {
export function arrayEquals(array1: any[], array2: any[], strict = false) {
if (array1.length != array2.length) return false;

for (let i = 0; i < array1.length; i++) {
Expand All @@ -26,12 +26,10 @@ export function arrayEquals(array1: any[], array2: any[], strict: boolean = fals
}

export function stringToBytes(str: string) {
const bytes = new Array();
let len: number;
const bytes = [];
const len: number = str.length;
let c: number;

len = str.length;

for (let i = 0; i < len; i++) {
c = str.charCodeAt(i);

Expand Down Expand Up @@ -84,7 +82,7 @@ export function hexStr2byteArray(str: string, strict = false) {
len++;
}
}
const byteArray: number[] = Array();
const byteArray: number[] = [];
let d = 0;
let j = 0;
let k = 0;
Expand Down Expand Up @@ -157,7 +155,7 @@ export function getStringType(str: string) {
// } else
if (str.length == 40) {
for (; i < 40; i++) {
var c = str.charAt(i);
const c = str.charAt(i);

if (!isHexChar(c)) break;
}
Expand All @@ -166,15 +164,15 @@ export function getStringType(str: string) {
if (i == 40) return 1; //40 Hex, Address

for (i = 0; i < str.length; i++) {
var c = str.charAt(i);
const c = str.charAt(i);

if (!isNumber(c)) break;
}

if (i == str.length) return 2; // All Decimal number, BlockNumber

for (i = 0; i < str.length; i++) {
var c = str.charAt(i);
const c = str.charAt(i);

if (c > ' ') return 3; // At least one visible character
}
Expand Down
9 changes: 5 additions & 4 deletions src/utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export function _signTypedData(

export function getRowBytesFromTransactionBase64(base64Data: string): Uint8Array {
const bytesDecode = base64DecodeFromString(base64Data);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const transaction = proto.protocol.Transaction.deserializeBinary(bytesDecode);
const raw = transaction.getRawData();
Expand All @@ -122,7 +123,7 @@ export function computeAddress(pubBytes: BytesLike) {
}

export function getAddressFromPriKey(priKeyBytes: BytesLike) {
let pubBytes = getPubKeyFromPriKey(priKeyBytes);
const pubBytes = getPubKeyFromPriKey(priKeyBytes);
return computeAddress(pubBytes);
}

Expand Down Expand Up @@ -208,8 +209,8 @@ export function getPubKeyFromPriKey(priKeyBytes: BytesLike) {
const x = pubkey.x;
const y = pubkey.y;

let xHex = x.toString(16).padStart(64, '0');
let yHex = y.toString(16).padStart(64, '0');
const xHex = x.toString(16).padStart(64, '0');
const yHex = y.toString(16).padStart(64, '0');

const pubkeyHex = `04${xHex}${yHex}`;
const pubkeyBytes = hexStr2byteArray(pubkeyHex);
Expand Down Expand Up @@ -240,7 +241,7 @@ export function passwordToAddress(password: string) {
return getBase58CheckAddress(com_addressBytes);
}

export function pkToAddress(privateKey: string, strict: boolean = false) {
export function pkToAddress(privateKey: string, strict = false) {
const com_priKeyBytes = hexStr2byteArray(privateKey, strict);
const com_addressBytes = getAddressFromPriKey(com_priKeyBytes);

Expand Down
Loading

0 comments on commit e4cbaae

Please sign in to comment.