Skip to content

Commit

Permalink
feat: address utils
Browse files Browse the repository at this point in the history
  • Loading branch information
rodolfopietro97 committed May 31, 2024
1 parent 9bb4298 commit 86dbd75
Showing 1 changed file with 12 additions and 37 deletions.
49 changes: 12 additions & 37 deletions tests/e2e/src/extension/utils/AddressUtils/AddressUtils.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,39 @@
import { address } from 'thor-devkit';

const PREFIX = '0x';
const PREFIX_REGEX = /^0[xX]/;

/**
* Returns the provided hex string with the hex prefix added.
* If the prefix already exists the string is returned unmodified.
* If the string contains an UPPER case `X` in the prefix it will be replaced with a lower case `x`
* @param hex - the input hex string
* @returns the input hex string with the hex prefix added
* @throws an error if the input is not a valid hex string
*/
const addPrefix = (hex: string): string => {
validate(hex);
return PREFIX_REGEX.test(hex)
? hex.replace(PREFIX_REGEX, PREFIX)
: `${PREFIX}${hex}`;
};
import { addressUtils, Hex0x } from '@vechain/sdk-core';

/**
* Validate the hex string. Throws an Error if not valid
* @param hex - the input hex string
* @throws an error if the input is not a valid hex string
*/
const validate = (hex: string) => {
if (!isValid(hex)) throw Error(`Provided hex value is not valid ${hex}`);
};

export const isValid = (addr: string): boolean => {
try {
address.toChecksumed(addPrefix(addr));
return true;
} catch (e) {
return false;
}
if (!addressUtils.isAddress(hex))
throw Error(`Provided hex value is not valid ${hex}`);
};

/**
* Checks if two addresses are equal. Returns true if both values are strings AND:
* - The two values are equal OR
* - The checksumed addresses are equal
*
* @param address1
* @param address2
* @param address1 - the first address
* @param address2 - the second address
* @returns true if the addresses are equal
*/
export const compareAddresses = (
address1: unknown,
address2: unknown,
address1: unknown | string,
address2: unknown | string,
): boolean => {
if (typeof address1 !== 'string' || typeof address2 !== 'string')
return false;

if (address2 === address1) return true;

try {
address1 = addPrefix(address1);
address2 = addPrefix(address2);
address1 = Hex0x(address1);
address2 = Hex0x(address2);
return (
address.toChecksumed(address1 as string) ===
address.toChecksumed(address2 as string)
addressUtils.toERC55Checksum(address1 as string) ===
addressUtils.toERC55Checksum(address2 as string)
);
} catch (e) {
return false;
Expand Down

0 comments on commit 86dbd75

Please sign in to comment.