[internal] Simplify eth-address handling, smaller dependency (#4200)

* Simplify eth-address handling, smaller dependency

* Remove web3 dependency

Co-authored-by: Catenocrypt <catenocrypt@users.noreply.github.com>
This commit is contained in:
Adam R 2020-09-29 15:01:34 +02:00 committed by GitHub
parent 87d2340266
commit 0df2291872
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 2384 deletions

2372
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -55,8 +55,7 @@
"tinify": "^1.6.0-beta.2",
"ts-jest": "^25.5.1",
"ts-node": "^8.10.2",
"typescript": "^3.9.7",
"web3": "^1.2.11"
"typescript": "^3.9.7"
},
"dependencies": {
"codecov": "^3.7.2"

View File

@ -1,9 +1,8 @@
import { toChecksumAddress, checkAddressChecksum } from "ethereum-checksum-address";
import { reverseCase } from "./types";
const web3 = new (require('web3'))('ws://localhost:8546');
export const isChecksumEthereum = (address: string): boolean => web3.utils.checkAddressChecksum(address);
export const toChecksumEthereum = (address: string): string => web3.utils.toChecksumAddress(address);
export const isChecksumEthereum = (address: string): boolean => checkAddressChecksum(address);
export const toChecksumEthereum = (address: string): string => toChecksumAddress(address);
export function toChecksum(address: string, chain = "ethereum"): string {
const checksumEthereum = toChecksumEthereum(address);
@ -28,5 +27,16 @@ export function isChecksum(address: string, chain = "ethereum"): boolean {
}
export function isEthereumAddress(address: string): boolean {
return web3.utils.isAddress(address)
if (!(address.length == 40 || (address.length == 42 && address.substring(0, 2) === '0x'))) {
return false;
}
try {
const check1 = toChecksum(address);
if (toChecksum(check1) !== check1) {
return false;
}
return true;
} catch (error) {
return false;
}
}

View File

@ -19,7 +19,7 @@ import {
readDirSync,
isPathExistsSync,
} from "../generic/filesystem";
import { toChecksum } from "../generic/eth-web3";
import { toChecksum } from "../generic/eth-address";
import { ActionInterface, CheckStepInterface } from "../generic/interface";
import { isAssetInfoOK } from "../generic/asset-info";
import * as bluebird from "bluebird";

View File

@ -6,7 +6,7 @@ import {
isChecksum,
toChecksum,
isEthereumAddress
} from "../script/generic/eth-web3";
} from "../script/generic/eth-address";
import {
isDimensionTooLarge,
isDimensionOK,
@ -22,11 +22,13 @@ import {
} from "../script/generic/types";
import { findImagesToFetch } from "../script/blockchain/binance";
describe("Test eth-web3 helpers", () => {
describe("Test eth-address helpers", () => {
test(`Test isChecksum`, () => {
expect(isChecksum("0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee", "ethereum"), `checksum`).toBe(true);
expect(isChecksum("0x7bb09bc8ade747178e95b1d035ecbeebbb18cfee", "ethereum"), `lowercase`).toBe(false);
expect(isChecksum("0x7Bb09bC8aDE747178e95B1D035ecBe", "ethereum"), `too short`).toBe(false);
expect(isChecksum("7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee", "ethereum"), `checksum, no prefix`).toBe(true);
expect(isChecksum("7bb09bc8ade747178e95b1d035ecbeebbb18cfee", "ethereum"), `lowercase, no prefix`).toBe(false);
expect(isChecksum("0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee", "wanchain"), `wanchain wrong checksum`).toBe(false);
expect(isChecksum("0x7bb09bc8ade747178e95b1d035ecbeebbb18cfee", "wanchain"), `wanchain lowercase`).toBe(false);
expect(isChecksum("0x7bB09Bc8Ade747178E95b1d035ECbEebBb18CfEE", "wanchain"), `wanchain checksum`).toBe(true);
@ -34,11 +36,15 @@ describe("Test eth-web3 helpers", () => {
test(`Test toChecksum`, () => {
expect(toChecksum("0x7bb09bc8ade747178e95b1d035ecbeebbb18cfee", "ethereum"), `from lowercase`).toEqual("0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee");
expect(toChecksum("0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee", "ethereum"), `from checksum`).toEqual("0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee");
expect(toChecksum("7bb09bc8ade747178e95b1d035ecbeebbb18cfee", "ethereum"), `from lowercase, no prefix`).toEqual("0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee");
expect(toChecksum("0x7bb09bc8ade747178e95b1d035ecbeebbb18cfee", "wanchain"), `wanchain, from lowercase`).toEqual("0x7bB09Bc8Ade747178E95b1d035ECbEebBb18CfEE");
});
test(`Test isEthereumAddress`, () => {
expect(isEthereumAddress("0x7bb09bc8ade747178e95b1d035ecbeebbb18cfee")).toBe(true);
expect(isEthereumAddress("b09bc8ade747178e95b1d035ecbeebbb18cfee")).toBe(false);
expect(isEthereumAddress("0x7bb09bc8ade747178e95b1d035ecbeebbb18cfee"), `valid, lowercase`).toBe(true);
expect(isEthereumAddress("0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee"), `valid, checksum`).toBe(true);
expect(isEthereumAddress("b09bc8ade747178e95b1d035ecbeebbb18cfee"), `invalid, short`).toBe(false);
expect(isEthereumAddress("7bb09bc8ade747178e95b1d035ecbeebbb18cfee"), `valid, no prefix`).toBe(true);
expect(isEthereumAddress("0x7bb09bc8qde747178e95b1d035ecbeebbb18cfee"), `invalid, length ok, invalid char`).toBe(false);
});
});