mirror of
https://github.com/Instadapp/trustwallet-assets.git
synced 2024-07-29 22:37:31 +00:00
[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:
parent
87d2340266
commit
0df2291872
2372
package-lock.json
generated
2372
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
|
@ -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"
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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";
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user