Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
🌱 Add randomModule API
Browse files Browse the repository at this point in the history
- isSeedRevealValid
- getRandomBytes
  • Loading branch information
ishantiw committed Oct 22, 2021
1 parent cd1b814 commit 51d3bcb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
36 changes: 35 additions & 1 deletion framework/src/modules/random/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,40 @@
* Removal or modification of this copyright notice is prohibited.
*/

import { ImmutableAPIContext } from '../../node/state_machine';
import { BaseAPI } from '../base_api';
import { EMPTY_KEY } from '../validators/constants';
import { STORE_PREFIX_RANDOM } from './constants';
import { seedRevealSchema } from './schemas';
import { ValidatorReveals } from './types';
import { isSeedRevealValidUtil, randomBytesUtil } from './utils';

export class RandomAPI extends BaseAPI {}
export class RandomAPI extends BaseAPI {
public async isSeedRevealValid(
apiContext: ImmutableAPIContext,
generatorAddress: Buffer,
seedReveal: Buffer,
): Promise<boolean> {
const randomDataStore = apiContext.getStore(this.moduleID, STORE_PREFIX_RANDOM);
const { validatorReveals } = await randomDataStore.getWithSchema<ValidatorReveals>(
EMPTY_KEY,
seedRevealSchema,
);

return isSeedRevealValidUtil(generatorAddress, seedReveal, validatorReveals);
}

public async getRandomBytes(
apiContext: ImmutableAPIContext,
height: number,
numberOfSeeds: number,
): Promise<Buffer> {
const randomDataStore = apiContext.getStore(this.moduleID, STORE_PREFIX_RANDOM);
const { validatorReveals } = await randomDataStore.getWithSchema<ValidatorReveals>(
EMPTY_KEY,
seedRevealSchema,
);

return randomBytesUtil(height, numberOfSeeds, validatorReveals);
}
}
2 changes: 2 additions & 0 deletions framework/src/modules/random/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ export const DEFAULT_MAX_LENGTH_REVEALS = 206;
export const STORE_PREFIX_RANDOM = 0x0000;
export const STORE_PREFIX_REGISTERED_HASH_ONION = Buffer.from('00', 'hex');
export const STORE_PREFIX_USED_HASH_ONION = Buffer.from('01', 'hex');
export const RANDOM_SEED_BYTE_SIZE = 8;
export const EMPTY_KEY = Buffer.alloc(0);
2 changes: 1 addition & 1 deletion framework/src/modules/random/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export const seedRevealSchema = {
required: ['generatorAddress', 'seedReveal', 'height', 'valid'],
},
},
required: ['validatorReveals'],
},
required: ['validatorReveals'],
};

export const blockHeaderAssetRandomModule = {
Expand Down
11 changes: 11 additions & 0 deletions framework/src/modules/random/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,14 @@ export interface GeneratorConfig {
readonly defaultPassword?: string;
readonly waitThreshold: number;
}

export interface ValidatorSeedReveal {
generatorAddress: Buffer;
seedReveal: Buffer;
height: number;
valid: boolean;
}

export interface ValidatorReveals {
validatorReveals: ValidatorSeedReveal[];
}

0 comments on commit 51d3bcb

Please sign in to comment.