Bytes library and CAIP2/CAIP10 helpers (#5252)

Co-authored-by: cairo <cairoeth@protonmail.com>
Co-authored-by: Ernesto García <ernestognw@gmail.com>
Co-authored-by: Arr00 <13561405+arr00@users.noreply.github.com>
This commit is contained in:
Hadrien Croubois
2024-10-14 22:41:08 +02:00
committed by GitHub
parent bd588959ad
commit fe6249ec2c
10 changed files with 476 additions and 0 deletions

88
test/utils/Bytes.test.js Normal file
View File

@ -0,0 +1,88 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
async function fixture() {
const mock = await ethers.deployContract('$Bytes');
return { mock };
}
const lorem = ethers.toUtf8Bytes(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
);
const present = lorem.at(1);
const absent = 255;
describe('Bytes', function () {
before(async function () {
Object.assign(this, await loadFixture(fixture));
});
describe('indexOf', function () {
it('first', async function () {
expect(await this.mock.$indexOf(lorem, ethers.toBeHex(present))).to.equal(lorem.indexOf(present));
});
it('from index', async function () {
for (const start in Array(lorem.length + 10).fill()) {
const index = lorem.indexOf(present, start);
const result = index === -1 ? ethers.MaxUint256 : index;
expect(await this.mock.$indexOf(lorem, ethers.toBeHex(present), ethers.Typed.uint256(start))).to.equal(result);
}
});
it('absent', async function () {
expect(await this.mock.$indexOf(lorem, ethers.toBeHex(absent))).to.equal(ethers.MaxUint256);
});
});
describe('lastIndexOf', function () {
it('first', async function () {
expect(await this.mock.$lastIndexOf(lorem, ethers.toBeHex(present))).to.equal(lorem.lastIndexOf(present));
});
it('from index', async function () {
for (const start in Array(lorem.length + 10).fill()) {
const index = lorem.lastIndexOf(present, start);
const result = index === -1 ? ethers.MaxUint256 : index;
expect(await this.mock.$lastIndexOf(lorem, ethers.toBeHex(present), ethers.Typed.uint256(start))).to.equal(
result,
);
}
});
it('absent', async function () {
expect(await this.mock.$lastIndexOf(lorem, ethers.toBeHex(absent))).to.equal(ethers.MaxUint256);
});
});
describe('slice', function () {
describe('slice(bytes, uint256)', function () {
for (const [descr, start] of Object.entries({
'start = 0': 0,
'start within bound': 10,
'start out of bound': 1000,
})) {
it(descr, async function () {
const result = ethers.hexlify(lorem.slice(start));
expect(await this.mock.$slice(lorem, start)).to.equal(result);
});
}
});
describe('slice(bytes, uint256, uint256)', function () {
for (const [descr, [start, end]] of Object.entries({
'start = 0': [0, 42],
'start and end within bound': [17, 42],
'end out of bound': [42, 1000],
'start = end': [17, 17],
'start > end': [42, 17],
})) {
it(descr, async function () {
const result = ethers.hexlify(lorem.slice(start, end));
expect(await this.mock.$slice(lorem, start, ethers.Typed.uint256(end))).to.equal(result);
});
}
});
});
});

53
test/utils/CAIP.test.js Normal file
View File

@ -0,0 +1,53 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { CHAINS, getLocalCAIP } = require('../helpers/chains');
async function fixture() {
const caip2 = await ethers.deployContract('$CAIP2');
const caip10 = await ethers.deployContract('$CAIP10');
return { caip2, caip10 };
}
describe('CAIP utilities', function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
});
describe('CAIP-2', function () {
it('local()', async function () {
const { caip2 } = await getLocalCAIP();
expect(await this.caip2.$local()).to.equal(caip2);
});
for (const { namespace, reference, caip2 } of Object.values(CHAINS))
it(`format(${namespace}, ${reference})`, async function () {
expect(await this.caip2.$format(namespace, reference)).to.equal(caip2);
});
for (const { namespace, reference, caip2 } of Object.values(CHAINS))
it(`parse(${caip2})`, async function () {
expect(await this.caip2.$parse(caip2)).to.deep.equal([namespace, reference]);
});
});
describe('CAIP-10', function () {
const { address: account } = ethers.Wallet.createRandom();
it(`local(${account})`, async function () {
const { caip10 } = await getLocalCAIP(account);
expect(await this.caip10.$local(ethers.Typed.address(account))).to.equal(caip10);
});
for (const { account, caip2, caip10 } of Object.values(CHAINS))
it(`format(${caip2}, ${account})`, async function () {
expect(await this.caip10.$format(ethers.Typed.string(caip2), ethers.Typed.string(account))).to.equal(caip10);
});
for (const { account, caip2, caip10 } of Object.values(CHAINS))
it(`parse(${caip10})`, async function () {
expect(await this.caip10.$parse(caip10)).to.deep.equal([caip2, account]);
});
});
});