Skip to content

Commit

Permalink
fix: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
crypto-matto committed Jun 7, 2024
1 parent 3eeb12d commit 60ea7b0
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 25 deletions.
7 changes: 0 additions & 7 deletions lib/src/utils/address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
isUint8Array,
stripHexPrefix,
uint8ArrayToHexString,
ensureIfUint8Array,
isAddress,
checkAddressCheckSum,
} from './address';
Expand Down Expand Up @@ -111,12 +110,6 @@ describe('Validate address against network and checksums', function () {
it(`uint8ArrayToHexString convert Uint8Array to HexString`, function () {
expect(uint8ArrayToHexString(new Uint8Array([0x1f, 0x2f, 0x3f, 0x4f]))).to.be.eq('0x1f2f3f4f');
});
it(`ensureIfUint8Array should return Uint8Array instance with Uint8Array input`, function () {
expect(ensureIfUint8Array(new Uint8Array([21, 31]))).to.be.instanceOf(Uint8Array);
});
it(`ensureIfUint8Array should return Array instance with non-array input`, function () {
expect(ensureIfUint8Array([21, 31])).to.be.instanceOf(Array);
});
it(`isAddress should return false for valid EVM address`, function () {
expect(isAddress(address.tendermint)).to.be.eq(false);
});
Expand Down
19 changes: 1 addition & 18 deletions lib/src/utils/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,10 @@ export enum AddressType {
}

export function isHexPrefixed(str: string): boolean {
if (typeof str !== 'string') {
throw new Error(`[isHexPrefixed] input must be type 'string', received type ${typeof str}`);
}

return str.startsWith('0x');
}

export function stripHexPrefix(str: string): string {
if (typeof str !== 'string')
throw new Error(`[stripHexPrefix] input must be type 'string', received ${typeof str}`);

return isHexPrefixed(str) ? str.slice(2) : str;
}

Expand All @@ -49,22 +42,12 @@ export function isHexStrict(hex: Uint8Array | bigint | string | number | boolean
return typeof hex === 'string' && /^((-)?0x[0-9a-f]+|(0x))$/i.test(hex);
}

export function ensureIfUint8Array<T = any>(data: T) {
if (
!(data instanceof Uint8Array) &&
(data as { constructor: { name: string } })?.constructor?.name === 'Uint8Array'
) {
return Uint8Array.from((data as unknown) as Uint8Array);
}
return data;
}

export function checkAddressCheckSum(data: string): boolean {
if (!/^(0x)?[0-9a-f]{40}$/i.test(data)) return false;
const address = data.slice(2);
const updatedData = utf8ToBytes(address.toLowerCase());

const addressHash = uint8ArrayToHexString(keccak256(ensureIfUint8Array(updatedData))).slice(2);
const addressHash = uint8ArrayToHexString(keccak256(updatedData)).slice(2);

for (let i = 0; i < 40; i += 1) {
// the nth letter should be uppercase if the nth digit of casemap is 1
Expand Down

0 comments on commit 60ea7b0

Please sign in to comment.