Skip to content

Commit

Permalink
✅(migration) Migrate ethers and hardhat tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Nakasar committed Sep 11, 2024
1 parent 2467bfe commit 2461768
Show file tree
Hide file tree
Showing 12 changed files with 543 additions and 542 deletions.
2 changes: 1 addition & 1 deletion test/claim-issuers/claim-issuer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('ClaimIssuer - Reference (with revoke)', () => {
it('should return with a zero address with signature is not of proper length', async () => {
const { claimIssuer, aliceClaim666 } = await loadFixture(deployIdentityFixture);

expect(await claimIssuer.getRecoveredAddress(aliceClaim666.signature + "00", ethers.utils.arrayify(ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(['address', 'uint256', 'bytes'], [aliceClaim666.identity, aliceClaim666.topic, aliceClaim666.data]))))).to.be.equal(ethers.constants.AddressZero);
expect(await claimIssuer.getRecoveredAddress(aliceClaim666.signature + "00", ethers.getBytes(ethers.keccak256(ethers.AbiCoder.defaultAbiCoder().encode(['address', 'uint256', 'bytes'], [aliceClaim666.identity, aliceClaim666.topic, aliceClaim666.data]))))).to.be.equal(ethers.ZeroAddress);
});
});
});
60 changes: 30 additions & 30 deletions test/factory/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,59 @@ describe('IdFactory', () => {
const [deployerWallet] = await ethers.getSigners();

const IdFactory = await ethers.getContractFactory('IdFactory');
await expect(IdFactory.connect(deployerWallet).deploy(ethers.constants.AddressZero)).to.be.revertedWith('invalid argument - zero address');
await expect(IdFactory.connect(deployerWallet).deploy(ethers.ZeroAddress)).to.be.revertedWith('invalid argument - zero address');
});

it('should revert because sender is not allowed to create identities', async () => {
const {identityFactory, aliceWallet} = await loadFixture(deployIdentityFixture);

await expect(identityFactory.connect(aliceWallet).createIdentity(ethers.constants.AddressZero, 'salt1')).to.be.revertedWith('Ownable: caller is not the owner');
await expect(identityFactory.connect(aliceWallet).createIdentity(ethers.ZeroAddress, 'salt1')).to.be.revertedWithCustomError(identityFactory, 'OwnableUnauthorizedAccount');
});

it('should revert because wallet of identity cannot be Zero address', async () => {
const {identityFactory, deployerWallet} = await loadFixture(deployIdentityFixture);

await expect(identityFactory.connect(deployerWallet).createIdentity(ethers.constants.AddressZero, 'salt1')).to.be.revertedWith('invalid argument - zero address');
await expect(identityFactory.connect(deployerWallet).createIdentity(ethers.ZeroAddress, 'salt1')).to.be.revertedWith('invalid argument - zero address');
});

it('should revert because salt cannot be empty', async () => {
const {identityFactory, deployerWallet, davidWallet} = await loadFixture(deployIdentityFixture);

await expect(identityFactory.connect(deployerWallet).createIdentity(davidWallet.address, '')).to.be.revertedWith('invalid argument - empty string');
await expect(identityFactory.connect(deployerWallet).createIdentity(davidWallet, '')).to.be.revertedWith('invalid argument - empty string');
});

it('should revert because salt cannot be already used', async () => {
const {identityFactory, deployerWallet, davidWallet, carolWallet} = await loadFixture(deployIdentityFixture);

await identityFactory.connect(deployerWallet).createIdentity(carolWallet.address, 'saltUsed');
await identityFactory.connect(deployerWallet).createIdentity(carolWallet, 'saltUsed');

await expect(identityFactory.connect(deployerWallet).createIdentity(davidWallet.address, 'saltUsed')).to.be.revertedWith('salt already taken');
await expect(identityFactory.connect(deployerWallet).createIdentity(davidWallet, 'saltUsed')).to.be.revertedWith('salt already taken');
});

it('should revert because wallet is already linked to an identity', async () => {
const {identityFactory, deployerWallet, aliceWallet} = await loadFixture(deployIdentityFixture);

await expect(identityFactory.connect(deployerWallet).createIdentity(aliceWallet.address, 'newSalt')).to.be.revertedWith('wallet already linked to an identity');
await expect(identityFactory.connect(deployerWallet).createIdentity(aliceWallet, 'newSalt')).to.be.revertedWith('wallet already linked to an identity');
});

describe('link/unlink wallet', () => {
describe('linkWallet', () => {
it('should revert for new wallet being zero address', async () => {
const { identityFactory, aliceWallet } = await loadFixture(deployIdentityFixture);

await expect(identityFactory.connect(aliceWallet).linkWallet(ethers.constants.AddressZero)).to.be.revertedWith('invalid argument - zero address');
await expect(identityFactory.connect(aliceWallet).linkWallet(ethers.ZeroAddress)).to.be.revertedWith('invalid argument - zero address');
});

it('should revert for sender wallet being not linked', async () => {
const { identityFactory, davidWallet } = await loadFixture(deployIdentityFixture);

await expect(identityFactory.connect(davidWallet).linkWallet(davidWallet.address)).to.be.revertedWith('wallet not linked to an identity contract');
await expect(identityFactory.connect(davidWallet).linkWallet(davidWallet)).to.be.revertedWith('wallet not linked to an identity contract');
});

it('should revert for new wallet being already linked', async () => {
const { identityFactory, bobWallet, aliceWallet } = await loadFixture(deployIdentityFixture);

await expect(identityFactory.connect(bobWallet).linkWallet(aliceWallet.address)).to.be.revertedWith('new wallet already linked');
await expect(identityFactory.connect(bobWallet).linkWallet(aliceWallet)).to.be.revertedWith('new wallet already linked');
});

it('should revert for new wallet being already to a token identity', async () => {
Expand All @@ -73,40 +73,40 @@ describe('IdFactory', () => {
it('should link the new wallet to the existing identity', async () => {
const { identityFactory, aliceIdentity, aliceWallet, davidWallet } = await loadFixture(deployIdentityFixture);

const tx = await identityFactory.connect(aliceWallet).linkWallet(davidWallet.address);
await expect(tx).to.emit(identityFactory, 'WalletLinked').withArgs(davidWallet.address, aliceIdentity.address);
const tx = await identityFactory.connect(aliceWallet).linkWallet(davidWallet);
await expect(tx).to.emit(identityFactory, 'WalletLinked').withArgs(davidWallet, aliceIdentity);

expect(await identityFactory.getWallets(aliceIdentity.address)).to.deep.equal([aliceWallet.address, davidWallet.address]);
expect(await identityFactory.getWallets(aliceIdentity)).to.deep.equal([await aliceWallet.getAddress(), await davidWallet.getAddress()]);
});
});

describe('unlinkWallet', () => {
it('should revert for wallet to unlink being zero address', async () => {
const { identityFactory, aliceWallet } = await loadFixture(deployIdentityFixture);

await expect(identityFactory.connect(aliceWallet).unlinkWallet(ethers.constants.AddressZero)).to.be.revertedWith('invalid argument - zero address');
await expect(identityFactory.connect(aliceWallet).unlinkWallet(ethers.ZeroAddress)).to.be.revertedWith('invalid argument - zero address');
});

it('should revert for sender wallet attemoting to unlink itself', async () => {
const { identityFactory, aliceWallet } = await loadFixture(deployIdentityFixture);

await expect(identityFactory.connect(aliceWallet).unlinkWallet(aliceWallet.address)).to.be.revertedWith('cannot be called on sender address');
await expect(identityFactory.connect(aliceWallet).unlinkWallet(aliceWallet)).to.be.revertedWith('cannot be called on sender address');
});

it('should revert for sender wallet being not linked', async () => {
const { identityFactory, aliceWallet, davidWallet } = await loadFixture(deployIdentityFixture);

await expect(identityFactory.connect(davidWallet).unlinkWallet(aliceWallet.address)).to.be.revertedWith('only a linked wallet can unlink');
await expect(identityFactory.connect(davidWallet).unlinkWallet(aliceWallet)).to.be.revertedWith('only a linked wallet can unlink');
});

it('should unlink the wallet', async () => {
const { identityFactory, aliceIdentity, aliceWallet, davidWallet } = await loadFixture(deployIdentityFixture);

await identityFactory.connect(aliceWallet).linkWallet(davidWallet.address);
const tx = await identityFactory.connect(aliceWallet).unlinkWallet(davidWallet.address);
await expect(tx).to.emit(identityFactory, 'WalletUnlinked').withArgs(davidWallet.address, aliceIdentity.address);
await identityFactory.connect(aliceWallet).linkWallet(davidWallet);
const tx = await identityFactory.connect(aliceWallet).unlinkWallet(davidWallet);
await expect(tx).to.emit(identityFactory, 'WalletUnlinked').withArgs(davidWallet, aliceIdentity);

expect(await identityFactory.getWallets(aliceIdentity.address)).to.deep.equal([aliceWallet.address]);
expect(await identityFactory.getWallets(aliceIdentity)).to.deep.equal([await aliceWallet.getAddress()]);
});
});
});
Expand All @@ -116,17 +116,17 @@ describe('IdFactory', () => {
it('should revert', async () => {
const {identityFactory, deployerWallet, davidWallet} = await loadFixture(deployIdentityFixture);

await expect(identityFactory.connect(deployerWallet).createIdentityWithManagementKeys(davidWallet.address, 'salt1', [])).to.be.revertedWith('invalid argument - empty list of keys');
await expect(identityFactory.connect(deployerWallet).createIdentityWithManagementKeys(davidWallet, 'salt1', [])).to.be.revertedWith('invalid argument - empty list of keys');
});
});

describe('when the wallet is included in the management keys listed', () => {
it('should revert', async () => {
const {identityFactory, deployerWallet, aliceWallet, davidWallet} = await loadFixture(deployIdentityFixture);

await expect(identityFactory.connect(deployerWallet).createIdentityWithManagementKeys(davidWallet.address, 'salt1', [
ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(['address'], [aliceWallet.address])),
ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(['address'], [davidWallet.address])),
await expect(identityFactory.connect(deployerWallet).createIdentityWithManagementKeys(davidWallet, 'salt1', [
ethers.keccak256(ethers.AbiCoder.defaultAbiCoder().encode(['address'], [await aliceWallet.getAddress()])),
ethers.keccak256(ethers.AbiCoder.defaultAbiCoder().encode(['address'], [await davidWallet.getAddress()])),
])).to.be.revertedWith('invalid argument - wallet is also listed in management keys');
});
});
Expand All @@ -135,24 +135,24 @@ describe('IdFactory', () => {
it('should deploy the identity proxy, set keys and wallet as management, and link wallet to identity', async () => {
const {identityFactory, deployerWallet, aliceWallet, davidWallet} = await loadFixture(deployIdentityFixture);

const tx = await identityFactory.connect(deployerWallet).createIdentityWithManagementKeys(davidWallet.address, 'salt1', [ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(['address'], [aliceWallet.address]))]);
const tx = await identityFactory.connect(deployerWallet).createIdentityWithManagementKeys(davidWallet, 'salt1', [ethers.keccak256(ethers.AbiCoder.defaultAbiCoder().encode(['address'], [await aliceWallet.getAddress()]))]);

await expect(tx).to.emit(identityFactory, 'WalletLinked');
await expect(tx).to.emit(identityFactory, 'Deployed');

const identity = await ethers.getContractAt('Identity', await identityFactory.getIdentity(davidWallet.address));
const identity = await ethers.getContractAt('Identity', await identityFactory.getIdentity(davidWallet));

await expect(tx).to.emit(identity, 'KeyAdded').withArgs(ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(['address'], [aliceWallet.address])), 1, 1);
await expect(tx).to.emit(identity, 'KeyAdded').withArgs(ethers.keccak256(ethers.AbiCoder.defaultAbiCoder().encode(['address'], [await aliceWallet.getAddress()])), 1, 1);
await expect(identity.keyHasPurpose(
ethers.utils.defaultAbiCoder.encode(['address'], [identityFactory.address]),
ethers.AbiCoder.defaultAbiCoder().encode(['address'], [await identityFactory.getAddress()]),
1
)).to.eventually.be.false;
await expect(identity.keyHasPurpose(
ethers.utils.defaultAbiCoder.encode(['address'], [davidWallet.address]),
ethers.AbiCoder.defaultAbiCoder().encode(['address'], [await davidWallet.getAddress()]),
1
)).to.eventually.be.false;
await expect(identity.keyHasPurpose(
ethers.utils.defaultAbiCoder.encode(['address'], [aliceWallet.address]),
ethers.AbiCoder.defaultAbiCoder().encode(['address'], [await aliceWallet.getAddress()]),
1
)).to.eventually.be.false;
});
Expand Down
40 changes: 20 additions & 20 deletions test/factory/token-oid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,67 @@ describe('IdFactory', () => {
it('should manipulate Token factory list', async () => {
const { identityFactory, deployerWallet, aliceWallet, bobWallet } = await loadFixture(deployFactoryFixture);

await expect(identityFactory.connect(aliceWallet).addTokenFactory(aliceWallet.address)).to.be.revertedWith('Ownable: caller is not the owner');
await expect(identityFactory.connect(aliceWallet).addTokenFactory(aliceWallet)).to.be.revertedWithCustomError(identityFactory, 'OwnableUnauthorizedAccount');

await expect(identityFactory.connect(deployerWallet).addTokenFactory(ethers.constants.AddressZero)).to.be.revertedWith('invalid argument - zero address');
await expect(identityFactory.connect(deployerWallet).addTokenFactory(ethers.ZeroAddress)).to.be.revertedWith('invalid argument - zero address');

const addTx = await identityFactory.connect(deployerWallet).addTokenFactory(aliceWallet.address);
await expect(addTx).to.emit(identityFactory, 'TokenFactoryAdded').withArgs(aliceWallet.address);
const addTx = await identityFactory.connect(deployerWallet).addTokenFactory(aliceWallet);
await expect(addTx).to.emit(identityFactory, 'TokenFactoryAdded').withArgs(aliceWallet);

await expect(identityFactory.connect(deployerWallet).addTokenFactory(aliceWallet.address)).to.be.revertedWith('already a factory');
await expect(identityFactory.connect(deployerWallet).addTokenFactory(aliceWallet)).to.be.revertedWith('already a factory');

await expect(identityFactory.connect(aliceWallet).removeTokenFactory(bobWallet.address)).to.be.revertedWith('Ownable: caller is not the owner');
await expect(identityFactory.connect(aliceWallet).removeTokenFactory(bobWallet)).to.be.revertedWithCustomError(identityFactory, 'OwnableUnauthorizedAccount');

await expect(identityFactory.connect(deployerWallet).removeTokenFactory(ethers.constants.AddressZero)).to.be.revertedWith('invalid argument - zero address');
await expect(identityFactory.connect(deployerWallet).removeTokenFactory(ethers.ZeroAddress)).to.be.revertedWith('invalid argument - zero address');

await expect(identityFactory.connect(deployerWallet).removeTokenFactory(bobWallet.address)).to.be.revertedWith('not a factory');
await expect(identityFactory.connect(deployerWallet).removeTokenFactory(bobWallet)).to.be.revertedWith('not a factory');

const removeTx = await identityFactory.connect(deployerWallet).removeTokenFactory(aliceWallet.address);
await expect(removeTx).to.emit(identityFactory, 'TokenFactoryRemoved').withArgs(aliceWallet.address);
const removeTx = await identityFactory.connect(deployerWallet).removeTokenFactory(aliceWallet);
await expect(removeTx).to.emit(identityFactory, 'TokenFactoryRemoved').withArgs(aliceWallet);
});
});

describe('createTokenIdentity', () => {
it('should revert for being not authorized to deploy token', async () => {
const { identityFactory, aliceWallet } = await loadFixture(deployFactoryFixture);

await expect(identityFactory.connect(aliceWallet).createTokenIdentity(aliceWallet.address, aliceWallet.address, 'TST')).to.be.revertedWith('only Factory or owner can call');
await expect(identityFactory.connect(aliceWallet).createTokenIdentity(aliceWallet, aliceWallet, 'TST')).to.be.revertedWith('only Factory or owner can call');
});

it('should revert for token address being zero address', async () => {
const { identityFactory, deployerWallet, aliceWallet } = await loadFixture(deployFactoryFixture);

await expect(identityFactory.connect(deployerWallet).createTokenIdentity(ethers.constants.AddressZero, aliceWallet.address, 'TST')).to.be.revertedWith('invalid argument - zero address');
await expect(identityFactory.connect(deployerWallet).createTokenIdentity(ethers.ZeroAddress, aliceWallet, 'TST')).to.be.revertedWith('invalid argument - zero address');
});

it('should revert for owner being zero address', async () => {
const { identityFactory, deployerWallet, aliceWallet } = await loadFixture(deployFactoryFixture);

await expect(identityFactory.connect(deployerWallet).createTokenIdentity(aliceWallet.address, ethers.constants.AddressZero, 'TST')).to.be.revertedWith('invalid argument - zero address');
await expect(identityFactory.connect(deployerWallet).createTokenIdentity(aliceWallet, ethers.ZeroAddress, 'TST')).to.be.revertedWith('invalid argument - zero address');
});

it('should revert for salt being empty', async () => {
const { identityFactory, deployerWallet, aliceWallet } = await loadFixture(deployFactoryFixture);

await expect(identityFactory.connect(deployerWallet).createTokenIdentity(aliceWallet.address, aliceWallet.address, '')).to.be.revertedWith('invalid argument - empty string');
await expect(identityFactory.connect(deployerWallet).createTokenIdentity(aliceWallet, aliceWallet, '')).to.be.revertedWith('invalid argument - empty string');
});

it('should create one identity and then revert for salt/address being already used', async () => {
const { identityFactory, deployerWallet, aliceWallet, bobWallet } = await loadFixture(deployFactoryFixture);

expect(await identityFactory.isSaltTaken('Tokensalt1')).to.be.false;

const tx = await identityFactory.connect(deployerWallet).createTokenIdentity(aliceWallet.address, bobWallet.address, 'salt1');
const tokenIdentityAddress = await identityFactory.getIdentity(aliceWallet.address);
await expect(tx).to.emit(identityFactory, 'TokenLinked').withArgs(aliceWallet.address, tokenIdentityAddress);
const tx = await identityFactory.connect(deployerWallet).createTokenIdentity(aliceWallet, bobWallet, 'salt1');
const tokenIdentityAddress = await identityFactory.getIdentity(aliceWallet);
await expect(tx).to.emit(identityFactory, 'TokenLinked').withArgs(aliceWallet, tokenIdentityAddress);
await expect(tx).to.emit(identityFactory, 'Deployed').withArgs(tokenIdentityAddress);

expect(await identityFactory.isSaltTaken('Tokensalt1')).to.be.true;
expect(await identityFactory.isSaltTaken('Tokensalt2')).to.be.false;
expect(await identityFactory.getToken(tokenIdentityAddress)).to.deep.equal(aliceWallet.address);
expect(await identityFactory.getToken(tokenIdentityAddress)).to.deep.equal(await aliceWallet.getAddress());

await expect(identityFactory.connect(deployerWallet).createTokenIdentity(aliceWallet.address, aliceWallet.address, 'salt1')).to.be.revertedWith('salt already taken');
await expect(identityFactory.connect(deployerWallet).createTokenIdentity(aliceWallet.address, aliceWallet.address, 'salt2')).to.be.revertedWith('token already linked to an identity');
await expect(identityFactory.connect(deployerWallet).createTokenIdentity(aliceWallet, aliceWallet, 'salt1')).to.be.revertedWith('salt already taken');
await expect(identityFactory.connect(deployerWallet).createTokenIdentity(aliceWallet, aliceWallet, 'salt2')).to.be.revertedWith('token already linked to an identity');
});
});
});
Loading

0 comments on commit 2461768

Please sign in to comment.