Skip to content

Commit

Permalink
Merge pull request #555 from clrfund/fix/brightid-timestamp
Browse files Browse the repository at this point in the history
Do not check BrightId timestamp
  • Loading branch information
yuetloo authored Sep 13, 2022
2 parents 88caaae + 767856b commit 470a6f9
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 600 deletions.
39 changes: 1 addition & 38 deletions contracts/contracts/userRegistry/BrightIdUserRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,11 @@ contract BrightIdUserRegistry is Ownable, IUserRegistry {
string private constant ERROR_INVALID_VERIFIER = 'INVALID VERIFIER';
string private constant ERROR_INVALID_CONTEXT = 'INVALID CONTEXT';
string private constant ERROR_INVALID_SPONSOR = 'INVALID SPONSOR';
string private constant ERROR_INVALID_REGISTRATION_PERIOD = 'INVALID REGISTRATION PERIOD';
string private constant ERROR_EXPIRED_VERIFICATION = 'EXPIRED VERIFICATION';
string private constant ERROR_REGISTRATION_CLOSED = 'REGISTRATION CLOSED';

bytes32 public context;
address public verifier;
BrightIdSponsor public brightIdSponsor;

// Only register a verified user during this period
uint256 public registrationStartTime;
uint256 public registrationDeadline;

struct Verification {
uint256 time;
}
Expand All @@ -32,7 +25,6 @@ contract BrightIdUserRegistry is Ownable, IUserRegistry {
event SetBrightIdSettings(bytes32 context, address verifier);

event Registered(address indexed addr, uint256 timestamp);
event RegistrationPeriodChanged(uint256 startTime, uint256 deadline);
event SponsorChanged(address sponsor);

/**
Expand Down Expand Up @@ -83,19 +75,6 @@ contract BrightIdUserRegistry is Ownable, IUserRegistry {
emit SponsorChanged(_sponsor);
}

/**
* @notice Set the registration period for verified users
* @param _startTime Registration start time
* @param _deadline Registration deadline
*/
function setRegistrationPeriod(uint256 _startTime, uint256 _deadline) external onlyOwner {
require(_startTime <= _deadline, ERROR_INVALID_REGISTRATION_PERIOD);

registrationStartTime = _startTime;
registrationDeadline = _deadline;
emit RegistrationPeriodChanged(_startTime, _deadline);
}

/**
* @notice Check a user is verified or not
* @param _user BrightID context id used for verifying users
Expand All @@ -107,21 +86,7 @@ contract BrightIdUserRegistry is Ownable, IUserRegistry {
returns (bool)
{
Verification memory verification = verifications[_user];
return canRegister(verification.time);
}

/**
* @notice check if the registry is open for registration
* @param _timestamp timestamp
*/
function canRegister(uint256 _timestamp)
public
view
returns (bool)
{
return _timestamp > 0
&& _timestamp >= registrationStartTime
&& _timestamp < registrationDeadline;
return verification.time > 0;
}

/**
Expand All @@ -145,8 +110,6 @@ contract BrightIdUserRegistry is Ownable, IUserRegistry {
) external {
require(context == _context, ERROR_INVALID_CONTEXT);
require(verifications[_addr].time < _timestamp, ERROR_NEWER_VERIFICATION);
require(canRegister(block.timestamp), ERROR_REGISTRATION_CLOSED);
require(canRegister(_timestamp), ERROR_EXPIRED_VERIFICATION);

bytes32 message = keccak256(abi.encodePacked(_context, _addr, _verificationHash, _timestamp));
address signer = ecrecover(message, _v, _r, _s);
Expand Down
12 changes: 0 additions & 12 deletions contracts/scripts/deployRound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,6 @@ async function main() {
const maciAddress = await fundingRound.maci()
console.log('maci.address: ', maciAddress)

if (userRegistryType === 'brightid') {
const maci = await ethers.getContractAt('MACI', maciAddress)
const startTime = await maci.signUpTimestamp()
const endTime = await maci.calcSignUpDeadline()
const periodTx = await userRegistry.setRegistrationPeriod(
startTime,
endTime
)
console.log('Set user registration period', periodTx.hash)
await periodTx.wait()
}

console.log('*******************')
console.log('Deploy complete!')
console.log('*******************')
Expand Down
17 changes: 0 additions & 17 deletions contracts/scripts/deployTestRound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,23 +288,6 @@ async function main() {
const maciAddress = await fundingRound.maci()
console.log(`MACI address: ${maciAddress}`)

if (userRegistryType === 'brightid') {
const userRegistryAddress = await fundingRound.userRegistry()
const userRegistry = await ethers.getContractAt(
'BrightIdUserRegistry',
userRegistryAddress
)
const maci = await ethers.getContractAt('MACI', maciAddress)
const startTime = await maci.signUpTimestamp()
const endTime = await maci.calcSignUpDeadline()
const periodTx = await userRegistry.setRegistrationPeriod(
startTime,
endTime
)
console.log('Set user registration period', periodTx.hash)
await periodTx.wait()
}

const recipientRegistryType = process.env.RECIPIENT_REGISTRY_TYPE || 'simple'
const recipientRegistryAddress = await factory.recipientRegistry()
if (recipientRegistryType === 'simple') {
Expand Down
81 changes: 50 additions & 31 deletions contracts/scripts/newRound.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ethers } from 'hardhat'
import { utils, constants } from 'ethers'

async function main() {
console.log('*******************')
Expand All @@ -7,59 +8,77 @@ async function main() {
const [deployer] = await ethers.getSigners()
console.log('deployer.address: ', deployer.address)

const fundingRoundFactoryAddress = process.env.FUNDING_ROUND_FACTORY_ADDRESS
const fundingRoundFactoryAddress = process.env.FACTORY_ADDRESS
const userRegistryType = process.env.USER_REGISTRY_TYPE
const brightIdSponsor = process.env.BRIGHTID_SPONSOR
const brightIdVerifier = process.env.BRIGHTID_VERIFIER_ADDR

if (!fundingRoundFactoryAddress) {
throw new Error(
'Environment variable FUNDING_ROUND_FACTORY_ADDRESS is not setup'
)
throw new Error('Environment variable FACTORY_ADDRESS is not setup')
}

if (!userRegistryType) {
throw new Error('Environment variable USER_REGISTRY_TYPE is not setup')
}

if (userRegistryType === 'brightid') {
if (!brightIdSponsor) {
throw new Error('Environment variable BRIGHTID_SPONSOR is not setup')
}
if (!brightIdVerifier) {
throw new Error(
'Environment variable BRIGHTID_VERIFIER_ADDR is not setup'
)
}
}

const factory = await ethers.getContractAt(
'FundingRoundFactory',
fundingRoundFactoryAddress
)
console.log('funding round factory address ', factory.address)

const tx = await factory.deployNewRound()
console.log('deployNewRound tx hash: ', tx.hash)
await tx.wait()

const fundingRoundAddress = await factory.getCurrentRound()
console.log('new funding round address: ', fundingRoundAddress)

// for BrightId user registry, we need to activate the registry
// by setting the registration period to match maci signup period
if (userRegistryType === 'brightid') {
// get maci signup period
const fundingRound = await ethers.getContractAt(
// check if the current round is finalized before starting a new round to avoid revert
const currentRoundAddress = await factory.getCurrentRound()
if (currentRoundAddress !== constants.AddressZero) {
const currentRound = await ethers.getContractAt(
'FundingRound',
fundingRoundAddress
currentRoundAddress
)
const maciAddress = await fundingRound.maci()
console.log('maci address: ', maciAddress)
const maci = await ethers.getContractAt('MACI', maciAddress)
const startTime = await maci.signUpTimestamp()
const endTime = await maci.calcSignUpDeadline()
const isFinalized = await currentRound.isFinalized()
if (!isFinalized) {
throw new Error(
'Cannot start a new round as the current round is not finalized'
)
}
}

// set user registration period
const userRegistryAddress = await fundingRound.userRegistry()
const userRegistry = await ethers.getContractAt(
// deploy a new BrightId user registry for each new round
// to force users to link with BrightId every round
if (userRegistryType === 'brightid') {
const BrightIdUserRegistry = await ethers.getContractFactory(
'BrightIdUserRegistry',
userRegistryAddress
deployer
)
const periodTx = await userRegistry.setRegistrationPeriod(
startTime,
endTime
const userRegistry = await BrightIdUserRegistry.deploy(
utils.formatBytes32String(process.env.BRIGHTID_CONTEXT || 'clr.fund'),
brightIdVerifier,
brightIdSponsor
)
console.log('BrightId user registry address: ', userRegistry.address)
await userRegistry.deployTransaction.wait()

const setUserRegistryTx = await factory.setUserRegistry(
userRegistry.address
)
console.log('User registration period changed at', periodTx.hash)
await periodTx.wait()
await setUserRegistryTx.wait()
console.log('Set user registry in factory', setUserRegistryTx.hash)
}

const tx = await factory.deployNewRound()
console.log('deployNewRound tx hash: ', tx.hash)
await tx.wait()

console.log('*******************')
console.log('Script complete!')
console.log('*******************')
Expand Down
62 changes: 8 additions & 54 deletions contracts/tests/userRegistryBrightId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,6 @@ describe('BrightId User Registry', () => {
)
})

describe('registration period not set', () => {
it('rejects registration', async () => {
const timestamp = await getBlockTimestamp(provider)
const verification = generateVerification(user.address, timestamp)
await expect(register(registry, verification)).to.be.revertedWith(
'REGISTRATION CLOSED'
)
})
it('isVerifiedUser returns false', async () => {
expect(await registry.isVerifiedUser(deployer.address)).to.equal(false)
})
})

it('reject invalid registration period', async () => {
await expect(registry.setRegistrationPeriod(3, 2)).to.be.revertedWith(
'INVALID REGISTRATION PERIOD'
)
})
it('reject invalid verifier', async () => {
await expect(
registry.setSettings(context, ZERO_ADDRESS)
Expand All @@ -131,38 +113,10 @@ describe('BrightId User Registry', () => {
.withArgs(sponsor.address)
})

it('allows valid registration period', async () => {
await expect(registry.setRegistrationPeriod(1, 2))
.to.emit(registry, 'RegistrationPeriodChanged')
.withArgs(1, 2)
})

describe('registration', () => {
let blockTimestamp: number
let invalidTimestamp: number
let deadline: number

beforeEach(async () => {
const blockNumber = await provider.getBlockNumber()
const block = await provider.getBlock(blockNumber)
invalidTimestamp = block.timestamp + 1
const startTime = block.timestamp + 2
blockTimestamp = block.timestamp + 5
deadline = startTime + 500
const tx = await registry.setRegistrationPeriod(startTime, deadline)
await tx.wait()
})

describe('canRegister', () => {
it('returns true for valid timestamp', async () => {
await expect(await registry.canRegister(blockTimestamp)).to.equal(true)
})

it('returns false for invalid timestamp', async () => {
await expect(await registry.canRegister(invalidTimestamp)).to.equal(
false
)
})
blockTimestamp = await getBlockTimestamp(provider)
})

it('allows valid verified user to register', async () => {
Expand All @@ -175,6 +129,13 @@ describe('BrightId User Registry', () => {
expect(await registry.isVerifiedUser(user.address)).to.equal(true)
})

it('rejects verifications with 0 timestamp', async () => {
const verification = generateVerification(user.address, 0)
await expect(register(registry, verification)).to.be.revertedWith(
'NEWER VERIFICATION REGISTERED BEFORE'
)
})

it('rejects older verifications', async () => {
expect(await registry.isVerifiedUser(user.address)).to.equal(false)
const oldTime = blockTimestamp
Expand All @@ -201,13 +162,6 @@ describe('BrightId User Registry', () => {
)
})

it('rejects verifications after deadline', async () => {
const verification = generateVerification(user.address, deadline + 1)
await expect(register(registry, verification)).to.be.revertedWith(
'EXPIRED VERIFICATION'
)
})

it('rejects invalid context', async () => {
const verification = generateVerification(user.address, blockTimestamp)
const tx = await registry.setSettings(
Expand Down
6 changes: 3 additions & 3 deletions docs/brightid.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ Available envs:

| Network/Env | Context | Sponsor Contract |
| ----------- | ------- | ---------------- |
| goerli | clrfund-goerli | 0xF045234A776C87060DEEc5689056455A24a59c08 |
| xdai | clrfund-gnosis-chain |0x669A55Dd17a2f9F4dacC37C7eeB5Ed3e13f474f9|
| arbitrum | clrfund-arbitrum |0x669A55Dd17a2f9F4dacC37C7eeB5Ed3e13f474f9|

| arbitrum rinkeby | clrfund-arbitrum-rinkeby | 0xC7c81634Dac2de4E7f2Ba407B638ff003ce4534C |
| goerli | clrfund-goerli | 0xF045234A776C87060DEEc5689056455A24a59c08 |
| xdai | clrfund-gnosischain |0x669A55Dd17a2f9F4dacC37C7eeB5Ed3e13f474f9|

```.sh
# /vue-app/.env
Expand Down
Loading

0 comments on commit 470a6f9

Please sign in to comment.