starknet.js icon indicating copy to clipboard operation
starknet.js copied to clipboard

Implement SNIP-9

Open kfastov opened this issue 1 year ago • 10 comments

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, and EOutsideExecutionVersion.
  • Extended the Account class 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() and getSnip9Nonce().
  • 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

kfastov avatar May 01 '24 14:05 kfastov

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

kfastov avatar May 01 '24 14:05 kfastov

thanks!! we will take a deep look!

ivpavici avatar May 02 '24 12:05 ivpavici

We should add documentation for OutsideExecution usage

tabaktoni avatar May 03 '24 10:05 tabaktoni

@kfastov hi! Will you have time to add these couple of things left? 🙏

ivpavici avatar May 17 '24 14:05 ivpavici

@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 avatar May 22 '24 16:05 kfastov

@kfastov hi hi! gentle ping about this PR :)

ivpavici avatar Jun 18 '24 10:06 ivpavici

@ivpavici Thank you for waiting so long 🙏. I'll push the updates tomorrow

kfastov avatar Jun 19 '24 18:06 kfastov

Sorry out of time today, but I'll certainly do it on Friday

kfastov avatar Jun 20 '24 18:06 kfastov

@ivpavici @tabaktoni I've added docs and changed the file structure to match convention. Can you please review?

kfastov avatar Jun 22 '24 18:06 kfastov

@penovicp Can you please check now?

kfastov avatar Jun 28 '24 19:06 kfastov

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.

PhilippeR26 avatar Jul 03 '24 15:07 PhilippeR26

Is your code handling smoothly accounts that are not implementing the ERC165 (introspection)? You should try with this Testnet account class : 0x2ac408bab4a8b1451fd6395fef68be20484002f2c73faa87bd552b610699bfb

PhilippeR26 avatar Jul 04 '24 09:07 PhilippeR26

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 avatar Jul 05 '24 15:07 kfastov

@kfastov Have you finalized your modifications ?

PhilippeR26 avatar Jul 15 '24 18:07 PhilippeR26