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

fix: remove default from computeHintedClassHash to be able to export #1142

Merged
Merged
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
27 changes: 13 additions & 14 deletions __tests__/utils/classHash.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
computeCompiledClassHash,
computeContractClassHash,
computeSierraContractClassHash,
getSelectorFromName,
} from '../../src/utils/hash';
import { hash } from '../../src';
import {
compiledC260Casm,
compiledErc20,
Expand All @@ -16,30 +11,34 @@ import {

describe('Hash Tester', () => {
test('Test getSelectorFromName', () => {
const hash = getSelectorFromName('__validate__');
expect(hash).toEqual('0x162da33a4585851fe8d3af3c2a9c60b557814e221e0d4f30ff0b2189d9c7775');
const hash0 = hash.getSelectorFromName('__validate__');
expect(hash0).toEqual('0x162da33a4585851fe8d3af3c2a9c60b557814e221e0d4f30ff0b2189d9c7775');
});

describe('Compute ClassHash of various contracts Cairo0', () => {
test('ERC20 Contract ClassHash', () => {
const classHash = computeContractClassHash(compiledErc20);
const classHash = hash.computeContractClassHash(compiledErc20);

expect(classHash).toEqual(erc20ClassHash);
expect(classHash).toMatchInlineSnapshot(
`"0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a"`
);
const hintedClassH = hash.computeHintedClassHash(compiledErc20);
expect(hintedClassH).toBe(
'0x2819cbfc03fb25e1816c2aa6ec990062539a4470a8f57b78d27a6efbd8e1446'
);
});

test('OZ ERC20 Contract ClassHash', () => {
const classHash = computeContractClassHash(compiledOpenZeppelinAccount);
const classHash = hash.computeContractClassHash(compiledOpenZeppelinAccount);

expect(classHash).toMatchInlineSnapshot(
`"0x36c7e49a16f8fc760a6fbdf71dde543d98be1fee2eda5daff59a0eeae066ed9"`
);
});

test('Test DApp Contract ClassHash', () => {
const classHash = computeContractClassHash(compiledTestDapp);
const classHash = hash.computeContractClassHash(compiledTestDapp);

expect(classHash).toMatchInlineSnapshot(
`"0x4367b26fbb92235e8d1137d19c080e6e650a6889ded726d00658411cc1046f5"`
Expand All @@ -49,21 +48,21 @@ describe('Hash Tester', () => {

describe('Compute CompiledClassHash & ClassHash Cairo1', () => {
test('Hello - CompiledClassHash', () => {
const compiledClassHash = computeCompiledClassHash(compiledHashSierraCasm);
const compiledClassHash = hash.computeCompiledClassHash(compiledHashSierraCasm);
expect(compiledClassHash).toEqual(
'0x5c82c98f2ab111bd50293ba64bb18cf49037374783ad2486c712709c4ba0d89'
);
});

test('Hello - CompiledClassHash Cairo2.6.0 Sierra1.5.0', () => {
const compiledClassHash = computeCompiledClassHash(compiledC260Casm);
const compiledClassHash = hash.computeCompiledClassHash(compiledC260Casm);
expect(compiledClassHash).toEqual(
'0x1725af24fbfa8050f4514651990b30e06bb9993e4e5c1051206f1bef218b1c6'
);
});

test('Hello - ClassHash', () => {
const classHash = computeSierraContractClassHash(compiledHashSierra);
const classHash = hash.computeSierraContractClassHash(compiledHashSierra);
expect(classHash).toEqual(
'0x345df0a9b35ce05d03772ba7938acad66921c5c39c1a5af74aee72aa25c363e'
);
Expand Down
11 changes: 7 additions & 4 deletions src/utils/hash/classHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,16 @@ export function formatSpaces(json: string): string {
* Compute hinted class hash for legacy compiled contract (Cairo 0)
* @param {LegacyCompiledContract} compiledContract
* @returns {string} hex-string
* No example, as this function is not exported to user
*/
export default function computeHintedClassHash(compiledContract: LegacyCompiledContract) {
* @example
* ```typescript
* const compiledCairo0 = json.parse(fs.readFileSync("./cairo0contract.json").toString("ascii"));
* const result=hash.computeHintedClassHash(compiledCairo0);
* // result = "0x293eabb06955c0a1e55557014675aa4e7a1fd69896147382b29b2b6b166a2ac"
* ``` */
export function computeHintedClassHash(compiledContract: LegacyCompiledContract): string {
ivpavici marked this conversation as resolved.
Show resolved Hide resolved
const { abi, program } = compiledContract;
const contractClass = { abi, program };
const serializedJson = formatSpaces(stringify(contractClass, nullSkipReplacer));

return addHexPrefix(starkCurve.keccak(utf8ToArray(serializedJson)).toString(16));
}

Expand Down