Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check the round on returned beacons #74

Merged
merged 6 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/beacon-verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ import { bls12_381 as bls } from '@noble/curves/bls12-381'
import { sha256 } from '@noble/hashes/sha256'
import { ensureBytes } from '@noble/curves/abstract/utils'
import {Buffer} from 'buffer'


type PointG1 = typeof bls.G1.ProjectivePoint.ZERO
type PointG2 = typeof bls.G2.ProjectivePoint.ZERO

import {
G2ChainedBeacon,
ChainInfo,
Expand All @@ -19,9 +14,16 @@ import {
isG1Rfc9380
} from './index'

async function verifyBeacon(chainInfo: ChainInfo, beacon: RandomnessBeacon): Promise<boolean> {
type PointG1 = typeof bls.G1.ProjectivePoint.ZERO
type PointG2 = typeof bls.G2.ProjectivePoint.ZERO

async function verifyBeacon(chainInfo: ChainInfo, beacon: RandomnessBeacon, expectedRound: number): Promise<boolean> {
const publicKey = chainInfo.public_key

if (beacon.round !== expectedRound) {
return false
}

if (!await randomnessIsValid(beacon)) {
return false
}
Expand Down
10 changes: 6 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ export async function fetchBeacon(client: ChainClient, roundNumber?: number): Pr
beacon = await client.get(roundNumber)
}

return validatedBeacon(client, beacon)
const expectedRound = roundNumber || roundAt(Date.now(), await client.chain().info())

return validatedBeacon(client, beacon, expectedRound)
}

// fetch the most recent beacon to have been emitted at a given `time` in epoch ms
Expand All @@ -101,7 +103,7 @@ export async function* watch(
await sleep(roundTime(info, currentRound) - now)

const beacon = await retryOnError(async () => client.get(currentRound), options.retriesOnFailure)
yield validatedBeacon(client, beacon)
yield validatedBeacon(client, beacon, currentRound)
currentRound = currentRound + 1
}
}
Expand All @@ -118,12 +120,12 @@ const defaultWatchOptions = {
}

// internal function for validating a beacon if validation has not been disabled in the client options
async function validatedBeacon(client: ChainClient, beacon: RandomnessBeacon): Promise<RandomnessBeacon> {
async function validatedBeacon(client: ChainClient, beacon: RandomnessBeacon, expectedRound: number): Promise<RandomnessBeacon> {
if (client.options.disableBeaconVerification) {
return beacon
}
const info = await client.chain().info()
if (!await verifyBeacon(info, beacon)) {
if (!await verifyBeacon(info, beacon, expectedRound)) {
throw Error('The beacon retrieved was not valid!')
}

Expand Down
Loading