test: adds tests for Common contract (fixed history) (#57)
* test: adds output function to print hashes used in solidity/test/domainHashTestCases.json * test: adds domainHashTestCases.json file * test: adds signature and domainHash calculation tests to common tests * fix: runs prettier on contractsbuddies-main-deployment
parent
188178263b
commit
1f22e023eb
@ -0,0 +1,32 @@ |
||||
// SPDX-License-Identifier: MIT OR Apache-2.0 |
||||
pragma solidity >=0.6.11; |
||||
|
||||
import "../Common.sol"; |
||||
|
||||
contract TestCommon is Common { |
||||
constructor( |
||||
uint32 _originSLIP44, |
||||
address _updater, |
||||
bytes32 _current |
||||
) Common(_originSLIP44, _updater, _current) {} |
||||
|
||||
function testCheckSig( |
||||
bytes32 _oldRoot, |
||||
bytes32 _newRoot, |
||||
bytes memory _signature |
||||
) external view returns (bool) { |
||||
return checkSig(_oldRoot, _newRoot, _signature); |
||||
} |
||||
|
||||
function fail() internal override { |
||||
_setFailed(); |
||||
} |
||||
|
||||
function testDomainHash(uint32 _originSLIP44) |
||||
external |
||||
pure |
||||
returns (bytes32) |
||||
{ |
||||
return keccak256(abi.encodePacked(_originSLIP44, "OPTICS")); |
||||
} |
||||
} |
@ -0,0 +1,77 @@ |
||||
const { waffle, ethers } = require('hardhat'); |
||||
const { provider, deployMockContract } = waffle; |
||||
const { expect } = require('chai'); |
||||
|
||||
const { testCases } = require('./domainHashTestCases.json'); |
||||
|
||||
const ACTIVE = 0; |
||||
const FAILED = 1; |
||||
const originSLIP44 = 1234; |
||||
|
||||
describe('Common', async () => { |
||||
let common, signer, fakeSigner, updater, fakeUpdater, initialRoot; |
||||
|
||||
before(async () => { |
||||
[signer, fakeSigner] = provider.getWallets(); |
||||
updater = await optics.Updater.fromSigner(signer, originSLIP44); |
||||
fakeUpdater = await optics.Updater.fromSigner(fakeSigner, originSLIP44); |
||||
initialRoot = ethers.utils.formatBytes32String('initial root'); |
||||
}); |
||||
|
||||
beforeEach(async () => { |
||||
const CommonFactory = await ethers.getContractFactory('TestCommon'); |
||||
common = await CommonFactory.deploy( |
||||
originSLIP44, |
||||
updater.signer.address, |
||||
initialRoot, |
||||
); |
||||
await common.deployed(); |
||||
}); |
||||
|
||||
it('Accepts updater signature', async () => { |
||||
const oldRoot = ethers.utils.formatBytes32String('old root'); |
||||
const newRoot = ethers.utils.formatBytes32String('new root'); |
||||
|
||||
const { signature } = await updater.signUpdate(oldRoot, newRoot); |
||||
expect(await common.testCheckSig(oldRoot, newRoot, signature)).to.be.true; |
||||
}); |
||||
|
||||
it('Rejects non-updater signature', async () => { |
||||
const oldRoot = ethers.utils.formatBytes32String('old root'); |
||||
const newRoot = ethers.utils.formatBytes32String('new root'); |
||||
|
||||
const { signature: fakeSignature } = await fakeUpdater.signUpdate( |
||||
oldRoot, |
||||
newRoot, |
||||
); |
||||
expect(await common.testCheckSig(oldRoot, newRoot, fakeSignature)).to.be |
||||
.false; |
||||
}); |
||||
|
||||
it('Fails on valid double update proof', async () => { |
||||
const oldRoot = ethers.utils.formatBytes32String('old root'); |
||||
const newRoot = ethers.utils.formatBytes32String('new root 1'); |
||||
const newRoot2 = ethers.utils.formatBytes32String('new root 2'); |
||||
|
||||
const { signature } = await updater.signUpdate(oldRoot, newRoot); |
||||
const { signature: signature2 } = await updater.signUpdate( |
||||
oldRoot, |
||||
newRoot2, |
||||
); |
||||
|
||||
await expect( |
||||
common.doubleUpdate(oldRoot, [newRoot, newRoot2], signature, signature2), |
||||
).to.emit(common, 'DoubleUpdate'); |
||||
|
||||
expect(await common.state()).to.equal(FAILED); |
||||
}); |
||||
|
||||
it('Calculates domain hashes from originSLIP44', async () => { |
||||
// Compare Rust output in json file to solidity output
|
||||
for (let testCase of testCases) { |
||||
const { originSlip44, expectedDomainHash } = testCase; |
||||
const solidityDomainHash = await common.testDomainHash(originSlip44); |
||||
expect(solidityDomainHash).to.equal(expectedDomainHash); |
||||
} |
||||
}); |
||||
}); |
@ -0,0 +1,16 @@ |
||||
{ |
||||
"testCases": [ |
||||
{ |
||||
"originSlip44": 1, |
||||
"expectedDomainHash": "0xfa923b9e3942a539792474b49136600ed6690d0e51c6993e9238819047dfea63" |
||||
}, |
||||
{ |
||||
"originSlip44": 2, |
||||
"expectedDomainHash": "0x8caa11ba0b38c460170899d835c99eb38b9319603fe5d391c469da2657e01039" |
||||
}, |
||||
{ |
||||
"originSlip44": 3, |
||||
"expectedDomainHash": "0xa2718888038cf3886089211e47a6f2fb003fc887a4f748fd6278156f23f9ecbf" |
||||
} |
||||
] |
||||
} |
Loading…
Reference in new issue