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 7a34c66 commit 3eeb12d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 54 deletions.
3 changes: 0 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# Changelog

All notable changes to this project will be documented in this file.
## v1.1.3
- Upgrade `web3` to `4.0.1`

## v1.1.2
- Added CRO `Croeseid-5` Testnet network
- Update CRO `Mainnet` & `Croeseid-4` Testnet network endpoints
Expand Down
84 changes: 83 additions & 1 deletion lib/src/utils/address.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import 'mocha';
import { expect } from 'chai';
import { AddressType, AddressValidator, validateAddress, getBech32AddressFromEVMAddress } from './address';
import {
AddressType,
AddressValidator,
validateAddress,
getBech32AddressFromEVMAddress,
isHexPrefixed,
isHexStrict,
isUint8Array,
stripHexPrefix,
uint8ArrayToHexString,
ensureIfUint8Array,
isAddress,
checkAddressCheckSum,
} from './address';
import { CroNetwork } from '../core/cro';

describe('Validate address against network and checksums', function () {
Expand Down Expand Up @@ -52,6 +65,75 @@ describe('Validate address against network and checksums', function () {
).to.throw('Invalid checksum for tcrocncl1reyshfdygf7673xm9p8v0xvtd96m6cd6canhu3xcqa');
});

describe('Hex and Bytes conversion checking', function () {
const address = {
tendermint: 'cro1pndm4ywdf4qtmupa0fqe75krmqed2znjyj6x8f',
EVM: '0xD47286f025F947482a2C374Fb70e9D4c94d809CF',
};
it('isHexPrefixed should return false for tendermint address', function () {
expect(isHexPrefixed(address.tendermint)).to.be.eq(false);
});

it('isHexPrefixed should return true for EVM address', function () {
expect(isHexPrefixed(address.EVM)).to.be.eq(true);
});

it('isHexStrict should return true for hexString', function () {
expect(isHexStrict('0xc1912')).to.be.eq(true);
});
it('isHexStrict should return false for hex', function () {
expect(isHexStrict(0xc1912)).to.be.eq(false);
});
it('isHexStrict should return false for normal string', function () {
expect(isHexStrict('c1912')).to.be.eq(false);
});
it('isHexStrict should return false for number', function () {
expect(isHexStrict(345)).to.be.eq(false);
});
it('isUint8Array should return true for Uint8Array instance', function () {
expect(isUint8Array(new Uint8Array([21, 31]))).to.be.eq(true);
});
it('isUint8Array should return true for Buffer instance', function () {
expect(isUint8Array(Buffer.from([21, 31]))).to.be.eq(true);
});
it('isUint8Array should return false for string', function () {
expect(isUint8Array('string')).to.be.eq(false);
});
it(`stripHexPrefix should return ${address.tendermint} without modification`, function () {
expect(stripHexPrefix(address.tendermint)).to.be.eq(address.tendermint);
});
it(`stripHexPrefix should trim '0x' from ${address.EVM}`, function () {
expect(stripHexPrefix(address.EVM)).to.be.eq('D47286f025F947482a2C374Fb70e9D4c94d809CF');
});
it(`uint8ArrayToHexString convert empty Uint8Array to HexString`, function () {
expect(uint8ArrayToHexString(new Uint8Array(4))).to.be.eq('0x00000000');
});
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);
});
it(`isAddress should return true for valid EVM address`, function () {
expect(isAddress(address.EVM)).to.be.eq(true);
});
it(`isAddress should return true for valid trimmed EVM address`, function () {
expect(isAddress(stripHexPrefix(address.EVM))).to.be.eq(true);
});
it(`checkAddressCheckSum should return true for valid EVM address`, function () {
expect(checkAddressCheckSum(address.EVM)).to.be.eq(true);
});
it(`checkAddressCheckSum should return false for invalid EVM address`, function () {
expect(checkAddressCheckSum('0x6c46a1e212f127a6a8787b456a243c0d')).to.be.eq(false);
});
});

describe('AddressValidator', function () {
it('validate should throw Error when the address is invalid', function () {
const addressProps = {
Expand Down
50 changes: 0 additions & 50 deletions lib/src/utils/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ export enum AddressType {
USER,
VALIDATOR,
}
// https://stackoverflow.com/questions/49434751/how-to-declare-a-function-that-throws-an-error-in-typescript
/**
* Check address validity against its type and provided network
* @param {AddressValidationProperties} addressProps
* @returns {boolean}
* @throws {Error} when Bech32 encoding is not correct
*/

export function isHexPrefixed(str: string): boolean {
if (typeof str !== 'string') {
Expand Down Expand Up @@ -112,49 +105,6 @@ export const isAddress = (value: Uint8Array | bigint | string | number | boolean
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"test:e2e:tx-decoder": "NODE_ENV=test mocha --timeout 0 --require ./node_modules/ts-node/register --exit --color ./lib/e2e/tx-decoder/*.ts",
"test:e2e:offline-signing": "NODE_ENV=test mocha --timeout 0 --require ./node_modules/ts-node/register --exit --color ./lib/e2e/offline-signing/*.ts",
"test:e2e:ibc": "NODE_ENV=test mocha --timeout 0 --require ./node_modules/ts-node/register --exit --color ./lib/e2e/ibc.spec.ts",
"test:address": "NODE_ENV=test mocha --timeout 10000 --require ./node_modules/ts-node/register --exit --color 'lib/src/utils/address.spec.ts'",
"preget-proto": "rm -rf proto",
"get-proto": "COSMOS_REF=v0.43.0 ICS23_REF=v0.6.3 CHAIN_MAIN_REF=v2.1.1 COSMOS_IBC_REF=v1.0.0 ./lib/src/cosmos/v1beta1/scripts/get-proto.sh",
"predefine-proto": "./lib/src/cosmos/v1beta1/scripts/predefine-proto.sh",
Expand Down

0 comments on commit 3eeb12d

Please sign in to comment.