Merge branch 'master' into next-v5.0
This commit is contained in:
@ -6,6 +6,7 @@ const EIP712Domain = [
|
||||
{ name: 'version', type: 'string' },
|
||||
{ name: 'chainId', type: 'uint256' },
|
||||
{ name: 'verifyingContract', type: 'address' },
|
||||
{ name: 'salt', type: 'bytes32' },
|
||||
];
|
||||
|
||||
const Permit = [
|
||||
@ -24,25 +25,43 @@ function hexStringToBuffer(hexstr) {
|
||||
return Buffer.from(hexstr.replace(/^0x/, ''), 'hex');
|
||||
}
|
||||
|
||||
async function domainSeparator({ name, version, chainId, verifyingContract }) {
|
||||
async function getDomain(contract) {
|
||||
const { fields, name, version, chainId, verifyingContract, salt, extensions } = await contract.eip712Domain();
|
||||
|
||||
if (extensions.length > 0) {
|
||||
throw Error('Extensions not implemented');
|
||||
}
|
||||
|
||||
const domain = { name, version, chainId, verifyingContract, salt };
|
||||
for (const [i, { name }] of EIP712Domain.entries()) {
|
||||
if (!(fields & (1 << i))) {
|
||||
delete domain[name];
|
||||
}
|
||||
}
|
||||
|
||||
return domain;
|
||||
}
|
||||
|
||||
function domainType(domain) {
|
||||
return EIP712Domain.filter(({ name }) => domain[name] !== undefined);
|
||||
}
|
||||
|
||||
function domainSeparator(domain) {
|
||||
return bufferToHexString(
|
||||
ethSigUtil.TypedDataUtils.hashStruct(
|
||||
'EIP712Domain',
|
||||
{ name, version, chainId, verifyingContract },
|
||||
{ EIP712Domain },
|
||||
),
|
||||
ethSigUtil.TypedDataUtils.hashStruct('EIP712Domain', domain, { EIP712Domain: domainType(domain) }),
|
||||
);
|
||||
}
|
||||
|
||||
async function hashTypedData(domain, structHash) {
|
||||
return domainSeparator(domain).then(separator =>
|
||||
bufferToHexString(keccak256(Buffer.concat(['0x1901', separator, structHash].map(str => hexStringToBuffer(str))))),
|
||||
function hashTypedData(domain, structHash) {
|
||||
return bufferToHexString(
|
||||
keccak256(Buffer.concat(['0x1901', domainSeparator(domain), structHash].map(str => hexStringToBuffer(str)))),
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
EIP712Domain,
|
||||
Permit,
|
||||
getDomain,
|
||||
domainType,
|
||||
domainSeparator,
|
||||
hashTypedData,
|
||||
};
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
const { time } = require('@openzeppelin/test-helpers');
|
||||
const { forward } = require('../helpers/time');
|
||||
|
||||
function zip(...args) {
|
||||
return Array(Math.max(...args.map(array => array.length)))
|
||||
@ -15,8 +15,9 @@ function concatOpts(args, opts = null) {
|
||||
}
|
||||
|
||||
class GovernorHelper {
|
||||
constructor(governor) {
|
||||
constructor(governor, mode = 'blocknumber') {
|
||||
this.governor = governor;
|
||||
this.mode = mode;
|
||||
}
|
||||
|
||||
delegate(delegation = {}, opts = null) {
|
||||
@ -62,14 +63,25 @@ class GovernorHelper {
|
||||
);
|
||||
}
|
||||
|
||||
cancel(opts = null) {
|
||||
cancel(visibility = 'external', opts = null) {
|
||||
const proposal = this.currentProposal;
|
||||
|
||||
return proposal.useCompatibilityInterface
|
||||
? this.governor.methods['cancel(uint256)'](...concatOpts([proposal.id], opts))
|
||||
: this.governor.methods['$_cancel(address[],uint256[],bytes[],bytes32)'](
|
||||
switch (visibility) {
|
||||
case 'external':
|
||||
if (proposal.useCompatibilityInterface) {
|
||||
return this.governor.methods['cancel(uint256)'](...concatOpts([proposal.id], opts));
|
||||
} else {
|
||||
return this.governor.methods['cancel(address[],uint256[],bytes[],bytes32)'](
|
||||
...concatOpts(proposal.shortProposal, opts),
|
||||
);
|
||||
}
|
||||
case 'internal':
|
||||
return this.governor.methods['$_cancel(address[],uint256[],bytes[],bytes32)'](
|
||||
...concatOpts(proposal.shortProposal, opts),
|
||||
);
|
||||
default:
|
||||
throw new Error(`unsuported visibility "${visibility}"`);
|
||||
}
|
||||
}
|
||||
|
||||
vote(vote = {}, opts = null) {
|
||||
@ -79,7 +91,7 @@ class GovernorHelper {
|
||||
? // if signature, and either params or reason →
|
||||
vote.params || vote.reason
|
||||
? vote
|
||||
.signature({
|
||||
.signature(this.governor, {
|
||||
proposalId: proposal.id,
|
||||
support: vote.support,
|
||||
reason: vote.reason || '',
|
||||
@ -91,7 +103,7 @@ class GovernorHelper {
|
||||
),
|
||||
)
|
||||
: vote
|
||||
.signature({
|
||||
.signature(this.governor, {
|
||||
proposalId: proposal.id,
|
||||
support: vote.support,
|
||||
})
|
||||
@ -109,23 +121,22 @@ class GovernorHelper {
|
||||
: this.governor.castVote(...concatOpts([proposal.id, vote.support], opts));
|
||||
}
|
||||
|
||||
waitForSnapshot(offset = 0) {
|
||||
async waitForSnapshot(offset = 0) {
|
||||
const proposal = this.currentProposal;
|
||||
return this.governor
|
||||
.proposalSnapshot(proposal.id)
|
||||
.then(blockNumber => time.advanceBlockTo(blockNumber.addn(offset)));
|
||||
const timepoint = await this.governor.proposalSnapshot(proposal.id);
|
||||
return forward[this.mode](timepoint.addn(offset));
|
||||
}
|
||||
|
||||
waitForDeadline(offset = 0) {
|
||||
async waitForDeadline(offset = 0) {
|
||||
const proposal = this.currentProposal;
|
||||
return this.governor
|
||||
.proposalDeadline(proposal.id)
|
||||
.then(blockNumber => time.advanceBlockTo(blockNumber.addn(offset)));
|
||||
const timepoint = await this.governor.proposalDeadline(proposal.id);
|
||||
return forward[this.mode](timepoint.addn(offset));
|
||||
}
|
||||
|
||||
waitForEta(offset = 0) {
|
||||
async waitForEta(offset = 0) {
|
||||
const proposal = this.currentProposal;
|
||||
return this.governor.proposalEta(proposal.id).then(timestamp => time.increaseTo(timestamp.addn(offset)));
|
||||
const timestamp = await this.governor.proposalEta(proposal.id);
|
||||
return forward.timestamp(timestamp.addn(offset));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -4,6 +4,21 @@ function toEthSignedMessageHash(messageHex) {
|
||||
return web3.utils.sha3(Buffer.concat([prefix, messageBuffer]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a signed data with intended validator according to the version 0 of EIP-191
|
||||
* @param validatorAddress The address of the validator
|
||||
* @param dataHex The data to be concatenated with the prefix and signed
|
||||
*/
|
||||
function toDataWithIntendedValidatorHash(validatorAddress, dataHex) {
|
||||
const validatorBuffer = Buffer.from(web3.utils.hexToBytes(validatorAddress));
|
||||
const dataBuffer = Buffer.from(web3.utils.hexToBytes(dataHex));
|
||||
const preambleBuffer = Buffer.from('\x19');
|
||||
const versionBuffer = Buffer.from('\x00');
|
||||
const ethMessage = Buffer.concat([preambleBuffer, versionBuffer, validatorBuffer, dataBuffer]);
|
||||
|
||||
return web3.utils.sha3(ethMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@ -43,5 +58,6 @@ const getSignFor =
|
||||
|
||||
module.exports = {
|
||||
toEthSignedMessageHash,
|
||||
toDataWithIntendedValidatorHash,
|
||||
getSignFor,
|
||||
};
|
||||
|
||||
17
test/helpers/time.js
Normal file
17
test/helpers/time.js
Normal file
@ -0,0 +1,17 @@
|
||||
const ozHelpers = require('@openzeppelin/test-helpers');
|
||||
const helpers = require('@nomicfoundation/hardhat-network-helpers');
|
||||
|
||||
module.exports = {
|
||||
clock: {
|
||||
blocknumber: () => helpers.time.latestBlock(),
|
||||
timestamp: () => helpers.time.latest(),
|
||||
},
|
||||
clockFromReceipt: {
|
||||
blocknumber: receipt => Promise.resolve(receipt.blockNumber),
|
||||
timestamp: receipt => web3.eth.getBlock(receipt.blockNumber).then(block => block.timestamp),
|
||||
},
|
||||
forward: {
|
||||
blocknumber: ozHelpers.time.advanceBlockTo,
|
||||
timestamp: helpers.time.increaseTo,
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user