Skip to content

Commit

Permalink
chore: Add JsDoc comments and examples for selector.ts file (#1106)
Browse files Browse the repository at this point in the history
  • Loading branch information
NueloSE authored Apr 29, 2024
1 parent f77bae5 commit 0b25aba
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions src/utils/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import { hexToBytes, isHex, isStringWholeNumber, toHex, toHexString } from './nu

/**
* Calculate hex-string keccak hash for a given BigNumberish
*
* BigNumberish -> hex-string keccak hash
* @returns format: hex-string
* @param value The value you want to get the keccak hash from.
* @returns format: hex-string keccak hash
* @example
* ```typescript
* const hash: string = keccakBn(123456789);
* // hash = "0x6c1eebcad9e5b7e0f13855f5e4b56e85ad24544b"
* ```
*/
export function keccakBn(value: BigNumberish): string {
const hexWithoutPrefix = removeHexPrefix(toHex(BigInt(value)));
Expand All @@ -19,9 +23,14 @@ export function keccakBn(value: BigNumberish): string {

/**
* Calculate hex-string keccak hash for a given string
*
* @param str The value you want to get the keccak hash from.
* String -> hex-string keccak hash
* @returns format: hex-string
* @example
* ```typescript
* const hash: string = keccakHex("Hello, world!");
* // hash = "0x3ad6fcbda8fc87e9fb42f7f0cd36d27da079ffafc6f0dcf36b6a6140e0f67c84"
* ```
*/
function keccakHex(str: string): string {
return addHexPrefix(keccak(utf8ToArray(str)).toString(16));
Expand All @@ -35,6 +44,10 @@ function keccakHex(str: string): string {
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L17-L22)
* @param str the value you want to get the keccak hash from
* @returns starknet keccak hash as BigInt
* @example
* ```typescript
* const hash: bigint = starknetKeccak("Hello, world!");
* // hash = "38418923196344919485056939258679159916n"
*/
export function starknetKeccak(str: string): bigint {
const hash = BigInt(keccakHex(str));
Expand All @@ -48,8 +61,13 @@ export function starknetKeccak(str: string): bigint {
* Abi-function-name -> hex-string selector
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
* @param funcName ascii-string of 'abi function name'
* @returns format: hex-string; selector for 'abi function name'
* @param funcName ascii-string of 'abi function name'.
* @returns format: hex-string; selector for 'abi function name'.
* @example
* ```typescript
* const selector: string = getSelectorFromName("myFunction");
* // selector = "0x7e44baf0"
* ```
*/
export function getSelectorFromName(funcName: string) {
// sometimes BigInteger pads the hex string with zeros, which is not allowed in the starknet api
Expand All @@ -63,6 +81,17 @@ export function getSelectorFromName(funcName: string) {
*
* @param value hex-string | dec-string | ascii-string
* @returns format: hex-string
* @example
* ```typescript
* const selector: string = getSelector("myFunction");
* // selector = "0x7e44bafo"
*
* const selector1: string = getSelector("0x123abc");
* // selector1 = "0x123abc"
*
* const selector2: string = getSelector("123456");
* // selector2 = "0x1e240"
* ```
*/
export function getSelector(value: string) {
if (isHex(value)) {
Expand Down

0 comments on commit 0b25aba

Please sign in to comment.