diff --git a/src/provider/interface.ts b/src/provider/interface.ts index 7b8067d61..4e4a18471 100644 --- a/src/provider/interface.ts +++ b/src/provider/interface.ts @@ -1,5 +1,4 @@ import { RPC06, RPC07 } from '../channel'; -import { StarknetChainId } from '../constants'; import type { AccountInvocations, BigNumberish, @@ -7,6 +6,7 @@ import type { BlockIdentifier, Call, CallContractResponse, + ChainId, ContractClassResponse, ContractVersion, DeclareContractResponse, @@ -41,7 +41,7 @@ export abstract class ProviderInterface { * * @returns the chain Id */ - public abstract getChainId(): Promise; + public abstract getChainId(): Promise; /** * Calls a function on the Starknet contract. diff --git a/src/types/lib/index.ts b/src/types/lib/index.ts index 69be02c71..f53506ca7 100644 --- a/src/types/lib/index.ts +++ b/src/types/lib/index.ts @@ -1,4 +1,3 @@ -import { StarknetChainId } from '../../constants'; import { weierstrass } from '../../utils/ec'; import { EDataAvailabilityMode, ResourceBounds } from '../api'; import { CairoEnum } from '../cairoEnum'; @@ -159,7 +158,7 @@ export type Details = { nonce: BigNumberish; maxFee: BigNumberish; version: BigNumberish; - chainId: StarknetChainId; + chainId: ChainId; }; export type InvocationsDetailsWithNonce = @@ -313,4 +312,6 @@ export type ContractVersion = { compiler: CompilerVersion; }; +export type ChainId = `0x${string}`; + export * from './contract'; diff --git a/src/types/provider/configuration.ts b/src/types/provider/configuration.ts index e6be05493..0f17337d0 100644 --- a/src/types/provider/configuration.ts +++ b/src/types/provider/configuration.ts @@ -1,5 +1,5 @@ import { NetworkName } from '../../constants'; -import { BlockIdentifier } from '../lib'; +import { BlockIdentifier, ChainId } from '../lib'; export interface ProviderOptions extends RpcProviderOptions {} @@ -21,4 +21,4 @@ export type RpcProviderOptions = { batch?: false | number; }; -export type ChainId = `0x${string}`; +// export type ChainId = `0x${string}`; diff --git a/src/types/signer.ts b/src/types/signer.ts index 80d98a8d6..5f0f25c25 100644 --- a/src/types/signer.ts +++ b/src/types/signer.ts @@ -1,8 +1,8 @@ -import { StarknetChainId } from '../constants'; import { ETransactionVersion, ETransactionVersion2, ETransactionVersion3 } from './api'; import { BigNumberish, CairoVersion, + ChainId, DeployAccountContractPayload, InvocationsDetails, V3TransactionDetails, @@ -16,7 +16,7 @@ export type InvocationsSignerDetails = (V2InvocationsSignerDetails | V3Invocatio export type V2InvocationsSignerDetails = { walletAddress: string; cairoVersion: CairoVersion; - chainId: StarknetChainId; + chainId: ChainId; nonce: BigNumberish; maxFee: BigNumberish; version: `${ETransactionVersion2}`; @@ -25,7 +25,7 @@ export type V2InvocationsSignerDetails = { export type V3InvocationsSignerDetails = V3TransactionDetails & { walletAddress: string; cairoVersion: CairoVersion; - chainId: StarknetChainId; + chainId: ChainId; version: `${ETransactionVersion3}`; }; @@ -37,7 +37,7 @@ export type V2DeclareSignerDetails = Required & { classHash: string; compiledClassHash?: string; senderAddress: string; - chainId: StarknetChainId; + chainId: ChainId; version: `${ETransactionVersion2}`; }; @@ -45,7 +45,7 @@ export type V3DeclareSignerDetails = V3TransactionDetails & { classHash: string; compiledClassHash: string; senderAddress: string; - chainId: StarknetChainId; + chainId: ChainId; version: `${ETransactionVersion3}`; }; @@ -56,13 +56,13 @@ export type DeployAccountSignerDetails = export type V2DeployAccountSignerDetails = Required & Required & { contractAddress: BigNumberish; - chainId: StarknetChainId; + chainId: ChainId; version: `${ETransactionVersion2}`; }; export type V3DeployAccountSignerDetails = Required & V3TransactionDetails & { contractAddress: BigNumberish; - chainId: StarknetChainId; + chainId: ChainId; version: `${ETransactionVersion3}`; }; diff --git a/src/utils/calldata/formatter.ts b/src/utils/calldata/formatter.ts index 5aa3d6a77..87e3f1239 100644 --- a/src/utils/calldata/formatter.ts +++ b/src/utils/calldata/formatter.ts @@ -4,7 +4,7 @@ import { decodeShortString } from '../shortString'; const guard = { /** * Checks if the data is a BigInt (BN) and throws an error if not. - * + * * @param {Record} data - The data object containing the key to check. * @param {Record} type - The type definition object. * @param {string} key - The key in the data object to check. @@ -21,7 +21,7 @@ const guard = { /** * Throws an error for unhandled formatter types. - * + * * @param {Record} data - The data object containing the key. * @param {Record} type - The type definition object. * @param {string} key - The key in the data object to check. @@ -39,21 +39,21 @@ const guard = { * @param {Record} type - The type definition for the data. * @param {any} [sameType] - The same type definition to be used (optional). * @returns {Record} The formatted data. - * + * * @example * // Example 1: Formatting a simple object * const data = { value: '123', name: 'test' }; * const type = { value: 'number', name: 'string' }; * const formatted = formatter(data, type); * // formatted: { value: 123, name: 'test' } - * + * * @example * // Example 2: Formatting an object with nested structures * const data = { user: { id: '123', age: '30' }, active: '1' }; * const type = { user: { id: 'number', age: 'number' }, active: 'number' }; * const formatted = formatter(data, type); * // formatted: { user: { id: 123, age: 30 }, active: 1 } - * + * * @example * // Example 3: Handling arrays in the data object * const data = { items: ['1', '2', '3'], name: 'test' }; diff --git a/src/utils/calldata/tuple.ts b/src/utils/calldata/tuple.ts index a76f53bb1..6949b3601 100644 --- a/src/utils/calldata/tuple.ts +++ b/src/utils/calldata/tuple.ts @@ -106,22 +106,22 @@ function extractCairo1Tuple(type: string): string[] { /** * Convert a tuple string definition into an object-like definition. * Supports both Cairo 0 and Cairo 1 tuple formats. - * + * * @param type - The tuple string definition (e.g., "(u8, u8)" or "(x:u8, y:u8)"). * @returns An array of strings or objects representing the tuple components. - * + * * @example * // Cairo 0 Tuple * const cairo0Tuple = "(u8, u8)"; * const result = extractTupleMemberTypes(cairo0Tuple); * // result: ["u8", "u8"] - * + * * @example * // Named Cairo 0 Tuple * const namedCairo0Tuple = "(x:u8, y:u8)"; * const namedResult = extractTupleMemberTypes(namedCairo0Tuple); * // namedResult: [{ name: "x", type: "u8" }, { name: "y", type: "u8" }] - * + * * @example * // Cairo 1 Tuple * const cairo1Tuple = "(core::result::Result::, u8)"; diff --git a/src/utils/hash/transactionHash/index.ts b/src/utils/hash/transactionHash/index.ts index f0a28d6f4..340e6477e 100644 --- a/src/utils/hash/transactionHash/index.ts +++ b/src/utils/hash/transactionHash/index.ts @@ -2,8 +2,7 @@ * Transaction Hash based on Transaction Version */ -import { StarknetChainId } from '../../../constants'; -import { BigNumberish, Calldata } from '../../../types'; +import { BigNumberish, Calldata, ChainId } from '../../../types'; import { EDAMode, ETransactionVersion, @@ -38,7 +37,7 @@ type CalcV2InvokeTxHashArgs = { version: `${ETransactionVersion2}`; compiledCalldata: Calldata; maxFee: BigNumberish; - chainId: StarknetChainId; + chainId: ChainId; nonce: BigNumberish; }; @@ -46,7 +45,7 @@ type CalcV3InvokeTxHashArgs = { senderAddress: BigNumberish; version: `${ETransactionVersion3}`; compiledCalldata: Calldata; - chainId: StarknetChainId; + chainId: ChainId; nonce: BigNumberish; accountDeploymentData: BigNumberish[]; nonceDataAvailabilityMode: EDAMode; @@ -96,7 +95,7 @@ type CalcV2DeclareTxHashArgs = { senderAddress: BigNumberish; version: `${ETransactionVersion2}`; maxFee: BigNumberish; - chainId: StarknetChainId; + chainId: ChainId; nonce: BigNumberish; compiledClassHash?: string; }; @@ -106,7 +105,7 @@ type CalcV3DeclareTxHashArgs = { compiledClassHash: string; senderAddress: BigNumberish; version: `${ETransactionVersion3}`; - chainId: StarknetChainId; + chainId: ChainId; nonce: BigNumberish; accountDeploymentData: BigNumberish[]; nonceDataAvailabilityMode: EDAMode; @@ -164,7 +163,7 @@ type CalcV2DeployAccountTxHashArgs = { salt: BigNumberish; version: `${ETransactionVersion2}`; maxFee: BigNumberish; - chainId: StarknetChainId; + chainId: ChainId; nonce: BigNumberish; }; @@ -174,7 +173,7 @@ type CalcV3DeployAccountTxHashArgs = { compiledConstructorCalldata: Calldata; salt: BigNumberish; version: `${ETransactionVersion3}`; - chainId: StarknetChainId; + chainId: ChainId; nonce: BigNumberish; nonceDataAvailabilityMode: EDAMode; feeDataAvailabilityMode: EDAMode; diff --git a/src/utils/hash/transactionHash/v2.ts b/src/utils/hash/transactionHash/v2.ts index 1ba81088c..bb0aa6651 100644 --- a/src/utils/hash/transactionHash/v2.ts +++ b/src/utils/hash/transactionHash/v2.ts @@ -5,7 +5,7 @@ /* eslint-disable no-param-reassign */ /* eslint-disable import/extensions */ import { StarknetChainId, TransactionHashPrefix } from '../../../constants'; -import { BigNumberish, RawCalldata } from '../../../types'; +import { BigNumberish, ChainId, RawCalldata } from '../../../types'; import { starkCurve } from '../../ec'; import { toBigInt } from '../../num'; import { getSelector } from '../selector'; @@ -33,7 +33,7 @@ export function calculateTransactionHashCommon( entryPointSelector: BigNumberish, calldata: RawCalldata, maxFee: BigNumberish, - chainId: StarknetChainId, + chainId: ChainId, additionalData: BigNumberish[] = [] ): string { const calldataHash = computeHashOnElements(calldata); @@ -61,7 +61,7 @@ export function calculateDeclareTransactionHash( senderAddress: BigNumberish, version: BigNumberish, maxFee: BigNumberish, - chainId: StarknetChainId, + chainId: ChainId, nonce: BigNumberish, compiledClassHash?: string ): string { @@ -88,7 +88,7 @@ export function calculateDeployAccountTransactionHash( salt: BigNumberish, version: BigNumberish, maxFee: BigNumberish, - chainId: StarknetChainId, + chainId: ChainId, nonce: BigNumberish ) { const calldata = [classHash, salt, ...constructorCalldata]; @@ -114,7 +114,7 @@ export function calculateTransactionHash( version: BigNumberish, calldata: RawCalldata, maxFee: BigNumberish, - chainId: StarknetChainId, + chainId: ChainId, nonce: BigNumberish ): string { return calculateTransactionHashCommon( diff --git a/src/utils/hash/transactionHash/v3.ts b/src/utils/hash/transactionHash/v3.ts index 7746c67be..a224c6b8a 100644 --- a/src/utils/hash/transactionHash/v3.ts +++ b/src/utils/hash/transactionHash/v3.ts @@ -4,8 +4,8 @@ import { poseidonHashMany } from '@scure/starknet'; -import { StarknetChainId, TransactionHashPrefix } from '../../../constants'; -import { BigNumberish, Calldata } from '../../../types'; +import { TransactionHashPrefix } from '../../../constants'; +import { BigNumberish, Calldata, ChainId } from '../../../types'; import { EDAMode, ResourceBounds } from '../../../types/api'; import { toHex } from '../../num'; import { encodeShortString } from '../../shortString'; @@ -42,7 +42,7 @@ export function calculateTransactionHashCommon( txHashPrefix: TransactionHashPrefix, version: BigNumberish, senderAddress: BigNumberish, - chainId: StarknetChainId, + chainId: ChainId, nonce: BigNumberish, tip: BigNumberish, paymasterData: BigNumberish[], @@ -77,7 +77,7 @@ export function calculateDeployAccountTransactionHash( compiledConstructorCalldata: Calldata, salt: BigNumberish, version: BigNumberish, - chainId: StarknetChainId, + chainId: ChainId, nonce: BigNumberish, nonceDataAvailabilityMode: EDAMode, feeDataAvailabilityMode: EDAMode, @@ -109,7 +109,7 @@ export function calculateDeclareTransactionHash( compiledClassHash: string, senderAddress: BigNumberish, version: BigNumberish, - chainId: StarknetChainId, + chainId: ChainId, nonce: BigNumberish, accountDeploymentData: BigNumberish[], nonceDataAvailabilityMode: EDAMode, @@ -141,7 +141,7 @@ export function calculateInvokeTransactionHash( senderAddress: BigNumberish, version: BigNumberish, compiledCalldata: Calldata, - chainId: StarknetChainId, + chainId: ChainId, nonce: BigNumberish, accountDeploymentData: BigNumberish[], nonceDataAvailabilityMode: EDAMode, diff --git a/src/utils/starknetId.ts b/src/utils/starknetId.ts index 5df3ad48e..c9bc2e7da 100644 --- a/src/utils/starknetId.ts +++ b/src/utils/starknetId.ts @@ -1,5 +1,5 @@ import { StarknetChainId, ZERO } from '../constants'; -import { BigNumberish } from '../types'; +import { BigNumberish, ChainId } from '../types'; import { tuple } from './calldata/cairo'; import { CairoCustomEnum } from './calldata/enum/CairoCustomEnum'; /* eslint-disable no-param-reassign */ @@ -132,7 +132,7 @@ export const StarknetIdContract = { /** * Returns the Starknet ID contract address based on the provided chain ID. * - * @param {StarknetChainId} chainId The chain ID of the Starknet network. + * @param {ChainId} chainId The chain ID of the Starknet network. * @return {string} The Starknet ID contract address. * @throws {Error} Throws an error if the Starknet ID contract is not deployed on the network. * @example @@ -141,7 +141,7 @@ export const StarknetIdContract = { * // result = "0x154bc2e1af9260b9e66af0e9c46fc757ff893b3ff6a85718a810baf1474" * ``` */ -export function getStarknetIdContract(chainId: StarknetChainId): string { +export function getStarknetIdContract(chainId: ChainId): string { switch (chainId) { case StarknetChainId.SN_MAIN: return StarknetIdContract.MAINNET; @@ -162,7 +162,7 @@ export const StarknetIdIdentityContract = { /** * Returns the Starknet ID identity contract address for the given chain ID. * - * @param {StarknetChainId} chainId The chain ID for the specified network. + * @param {ChainId} chainId The chain ID for the specified network. * * @return {string} The Starknet ID identity contract address for the specified network. * @@ -173,7 +173,7 @@ export const StarknetIdIdentityContract = { * // result = "0x3697660a0981d734780731949ecb2b4a38d6a58fc41629ed611e8defda" * ``` */ -export function getStarknetIdIdentityContract(chainId: StarknetChainId): string { +export function getStarknetIdIdentityContract(chainId: ChainId): string { switch (chainId) { case StarknetChainId.SN_MAIN: return StarknetIdIdentityContract.MAINNET; @@ -192,7 +192,7 @@ export const StarknetIdMulticallContract = /** * Returns the Starknet.id multicall contract address based on the provided chainId. * - * @param {StarknetChainId} chainId - The chainId of the network. + * @param {ChainId} chainId - The chainId of the network. * @return {string} - The address of the Starknet.id multicall contract. * @throws {Error} - If the Starknet.id multicall contract is not deployed on the network. * @example @@ -201,7 +201,7 @@ export const StarknetIdMulticallContract = * // result = "0x034ffb8f4452df7a613a0210824d6414dbadcddce6c6e19bf4ddc9e22ce5f970" * ``` */ -export function getStarknetIdMulticallContract(chainId: StarknetChainId): string { +export function getStarknetIdMulticallContract(chainId: ChainId): string { switch (chainId) { case StarknetChainId.SN_MAIN: return StarknetIdMulticallContract; @@ -222,7 +222,7 @@ export const StarknetIdVerifierContract = { /** * Returns the address of the Starknet ID Verifier contract based on the specified chain ID. * - * @param {StarknetChainId} chainId - The ID of the Starknet chain. + * @param {ChainId} chainId - The ID of the Starknet chain. * @return {string} - The address of the Starknet ID Verifier contract. * @throws {Error} - If the Starknet ID Verifier contract is not deployed on the specified network. * @example @@ -231,7 +231,7 @@ export const StarknetIdVerifierContract = { * // result = "0x60B94fEDe525f815AE5E8377A463e121C787cCCf3a36358Aa9B18c12c4D566" * ``` */ -export function getStarknetIdVerifierContract(chainId: StarknetChainId): string { +export function getStarknetIdVerifierContract(chainId: ChainId): string { switch (chainId) { case StarknetChainId.SN_MAIN: return StarknetIdVerifierContract.MAINNET; @@ -252,7 +252,7 @@ export const StarknetIdPfpContract = { /** * Retrieves the contract address of the Starknet.id profile picture verifier contract based on the given chain ID. * - * @param {StarknetChainId} chainId - The chain ID of the network. + * @param {ChainId} chainId - The chain ID of the network. * @returns {string} - The contract address of the Starknet.id profile picture verifier contract. * @throws {Error} - Throws an error if the Starknet.id profile picture verifier contract is not yet deployed on the network. * @example @@ -261,7 +261,7 @@ export const StarknetIdPfpContract = { * // result = "0x9e7bdb8dabd02ea8cfc23b1d1c5278e46490f193f87516ed5ff2dfec02" * ``` */ -export function getStarknetIdPfpContract(chainId: StarknetChainId): string { +export function getStarknetIdPfpContract(chainId: ChainId): string { switch (chainId) { case StarknetChainId.SN_MAIN: return StarknetIdPfpContract.MAINNET; @@ -284,7 +284,7 @@ export const StarknetIdPopContract = { /** * Retrieves the Starknet ID Proof of Personhood (IdPop) verifier contract address for the given chain ID. * - * @param {StarknetChainId} chainId - The chain ID of the Starknet network. + * @param {ChainId} chainId - The chain ID of the Starknet network. * @return {string} - The Starknet ID Pop contract address. * @throws {Error} - If the Starknet ID Pop contract is not deployed on the specified network. * @example @@ -293,7 +293,7 @@ export const StarknetIdPopContract = { * // result = "0x15ae88ae054caa74090b89025c1595683f12edf7a4ed2ad0274de3e1d4a" * ``` */ -export function getStarknetIdPopContract(chainId: StarknetChainId): string { +export function getStarknetIdPopContract(chainId: ChainId): string { switch (chainId) { case StarknetChainId.SN_MAIN: return StarknetIdPopContract.MAINNET; diff --git a/src/wallet/account.ts b/src/wallet/account.ts index 8a1bccffc..12a8ae4ef 100644 --- a/src/wallet/account.ts +++ b/src/wallet/account.ts @@ -11,6 +11,7 @@ import { AllowArray, CairoVersion, Call, + ChainId, CompiledSierra, DeclareContractPayload, MultiDeployContractResponse, @@ -34,7 +35,6 @@ import { watchAsset, } from './connect'; import { StarknetWalletProvider } from './types'; -import { StarknetChainId } from '../constants'; // Represent 'Selected Active' Account inside Connected Wallet export class WalletAccount extends Account implements AccountInterface { @@ -61,7 +61,7 @@ export class WalletAccount extends Account implements AccountInterface { if (!res) return; // Determine is it better to set chainId or replace channel with new one // At the moment channel is stateless but it could change - this.channel.setChainId(res as StarknetChainId); + this.channel.setChainId(res as ChainId); }); // Get and Set Address !!! Post constructor initial empty string @@ -99,7 +99,7 @@ export class WalletAccount extends Account implements AccountInterface { return getPermissions(this.walletProvider); } - public switchStarknetChain(chainId: StarknetChainId) { + public switchStarknetChain(chainId: ChainId) { return switchStarknetChain(this.walletProvider, chainId); }