Support Web3 wallets in StarkNet
Goal
Support Web3 wallets in StarkNet (SN) without applying any changes to the wallets themselves. The wallet will be configured with a custom RPC endpoint and work as usual without being aware of SN.
What are the gaps today?
-
Signature - Today SN only supports a native signature while Web3 wallets are signing using an Ethereum signature (SECP256k1 curve).
-
EOA address - Ethereum uses an EOA address while SN uses account abstraction and the address is calculated as follows:
pedersen(PREFIX, caller_address, salt, contract_hash, ctor_arguments)
-
API - Even though SN API tries to follow Ethereum’s API, the transaction structure is not exactly the same.
Proposed Solution
High-Level Overview
The signature gap will be solved internally by StarkWare; the ability to verify Ethereum signatures is already in the work and should be available soon. This will enable the account contract to verify the Ethereum signature (however, it will be less efficient than the native signature).
For the address and API gaps, we propose to add an adapter between the wallet and SN’s sequencer. This adapter will have two roles:
- Translate the Ethereum transaction format to a format corresponding to SN API
- Calculate the SN account contract address from the available data in the transaction - the Ethereum signature.
In addition, a web3 wallet user will have a new type of an account contract that can verify Ethereum signatures and parse the messages that are constructed by the adapter (the beauty of account abstraction ).
Based on those building blocks, we can describe the following flow (see the figure below):
- The Application sends the transaction to be signed to the wallet.
- The wallet signs the transaction with the user’s Ethereum key and sends the signed transaction to the SN custom RPC node.
- The RPC node contains the adapter, which reformats the transaction fields and calculates the SN account address and then sends it to the SN’s sequencer to be executed.
- The account contract gets the transaction, verifies that the Ethereum signature is valid against the user’s Ethereum address, which is stored in the contract. Eventually, it activates the execution of the transaction in the target contract.
How to Adapt the Transaction?
A transaction submitted to a web3 wallet will be encoded as follows:
From
- contains the user’s Ethereum addressTo
- is not used since its size is only 20 bytes and SN addresses are 31 bytesData
- contains the target contract address, the target contract selector and the call data of the transaction- Gas, nonce, and value fields are not used
For example, here is a web3 transaction:
{
from: "0x112233...4455",//20 bytes - the ethereum address of the user
to: "", //the target account address is provided in the call data
data : "0x54321 98765 12345 13579 100" ,
gasLimit: "0",
maxFeePerGas: "0",
maxPriorityFeePerGas: "0",
nonce: "0",
value: "0",
R: ”r”,
S: “s”,
V: “v”,
}
This Web3 transaction is adapted to the following SN transaction format:
{
contract_address: “0x12345” , // The SN account address
entry_point_selecter: “0x67891”,
call_data: "0x54321 98765 12345 13579 100" ,
signature: [r, s, v]
}
How to Calculate the SN Address from the Ethereum Address?
The contract_address field is computed as follows:
contract_address:= pedersen(PREFIX, caller_address, salt,contract_hash, ctor_arguments)
where:
PREFIX="SN_CONTRACT_ADDRESS"
caller_address=0
salt
=pre define constant
contract_hash
= hash over a known contract byte code, configured once in the proxy
ctor_arguments
= Ethereum address = keccak(ecrecover(msg_hash, v, r,s))[12:32]
More Wallet Functionalities to Support
Get Balance
- Problem: When you configure a new ERC-20 in your wallet you need to provide a 20-byte address
- Solution: Store a mapping of ERC-20 Ethereum addresses to SN addresses
- Con: Users won’t be able to add custom ERC-20 tokens, only “whitelisted” tokens are mapped
Send (ETH/ERC-20)
- Problem:
To
address field is only 20 bytes - Solutions: The
To
field will provide the Ethereum address of the receiver. The adapter will translate theTo
field from an Ethereum address to a SN address as described above.- Con: It is a bit confusing for the user to send the funds to an Ethereum address, which is not the real address where the funds will be received.
The diagram below summarizes both the Send Transaction and Get Balance flows in the adapter: