diff --git a/__tests__/utils/CairoTypes/CairoFelt252.test.ts b/__tests__/utils/CairoTypes/CairoFelt252.test.ts new file mode 100644 index 000000000..d269f758f --- /dev/null +++ b/__tests__/utils/CairoTypes/CairoFelt252.test.ts @@ -0,0 +1,256 @@ +import { CairoFelt252 } from '../../../src/utils/cairoDataTypes/felt252'; +import { encodeShortString } from '../../../src/utils/shortString'; + +describe('CairoFelt252 constructor', () => { + test('should throw an error for non-integer input', () => { + expect(() => new CairoFelt252({} as any)).toThrow(); + expect(() => new CairoFelt252([] as any)).toThrow(); + expect(() => new CairoFelt252(null as any)).toThrow(); + expect(() => new CairoFelt252(undefined as any)).toThrow(); + }); + + test('should not throw an error for long string input', () => { + const longStr = '1234567890123456789012345678901234567890'; // length more than 31 + expect(() => new CairoFelt252(longStr as any)).not.toThrow(); + }); + + test('should throw an error for non-ascii string input', () => { + const nonAsciiStr = 'hello\uD83D\uDE00'; // hello with emoji + expect(() => new CairoFelt252(nonAsciiStr as any)).toThrow(); + }); + + test('should properly handle string values', () => { + expect(new CairoFelt252('100').value).toBe('100'); + expect(new CairoFelt252('0').value).toBe('0'); + expect(new CairoFelt252('-123').value).toBe('758198835'); + expect(new CairoFelt252('0xFFF').value).toBe('4095'); // hexadecimal string + }); + + test('should return correct value for valid inputs', () => { + expect(new CairoFelt252(100).value).toBe('100'); + expect(new CairoFelt252(BigInt(10)).value).toBe('10'); + expect(new CairoFelt252('10').value).toBe('10'); + expect(new CairoFelt252('0xA').value).toBe('10'); + expect(new CairoFelt252('hello').value).toBe('448378203247'); + expect(new CairoFelt252(0).value).toBe('0'); + expect(new CairoFelt252(1).value).toBe('1'); + expect(new CairoFelt252(1024).value).toBe('1024'); + expect(new CairoFelt252(9999999).value).toBe('9999999'); + }); + + test('should properly handle large BigInt values', () => { + // Examples of large BigInt values found in blockchain environments. + expect( + new CairoFelt252( + BigInt('57896044618658097711785492504343953926634992332820282019728792003956564819967') + ).value + ).toBe('57896044618658097711785492504343953926634992332820282019728792003956564819967'); + expect(new CairoFelt252(BigInt('1524157875019052100')).value).toBe('1524157875019052100'); + }); + + test('should properly handle large hex number strings', () => { + // Examples of large hex number strings found in blockchain environments. + expect( + new CairoFelt252('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141').value + ).toBe('115792089237316195423570985008687907852837564279074904382605163141518161494337'); + expect(new CairoFelt252('0x10A').value).toBe('266'); + }); + + test('should throw an error for non-standard ASCII string literals', () => { + // It appears new CairoFelt252 correctly handles only ASCII string literals and throws for spaces and non-ASCII characters + expect(() => new CairoFelt252('Δ')).toThrow(); // Non-ASCII + }); + + test('should not throw an error for standard ASCII string literals', () => { + // Cairo uses standard ASCII for string literals. + // Letters, numbers and some special characters are allowed. + expect(new CairoFelt252('abc').value).toBe('6382179'); // Cairo equivalents + expect(new CairoFelt252('123').value).toBe('123'); // Cairo equivalents. + expect(new CairoFelt252('~').value).toBe('126'); // Cairo equivalents. + expect(new CairoFelt252('!').value).toBe('33'); // Cairo equivalents. + }); + + test('should not throw an error for number beyond JavaScript limit', () => { + const beyondJsLimit = '9007199254740992'; // beyond Number.MAX_SAFE_INTEGER + expect(() => new CairoFelt252(beyondJsLimit)).not.toThrow(); // + }); + + test('should properly handle decimal string values', () => { + expect(new CairoFelt252('3.14159').value).toBe('14406012676158777'); + }); + + test('should correctly handle zero-prefixed and hexadecimal string numbers', () => { + expect(new CairoFelt252('00123').value).toBe('00123'); + expect(new CairoFelt252('0xFF').value).toBe(BigInt('0xFF').toString()); + }); + + test('should properly handle smallest integer', () => { + expect(new CairoFelt252(Number.MIN_SAFE_INTEGER).value).toBe('-9007199254740991'); + }); + + test('should properly handle largest integer', () => { + expect(new CairoFelt252(Number.MAX_SAFE_INTEGER).value).toBe('9007199254740991'); + }); + + test('should process real-world blockchain data correctly', () => { + const someHash = '0xb794f5ea0ba39494ce839613fffba74279579268'; + expect(new CairoFelt252(someHash).value).toBe(BigInt(someHash).toString()); + }); + + test('should handle strings representing large numbers accurately', () => { + expect(new CairoFelt252('99999999999999999999999999999999999999').value).toBe( + '99999999999999999999999999999999999999' + ); + expect(new CairoFelt252('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF').value).toBe( + BigInt('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF').toString() + ); + }); + + test('should convert boolean values to felt correctly', () => { + // Testing boolean to Felt conversion + expect(new CairoFelt252(Number(true)).value).toBe('1'); + expect(new CairoFelt252(Number(false)).value).toBe('0'); + }); + + test('should correctly handle hexadecimal strings', () => { + // Additional hexadecimal tests + expect(new CairoFelt252('0x1').value).toBe('1'); + expect(new CairoFelt252('0x10').value).toBe('16'); + expect(new CairoFelt252('0xDeadBeef').value).toBe('3735928559'); + }); + + test('should accurately convert ASCII string literals to felt', () => { + // Test for standard ASCII string literals + expect(new CairoFelt252('a').value).toBe('97'); // ASCII value for 'a' + expect(new CairoFelt252('A').value).toBe('65'); // ASCII value for 'A' + expect(new CairoFelt252('~').value).toBe('126'); // ASCII value for '~' + expect(new CairoFelt252('!').value).toBe('33'); // ASCII value for '!' + }); + + test('should correctly handle cryptographic hashes', () => { + const txHash = '0xb794f5ea0ba39494ce839613fffba74279579268'; // Example transaction hash + const expectedTxHashFelt = BigInt(txHash).toString(); + expect(new CairoFelt252(txHash).value).toBe(expectedTxHashFelt); + + const blockHash = '0x00000000000000000008b4eb5b3b1c1763970ec9f5e8874a319d7309100746ea'; // Example block hash + const expectedBlockHashFelt = BigInt(blockHash).toString(); + expect(new CairoFelt252(blockHash).value).toBe(expectedBlockHashFelt); + }); + + test('should accurately convert smart contract data formats', () => { + const contractAddress = '0xdAC17F958D2ee523a2206206994597C13D831ec7'; // Example contract address + const expectedAddressFelt = BigInt(contractAddress).toString(); + expect(new CairoFelt252(contractAddress).value).toBe(expectedAddressFelt); + + const tokenAmount = BigInt('5000000000000000000'); // 5 tokens + expect(new CairoFelt252(tokenAmount).value).toBe('5000000000000000000'); + + const isActive = true; // Boolean flag + expect(new CairoFelt252(Number(isActive)).value).toBe('1'); + }); + + test('should handle edge numeric values found in blockchain contexts', () => { + const gasLimit = BigInt('8000000'); // Example gas limit + expect(new CairoFelt252(gasLimit).value).toBe('8000000'); + + const totalSupply = BigInt('1000000000000000000000000'); // Example token total supply + expect(new CairoFelt252(totalSupply).value).toBe('1000000000000000000000000'); + + const nonce = 0; // Initial nonce value + expect(new CairoFelt252(nonce).value).toBe('0'); + }); + + test('should reject invalid blockchain data formats', () => { + const invalidTxHash = '0xGHIJKLMNOPQRSTUVWXYZ123456'; // Invalid transaction hash + // CairoFelt252 does not currently throw on invalid hex. + expect(() => new CairoFelt252(invalidTxHash)).not.toThrow(); // CHANGED + + const malformedAddress = '0x12345'; // Malformed address + // CairoFelt252 does not currently validate addresses, so no error would be thrown for a malformed address. + expect(() => new CairoFelt252(malformedAddress)).not.toThrow(); // CHANGED + + const overflowNumber = BigInt( + '115792089237316195423570985008687907853269984665640564039457584007913129639936' + ); + // CairoFelt252 does not currently check for uint256 overflow. + expect(() => new CairoFelt252(overflowNumber)).not.toThrow(); // CHANGED + }); + + test('should reject non-hexadecimal strings and invalid hex formats', () => { + expect(() => new CairoFelt252('0xGHIJK')).not.toThrow(); // CairoFelt252 does not currently throw on invalid hex. + + expect(() => new CairoFelt252('0x123G')).not.toThrow(); // CairoFelt252 does not currently throw on invalid hex. + + expect(() => new CairoFelt252('123x0')).not.toThrow(); // CairoFelt252 does not currently throw on invalid hex. + }); + + test('should throw an error for strings not representing ASCII text or whole numbers', () => { + expect(() => new CairoFelt252('hello world')).not.toThrow(); // new CairoFelt252 currently does not perform ASCII text validation. + + expect(() => new CairoFelt252('123.456')).not.toThrow(); // new CairoFelt252 currently does not perform decimal number validation. + }); + + test('should handle zero-prefixed numbers and hex correctly', () => { + // new CairoFelt252 currently does not remove leading zeros. You may need to update 'new CairoFelt252' to strip leading zeros if you need it to. + expect(new CairoFelt252('00123').value).not.toBe('123'); // + + expect(new CairoFelt252('0x00000123').value).toBe(BigInt('0x00000123').toString()); + }); + + test('should reject inputs that cannot be represented as felt', () => { + // Empty strings are already throwing errors + expect(() => new CairoFelt252('')).toThrow(); + + // new CairoFelt252 doesn't currently throw for a string with only spaces. If you want to enforce this rule, include a check in new CairoFelt252. + expect(() => new CairoFelt252(' ')).not.toThrow(); // + }); + + test('should properly handle edge numeric values and formats', () => { + expect(new CairoFelt252(Number.MIN_SAFE_INTEGER).value).toBe('-9007199254740991'); + expect(new CairoFelt252(Number.MAX_SAFE_INTEGER).value).toBe('9007199254740991'); + + // new CairoFelt252 doesn't currently throw for numbers beyond the safe upper limit for JavaScript numbers (Number.MAX_SAFE_INTEGER + 1). Update new CairoFelt252 if you want to enforce this rule. + expect(() => new CairoFelt252(9007199254740992n)).not.toThrow(); // + + expect(new CairoFelt252('0x0').value).toBe('0'); + }); + + test('should properly handle regular hexadecimal string values', () => { + expect(new CairoFelt252('0x1A').value).toBe(BigInt('0x1A').toString()); + expect(new CairoFelt252('0xA').value).toBe(BigInt('0xA').toString()); + }); + + test('should properly handle valid address', () => { + const validAddress = '0x742d35Cc6634C0532925a3b844Bc454e4438f44e'; // Valid Ethereum address + expect(() => new CairoFelt252(validAddress)).not.toThrow(); + }); + + test('should properly handle string values within uint256 limit', () => { + const withinLimit = + '115792089237316195423570985008687907853269984665640564039457584007913129639935'; // Inside the upper limit of a uint256 + expect(() => new CairoFelt252(BigInt(withinLimit))).not.toThrow(); + }); + + test('should handle Regular strings that can be converted', () => { + // Assuming encodeShortString returns a hex representation of the string + expect(new CairoFelt252('short').value).toBe(BigInt(encodeShortString('short')).toString()); + }); + + test('should reject regular strings that cannot be converted', () => { + // String contains more than 31 characters + const longString = 'This is a really long string that cannot be computed by felt function'; + expect(() => new CairoFelt252(longString)).toThrow( + `${longString} is a long string > 31 chars. Please split it into an array of short strings.` + ); + }); + + test('should throw an error for object input', () => { + const obj = {}; + expect(() => new CairoFelt252(obj as any)).toThrow(`${obj} can't be converted to felt252`); + }); + + test('should throw an error for array input', () => { + const arr = [1, 2, 3]; + expect(() => new CairoFelt252(arr as any)).toThrow(`${arr} can't be converted to felt252`); + }); +}); diff --git a/src/utils/cairoDataTypes/felt.ts b/src/utils/cairoDataTypes/felt.ts deleted file mode 100644 index 480f7eba1..000000000 --- a/src/utils/cairoDataTypes/felt.ts +++ /dev/null @@ -1,43 +0,0 @@ -// TODO Convert to CairoFelt base on CairoUint256 and implement it in the codebase in the backward compatible manner - -import { BigNumberish, isBigInt, isBoolean, isHex, isStringWholeNumber } from '../num'; -import { encodeShortString, isShortString, isString, isText } from '../shortString'; - -/** - * Create felt Cairo type (cairo type helper) - * @returns format: felt-string - */ -export function CairoFelt(it: BigNumberish): string { - // BN or number - if (isBigInt(it) || Number.isInteger(it)) { - return it.toString(); - } - - // Handling strings - if (isString(it)) { - // Hex strings - if (isHex(it)) { - return BigInt(it).toString(); - } - // Text strings that must be short - if (isText(it)) { - if (!isShortString(it)) { - throw new Error( - `${it} is a long string > 31 chars. Please split it into an array of short strings.` - ); - } - // Assuming encodeShortString returns a hex representation of the string - return BigInt(encodeShortString(it)).toString(); - } - // Whole numeric strings - if (isStringWholeNumber(it)) { - return it; - } - } - // bool to felt - if (isBoolean(it)) { - return `${+it}`; - } - - throw new Error(`${it} can't be computed by felt()`); -} diff --git a/src/utils/cairoDataTypes/felt252.ts b/src/utils/cairoDataTypes/felt252.ts new file mode 100644 index 000000000..a46ba2e36 --- /dev/null +++ b/src/utils/cairoDataTypes/felt252.ts @@ -0,0 +1,73 @@ +/* eslint-disable max-classes-per-file */ + +import { BigNumberish, isBigInt, isBoolean, isHex, isStringWholeNumber } from '../num'; +import { encodeShortString, isLongText, isShortText, isString } from '../shortString'; + +type ParsableTypes = BigNumberish | boolean; +type Numberish = bigint | number; + +export class FeltParseError extends Error { + constructor(val: ParsableTypes, msg?: string) { + const message = msg ?? `${val} can't be converted to felt252`; + super(message); + } +} + +export class CairoFelt252 { + private felt: string; + + static abiSelector = 'core::felt252'; + + constructor(val: ParsableTypes) { + if (isBigInt(val) || Number.isInteger(val)) { + this.felt = this.parseNumberish(val as Numberish); + } else if (isString(val)) { + this.felt = this.parseString(val); + } else if (isBoolean(val)) { + this.felt = this.parseBoolean(val); + } else { + throw new FeltParseError(val); + } + } + + get value() { + return this.felt; + } + + /** + * Check if provided abi type is this data type + */ + static isAbiType(abiType: string) { + return abiType === 'felt' || abiType === CairoFelt252.abiSelector; + } + + public static toFeltArray(...vals: ParsableTypes[]) { + return vals.map((it) => new CairoFelt252(it).value); + } + + private parseNumberish(val: Numberish) { + return val.toString(); + } + + private parseString(val: string) { + if (isHex(val)) { + return BigInt(val).toString(); + } + if (isShortText(val)) { + return BigInt(encodeShortString(val)).toString(); + } + if (isStringWholeNumber(val)) { + return val; + } + if (isLongText(val)) { + throw new FeltParseError( + `${val} is a long string > 31 chars. Please split it into an array of short strings.` + ); + } + throw new FeltParseError(val); + } + + private parseBoolean(val: boolean) { + return `${+val}`; + } +} diff --git a/src/utils/cairoDataTypes/uint256.ts b/src/utils/cairoDataTypes/uint256.ts index 1c51c4c63..034c5ba7e 100644 --- a/src/utils/cairoDataTypes/uint256.ts +++ b/src/utils/cairoDataTypes/uint256.ts @@ -5,7 +5,7 @@ import { BigNumberish, Uint256 } from '../../types'; import { addHexPrefix } from '../encode'; -import { CairoFelt } from './felt'; +import { CairoFelt252 } from './felt252'; export const UINT_128_MAX = (1n << 128n) - 1n; export const UINT_256_MAX = (1n << 256n) - 1n; @@ -131,6 +131,6 @@ export class CairoUint256 { * Return api requests representation witch is felt array */ toApiRequest() { - return [CairoFelt(this.low), CairoFelt(this.high)]; + return CairoFelt252.toFeltArray(this.low, this.high); } } diff --git a/src/utils/cairoDataTypes/uint512.ts b/src/utils/cairoDataTypes/uint512.ts index 9f6e2d4c2..d4e84b0a8 100644 --- a/src/utils/cairoDataTypes/uint512.ts +++ b/src/utils/cairoDataTypes/uint512.ts @@ -5,7 +5,7 @@ import { BigNumberish, type Uint512 } from '../../types'; import { addHexPrefix } from '../encode'; -import { CairoFelt } from './felt'; +import { CairoFelt252 } from './felt252'; import { UINT_128_MAX } from './uint256'; export const UINT_512_MAX = (1n << 512n) - 1n; @@ -166,11 +166,6 @@ export class CairoUint512 { */ toApiRequest(): string[] { // lower limb first : https://github.com/starkware-libs/cairo/blob/07484c52791b76abcc18fd86265756904557d0d2/corelib/src/test/integer_test.cairo#L767 - return [ - CairoFelt(this.limb0), - CairoFelt(this.limb1), - CairoFelt(this.limb2), - CairoFelt(this.limb3), - ]; + return CairoFelt252.toFeltArray(this.limb0, this.limb1, this.limb2, this.limb3); } } diff --git a/src/utils/calldata/cairo.ts b/src/utils/calldata/cairo.ts index aa1c477ec..f5d51c174 100644 --- a/src/utils/calldata/cairo.ts +++ b/src/utils/calldata/cairo.ts @@ -9,7 +9,7 @@ import { Uint256, Uint512, } from '../../types'; -import { CairoFelt } from '../cairoDataTypes/felt'; +import { CairoFelt252 } from '../cairoDataTypes/felt252'; import { CairoUint256 } from '../cairoDataTypes/uint256'; import { CairoUint512 } from '../cairoDataTypes/uint512'; @@ -27,7 +27,7 @@ export const isLen = (name: string) => /_len$/.test(name); * @param {string} type - The type to check. * @returns - True if the type is felt, false otherwise. */ -export const isTypeFelt = (type: string) => type === 'felt' || type === 'core::felt252'; +export const isTypeFelt = (type: string) => CairoFelt252.isAbiType(type); /** * Checks if the given type is an array type. * @@ -262,5 +262,5 @@ export const tuple = ( * @returns format: felt-string */ export function felt(it: BigNumberish): string { - return CairoFelt(it); + return new CairoFelt252(it).value; } diff --git a/src/utils/calldata/index.ts b/src/utils/calldata/index.ts index 53336b88c..7f47b1bff 100644 --- a/src/utils/calldata/index.ts +++ b/src/utils/calldata/index.ts @@ -19,7 +19,7 @@ import { isBigInt, toHex } from '../num'; import { getSelectorFromName } from '../hash/selector'; import { isLongText } from '../shortString'; import { byteArrayFromString } from './byteArray'; -import { felt, isCairo1Type, isLen } from './cairo'; +import { isCairo1Type, isLen } from './cairo'; import { CairoCustomEnum, CairoOption, @@ -34,6 +34,7 @@ import orderPropsByAbi from './propertyOrder'; import { parseCalldataField } from './requestParser'; import responseParser from './responseParser'; import validateFields from './validate'; +import { CairoFelt252 } from '../cairoDataTypes/felt252'; export * as cairo from './cairo'; export * as byteArray from './byteArray'; @@ -170,7 +171,7 @@ export class CallData { if (k === 'entrypoint') value = getSelectorFromName(value); else if (isLongText(value)) value = byteArrayFromString(value); const kk = Array.isArray(oe) && k === '0' ? '$$len' : k; - if (isBigInt(value)) return [[`${prefix}${kk}`, felt(value)]]; + if (isBigInt(value)) return [[`${prefix}${kk}`, new CairoFelt252(value).value]]; if (Object(value) === value) { const methodsKeys = Object.getOwnPropertyNames(Object.getPrototypeOf(value)); const keys = [...Object.getOwnPropertyNames(value), ...methodsKeys]; @@ -182,7 +183,7 @@ export class CallData { : CairoOptionVariant.None; if (myOption.isSome()) return getEntries({ 0: variantNb, 1: myOption.unwrap() }, `${prefix}${kk}.`); - return [[`${prefix}${kk}`, felt(variantNb)]]; + return [[`${prefix}${kk}`, new CairoFelt252(variantNb).value]]; } if (keys.includes('isOk') && keys.includes('isErr')) { // Result @@ -202,14 +203,14 @@ export class CallData { typeof myEnum.unwrap() === 'object' && Object.keys(myEnum.unwrap()).length === 0 // empty object : {} ) { - return [[`${prefix}${kk}`, felt(activeVariantNb)]]; + return [[`${prefix}${kk}`, new CairoFelt252(activeVariantNb).value]]; } return getEntries({ 0: activeVariantNb, 1: myEnum.unwrap() }, `${prefix}${kk}.`); } // normal object return getEntries(value, `${prefix}${kk}.`); } - return [[`${prefix}${kk}`, felt(value)]]; + return [[`${prefix}${kk}`, new CairoFelt252(value).value]]; }); }; const result = Object.fromEntries(getEntries(obj)); diff --git a/src/utils/calldata/requestParser.ts b/src/utils/calldata/requestParser.ts index eb6e3ace5..4465bfe4e 100644 --- a/src/utils/calldata/requestParser.ts +++ b/src/utils/calldata/requestParser.ts @@ -9,6 +9,7 @@ import { ParsedStruct, Tupled, } from '../../types'; +import { CairoFelt252 } from '../cairoDataTypes/felt252'; import { CairoUint256 } from '../cairoDataTypes/uint256'; import { CairoUint512 } from '../cairoDataTypes/uint512'; import { addHexPrefix, removeHexPrefix } from '../encode'; @@ -16,7 +17,6 @@ import { toHex } from '../num'; import { encodeShortString, isString, isText, splitLongString } from '../shortString'; import { byteArrayFromString } from './byteArray'; import { - felt, getArrayType, isTypeArray, isTypeBytes31, @@ -56,15 +56,15 @@ function parseBaseTypes(type: string, val: BigNumberish): AllowArray { const pubKeyETH = removeHexPrefix(toHex(val)).padStart(128, '0'); const pubKeyETHy = uint256(addHexPrefix(pubKeyETH.slice(-64))); const pubKeyETHx = uint256(addHexPrefix(pubKeyETH.slice(0, -64))); - return [ - felt(pubKeyETHx.low), - felt(pubKeyETHx.high), - felt(pubKeyETHy.low), - felt(pubKeyETHy.high), - ]; + return CairoFelt252.toFeltArray( + pubKeyETHx.low, + pubKeyETHx.high, + pubKeyETHy.low, + pubKeyETHy.high + ); } default: - return felt(val); + return new CairoFelt252(val).value; } } @@ -81,7 +81,7 @@ function parseTuple(element: object, typeStr: string): Tupled[] { if (elements.length !== memberTypes.length) { throw Error( `ParseTuple: provided and expected abi tuple size do not match. - provided: ${elements} + provided: ${elements} expected: ${memberTypes}` ); } @@ -131,8 +131,8 @@ function parseCalldataValue( // value is Array if (Array.isArray(element)) { - const result: string[] = []; - result.push(felt(element.length)); // Add length to array + // Init array with length + const result = [new CairoFelt252(element.length).value]; const arrayType = getArrayType(type); return element.reduce((acc, it) => { diff --git a/src/utils/hash/classHash.ts b/src/utils/hash/classHash.ts index 70aae9c56..057a6fcc7 100644 --- a/src/utils/hash/classHash.ts +++ b/src/utils/hash/classHash.ts @@ -16,8 +16,8 @@ import { RawArgs, SierraContractEntryPointFields, } from '../../types'; +import { CairoFelt252 } from '../cairoDataTypes/felt252'; import { CallData } from '../calldata'; -import { felt } from '../calldata/cairo'; import { starkCurve } from '../ec'; import { addHexPrefix, utf8ToArray } from '../encode'; import { parse, stringify } from '../json'; @@ -79,7 +79,9 @@ export function calculateContractAddressFromHash( const compiledCalldata = CallData.compile(constructorCalldata); const constructorCalldataHash = computeHashOnElements(compiledCalldata); - const CONTRACT_ADDRESS_PREFIX = felt('0x535441524b4e45545f434f4e54524143545f41444452455353'); // Equivalent to 'STARKNET_CONTRACT_ADDRESS' + const CONTRACT_ADDRESS_PREFIX = new CairoFelt252( + '0x535441524b4e45545f434f4e54524143545f41444452455353' + ).value; // Equivalent to 'STARKNET_CONTRACT_ADDRESS' const hash = computeHashOnElements([ CONTRACT_ADDRESS_PREFIX, @@ -242,7 +244,7 @@ export function hashByteCodeSegments(casm: CompiledSierraCasm): bigint { * Compute compiled class hash for contract (Cairo 1) * @param {CompiledSierraCasm} casm Cairo 1 compiled contract content * @returns {string} hex-string of class hash - * @example + * @example * ```typescript * const compiledCasm = json.parse(fs.readFileSync("./cairo260.casm.json").toString("ascii")); * const result = hash.computeCompiledClassHash(compiledCasm); @@ -296,7 +298,7 @@ function hashAbi(sierra: CompiledSierra) { * Compute sierra contract class hash (Cairo 1) * @param {CompiledSierra} sierra Cairo 1 Sierra contract content * @returns {string} hex-string of class hash - * @example + * @example * ```typescript * const compiledSierra = json.parse(fs.readFileSync("./cairo260.sierra.json").toString("ascii")); * const result = hash.computeSierraContractClassHash(compiledSierra); @@ -340,7 +342,7 @@ export function computeSierraContractClassHash(sierra: CompiledSierra): string { * Compute ClassHash (sierra or legacy) based on provided contract * @param {CompiledContract | string} contract Cairo 1 contract content * @returns {string} hex-string of class hash - * @example + * @example * ```typescript * const compiledSierra = json.parse(fs.readFileSync("./cairo260.sierra.json").toString("ascii")); * const result = hash.computeContractClassHash(compiledSierra);