Skip to content

Commit

Permalink
contracts: added test to verify calldata is being encoded
Browse files Browse the repository at this point in the history
  • Loading branch information
CedarMist committed Sep 17, 2023
1 parent d1f066c commit 35e7a90
Show file tree
Hide file tree
Showing 4 changed files with 3,412 additions and 3,369 deletions.
14 changes: 14 additions & 0 deletions contracts/contracts/tests/EIP155Tests.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ contract EIP155Tests {
return EIP155Signer.sign(publicAddr, secretKey, transaction);
}

function signWithSecret(
EIP155Signer.EthTx memory transaction,
address fromPublicAddr,
bytes32 fromSecret
)
external
view
returns (bytes memory)
{
transaction.data = abi.encodeWithSelector(this.example.selector);
transaction.chainId = block.chainid;
return EIP155Signer.sign(fromPublicAddr, fromSecret, transaction);
}

event ExampleEvent(bytes32 x);

function example() external {
Expand Down
2 changes: 2 additions & 0 deletions contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"contracts"
],
"devDependencies": {
"@ethersproject/abi": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"@nomicfoundation/hardhat-chai-matchers": "^1.0.5",
"@nomiclabs/hardhat-ethers": "^2.1.1",
"@oasisprotocol/sapphire-hardhat": "workspace:^",
Expand Down
47 changes: 30 additions & 17 deletions contracts/test/eip155.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,45 @@ import { ethers } from 'hardhat';
import { EIP155Tests__factory } from '../typechain-types/factories/contracts/tests';
import { EIP155Tests } from '../typechain-types/contracts/tests/EIP155Tests';

// Shannon entropy
function entropy(str:string) {
return [...new Set(str)]
.map(chr => {
return str.match(new RegExp(chr, 'g'))!.length;
})
.reduce((sum, frequency) => {
let p = frequency / str.length;
return sum + p * Math.log2(1 / p);
}, 0);
};

/*
* Test cases:
* Sign with different 'from' address
* - This requires a separate provider?
* Sign with same 'from' address
* - This should automagically encrypt
*
* TODO: verify that transaction calldata was submitted encrypted on-chian
*/

describe('EIP-155', function () {
let testContract : EIP155Tests;
let testContract: EIP155Tests;
before(async () => {
const factory = (await ethers.getContractFactory(
'EIP155Tests',
)) as EIP155Tests__factory;
testContract = await factory.deploy({
value: ethers.utils.parseEther('1'),
});
await testContract.deployed();
});

it('Encrypts pre-signed transactions', async function () {
const txobj = {
nonce: 0,
gasPrice: await testContract.provider.getGasPrice(),
gasLimit: 250000,
to: testContract.address,
value: 0,
data: '0x',
chainId: 0,
};
const signedTx = await testContract.sign(txobj);
const response = await testContract.provider.sendTransaction(signedTx);
const receipt = await response.wait();
expect(receipt.logs[0].data).equal(
'0xfedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210',
);
it('Wrapper encrypts transaction calldata', async function () {
const tx = await testContract.example();
expect(entropy(tx.data)).gte(3.8);
expect(tx.data).not.eq(testContract.interface.encodeFunctionData("example"));
expect(tx.data.length).eq(218);
});

it('Signed transactions can be submitted', async function () {
Expand All @@ -54,6 +66,7 @@ describe('EIP-155', function () {
let receipt = await testContract.provider.waitForTransaction(
plainResp.hash,
);
expect(plainResp.data).eq(testContract.interface.encodeFunctionData("example"));
expect(receipt.logs[0].data).equal(
'0xfedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210',
);
Expand Down
Loading

0 comments on commit 35e7a90

Please sign in to comment.