Skip to content

Commit

Permalink
Improve getRandomHex
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangoree committed Jan 12, 2025
1 parent 8ef7e9f commit be1e986
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-dogs-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@delvtech/drift": patch
---

Patched `getRandomHex` testing util for better distribution and better byte alignment with odd length prefixes.
2 changes: 1 addition & 1 deletion packages/drift/src/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export {
type SerializableKey,
} from "src/utils/createSerializableKey";
export { extendInstance } from "src/utils/extendInstance";
export { getRandomHexChar as getRandomHex } from "src/utils/testing/getRandomHex";
export { getRandomHex } from "src/utils/testing/getRandomHex";
export type {
AnyFunction,
AnyObject,
Expand Down
43 changes: 43 additions & 0 deletions packages/drift/src/utils/testing/getRandomHex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { getRandomHex } from "src/exports/testing";
import { describe, expect, it } from "vitest";

describe("getRandomHex", () => {
it(
"creates valid hex strings",
{
repeats: 20,
},
() => {
const hex = getRandomHex();
expect(hex).toMatch(/^0x[0-9a-f]+$/);
},
);

it(
"creates a valid hex string with a given byte length",
{
repeats: 20,
},
() => {
const bytes = Math.floor(Math.random() * 127) + 1;
const hex = getRandomHex(bytes);
expect(hex.length).toBe(bytes * 2 + 2);
expect(hex).toMatch(/^0x[0-9a-f]+$/);
},
);

const prefix = "abcde";
it(
`maintains the correct byte length when provided a prefix ("${prefix}")`,
{
repeats: 20,
},
() => {
const bytes = Math.floor(Math.random() * 108) + 20;
const hex = getRandomHex(bytes, prefix);
expect(hex.length).toBe(bytes * 2 + 2);
const generatedChars = hex.slice(2 + prefix.length);
expect(generatedChars).toMatch(/^[0-9a-f]+$/);
},
);
});
17 changes: 7 additions & 10 deletions packages/drift/src/utils/testing/getRandomHex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ import type { HexString } from "src/adapter/types/Abi";
* ```
*/
export function getRandomHex(bytes = 32, prefix = ""): HexString {
let hex = prefix;
const length = bytes * 2 - prefix.length;
for (let i = 0; i < length; i++) {
hex += getRandomHexChar();
}
return `0x${hex}`;
}

export function getRandomHexChar() {
return Math.floor(Math.random() * 16).toString(16);
const array = new Uint8Array(bytes);
crypto.getRandomValues(array);
const hex = Array.from(array, (b) => b.toString(16).padStart(2, "0")).join(
"",
);
const paddedPrefix = prefix.length % 2 ? `${prefix}0` : prefix;
return `0x${paddedPrefix}${hex.slice(paddedPrefix.length)}` as HexString;
}

0 comments on commit be1e986

Please sign in to comment.