-
Notifications
You must be signed in to change notification settings - Fork 93
/
RandomNumber.ts
35 lines (30 loc) · 973 Bytes
/
RandomNumber.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* @dev Class for managing random numbers
* @dev In the Umbra Protocol, all random numbers should be 32 bytes to ensure sufficient security
*/
import { BigNumber, hexZeroPad } from '../ethers';
import { utils } from '@noble/secp256k1';
export class RandomNumber {
readonly sizeInBytes = 32; // generated random number will always be 32 bytes
readonly value: BigNumber; // random number value
/**
* @notice Generate a new 32 byte random number
*/
constructor() {
// Randomly generate 32 bytes and save them as a BigNumber
const randomNumberAsBytes = utils.randomPrivateKey();
this.value = BigNumber.from(randomNumberAsBytes);
}
/**
* @notice Get random number as hex string
*/
get asHex() {
return hexZeroPad(this.value.toHexString(), this.sizeInBytes);
}
/**
* @notice Get random number as hex string without 0x prefix
*/
get asHexSlim() {
return hexZeroPad(this.asHex, this.sizeInBytes).slice(2);
}
}