Skip to content

Commit

Permalink
update: remove web3 dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
crypto-matto committed Jun 6, 2024
1 parent 37bd180 commit 7a34c66
Show file tree
Hide file tree
Showing 3 changed files with 451 additions and 5,300 deletions.
139 changes: 136 additions & 3 deletions lib/src/utils/address.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import bech32 from 'bech32';
import { isAddress } from 'web3-validator';
import { stripHexPrefix } from 'web3-eth-accounts';
import { keccak256 } from 'ethereum-cryptography/keccak';
import { utf8ToBytes } from 'ethereum-cryptography/utils';
import { Network } from '../network/network';
import { Bytes } from './bytes/bytes';
// import { toBase64 } from '@cosmjs/encoding';

export interface AddressValidationProperties {
address: string;
Expand All @@ -22,6 +21,140 @@ export enum AddressType {
* @returns {boolean}
* @throws {Error} when Bech32 encoding is not correct
*/

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

Check warning on line 27 in lib/src/utils/address.ts

View check run for this annotation

Codecov / codecov/patch

lib/src/utils/address.ts#L27

Added line #L27 was not covered by tests
}

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}`);

Check warning on line 35 in lib/src/utils/address.ts

View check run for this annotation

Codecov / codecov/patch

lib/src/utils/address.ts#L35

Added line #L35 was not covered by tests

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

export function isUint8Array(data: unknown | Uint8Array): data is Uint8Array {
return (
data instanceof Uint8Array ||
(data as { constructor: { name: string } })?.constructor?.name === 'Uint8Array' ||
(data as { constructor: { name: string } })?.constructor?.name === 'Buffer'
);
}

export function uint8ArrayToHexString(uint8Array: Uint8Array): string {
return uint8Array.reduce((hexString, e) => {
const hex = e.toString(16);
return hexString + (hex.length === 1 ? `0${hex}` : hex);
}, '0x');
}

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'

Check warning on line 62 in lib/src/utils/address.ts

View check run for this annotation

Codecov / codecov/patch

lib/src/utils/address.ts#L62

Added line #L62 was not covered by tests
) {
return Uint8Array.from((data as unknown) as Uint8Array);

Check warning on line 64 in lib/src/utils/address.ts

View check run for this annotation

Codecov / codecov/patch

lib/src/utils/address.ts#L64

Added line #L64 was not covered by tests
}
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);

for (let i = 0; i < 40; i += 1) {
// the nth letter should be uppercase if the nth digit of casemap is 1
if (
(parseInt(addressHash[i], 16) > 7 && address[i].toUpperCase() !== address[i]) ||
(parseInt(addressHash[i], 16) <= 7 && address[i].toLowerCase() !== address[i])
) {
return false;

Check warning on line 82 in lib/src/utils/address.ts

View check run for this annotation

Codecov / codecov/patch

lib/src/utils/address.ts#L82

Added line #L82 was not covered by tests
}
}
return true;
}

export const isAddress = (value: Uint8Array | bigint | string | number | boolean, checkChecksum = true) => {
if (typeof value !== 'string' && !isUint8Array(value)) {
return false;

Check warning on line 90 in lib/src/utils/address.ts

View check run for this annotation

Codecov / codecov/patch

lib/src/utils/address.ts#L90

Added line #L90 was not covered by tests
}

let valueToCheck: string;

if (isUint8Array(value)) {
valueToCheck = uint8ArrayToHexString(value);

Check warning on line 96 in lib/src/utils/address.ts

View check run for this annotation

Codecov / codecov/patch

lib/src/utils/address.ts#L96

Added line #L96 was not covered by tests
} else if (typeof value === 'string' && !isHexStrict(value)) {
valueToCheck = value.toLowerCase().startsWith('0x') ? value : `0x${value}`;
} else {
valueToCheck = value;
}

// check if it has the basic requirements of an address
if (!/^(0x)?[0-9a-f]{40}$/i.test(valueToCheck)) {
return false;
}
// If it's ALL lowercase or ALL upppercase
if (/^(0x|0X)?[0-9a-f]{40}$/.test(valueToCheck) || /^(0x|0X)?[0-9A-F]{40}$/.test(valueToCheck)) {
return true;

Check warning on line 109 in lib/src/utils/address.ts

View check run for this annotation

Codecov / codecov/patch

lib/src/utils/address.ts#L109

Added line #L109 was not covered by tests
// Otherwise check each case
}
return checkChecksum ? checkAddressCheckSum(valueToCheck) : true;
};

/**
* Checks if the given string is an address
*
* @method isAddress
* @param {String} address the given HEX adress
* @return {Boolean}
*/
// export function isAddress(address: string) {
// if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
// // check if it has the basic requirements of an address
// return false;
// }
// if (/^(0x)?[0-9a-f]{40}$/.test(address) || /^(0x)?[0-9A-F]{40}$/.test(address)) {
// // If it's all small caps or all all caps, return true
// return true;
// }
// // Otherwise check each case
// return isChecksumAddress(address);
// }

// /**
// * Checks if the given string is a checksummed address
// *
// * @method isChecksumAddress
// * @param {String} address the given HEX adress
// * @return {Boolean}
// */
// export function isChecksumAddress(address: string) {
// // Check each case
// const checkSumAddress = address.replace('0x', '');
// let addressHash = sha3(checkSumAddress.toLowerCase());
// for (let i = 0; i < 40; i++) {
// // the nth letter should be uppercase if the nth digit of casemap is 1
// if (
// (parseInt(addressHash[i], 16) > 7 && address[i].toUpperCase() !== address[i]) ||
// (parseInt(addressHash[i], 16) <= 7 && address[i].toLowerCase() !== address[i])
// ) {
// return false;
// }
// }
// return true;
// }

export function validateAddress(addressProps: AddressValidationProperties): boolean | never {
const { network } = addressProps;
const bech32Decoded = bech32.decode(addressProps.address);
Expand Down
Loading

0 comments on commit 7a34c66

Please sign in to comment.