Skip to content
Open
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
92 changes: 91 additions & 1 deletion __tests__/utils/transactionHash.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { constants, v2hash } from '../../src';
import { constants, hash, v2hash, v3hash } from '../../src';

describe('TxV2 Hash Tests', () => {
describe('calculateTransactionHashCommon()', () => {
Expand All @@ -17,6 +17,96 @@ describe('TxV2 Hash Tests', () => {
});
});

describe('TxV3 Invoke proofFacts Hash Tests', () => {
const commonParams = {
senderAddress: '0x12fd538',
version: '0x3' as const,
compiledCalldata: ['0x11', '0x26'] as string[],
chainId: constants.StarknetChainId.SN_SEPOLIA,
nonce: 9,
accountDeploymentData: [] as string[],
nonceDataAvailabilityMode: 0,
feeDataAvailabilityMode: 0,
resourceBounds: {
l2_gas: { max_amount: 0n, max_price_per_unit: 0n },
l1_gas: { max_amount: 0x7c9n, max_price_per_unit: 1n },
l1_data_gas: { max_amount: 0n, max_price_per_unit: 0n },
},
tip: 0,
paymasterData: [] as string[],
};

test('empty proofFacts produces the same hash as omitted proofFacts', () => {
const hashWithout = hash.calculateInvokeTransactionHash({ ...commonParams });
const hashEmpty = hash.calculateInvokeTransactionHash({ ...commonParams, proofFacts: [] });
expect(hashEmpty).toBe(hashWithout);
});

test('non-empty proofFacts changes the transaction hash', () => {
const hashWithout = hash.calculateInvokeTransactionHash({ ...commonParams });
const hashWithPF = hash.calculateInvokeTransactionHash({
...commonParams,
proofFacts: ['0x1', '0x2'],
});
expect(hashWithPF).not.toBe(hashWithout);
});

test('proofFacts order matters for the hash', () => {
const hash1 = hash.calculateInvokeTransactionHash({
...commonParams,
proofFacts: ['0x1', '0x2'],
});
const hash2 = hash.calculateInvokeTransactionHash({
...commonParams,
proofFacts: ['0x2', '0x1'],
});
expect(hash1).not.toBe(hash2);
});
});

describe('TxV3 hashFeeFieldV3B3 — blockifier test vectors', () => {
// Test vectors from blockifier APOLLO-PRE-PROOF-DEMO-11
// crates/starknet_api/src/transaction_hash_test.rs::test_tip_resource_bounds_hash_vectors

test('L1Gas variant (no l1_data_gas) matches blockifier', () => {
// ValidResourceBounds::L1Gas: hashes [tip, l1, l2] (3 elements)
// get_l2_bounds returns default (0,0) for L1Gas variant
const bounds = {
l1_gas: { max_amount: 0x1000n, max_price_per_unit: 0x2000n },
l2_gas: { max_amount: 0n, max_price_per_unit: 0n },
};
const result = v3hash.hashFeeFieldV3B3(0, bounds);
expect(result.toString(16)).toBe(
'25630b34ab588bfc7763f0428f8ecb8fe9b1c149b82e0aee3ca10583eed2ebe'
);
});

test('AllResources variant (with l1_data_gas) matches blockifier', () => {
// ValidResourceBounds::AllResources: hashes [tip, l1, l2, l1_data] (4 elements)
const bounds = {
l1_gas: { max_amount: 0x1000n, max_price_per_unit: 0x2000n },
l2_gas: { max_amount: 0x3000n, max_price_per_unit: 0x4000n },
l1_data_gas: { max_amount: 0x5000n, max_price_per_unit: 0x6000n },
};
const result = v3hash.hashFeeFieldV3B3(0, bounds);
expect(result.toString(16)).toBe(
'3d848944220686a0d567e2a3895f3651ad616d4eccb473a03a93150c40b5e13'
);
});

test('AllResources with zero l1_data_gas matches blockifier', () => {
const bounds = {
l1_gas: { max_amount: 0x1000n, max_price_per_unit: 0x2000n },
l2_gas: { max_amount: 0x3000n, max_price_per_unit: 0x4000n },
l1_data_gas: { max_amount: 0n, max_price_per_unit: 0n },
};
const result = v3hash.hashFeeFieldV3B3(0, bounds);
expect(result.toString(16)).toBe(
'6916420cf10b91926a408900e0e8f5548bbd3baccb11b2e0ad1a58246b0ffe0'
);
});
});

// TODO: create new tests with rpc0.8+ v3 tx
/* describe('TxV3Old Hash Tests', () => {
test('DaMode', () => {
Expand Down
24 changes: 0 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,10 @@ export class Account extends Provider implements AccountInterface {
const { resourceBounds: providedResourceBounds } = transactionsDetail;
let resourceBounds = providedResourceBounds;
if (!resourceBounds) {
const estimateResponse = await this.estimateInvokeFee(calls, detailsWithTip);
const estimateResponse = await this.estimateInvokeFee(calls, {
...detailsWithTip,
proof: undefined,
});
resourceBounds = estimateResponse.resourceBounds;
}

Expand Down
2 changes: 2 additions & 0 deletions src/account/types/index.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export interface UniversalDetails {
version?: BigNumberish;
resourceBounds?: ResourceBoundsBN; // ignored on estimate
skipValidate?: boolean; // ignored on non-estimate
proofFacts?: BigNumberish[];
proof?: string;
}

export interface PaymasterDetails {
Expand Down
6 changes: 5 additions & 1 deletion src/channel/rpc_0_10_0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from '../types';
import assert from '../utils/assert';
import { ETransactionType, JRPC, RPCSPEC010 as RPC } from '../types/api';
import type { INVOKE_TXN_V3_WITH_PROOF } from '../provider/types/spec.type';
import { BatchClient } from '../utils/batch';
import { CallData } from '../utils/calldata';
import { isSierra } from '../utils/contract';
Expand Down Expand Up @@ -771,11 +772,14 @@ export class RpcChannel {
};

if (invocation.type === ETransactionType.INVOKE) {
const btx: RPC.INVOKE_TXN_V3 = {
const proofFacts = invocation.proofFacts?.map((it) => toHex(it)) ?? [];
const btx: INVOKE_TXN_V3_WITH_PROOF = {
type: RPC.ETransactionType.INVOKE,
sender_address: invocation.contractAddress,
calldata: CallData.toHex(invocation.calldata),
...details,
...(invocation.proofFacts !== undefined && { proof_facts: proofFacts }),
...(invocation.proof !== undefined && { proof: invocation.proof }),
};
return btx as any; // This 'as any' is internal to the generic function - the external API is type-safe
}
Expand Down
6 changes: 5 additions & 1 deletion src/channel/rpc_0_9_0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from '../types';
import assert from '../utils/assert';
import { ETransactionType, JRPC, RPCSPEC09 as RPC } from '../types/api';
import type { INVOKE_TXN_V3_WITH_PROOF_09 } from '../provider/types/spec.type';
import { BatchClient } from '../utils/batch';
import { CallData } from '../utils/calldata';
import { isSierra } from '../utils/contract';
Expand Down Expand Up @@ -773,11 +774,14 @@ export class RpcChannel {
};

if (invocation.type === ETransactionType.INVOKE) {
const btx: RPC.INVOKE_TXN_V3 = {
const proofFacts = invocation.proofFacts?.map((it) => toHex(it)) ?? [];
const btx: INVOKE_TXN_V3_WITH_PROOF_09 = {
type: RPC.ETransactionType.INVOKE,
sender_address: invocation.contractAddress,
calldata: CallData.toHex(invocation.calldata),
...details,
...(invocation.proofFacts !== undefined && { proof_facts: proofFacts }),
...(invocation.proof !== undefined && { proof: invocation.proof }),
};
return btx as any; // This 'as any' is internal to the generic function - the external API is type-safe
}
Expand Down
10 changes: 10 additions & 0 deletions src/provider/types/spec.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,13 @@ export const { ETransactionExecutionStatus: TransactionExecutionStatus } = RPCSP

export type BlockTag = RPCSPEC09.EBlockTag;
export const { EBlockTag: BlockTag } = RPCSPEC09;

// Extended invoke types with proof_facts/proof support (remove once upstream types-js adds the fields)
export type INVOKE_TXN_V3_WITH_PROOF = RPCSPEC010.INVOKE_TXN_V3 & {
proof_facts?: FELT[];
proof?: string;
};
export type INVOKE_TXN_V3_WITH_PROOF_09 = RPCSPEC09.INVOKE_TXN_V3 & {
proof_facts?: RPCSPEC09.FELT[];
proof?: string;
};
2 changes: 2 additions & 0 deletions src/types/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ export type V3TransactionDetails = {
accountDeploymentData: BigNumberish[];
nonceDataAvailabilityMode: EDataAvailabilityMode;
feeDataAvailabilityMode: EDataAvailabilityMode;
proofFacts?: BigNumberish[];
proof?: string;
};

/**
Expand Down
Loading