Skip to main content

Starknet Snap API

With the [Starknet Snap] installed, dapps can use the Starknet Snap API to interact with users' Starknet accounts.

note

You can also communicate with the Starknet network using the Starknet API.

starkNet_createAccount

Deploys an account contract.

Parameters

  • addressIndex: integer - (Optional) Specific address index of the derived key in BIP-44.
  • deploy: boolean - (Optional) Indicate whether to include send the deploy transaction for the account contract. The default is false.
  • chainId: string - (Optional) ID of the target Starknet network. The default is the Starknet Goerli testnet.

Returns

The response from Starknet's gateway/add_transaction API endpoint.

Example

await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_createAccount",
"params": {
"addressIndex": 1,
"deploy": true,
"chainId": "0x1"
},
},
},
})

starkNet_displayPrivateKey

Extracts the private key from the deployed Starknet account and displays it in MetaMask.

Parameters

  • userAddress: string - Address of the account contract.
  • chainId: string - (Optional) ID of the target Starknet network. The default is the Starknet Goerli testnet.

Returns

Always returns null for security reasons. The private key is only shown in the MetaMask pop-up window.

Example

await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_displayPrivateKey",
params: {
userAddress: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
chainId: "0x1"
},
},
},
})

starkNet_estimateAccountDeployFee

Gets the estimated gas fee for deploying an account contract.

Parameters

  • addressIndex: integer - (Optional) Specific address index of the derived key in BIP-44.
  • chainId: string - (Optional) ID of the target Starknet network. The default is the Starknet Goerli testnet.

Returns

The response from Starknet's feeder_gateway/estimate_fee API endpoint.

Example

await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_estimateAccountDeployFee",
params: {
addressIndex: 0,
chainId: "0x1"
},
},
},
})

starkNet_estimateFee

Gets the estimated gas fee for calling a method on any contract.

Parameters

  • contractAddress: string - Address of the target contract.
  • contractFuncName: string - Target function name of the contract.
  • contractCallData: string - (Optional) Call data for the target function with , as a separator.
  • senderAddress: string - Address of the sender.
  • chainId: string - (Optional) ID of the target Starknet network. The default is the Starknet Goerli testnet.

Returns

The response from Starknet's feeder_gateway/estimate_fee API endpoint.

Example

await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_estimateFee",
params: {
contractAddress: "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
contractFuncName: "transfer",
contractCallData: "0x456...,0x789...,100",
senderAddress: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
chainId: "0x1"
},
},
},
})

starkNet_extractPublicKey

note

This method is integrated into get-starknet.

Extracts the public key from a Starknet account address.

Parameters

  • userAddress: string - Address of the account contract.
  • chainId: string - (Optional) ID of the target Starknet network. The default is the Starknet Goerli testnet.

Returns

The public key of the given account address (can be different from the actual signer).

Example

await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_extractPublicKey",
params: {
userAddress: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
chainId: "0x1"
},
},
},
})

starkNet_getErc20TokenBalance

Gets the user's current balance of an ERC-20 token.

Parameters

  • tokenAddress: string - Address of the ERC-20 token contract.
  • userAddress: string - Address of the user account.
  • chainId: string - (Optional) ID of the target Starknet network. The default is the Starknet Goerli testnet.

Returns

The token balance in hexadecimal.

Example

await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_getErc20TokenBalance",
params: {
tokenAddress: "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
userAddress: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
chainId: "0x1"
},
},
},
})

starkNet_getStoredUserAccounts

Gets a list of stored user accounts that are either initialized or initializing.

Parameters

chainId: string - (Optional) ID of the target Starknet network. The default is the Starknet Goerli testnet.

Returns

The list of the stored user accounts.

Example

await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_getStoredUserAccounts",
params: {
chainId: "0x1"
},
},
},
})

starkNet_getTransactions

Gets the transaction records from a sender address.

Parameters

  • senderAddress: string - Address of the sender.
  • contractAddress: string - (Optional) Address of the called contract.
  • pageSize: integer - (Optional) Page size when calling the Voyager get "api/txns" endpoint. Options are 10, 25, and 50. The default is 10.
  • txnsInLastNumOfDays: integer - (Optional) Number of past days of transaction records to be fetched from Voyager. The default is 5.
  • withDeployTxn: boolean - (Optional) Indicates whether to include the deploy transaction of the sender's account contract. The default is false.
  • onlyFromState: boolean - (Optional) Indicates whether to only retrieve transaction records that are stored in Snap state, that is, those in RECEIVED, PENDING, ACCEPTED_ON_L2, or REJECTED state. The default is false.
  • chainId: string - (Optional) ID of the target Starknet network. The default is the Starknet Goerli testnet.

Returns

The list of the transaction records.

Example

await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_getTransactions",
params: {
senderAddress: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
contractAddress: "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
pageSize: 25,
txnsInLastNumOfDays: 7,
withDeployTxn: true,
onlyFromState: false,
chainId: "0x1"
},
},
},
})

starkNet_getTransactionStatus

Gets the status of a transaction.

Parameters

  • transactionHash: string - Hash of the target transaction.
  • chainId: string - (Optional) ID of the target Starknet network. The default is the Starknet Goerli testnet.

Returns

The status of the transaction.

Example

await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_getTransactionStatus",
params: {
transactionHash: "0x05a56e2d52c817161883f50c441c3228cfe54d9f84b5b5b8b1c8b8e0e6f7e6d8",
chainId: "0x1"
},
},
},
})

starkNet_getValue

Calls a VIEW method on any contract.

Parameters

  • contractAddress: string - Address of the target contract.
  • contractFuncName: string - Target function name of the contract.
  • contractCallData: string - (Optional) Call data for the target function with , as a separator.
  • chainId: string - (Optional) ID of the target Starknet network. The default is the Starknet Goerli testnet.

Returns

The response from Starknet's feeder_gateway/call_contract API endpoint.

Example

await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_getValue",
params: {
contractAddress: "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
contractFuncName: "balanceOf",
contractCallData: "0x456...",
chainId: "0x1"
},
},
},
})

starkNet_recoverAccounts

note

This method is integrated into get-starknet.

Recovers deployed user accounts from the seed phrase of MetaMask based on BIP-44.

Parameters

  • startScanIndex: integer - (Optional) Starting address index of the derived key in BIP-44. The default is 0.
  • maxScanned: integer - (Optional) Maximum number of addresses to scan during the recovery process. The default is 1.
  • maxMissed: integer - (Optional) Maximum number of uninitialized addresses hit during the recovery process. The default is 1.
  • chainId: string - (Optional) ID of the target Starknet network. The default is the Starknet Goerli testnet.

Returns

The list of the scanned user accounts during the recovery process.

Example

await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_recoverAccounts",
params: {
startScanIndex: 0,
maxScanned: 5,
maxMissed: 2,
chainId: "0x1"
},
},
},
})

starkNet_sendTransaction

Signs and sends a transaction.

Parameters

  • contractAddress: string - Address of the target contract.
  • contractFuncName: string - Target function name of the contract.
  • contractCallData: string - (Optional) Call data for the target function with , as a separator.
  • senderAddress: string - Address of the sender.
  • maxFee: string - (Optional) Maximum gas fee allowed from the sender. If not specified, the maximum fee is automatically calculated.
  • chainId: string - (Optional) ID of the target Starknet network. The default is the Starknet Goerli testnet.

Returns

The response from Starknet's gateway/add_transaction API endpoint.

Example

await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_sendTransaction",
params: {
contractAddress: "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
contractFuncName: "transfer",
contractCallData: "0x456...,0x789...,100",
senderAddress: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
maxFee: "1000000000000000",
chainId: "0x1"
},
},
},
})

starkNet_signMessage

note

This method is integrated into get-starknet.

Signs a typed data message.

Parameters

  • typedDataMessage: string - JSON representation of the typed data to be signed.
  • signerAddress: string - Address of the signer.
  • chainId: string - (Optional) ID of the target Starknet network. The default is the Starknet Goerli testnet.

Returns

The signed hash of typed data.

Example

await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_signMessage",
params: {
typedDataMessage: "{ ... }",
signerAddress: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
chainId: "0x1"
},
},
},
})

starkNet_upgradeAccContract

Upgrades an account contract.

Parameters

  • contractAddress: string - Address of the target contract.
  • maxFee: string - (Optional) Maximum gas fee allowed from the sender. If not specified, the maximum fee is automatically calculated.
  • chainId: string - (Optional) ID of the target Starknet network. The default is the Starknet Goerli testnet.

Returns

The response from Starknet's gateway/call_contract API endpoint.

Example

await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_upgradeAccContract",
params: {
contractAddress: "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
maxFee: "1000000000000000",
chainId: "0x1"
},
},
},
})

starkNet_verifyMessage

Verifies a signed typed data message.

Parameters

  • typedDataMessage: string - JSON representation of the original typed data message to be verified.
  • signerAddress: string - Address of the signer.
  • signature: string - Signature of the typed data message.
  • chainId: string - (Optional) ID of the target Starknet network. The default is the Starknet Goerli testnet.

Returns

true if the signature is verified, false otherwise.

Example

await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_verifyMessage",
params: {
typedDataMessage: "{ ... }",
signerAddress: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
signature: "1234567890,9876543210",
chainId: "0x1"
},
},
},
})