diff --git a/packages/thirdweb/src/utils/encoding/helpers/role-bytes.test.ts b/packages/thirdweb/src/utils/encoding/helpers/role-bytes.test.ts new file mode 100644 index 00000000000..1e8f9e37b80 --- /dev/null +++ b/packages/thirdweb/src/utils/encoding/helpers/role-bytes.test.ts @@ -0,0 +1,10 @@ +import { describe, expect, it } from "vitest"; +import { roleBytes } from "./role-bytes.js"; + +describe("roleBytes", () => { + it("should calculate the value of lister role", () => { + expect(roleBytes("LISTER_ROLE")).toBe( + "0xf94103142c1baabe9ac2b5d1487bf783de9e69cfeea9a72f5c9c94afd7877b8c", + ); + }); +}); diff --git a/packages/thirdweb/src/utils/encoding/helpers/role-bytes.ts b/packages/thirdweb/src/utils/encoding/helpers/role-bytes.ts new file mode 100644 index 00000000000..8af2d05178f --- /dev/null +++ b/packages/thirdweb/src/utils/encoding/helpers/role-bytes.ts @@ -0,0 +1,14 @@ +import { keccak256 } from "../../hashing/keccak256.js"; +import { toBytes } from "../to-bytes.js"; + +/** + * Generates a 256-bit hash of a given role string in bytes form using the keccak256 algorithm. + * + * @param {string} role - The role string to be converted into bytes and hashed. + * @returns {`0x${string}`} A 256-bit hash of the input role as a byte array. + * @example + * const AdminRole = roleBytes("ADMIN_ROLE"); + */ +export const roleBytes = (role: string): `0x${string}` => { + return keccak256(toBytes(role)); +};