Skip to content

Commit

Permalink
[internal] Wanchain uses special reverse checksum format (trustwallet…
Browse files Browse the repository at this point in the history
…#3216)

* ReverseCase

* Wanchain uses special reverse checksum format.

* Fix existing 6 wanchain assets, revert checksum format.

Co-authored-by: Catenocrypt <[email protected]>
  • Loading branch information
optout21 and Catenocrypt authored Aug 10, 2020
1 parent 483c966 commit cbe4ef9
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 18 deletions.
12 changes: 6 additions & 6 deletions blockchains/wanchain/whitelist.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[
"0x0F2E7c0e904A525a24ff6F658053F6Ba5cE7a209",
"0x350BB91c9c8eF4E8c0E19FBDC6a90a24BCB1Ec2a",
"0x3beb80170272F07Aca12987af86a59888255a807",
"0x51316966907EF568689775066E97C46A4fFDE1A4",
"0xa13A41A2349d09c1e3A0Dd4D9448aF4Ad799Fc4a",
"0xC6F4465A6a521124C8e3096B62575c157999D361"
"0x0f2e7C0E904a525A24FF6f658053f6bA5Ce7A209",
"0x350bb91C9C8Ef4e8C0e19fbdc6A90A24bcb1eC2A",
"0x3BEB80170272f07aCA12987AF86A59888255A807",
"0x51316966907ef568689775066e97c46a4Ffde1a4",
"0xA13a41a2349D09C1E3a0dD4d9448Af4aD799fC4A",
"0xc6f4465a6A521124c8E3096b62575C157999d361"
]
8 changes: 4 additions & 4 deletions script/action/eth-forks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ async function formatInfos() {
})
}

function checkAddressChecksum(assetsFolderPath: string, address: string) {
const checksumAddress = toChecksum(address);
function checkAddressChecksum(assetsFolderPath: string, address: string, chain: string) {
const checksumAddress = toChecksum(address, chain);
if (checksumAddress !== address) {
gitMove(assetsFolderPath, address, checksumAddress);
console.log(`Renamed to checksum format ${checksumAddress}`);
Expand All @@ -60,7 +60,7 @@ async function checkAddressChecksums() {
gitMove(getChainAssetPath(chain, address), file, logoFullName);
}
});
checkAddressChecksum(assetsPath, address);
checkAddressChecksum(assetsPath, address, chain);
});
});
}
Expand All @@ -84,7 +84,7 @@ export class EthForks implements ActionInterface {
if (!isPathExistsSync(assetPath)) {
error += `Expect directory at path: ${assetPath}\n`;
}
const inChecksum = toChecksum(address);
const inChecksum = toChecksum(address, chain);
if (address !== inChecksum) {
error += `Expect asset at path ${assetPath} in checksum: '${inChecksum}'\n`;
}
Expand Down
29 changes: 27 additions & 2 deletions script/common/eth-web3.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import { reverseCase } from "./types";

const Web3 = require('web3');

const web3 = new Web3('ws://localhost:8546');

export const isChecksum = (address: string): boolean => web3.utils.checkAddressChecksum(address);
export const toChecksum = (address: string): string => web3.utils.toChecksumAddress(address);
export const isChecksumEthereum = (address: string): boolean => web3.utils.checkAddressChecksum(address);
export const toChecksumEthereum = (address: string): string => web3.utils.toChecksumAddress(address);

export function toChecksum(address: string, chain: string = "ethereum"): string {
var checksumEthereum = toChecksumEthereum(address);

// special handling for Wanchain
if (chain.toLowerCase() === "wanchain") {
const checksumWanchain = reverseCase(checksumEthereum).replace("X", "x");
return checksumWanchain;
}

return checksumEthereum;
}

export function isChecksum(address: string, chain: string = "ethereum"): boolean {
// special handling for Wanchain
if (chain.toLowerCase() === "wanchain") {
const addressEthereum = reverseCase(address).replace("X", "x");
return isChecksumEthereum(addressEthereum);
}

return isChecksumEthereum(address);
}

16 changes: 16 additions & 0 deletions script/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,19 @@ export function arrayEqual(a1: any[], a2: any[]): boolean {
}
return true;
}

export function reverseCase(s: string): string {
const n = s.length;
var out: string = "";
for (var i = 0; i < n; ++i) {
const c = s[i];
if (isLowerCase(c)) {
out += c.toUpperCase();
} else if (isUpperCase(s[i])) {
out += c.toLowerCase();
} else {
out += c;
}
}
return out;
}
21 changes: 15 additions & 6 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ import {
makeUnique,
arrayDiff,
arrayDiffNocase,
arrayEqual
arrayEqual,
reverseCase
} from "../script/common/types";
import { findImagesToFetch } from "../script/action/binance";

describe("Test eth-web3 helpers", () => {
test(`Test isChecksum`, () => {
expect(isChecksum("0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee"), `checksum`).toBe(true);
expect(isChecksum("0x7bb09bc8ade747178e95b1d035ecbeebbb18cfee"), `lowercase`).toBe(false);
expect(isChecksum("0x7Bb09bC8aDE747178e95B1D035ecBe"), `too short`).toBe(false);
expect(isChecksum("0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee", "ethereum"), `checksum`).toBe(true);
expect(isChecksum("0x7bb09bc8ade747178e95b1d035ecbeebbb18cfee", "ethereum"), `lowercase`).toBe(false);
expect(isChecksum("0x7Bb09bC8aDE747178e95B1D035ecBe", "ethereum"), `too short`).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);
});
test(`Test toChecksum`, () => {
expect(toChecksum("0x7bb09bc8ade747178e95b1d035ecbeebbb18cfee"), `from lowercase`).toEqual("0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee");
expect(toChecksum("0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee"), `from checksum`).toEqual("0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee");
expect(toChecksum("0x7bb09bc8ade747178e95b1d035ecbeebbb18cfee", "ethereum"), `from lowercase`).toEqual("0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee");
expect(toChecksum("0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee", "ethereum"), `from checksum`).toEqual("0x7Bb09bC8aDE747178e95B1D035ecBeEBbB18cFee");
expect(toChecksum("0x7bb09bc8ade747178e95b1d035ecbeebbb18cfee", "wanchain"), `wanchain, from lowercase`).toEqual("0x7bB09Bc8Ade747178E95b1d035ECbEebBb18CfEE");
});
});

Expand Down Expand Up @@ -106,6 +111,10 @@ describe("Test type helpers", () => {
expect(arrayEqual(["a", "b", "c"], ["a", "b", "b"]), `length mismatch`).toBe(false);
expect(arrayEqual(["a", "b", "b"], ["a", "b", "c"]), `length mismatch`).toBe(false);
});
test(`Test reverseCase`, () => {
expect(reverseCase("abCDef12+-"), `mixed`).toEqual("ABcdEF12+-");
expect(reverseCase("ABcdEF12+-"), `mixed`).toEqual("abCDef12+-");
});
});

describe("Test action binance", () => {
Expand Down

0 comments on commit cbe4ef9

Please sign in to comment.