Skip to content

Commit

Permalink
Merge pull request #600 from gasp/fix-types-try-catch
Browse files Browse the repository at this point in the history
test: use chai to.be.rejectedWith & fix ts errors
  • Loading branch information
immortal-tofu authored Nov 15, 2024
2 parents bbdf6c0 + 6032b6a commit 43ad44f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 51 deletions.
36 changes: 14 additions & 22 deletions test/encryptedERC20/EncryptedERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,42 +102,34 @@ describe('EncryptedERC20', function () {
expect(balanceBob).to.equal(1337);

// on the other hand, Bob should be unable to read Alice's balance
try {
await this.instances.bob.reencrypt(
await expect(
this.instances.bob.reencrypt(
balanceHandleAlice,
privateKeyBob,
publicKeyBob,
signatureBob.replace('0x', ''),
this.contractAddress,
this.signers.bob.address,
);
expect.fail('Expected an error to be thrown - Bob should not be able to reencrypt Alice balance');
} catch (error) {
expect(error.message).to.equal('User is not authorized to reencrypt this handle!');
}
),
).to.be.rejectedWith('User is not authorized to reencrypt this handle!');

// and should be impossible to call reencrypt if contractAddress === userAddress
try {
const eip712b = this.instances.alice.createEIP712(publicKeyAlice, this.signers.alice.address);
const signatureAliceb = await this.signers.alice.signTypedData(
eip712b.domain,
{ Reencrypt: eip712b.types.Reencrypt },
eip712b.message,
);
await this.instances.alice.reencrypt(
const eip712b = this.instances.alice.createEIP712(publicKeyAlice, this.signers.alice.address);
const signatureAliceb = await this.signers.alice.signTypedData(
eip712b.domain,
{ Reencrypt: eip712b.types.Reencrypt },
eip712b.message,
);
await expect(
this.instances.alice.reencrypt(
balanceHandleAlice,
privateKeyAlice,
publicKeyAlice,
signatureAliceb.replace('0x', ''),
this.signers.alice.address,
this.signers.alice.address,
);
expect.fail('Expected an error to be thrown - userAddress and contractAddress cannot be equal');
} catch (error) {
expect(error.message).to.equal(
'userAddress should not be equal to contractAddress when requesting reencryption!',
);
}
),
).to.be.rejectedWith('userAddress should not be equal to contractAddress when requesting reencryption!');
});

it('should not transfer tokens between two users', async function () {
Expand Down
50 changes: 21 additions & 29 deletions test/reencryption/reencryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,49 +38,41 @@ describe('Reencryption', function () {
expect(decryptedValue).to.equal(1);

// on the other hand, Bob should be unable to read Alice's balance
try {
const { publicKey: publicKeyBob, privateKey: privateKeyBob } = this.instances.bob.generateKeypair();
const eip712Bob = this.instances.bob.createEIP712(publicKeyBob, this.contractAddress);
const signatureBob = await this.signers.bob.signTypedData(
eip712Bob.domain,
{ Reencrypt: eip712Bob.types.Reencrypt },
eip712Bob.message,
);
await this.instances.bob.reencrypt(
const { publicKey: publicKeyBob, privateKey: privateKeyBob } = this.instances.bob.generateKeypair();
const eip712Bob = this.instances.bob.createEIP712(publicKeyBob, this.contractAddress);
const signatureBob = await this.signers.bob.signTypedData(
eip712Bob.domain,
{ Reencrypt: eip712Bob.types.Reencrypt },
eip712Bob.message,
);
await expect(
this.instances.bob.reencrypt(
handle,
privateKeyBob,
publicKeyBob,
signatureBob.replace('0x', ''),
this.contractAddress,
this.signers.bob.address,
);
expect.fail('Expected an error to be thrown - Bob should not be able to reencrypt Alice balance');
} catch (error) {
expect(error.message).to.equal('User is not authorized to reencrypt this handle!');
}
),
).to.be.rejectedWith('User is not authorized to reencrypt this handle!');

// and should be impossible to call reencrypt if contractAddress === userAddress
try {
const eip712b = this.instances.alice.createEIP712(publicKey, this.signers.alice.address);
const signatureAliceb = await this.signers.alice.signTypedData(
eip712b.domain,
{ Reencrypt: eip712b.types.Reencrypt },
eip712b.message,
);
await this.instances.alice.reencrypt(
const eip712b = this.instances.alice.createEIP712(publicKey, this.signers.alice.address);
const signatureAliceb = await this.signers.alice.signTypedData(
eip712b.domain,
{ Reencrypt: eip712b.types.Reencrypt },
eip712b.message,
);
await expect(
this.instances.alice.reencrypt(
handle,
privateKey,
publicKey,
signatureAliceb.replace('0x', ''),
this.signers.alice.address,
this.signers.alice.address,
);
expect.fail('Expected an error to be thrown - userAddress and contractAddress cannot be equal');
} catch (error) {
expect(error.message).to.equal(
'userAddress should not be equal to contractAddress when requesting reencryption!',
);
}
),
).to.be.rejectedWith('userAddress should not be equal to contractAddress when requesting reencryption!');
});

it('test reencrypt euint4', async function () {
Expand Down

0 comments on commit 43ad44f

Please sign in to comment.