diff --git a/tests/make-tests/make-contract-events.js b/tests/make-tests/make-contract-events.js index ed8b4cb05..8634eff7f 100644 --- a/tests/make-tests/make-contract-events.js +++ b/tests/make-tests/make-contract-events.js @@ -89,10 +89,6 @@ function id(text) { return crypto.createHash('sha256').update(text).digest().toString('hex').substring(0, 10).toUpperCase(); } -function getEventName(types) { - return 'testEvent'; -} - function createContractSource(test, comments) { var events = ''; var source = ''; @@ -146,15 +142,12 @@ function createContractSource(test, comments) { values.push('s' + index); }); - events += ( - indent(1) + - 'event ' + - getEventName(types) + - '(' + named.join(', ') + ')' + - (test.anonymous ? ' anonymous': '') + - ';\n' - ); - source += indent(2) + getEventName(types) + '(' + values.join(', ') + ');\n'; + events += indent(1) + 'event testEvent(' + named.join(', ') + ')' + (test.anonymous ? ' anonymous': '') + ';\n'; + source += indent(2) + 'testEvent(' + values.join(', ') + ');\n'; + ['keccak256', 'ripemd160', 'sha256'].forEach(function(funcName) { + events += indent(1) + 'event test_' + funcName + '(bytes32 value);\n'; + source += indent(2) + 'test_' + funcName + '(' + funcName + '(' + values.join(', ') + '));\n'; + }); var sourceInit = ''; @@ -317,6 +310,16 @@ function makeTests() { ] }); + tests.push({ + name: 'bytes5-array', + params: [ + { type: 'bytes5[2]', value: [ + '0x1122334455', + '0x6677889900' + ] } + ] + }); + function generate(seed, onlyStatic) { switch (utils.randomNumber(seed + '-type', 0, (onlyStatic ? 3: 6))) { case 0: @@ -476,7 +479,7 @@ function makeTests() { }); }).then(function(tx) { - if (tx.logs.length !== 1) { + if (tx.logs.length !== 4) { console.log('What?', tx); process.exit(1); } @@ -544,17 +547,27 @@ function makeTests() { }); resolve({ - bytecode: '0x' + contract.bytecode, - data: tx.logs[0].data, - hashed: hashed, - indexed: indexed, - interface: contract.interface, - name: test.name, - source: source, - topics: tx.logs[0].topics, - types: types, - normalizedValues: normalizedValues, - values: values + event: { + bytecode: '0x' + contract.bytecode, + data: tx.logs[0].data, + hashed: hashed, + indexed: indexed, + interface: contract.interface, + name: test.name, + source: source, + topics: tx.logs[0].topics, + types: types, + normalizedValues: normalizedValues, + values: values + }, + solidityHash: { + name: test.name, + types: types, + keccak256: tx.logs[1].data, + ripemd160: tx.logs[2].data, + sha256: tx.logs[3].data, + values: values, + } }); }).catch(function(error) { console.log('TTT', test); @@ -565,7 +578,14 @@ function makeTests() { promiseRationing.all(promiseFuncs, 40).then(function(results) { console.log('complete', results); - utils.saveTests('contract-events', results); + var events = []; + var solidityHashes = []; + results.forEach(function(result) { + events.push(result.event); + solidityHashes.push(result.solidityHash); + }); + utils.saveTests('contract-events', events); + utils.saveTests('solidity-hashes', solidityHashes); }, function(error) { console.log(error); }); diff --git a/tests/make-tests/make-hashes.js b/tests/make-tests/make-hashes.js new file mode 100644 index 000000000..546c0e52e --- /dev/null +++ b/tests/make-tests/make-hashes.js @@ -0,0 +1,29 @@ +'use strict'; + +var crypto = require('crypto'); +var createKeccakHash = require('keccak'); + +var utils = require('../utils'); + +var output = []; + +function add(data) { + output.push({ + data: ('0x' + data.toString('hex')), + keccak256: '0x' + createKeccakHash('keccak256').update(data).digest().toString('hex'), + sha256: '0x' + crypto.createHash('sha256').update(data).digest().toString('hex'), + sha512: '0x' + crypto.createHash('sha512').update(data).digest().toString('hex'), + }); +} + +add(new Buffer([ ])); +add(new Buffer([ 0 ])); +add(new Buffer([ 1 ])); +add(new Buffer([ 0, 1 ])); + +for (var i = 0; i < 512; i++) { + var data = new Buffer(utils.randomBytes('data-' + i, 1, 128)); + add(data); +} + +utils.saveTests('hashes', output); diff --git a/tests/test-utils.js b/tests/test-utils.js index ba6cb663f..a02423933 100644 --- a/tests/test-utils.js +++ b/tests/test-utils.js @@ -106,7 +106,7 @@ describe('Test Namehash', function() { }); }); -describe('Test ID hash function', function () { +describe('Test ID Hash Function', function () { var id = require('../utils/id'); var tests = [ @@ -126,4 +126,36 @@ describe('Test ID hash function', function () { }); }); -// @TODO: Cryptographics hashes? +describe('Test Solidity Hash Functions', function() { + var solidity = require('../utils/solidity'); + + var tests = utils.loadTests('solidity-hashes'); + ['keccak256', 'sha256'].forEach(function(funcName) { + it(('computes ' + funcName + ' correctly'), function() { + tests.forEach(function(test, index) { + var result = solidity[funcName](test.types, test.values); + assert.equal(result, test[funcName], + ('computes solidity-' + funcName + '(' + JSON.stringify(test.values) + ') - ' + test.types)); + }); + }); + }); +}); + +describe('Test Hash Functions', function() { + var keccak256 = require('../utils/keccak256'); + var sha256 = require('../utils/sha2').sha256; + + var tests = utils.loadTests('hashes'); + + it('computes keccak256 correctly', function() { + tests.forEach(function(test) { + assert.equal(keccak256(test.data), test.keccak256, ('Keccak256 - ' + test.data)); + }); + }); + + it('computes sha2566 correctly', function() { + tests.forEach(function(test) { + assert.equal(sha256(test.data), test.sha256, ('SHA256 - ' + test.data)); + }); + }); +}); diff --git a/tests/tests/contract-events.json.gz b/tests/tests/contract-events.json.gz index 9be0e3607..d691deb84 100644 Binary files a/tests/tests/contract-events.json.gz and b/tests/tests/contract-events.json.gz differ diff --git a/tests/tests/hashes.json.gz b/tests/tests/hashes.json.gz new file mode 100644 index 000000000..e70cd1e4e Binary files /dev/null and b/tests/tests/hashes.json.gz differ diff --git a/tests/tests/solidity-hashes.json.gz b/tests/tests/solidity-hashes.json.gz new file mode 100644 index 000000000..ddc1031aa Binary files /dev/null and b/tests/tests/solidity-hashes.json.gz differ diff --git a/utils/index.js b/utils/index.js index 5a9fd607f..cec6999c7 100644 --- a/utils/index.js +++ b/utils/index.js @@ -11,6 +11,7 @@ var id = require('./id'); var keccak256 = require('./keccak256'); var namehash = require('./namehash'); var sha256 = require('./sha2').sha256; +var solidity = require('./solidity'); var randomBytes = require('./random-bytes'); var properties = require('./properties'); var RLP = require('./rlp'); @@ -56,6 +57,10 @@ module.exports = { sha256: sha256, randomBytes: randomBytes, + + solidityPack: solidity.pack, + solidityKeccak256: solidity.keccak256, + soliditySha256: solidity.sha256, } require('./standalone')({ diff --git a/utils/package.json b/utils/package.json index d9c0e9d08..9ffafb75d 100644 --- a/utils/package.json +++ b/utils/package.json @@ -1,6 +1,6 @@ { "name": "ethers-utils", - "version": "2.1.7", + "version": "2.1.8", "description": "Utilities for the Ethers Ethereum library.", "bugs": { "url": "http://github.com/ethers-io/ethers.js/issues", diff --git a/utils/solidity.js b/utils/solidity.js new file mode 100644 index 000000000..1baa2b36b --- /dev/null +++ b/utils/solidity.js @@ -0,0 +1,93 @@ +'use strict'; + +var bigNumberify = require('./bignumber').bigNumberify; +var convert = require('./convert'); +var getAddress = require('./address').getAddress; +var utf8 = require('./utf8'); + +var hashKeccak256 = require('./keccak256'); +var hashSha256 = require('./sha2').sha256; + +var regexBytes = new RegExp("^bytes([0-9]+)$"); +var regexNumber = new RegExp("^(u?int)([0-9]*)$"); +var regexArray = new RegExp("^(.*)\\[([0-9]*)\\]$"); + +var Zeros = '0000000000000000000000000000000000000000000000000000000000000000'; + +function _pack(type, value, isArray) { + switch(type) { + case 'address': + if (isArray) { return convert.padZeros(value, 32); } + return convert.arrayify(value); + case 'string': + return utf8.toUtf8Bytes(value); + case 'bytes': + return convert.arrayify(value); + } + + var match = type.match(regexNumber); + if (match) { + var signed = (match[1] === 'int') + var size = parseInt(match[2] || "256") + if ((size % 8 != 0) || size === 0 || size > 256) { + throw new Error('invalid number type - ' + type); + } + + if (isArray) { size = 256; } + + value = bigNumberify(value).toTwos(size); + + return convert.padZeros(value, size / 8); + } + + match = type.match(regexBytes); + if (match) { + var size = match[1]; + if (size != parseInt(size) || size === 0 || size > 32) { + throw new Error('invalid number type - ' + type); + } + size = parseInt(size); + if (convert.arrayify(value).byteLength !== size) { throw new Error('invalid value for ' + type); } + if (isArray) { return (value + Zeros).substring(0, 66); } + return value; + } + + match = type.match(regexArray); + if (match) { + var baseType = match[1]; + var count = parseInt(match[2] || value.length); + if (count != value.length) { throw new Error('invalid value for ' + type); } + var result = []; + value.forEach(function(value) { + value = _pack(baseType, value, true); + result.push(value); + }); + return convert.concat(result); + } + + throw new Error('unknown type - ' + type); +} + +function pack(types, values) { + if (types.length != values.length) { throw new Error('type/value count mismatch'); } + var tight = []; + types.forEach(function(type, index) { + tight.push(_pack(type, values[index])); + }); + return convert.hexlify(convert.concat(tight)); +} + +function keccak256(types, values) { + return hashKeccak256(pack(types, values)); +} + +function sha256(types, values) { + return hashSha256(pack(types, values)); +} + +module.exports = { + pack: pack, + + keccak256: keccak256, + sha256: sha256, +}