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: hex normalization in getTransactions #467

Merged
merged 9 commits into from
Dec 18, 2024
15 changes: 9 additions & 6 deletions packages/starknet-snap/src/__tests__/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
TransactionFinalityStatus,
TransactionExecutionStatus,
TransactionType,
validateAndParseAddress,
} from 'starknet';
import { v4 as uuidv4 } from 'uuid';

Expand Down Expand Up @@ -167,9 +168,7 @@ export async function generateAccounts(
0,
);

if (address.length < 66) {
address = address.replace('0x', `0x${'0'.repeat(66 - address.length)}`);
}
address = validateAndParseAddress(address);

accounts.push({
addressSalt: pubKey,
Expand Down Expand Up @@ -421,7 +420,7 @@ export function generateInvokeTransaction({
* @returns A transaction hash.
* */
export function getTransactionHash(base = SixtyThreeHexInBigInt) {
return `0x` + base.toString(16);
return `0x0` + base.toString(16);
}

export function generateTransactionRequests({
Expand Down Expand Up @@ -525,7 +524,9 @@ export function generateStarkScanTransactions({
newTx.sender_address = address;
newTx.account_calls[0].caller_address = address;
newTx.timestamp = transactionStartFrom;
newTx.transaction_hash = `0x${transactionStartFrom.toString(16)}`;
newTx.transaction_hash = validateAndParseAddress(
`0x${transactionStartFrom.toString(16)}`,
);
transactionStartFrom -= timestampReduction;
txs.push(newTx as unknown as StarkScanTransaction);
}
Expand All @@ -536,7 +537,9 @@ export function generateStarkScanTransactions({
account_calls: [...cairo0DeployTx.account_calls],
};
deployTx.contract_address = address;
deployTx.transaction_hash = `0x${transactionStartFrom.toString(16)}`;
deployTx.transaction_hash = validateAndParseAddress(
`0x${transactionStartFrom.toString(16)}`,
);
txs.push(deployTx as unknown as StarkScanTransaction);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { constants, TransactionType } from 'starknet';
import {
TransactionFinalityStatus,
TransactionExecutionStatus,
validateAndParseAddress,
} from 'starknet';
import { assert, enums, number } from 'superstruct';

Expand Down Expand Up @@ -307,7 +308,11 @@ export class TransactionStateManager extends StateManager<Transaction> {
if (dataInState) {
throw new Error(`Transaction already exist`);
}
state.transactions.push(data);
state.transactions.push({
...data,
// safe-guard to ensure the txnHash is converted to the expected format
txnHash: validateAndParseAddress(data.txnHash),
});
});
} catch (error) {
throw new StateManagerError(error.message);
Expand Down
8 changes: 2 additions & 6 deletions packages/starknet-snap/src/utils/starknetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,7 @@ export const getAccContractAddressAndCallData = (publicKey) => {
0,
);

if (address.length < 66) {
address = address.replace('0x', `0x${'0'.repeat(66 - address.length)}`);
}
address = _validateAndParseAddress(address);
return {
address,
callData,
Expand All @@ -733,9 +731,7 @@ export const getAccContractAddressAndCallDataLegacy = (publicKey) => {
callData,
0,
);
if (address.length < 66) {
address = address.replace('0x', `0x${'0'.repeat(66 - address.length)}`);
}
address = _validateAndParseAddress(address);
return {
address,
callData,
Expand Down
72 changes: 72 additions & 0 deletions packages/starknet-snap/src/utils/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,44 @@ describe('newInvokeTransaction', () => {
accountCalls: callsToTranscationAccountCalls(calls),
});
});

it('convert a 62 transaction hash to 66 transaction hash', async () => {
const chainId = constants.StarknetChainId.SN_SEPOLIA;
const [{ address: senderAddress }] = await generateAccounts(chainId, 1);
const { calls } = callsExamples.multipleCalls;
const txnVersion = 1;
const maxFee = '10';

const result = newInvokeTransaction({
// 62 transaction hash
txnHash:
'0xb28a089e7fb83debee4607b6334d687918644796b47d9e9e38ea8213833137',
senderAddress,
chainId,
maxFee,
calls,
txnVersion,
});

expect(result).toStrictEqual({
// 64 transaction hash
txnHash:
'0x00b28a089e7fb83debee4607b6334d687918644796b47d9e9e38ea8213833137',
txnType: TransactionType.INVOKE,
chainId,
senderAddress,
contractAddress: '',
finalityStatus: TransactionFinalityStatus.RECEIVED,
executionStatus: TransactionFinalityStatus.RECEIVED,
failureReason: '',
timestamp: expect.any(Number),
dataVersion: TransactionDataVersion.V2,
version: txnVersion,
maxFee,
actualFee: null,
accountCalls: callsToTranscationAccountCalls(calls),
});
});
});

describe('newDeployTransaction', () => {
Expand Down Expand Up @@ -273,4 +311,38 @@ describe('newDeployTransaction', () => {
accountCalls: null,
});
});

it('convert a 62 transaction hash to 66 transaction hash', async () => {
const chainId = constants.StarknetChainId.SN_SEPOLIA;
const [{ address: senderAddress }] = await generateAccounts(chainId, 1);
const txnVersion = 1;

const result = newDeployTransaction({
// 62 transaction hash
txnHash:
'0xb28a089e7fb83debee4607b6334d687918644796b47d9e9e38ea8213833137',
senderAddress,
chainId,
txnVersion,
});

expect(result).toStrictEqual({
// 64 transaction hash
txnHash:
'0x00b28a089e7fb83debee4607b6334d687918644796b47d9e9e38ea8213833137',
txnType: TransactionType.DEPLOY_ACCOUNT,
chainId,
senderAddress,
contractAddress: senderAddress,
finalityStatus: TransactionFinalityStatus.RECEIVED,
executionStatus: TransactionFinalityStatus.RECEIVED,
failureReason: '',
timestamp: expect.any(Number),
dataVersion: TransactionDataVersion.V2,
version: txnVersion,
maxFee: null,
actualFee: null,
accountCalls: null,
});
});
});
5 changes: 3 additions & 2 deletions packages/starknet-snap/src/utils/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
constants,
TransactionFinalityStatus,
TransactionType,
validateAndParseAddress,
} from 'starknet';

import { FeeToken } from '../types/snapApi';
Expand Down Expand Up @@ -155,7 +156,7 @@ export function newInvokeTransaction({
txnVersion: number;
}): V2Transaction {
return {
txnHash,
txnHash: validateAndParseAddress(txnHash),
txnType: TransactionType.INVOKE,
chainId,
senderAddress,
Expand Down Expand Up @@ -196,7 +197,7 @@ export function newDeployTransaction({
txnVersion: number;
}): V2Transaction {
return {
txnHash,
txnHash: validateAndParseAddress(txnHash),
txnType: TransactionType.DEPLOY_ACCOUNT,
chainId,
senderAddress,
Expand Down
Loading