ADD test for hashing

pull/1/head
pubkey 7 years ago
parent 753f3e2a8e
commit 70a3141474
  1. 25
      contracts/TestContract.sol
  2. 39
      test/integration.test.js

@ -8,17 +8,28 @@ pragma solidity 0.4.19;
contract TestContract {
uint public onePublicValue = 1337;
/**
* hashes the given values
* should be equal to own hash()-function in js
*/
function hashString(
bytes32 someString
) public constant returns(bytes32) {
return keccak256(
someString
);
}
function hashNumber(
uint someNumber
) public constant returns(bytes32) {
return keccak256(
someNumber
);
}
function hashString(
bytes32 someString
) public constant returns(bytes32) {
return keccak256(
someString
);
}
function hashSomething(
uint someNumber,

@ -50,13 +50,17 @@ describe('integration.test.js', () => {
state.contractAddress = receipt.contractAddress;
assert.ok(state.contractAddress);
});
it('create contract-instance', async()=>{
it('create contract-instance', async () => {
state.contract = new state.web3.eth.Contract(
compiled.interface,
state.contractAddress
);
assert.ok(state.contract);
});
it('should get the public value out of the contract', async () => {
const value = await state.contract.methods.onePublicValue().call();
assert.equal(value, 1337);
});
});
describe('privateKey', () => {
it('should be possible to use the keys with ganache', async () => {
@ -101,10 +105,37 @@ describe('integration.test.js', () => {
});
});
describe('hash', () => {
it('should create the same hash from string as solidity', async()=>{
const str = EthereumEncryption.hash(AsyncTestUtil.randomString(12));
const jshash = EthereumEncryption.hash(str);
it('number: should create the same hash as solidity', async () => {
const nr = 1337;
console.log('nr:');
console.dir(nr);
const web3Hash = state.web3.utils.soliditySha3(nr);
console.log('web3Hash:');
console.dir(web3Hash);
const solHash = await state.contract
.methods.hashNumber(nr)
.call();
console.log('solHash:');
console.dir(solHash);
assert.equal(web3Hash, solHash);
});
it('string: should create the same hash as solidity', async () => {
const str = 'foobar';
const jsHash = EthereumEncryption.hash(str);
const web3Hash = state.web3.utils.soliditySha3(str);
console.log('web3Hash:');
console.dir(web3Hash);
console.log('str:');
console.dir(str);
const solHash = await state.contract
.methods.hashString(str)
.call();
console.log('solHash:');
console.dir(solHash);
assert.equal(jsHash, solHash);
});
});
});

Loading…
Cancel
Save