From 4512e97f9b55607ce388aa6eb63a37fc196a5d9d Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Sat, 18 Feb 2023 14:00:42 -0500 Subject: [PATCH] Replaced substring from 0 index with startsWith (#3691). --- src.ts/abi/fragments.ts | 2 +- src.ts/abi/typed.ts | 2 +- src.ts/address/address.ts | 2 +- src.ts/contract/factory.ts | 2 +- src.ts/utils/data.ts | 2 +- src.ts/utils/maths.ts | 2 +- src.ts/wallet/json-keystore.ts | 2 +- src.ts/wallet/utils.ts | 4 ++-- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src.ts/abi/fragments.ts b/src.ts/abi/fragments.ts index 33889ce63b..0c8aa45abe 100644 --- a/src.ts/abi/fragments.ts +++ b/src.ts/abi/fragments.ts @@ -865,7 +865,7 @@ export class ParamType { return new ParamType(_guard, name || "", type, "array", indexed, null, arrayLength, arrayChildren); } - if (type === "tuple" || type.substring(0, 5) === "tuple(" || type[0] === "(") { + if (type === "tuple" || type.startsWith("tuple("/* fix: ) */) || type.startsWith("(" /* fix: ) */)) { const comps = (obj.components != null) ? obj.components.map((c: any) => ParamType.from(c)): null; const tuple = new ParamType(_guard, name || "", type, "tuple", indexed, comps, null, null); // @TODO: use lexer to validate and normalize type diff --git a/src.ts/abi/typed.ts b/src.ts/abi/typed.ts index bdd46f9625..9db9b08a34 100644 --- a/src.ts/abi/typed.ts +++ b/src.ts/abi/typed.ts @@ -102,7 +102,7 @@ export class Typed { } isData(): this is TypedData { - return (this.type.substring(0, 5) === "bytes"); + return this.type.startsWith("bytes"); } isString(): this is TypedString { diff --git a/src.ts/address/address.ts b/src.ts/address/address.ts index 089de113e5..08af8da7ad 100644 --- a/src.ts/address/address.ts +++ b/src.ts/address/address.ts @@ -123,7 +123,7 @@ export function getAddress(address: string): string { if (address.match(/^(0x)?[0-9a-fA-F]{40}$/)) { // Missing the 0x prefix - if (address.substring(0, 2) !== "0x") { address = "0x" + address; } + if (!address.startsWith("0x")) { address = "0x" + address; } const result = getChecksumAddress(address); diff --git a/src.ts/contract/factory.ts b/src.ts/contract/factory.ts index 4b5da3398f..039223f23d 100644 --- a/src.ts/contract/factory.ts +++ b/src.ts/contract/factory.ts @@ -33,7 +33,7 @@ export class ContractFactory = Array, I = BaseContract bytecode = hexlify(getBytes(bytecode)); } else { if (typeof(bytecode) === "object") { bytecode = bytecode.object; } - if (bytecode.substring(0, 2) !== "0x") { bytecode = "0x" + bytecode; } + if (!bytecode.startsWith("0x")) { bytecode = "0x" + bytecode; } bytecode = hexlify(getBytes(bytecode)); } diff --git a/src.ts/utils/data.ts b/src.ts/utils/data.ts index 0851729659..2006020981 100644 --- a/src.ts/utils/data.ts +++ b/src.ts/utils/data.ts @@ -147,7 +147,7 @@ export function dataSlice(data: BytesLike, start?: number, end?: number): string */ export function stripZerosLeft(data: BytesLike): string { let bytes = hexlify(data).substring(2); - while (bytes.substring(0, 2) == "00") { bytes = bytes.substring(2); } + while (bytes.startsWith("00")) { bytes = bytes.substring(2); } return "0x" + bytes; } diff --git a/src.ts/utils/maths.ts b/src.ts/utils/maths.ts index 66f13f790d..ad53c3766b 100644 --- a/src.ts/utils/maths.ts +++ b/src.ts/utils/maths.ts @@ -230,7 +230,7 @@ export function toBeArray(_value: BigNumberish): Uint8Array { */ export function toQuantity(value: BytesLike | BigNumberish): string { let result = hexlify(isBytesLike(value) ? value: toBeArray(value)).substring(2); - while (result.substring(0, 1) === "0") { result = result.substring(1); } + while (result.startsWith("0")) { result = result.substring(1); } if (result === "") { result = "0"; } return "0x" + result; } diff --git a/src.ts/wallet/json-keystore.ts b/src.ts/wallet/json-keystore.ts index 80fe87026e..0a53bbc922 100644 --- a/src.ts/wallet/json-keystore.ts +++ b/src.ts/wallet/json-keystore.ts @@ -96,7 +96,7 @@ function getAccount(data: any, _key: string): KeystoreAccount { const address = computeAddress(privateKey); if (data.address) { let check = data.address.toLowerCase(); - if (check.substring(0, 2) !== "0x") { check = "0x" + check; } + if (!check.startsWith("0x")) { check = "0x" + check; } assertArgument(getAddress(check) === address, "keystore address/privateKey mismatch", "address", data.address); } diff --git a/src.ts/wallet/utils.ts b/src.ts/wallet/utils.ts index f718c7eee8..5c79d1b929 100644 --- a/src.ts/wallet/utils.ts +++ b/src.ts/wallet/utils.ts @@ -7,8 +7,8 @@ import { } from "../utils/index.js"; export function looseArrayify(hexString: string): Uint8Array { - if (typeof(hexString) === 'string' && hexString.substring(0, 2) !== '0x') { - hexString = '0x' + hexString; + if (typeof(hexString) === "string" && !hexString.startsWith("0x")) { + hexString = "0x" + hexString; } return getBytesCopy(hexString); }