Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add roleBytes utility for contract role usage #5685

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/thirdweb/src/utils/encoding/helpers/role-bytes.test.ts
Original file line number Diff line number Diff line change
@@ -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",
);
});
});
14 changes: 14 additions & 0 deletions packages/thirdweb/src/utils/encoding/helpers/role-bytes.ts
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about a union type for this of 'known' role strings + | ({} & string) to allow for others:

type KnownRoles = "ADMIN_ROLE" | "MINTER_ROLE" ...
type Role = KnownRoles | ({} & string)

* @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}` => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to export it publicly, you can do that by exporting it from /exports

likely under /exports/extensions/common.ts where the other role stuff is

return keccak256(toBytes(role));
};
Loading