Migration to truffle 5 (and web3 1.0 (and BN)) (#1601)
* Now compiling using truffle 5.
* Migrated some test files, missing BN scientific notation usage.
* Now using BN time values.
* Migrate ERC20 tests.
* Migrate all ERC20 tests.
* Migrate utils, payment and ownership tests.
* All tests save ERC721 migrated.
* Migrated ERC721 tests.
* Fix lint errors.
* Delete old test helpers.
* Fix remaining crowdsale tests.
* Fix signature bouncer tests.
* Update how constants is used.
* Compile script pre-removes the build dir.
* Fix SafeMath tests.
* Revert "Compile script pre-removes the build dir."
This reverts commit 247e745113.
* Fix linter errors.
* Upgrade openzeppelin-test-helpers dependency.
* Update openzeppelin-test-helpers dependency.
* Define math constants globally.
* Remove unnecessary ether unit.
* Roll back reduced ether amounts in tests.
* Remove unnecessary toNumber conversions.
* Delete compile script.
* Fixed failing test.
This commit is contained in:
@ -1,38 +1,22 @@
|
||||
const { sha3, soliditySha3 } = require('web3-utils');
|
||||
|
||||
const REAL_SIGNATURE_SIZE = 2 * 65; // 65 bytes in hexadecimal string legnth
|
||||
const PADDED_SIGNATURE_SIZE = 2 * 96; // 96 bytes in hexadecimal string length
|
||||
|
||||
const DUMMY_SIGNATURE = `0x${web3.padLeft('', REAL_SIGNATURE_SIZE)}`;
|
||||
const DUMMY_SIGNATURE = `0x${web3.utils.padLeft('', REAL_SIGNATURE_SIZE)}`;
|
||||
|
||||
// messageHex = '0xdeadbeef'
|
||||
function toEthSignedMessageHash (messageHex) {
|
||||
const messageBuffer = Buffer.from(messageHex.substring(2), 'hex');
|
||||
const prefix = Buffer.from(`\u0019Ethereum Signed Message:\n${messageBuffer.length}`);
|
||||
return sha3(Buffer.concat([prefix, messageBuffer]));
|
||||
return web3.utils.sha3(Buffer.concat([prefix, messageBuffer]));
|
||||
}
|
||||
|
||||
// signs message in node (ganache auto-applies "Ethereum Signed Message" prefix)
|
||||
// messageHex = '0xdeadbeef'
|
||||
const signMessage = (signer, messageHex = '0x') => {
|
||||
return web3.eth.sign(signer, messageHex); // actually personal_sign
|
||||
};
|
||||
|
||||
// @TODO - remove this when we migrate to web3-1.0.0
|
||||
const transformToFullName = function (json) {
|
||||
if (json.name.indexOf('(') !== -1) {
|
||||
return json.name;
|
||||
}
|
||||
|
||||
const typeName = json.inputs.map(function (i) { return i.type; }).join();
|
||||
return json.name + '(' + typeName + ')';
|
||||
return web3.eth.sign(messageHex, signer);
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a signer between a contract and a signer for a voucher of method, args, and redeemer
|
||||
* Note that `method` is the web3 method, not the truffle-contract method
|
||||
* Well truffle is terrible, but luckily (?) so is web3 < 1.0, so we get to make our own method id
|
||||
* fetcher because the method on the contract isn't actually the SolidityFunction object ಠ_ಠ
|
||||
* @param contract TruffleContract
|
||||
* @param signer address
|
||||
* @param redeemer address
|
||||
@ -49,21 +33,17 @@ const getSignFor = (contract, signer) => (redeemer, methodName, methodArgs = [])
|
||||
if (methodName) {
|
||||
if (methodArgs.length > 0) {
|
||||
parts.push(
|
||||
contract.contract[methodName].getData(...methodArgs.concat([DUMMY_SIGNATURE])).slice(
|
||||
0,
|
||||
-1 * PADDED_SIGNATURE_SIZE
|
||||
)
|
||||
contract.contract.methods[methodName](...methodArgs.concat([DUMMY_SIGNATURE])).encodeABI()
|
||||
.slice(0, -1 * PADDED_SIGNATURE_SIZE)
|
||||
);
|
||||
} else {
|
||||
const abi = contract.abi.find(abi => abi.name === methodName);
|
||||
const name = transformToFullName(abi);
|
||||
const signature = sha3(name).slice(0, 10);
|
||||
parts.push(signature);
|
||||
parts.push(abi.signature);
|
||||
}
|
||||
}
|
||||
|
||||
// return the signature of the "Ethereum Signed Message" hash of the hash of `parts`
|
||||
const messageHex = soliditySha3(...parts);
|
||||
const messageHex = web3.utils.soliditySha3(...parts);
|
||||
return signMessage(signer, messageHex);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user