starknet.js
starknet.js copied to clipboard
Implement SNIP-9
Motivation and Resolution
This PR implements the SNIP-9 (outside execution) standard in starknet.js. SNIP-9 enables protocols to submit transactions on behalf of a user account, as long as they have the relevant signatures. This feature provides flexibility for various use cases such as delayed orders and fee subsidies.
The implementation follows the SNIP-9 specification and introduces new types, utilities, and account methods to support off-chain signatures. It includes the OutsideExecution class, which represents an outside transaction to be executed on behalf of the user account. The Account class has been extended with methods to get the supported SNIP-9 version, check nonce validity, and execute an outside execution.
RPC version (if applicable)
N/A
Usage related changes
- Added support for SNIP-9 outside execution in starknet.js.
- Introduced new types related to SNIP-9, including
OutsideExecution,OutsideCall, andEOutsideExecutionVersion. - Extended the
Accountclass with methods to support SNIP-9:getSnip9Version: Retrieves the supported SNIP-9 version of the account.isValidSnip9Nonce: Checks if a given nonce is valid for the account.executeFromOutside: Executes an outside execution on behalf of the account.
Development related changes
- Added new utility functions for SNIP-9, such as
buildExecuteFromOutsideCallData()andgetSnip9Nonce(). - Updated the documentation to include information about SNIP-9 and the new account methods.
Checklist:
- [x] Performed a self-review of the code
- [x] Rebased to the last commit of the target branch (or merged it into my branch)
- [x] Linked the issues which this PR resolves
- [ ] Documented the changes in code (API docs will be generated automatically)
- [x] Updated the tests
- [x] All tests are passing
Things left:
- I haven't covered the entire flow in the tests yet
- There is a bug in fee estimation, and the current implementation has maxFee hardcoded, which should be changed
- Not every function/method is documented yet
Fixes #948
To explain my choice of hashing algorithms:
In the SNIP-9 specification it's stated that SNIP-12 should be used for hashing. There are 2 revisions of SNIP-12 that use different hashing algorithms. Further in the section "For account builders" it says that version 1 of SNIP-9 implements revision 0 of SNIP-12, and version 2 implements revision 1. Studying the code of actual contracts confirms it. Current Argent account contract implements SNIP-9 v1 with SNIP-12 revision 0, and Braavos implements SNIP-9 v2 with SNIP-12 revision 1. Since Starknet.js itself supports both SNIP-12 revisions, I've added support for both SNIP-9 versions, using version parameter to determine the hash algorithm being used, and auto-determining the version where possible
thanks!! we will take a deep look!
We should add documentation for OutsideExecution usage
@kfastov hi! Will you have time to add these couple of things left? 🙏
@kfastov hi! Will you have time to add these couple of things left? 🙏
Hello! Sorry for being slow, had to make a forced break in development, but now I am here again. Made small fixes and added a test today. Now working on documentation
@kfastov hi hi! gentle ping about this PR :)
@ivpavici Thank you for waiting so long 🙏. I'll push the updates tomorrow
Sorry out of time today, but I'll certainly do it on Friday
@ivpavici @tabaktoni I've added docs and changed the file structure to match convention. Can you please review?
@penovicp Can you please check now?
Hello, I am trying to test your code in the guide. Have you executed it in a node script (importing starknet)? Because on my side, I am not able to run :
import { OutsideExecution, OutsideExecutionOptions } from 'starknet';
In fact it seems that nothing has been exported to the user level.
Is your code handling smoothly accounts that are not implementing the ERC165 (introspection)? You should try with this Testnet account class : 0x2ac408bab4a8b1451fd6395fef68be20484002f2c73faa87bd552b610699bfb
Hello, I am trying to test your code in the guide. Have you executed it in a node script (importing starknet)? Because on my side, I am not able to run :
import { OutsideExecution, OutsideExecutionOptions } from 'starknet';In fact it seems that nothing has been exported to the user level.
Oops, my bad, I'll fix it now
@kfastov Have you finalized your modifications ?