Skip to content

Commit

Permalink
feat: unify classHash for siera and legacy
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Mar 14, 2023
1 parent 9ec3147 commit fac2bfe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
14 changes: 12 additions & 2 deletions __tests__/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import fs from 'fs';
import path from 'path';

import { Account, ProviderInterface, RpcProvider, SequencerProvider, json } from '../src';
import { CompiledContract, CompiledSieraCasm, waitForTransactionOptions } from '../src/types';
import {
CompiledContract,
CompiledSiera,
CompiledSieraCasm,
waitForTransactionOptions,
} from '../src/types';
import { toHex } from '../src/utils/num';

const readContract = (name: string): CompiledContract =>
Expand All @@ -15,6 +20,11 @@ const readContractSieraCasm = (name: string): CompiledSieraCasm =>
fs.readFileSync(path.resolve(__dirname, `../__mocks__/${name}.casm`)).toString('ascii')
);

const readContractSiera = (name: string): CompiledSiera =>
json.parse(
fs.readFileSync(path.resolve(__dirname, `../__mocks__/${name}.json`)).toString('ascii')
);

export const compiledOpenZeppelinAccount = readContract('Account');
export const compiledErc20 = readContract('ERC20');
export const compiledErc20Echo = readContract('ERC20-echo');
Expand All @@ -24,7 +34,7 @@ export const compiledMulticall = readContract('multicall');
export const compiledTestDapp = readContract('TestDapp');
export const compiledStarknetId = readContract('starknetId_compiled');
export const compiledNamingContract = readContract('naming_compiled');
export const compiledHelloSiera = readContract('cairo/helloSiera/hello');
export const compiledHelloSiera = readContractSiera('cairo/helloSiera/hello');
export const compiledHelloSieraCasm = readContractSieraCasm('cairo/helloSiera/hello');

/* Default test config based on run `starknet-devnet --seed 0` */
Expand Down
17 changes: 16 additions & 1 deletion src/utils/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export default function computeHintedClassHash(compiledContract: CompiledContrac
}

// Computes the class hash of a given contract class
export function computeContractClassHash(contract: CompiledContract | string) {
export function computeLegacyContractClassHash(contract: CompiledContract | string) {
const compiledContract =
typeof contract === 'string' ? (parse(contract) as CompiledContract) : contract;

Expand Down Expand Up @@ -413,3 +413,18 @@ export function computeSieraContractClassHash(siera: CompiledSiera) {
])
);
}

/**
* Compute ClassHash (siera or legacy) based on provided contract
* @param contract CompiledContract | CompiledSiera | string
* @returns HexString ClassHash
*/
export function computeContractClassHash(contract: CompiledContract | CompiledSiera | string) {
const compiledContract = typeof contract === 'string' ? parse(contract) : contract;

if ('sierra_program' in compiledContract) {
return computeSieraContractClassHash(compiledContract as CompiledSiera);
}

return computeLegacyContractClassHash(compiledContract as CompiledContract);
}

0 comments on commit fac2bfe

Please sign in to comment.