UNPKG

@hyperlane-xyz/utils

Version:

General utilities and types for the Hyperlane network

54 lines 2.65 kB
import { expect } from 'chai'; import { addressToBytes, bytesToProtocolAddress, isZeroishAddress, padBytesToLength, } from './addresses.js'; import { ProtocolType } from './types.js'; const ETH_ZERO_ADDR = '0x0000000000000000000000000000000000000000'; const ETH_NON_ZERO_ADDR = '0x0000000000000000000000000000000000000001'; const COS_ZERO_ADDR = 'cosmos1000'; const COS_NON_ZERO_ADDR = 'neutron1jyyjd3x0jhgswgm6nnctxvzla8ypx50tew3ayxxwkrjfxhvje6kqzvzudq'; const SOL_ZERO_ADDR = '111111'; const SOL_NON_ZERO_ADDR = 'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb'; // TODO increase address utility test coverage describe('Address utilities', () => { describe('isZeroishAddress', () => { it('Identifies 0-ish addresses', () => { expect(isZeroishAddress('0x')).to.be.true; expect(isZeroishAddress(ETH_ZERO_ADDR)).to.be.true; expect(isZeroishAddress(COS_ZERO_ADDR)).to.be.true; expect(isZeroishAddress(SOL_ZERO_ADDR)).to.be.true; }); it('Identifies non-0-ish addresses', () => { expect(isZeroishAddress(ETH_NON_ZERO_ADDR)).to.be.false; expect(isZeroishAddress(COS_NON_ZERO_ADDR)).to.be.false; expect(isZeroishAddress(SOL_NON_ZERO_ADDR)).to.be.false; }); }); describe('addressToBytes', () => { it('Converts addresses to bytes', () => { expect(addressToBytes(ETH_NON_ZERO_ADDR).length).to.equal(32); }); it('Rejects zeroish addresses', () => { expect(() => addressToBytes(ETH_ZERO_ADDR)).to.throw(Error); expect(() => addressToBytes(COS_ZERO_ADDR)).to.throw(Error); expect(() => addressToBytes(SOL_ZERO_ADDR)).to.throw(Error); }); }); describe('padBytesToLength', () => { it('Pads bytes to a given length', () => { const bytes = Buffer.from([1, 2, 3]); expect(padBytesToLength(bytes, 5).equals(Buffer.from([0, 0, 1, 2, 3]))); }); it('Rejects bytes that exceed the target length', () => { const bytes = Buffer.from([1, 2, 3]); expect(() => padBytesToLength(bytes, 2)).to.throw(Error); }); }); describe('bytesToProtocolAddress', () => { it('Converts bytes to address', () => { expect(bytesToProtocolAddress(addressToBytes(ETH_NON_ZERO_ADDR), ProtocolType.Ethereum)).to.equal(ETH_NON_ZERO_ADDR); }); it('Rejects zeroish addresses', () => { expect(() => bytesToProtocolAddress(new Uint8Array([0, 0, 0]), ProtocolType.Ethereum)).to.throw(Error); }); }); }); //# sourceMappingURL=addresses.test.js.map