Starknet Snap API
With the [Starknet Snap] installed, dapps can use the Starknet Snap API to interact with users' Starknet accounts.
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 isfalse
.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
- Request
- Result
await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_createAccount",
"params": {
"addressIndex": 1,
"deploy": true,
"chainId": "0x1"
},
},
},
})
{
"transaction_hash": "0x05a56e2d52c817161883f50c441c3228cfe54d9f84b5b5b8b1c8b8e0e6f7e6d8",
"address": "0xb60e8dd61c5d32be8058bb8eb970870f07233155"
}
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
- Request
- Result
await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_displayPrivateKey",
params: {
userAddress: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
chainId: "0x1"
},
},
},
})
null
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
- Request
- Result
await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_estimateAccountDeployFee",
params: {
addressIndex: 0,
chainId: "0x1"
},
},
},
})
{
"suggestedMaxFee": "1000000000000000",
"overallFee": "900000000000000",
"gasConsumed": "1000000",
"gasPrice": "1000000000",
"unit": "wei",
"includeDeploy": true
}
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
- Request
- Result
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"
},
},
},
})
{
"suggestedMaxFee": "1000000000000000",
"overallFee": "900000000000000",
"gasConsumed": "1000000",
"gasPrice": "1000000000",
"unit": "wei",
"includeDeploy": false
}
starkNet_extractPublicKey
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
- Request
- Result
await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_extractPublicKey",
params: {
userAddress: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
chainId: "0x1"
},
},
},
})
"0x04bfcab3b7ca7e8b3f3b62b2f7f77e9e4b68080bbf8f0f4a1c8f890864d2c7c1d3c45d8b2e3f5f1c27dfeea4c2f5733e90bfc7484e2a690aa9b8ac4559d2e6a8d7"
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
- Request
- Result
await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_getErc20TokenBalance",
params: {
tokenAddress: "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
userAddress: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
chainId: "0x1"
},
},
},
})
"0x3e8"
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
- Request
- Result
await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_getStoredUserAccounts",
params: {
chainId: "0x1"
},
},
},
})
[
{
"address": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
"addressIndex": 0,
"publicKey": "0x04bfcab3b7ca7e8b3f3b62b2f7f77e9e4b68080bbf8f0f4a1c8f890864d2c7c1d3c45d8b2e3f5f1c27dfeea4c2f5733e90bfc7484e2a690aa9b8ac4559d2e6a8d7",
"addressSalt": "0x789...",
"deployTxnHash": "0x05a56e2d52c817161883f50c441c3228cfe54d9f84b5b5b8b1c8b8e0e6f7e6d8",
"derivationPath": "m/44'/9004'/0'/0/0",
"chainId": "0x1"
},
// ... more accounts
]
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 are10
,25
, and50
. The default is10
.txnsInLastNumOfDays
:integer
- (Optional) Number of past days of transaction records to be fetched from Voyager. The default is5
.withDeployTxn
:boolean
- (Optional) Indicates whether to include the deploy transaction of the sender's account contract. The default isfalse
.onlyFromState
:boolean
- (Optional) Indicates whether to only retrieve transaction records that are stored in Snap state, that is, those inRECEIVED
,PENDING
,ACCEPTED_ON_L2
, orREJECTED
state. The default isfalse
.chainId
:string
- (Optional) ID of the target Starknet network. The default is the Starknet Goerli testnet.
Returns
The list of the transaction records.
Example
- Request
- Result
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"
},
},
},
})
[
{
"txnHash": "0x05a56e2d52c817161883f50c441c3228cfe54d9f84b5b5b8b1c8b8e0e6f7e6d8",
"txnType": "invoke",
"senderAddress": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
"contractAddress": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
"contractFuncName": "transfer",
"contractCallData": ["0x789...", "100"],
"status": "ACCEPTED_ON_L2",
"failureReason": null,
"eventIds": ["0xdef..."],
"timestamp": 1234567890,
"chainId": "0x1"
},
// ... more transactions
]
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
- Request
- Result
await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_getTransactionStatus",
params: {
transactionHash: "0x05a56e2d52c817161883f50c441c3228cfe54d9f84b5b5b8b1c8b8e0e6f7e6d8",
chainId: "0x1"
},
},
},
})
"ACCEPTED_ON_L2"
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
- Request
- Result
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"
},
},
},
})
{
"result": ["1000"]
}
starkNet_recoverAccounts
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 is0
.maxScanned
:integer
- (Optional) Maximum number of addresses to scan during the recovery process. The default is1
.maxMissed
:integer
- (Optional) Maximum number of uninitialized addresses hit during the recovery process. The default is1
.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
- Request
- Result
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"
},
},
},
})
[
{
"address": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
"addressIndex": 0,
"publicKey": "0x04bfcab3b7ca7e8b3f3b62b2f7f77e9e4b68080bbf8f0f4a1c8f890864d2c7c1d3c45d8b2e3f5f1c27dfeea4c2f5733e90bfc7484e2a690aa9b8ac4559d2e6a8d7",
"addressSalt": "0x789...",
"deployTxnHash": "0x05a56e2d52c817161883f50c441c3228cfe54d9f84b5b5b8b1c8b8e0e6f7e6d8",
"derivationPath": "m/44'/9004'/0'/0/0",
"chainId": "0x1"
},
// ... more accounts
]
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
- Request
- Result
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"
},
},
},
})
{
"transaction_hash": "0x05a56e2d52c817161883f50c441c3228cfe54d9f84b5b5b8b1c8b8e0e6f7e6d8"
}
starkNet_signMessage
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
- Request
- Result
await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_signMessage",
params: {
typedDataMessage: "{ ... }",
signerAddress: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
chainId: "0x1"
},
},
},
})
"1234567890,9876543210"
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
- Request
- Result
await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_upgradeAccContract",
params: {
contractAddress: "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
maxFee: "1000000000000000",
chainId: "0x1"
},
},
},
})
{
"transaction_hash": "0xdef..."
}
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
- Request
- Result
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"
},
},
},
})
true