UNPKG

620 BJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util-crypto authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { u8aToHex } from '@polkadot/util';
4import { keccakAsU8a } from "../keccak/index.js";
5
6function isInvalidChar(char, byte) {
7 return char !== (byte > 7 ? char.toUpperCase() : char.toLowerCase());
8}
9
10export function isEthereumChecksum(_address) {
11 const address = _address.replace('0x', '');
12
13 const hash = u8aToHex(keccakAsU8a(address.toLowerCase()), -1, false);
14
15 for (let i = 0; i < 40; i++) {
16 if (isInvalidChar(address[i], parseInt(hash[i], 16))) {
17 return false;
18 }
19 }
20
21 return true;
22}
\No newline at end of file