Skip to content

Commit

Permalink
check the round on returned beacons (#74)
Browse files Browse the repository at this point in the history
* check the round on returned beacons

* account for passed in round number

Co-authored-by: Louis Bettens <[email protected]>

* removed ununsed import

* updated a dependency that dependabot complained about

* fixed some broken tests

* linter

---------

Co-authored-by: Louis Bettens <[email protected]>
  • Loading branch information
CluEleSsUK and Louis Bettens authored Jan 26, 2024
1 parent 5d7faa5 commit 074d2cb
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 127 deletions.
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

0 comments on commit 074d2cb

Please sign in to comment.