Getting Started with StarkNet Accounts with Ben Goebel

ETH Seoul 2022 · August 2022 · 24:32

Ben Goebel of StarkWare introduces account abstraction on Starknet, where every account is simply a smart contract with an execute entry point. The workshop walks through a minimal Cairo account, why Starknet offers account abstraction natively, and what it enables: alternative signature schemes, upgradeability, social recovery, session keys for gaming, multisigs, multicall and paymasters. It covers signing with the Pedersen hash and the Stark curve, nonces and the OpenZeppelin reference contracts, then closes with a hands-on exercise and learning resources such as Starklings and Awesome Starknet.

Speakers

Topics

From ETH Seoul 2022, hosted by Kryptoplanet.

Transcript

yeah some some whistles I hope I hope I didn't butcher that too bad okay okay um I'm Ben from starkware and today I'm going to be talking about starknet account abstraction okay so account abstraction on Stark net um there's there's some there's some caveats here but for the most part accounts on Stark Nets on Stark net are just smart contracts so the more we dig in today and and like Erica said we're going to get a little Technical and it is going to be a little bit of a workshop but for the most part accounts on starknet are simply smart contracts

so that's the model I want you to have in your head as we go through this talk um one of the caveats and there are just a few is that the entry point selector on the smart contract needs to have a function denoted with an execute function with the underscores in front and the underscores and back I'll show you a little bit why but by and large to keep a simple framework in your mind accounts on starknet are simply smart contracts so let's briefly look at some Cairo code uh just out of curiosity raise your hand and if you're a developer how many developers do we have in here okay cool and how many people have written a line of Cairo nice okay so for a lot of you this will be the first time looking at Cairo and it might be a little difficult to parse

but I'll just point you to line 11. that's a external function that's called by other smart contracts and like I said it has the canonical entry point of execute so double underscores in front double underscores and back and that notifies the starknet OS that this is in fact an account the other line I want to point you to is line 14. so what is the purpose of an abstracted account contract it's really so that we can call arbitrary functions on other smart contracts on the composable network but know it's from a known party and that includes a signature so we'll go over signatures in a little bit but on line 14 we're basically calling another contract from this contract and passing

along the um call data which is an array so don't worry if you can't read that but the most important parts are the execute function name and the call contract syscall so besides the starknet OS there's one other piece of tooling that needs to be aware that this is an account and that's the compiler so when we compile this account uh this account contract we need to pass the flag for uh account contract so a normal a normal um starknet smart contract will not be compiled with this account contract flag okay so you've seen the the basically the simplest account contract you can possibly have but why do we want account

abstraction what what's the purpose of it um one of the benefits of not being the first mover in a smart contract platform is that we get to kind of see the way the the developer Tides ebb and flow and it turns out ethereum actually has a strong push to implement account abstraction so if you want to know a little bit more about that go to eip29 2938 or at EIP 4337 you can see some of the history about account abstraction and attempts to implement account abstraction on ethereum but it definitely seems like devs are clamoring for for account abstraction so uh this is from vitalik the fact that ethereum protocol itself requires everything to be packed packaged in a transaction

originating from an ecdsa secured externally owned account eoa makes account abstraction very difficult so one thing you get if you're a developer coming to Cairo coming to starknet is you get account abstraction right out of the gates but again why do we why do we want it why is this a good thing one thing is better wallet ux so crucial piece of infrastructure wallets um on the right side are a couple of the Wallets on Stark net so Argent X is probably the most the most used right now um braavos is kind of a new new player they're awesome ux I recommend checking them out magnity is one and then Laura from consensus spoke about snaps so there's actually a beta snap that uh is

a POC right now I believe I don't know if Laura's here if she left um it's a in beta and basically metamask will be able to support starknet signatures and accounts so why do we want account abstraction for better wallets so better wallets what does that mean um you can have alternative signature algorithms so um being an eoa controlled by a public private key pair you are regimented into the I think it's scp-256 K1 yeah K1 signature algorithm so if you have account abstraction you can start playing around with other signature schemes really any signature scheme you also can get upgrade ability this is something that Argent X is

already has already integrated so um since it's just an account you can upgrade it like any other account it can have a proxy in the back end and you can Implement New Logic social recovery key management a lot of the ethereum communities I know vitalik talks highly about social recovery um Argent X specifically has a guardian a guardian setup so if you have maybe a couple close friends you can add as Guardians and they can vouch for your social recovery that's that's just not possible with the eoa right now um session keys for gaming we'll get into session keys for gaming a bit but the concept is basically what is the ux for gameplay

if you have to sign each and every transaction in each and every state transition in your game think about that through metamask you're literally having to open metamask and click sign for every single transaction as they propagate through your game so there's a concept of session Keys which basically is one authorized key that will give you a certain amount of authorized access for assigning transactions over a certain amount of time multi-cigs we we all basically know what multi-cigs are but multi-cigs become really easy in a account abstraction model we'll look we'll look at that um and pay masters so on starknet anytime an invocation transaction is called there needs to be

a fee paid and what what is that fee that fee is the L1 footprint of that transaction so how much um and then also how much compute it was to execute the transaction so pay masters you can think about architectures in which a third-party account or a third-party contract pays the fee for the invocation transaction there's lots of different use cases where that that might be helpful so payment abstraction not only can we go with different models for for transaction payments the in the encode Club hackathon in Paris there's actually an nft team who wanted to basically have an nft with all of the nft functions being paid for by a larger account

abstraction account um transaction payment made by the third party so we talked about that and then wallet to abstract the fee token so at the base layer of the protocol the token that will be paid in fees will be the Stark token but if that is not the most user friendly for certain D5 applications you can probably imagine um the account the account abstraction can basically abstract away which fee is being paid at the at that call level um so so really what it is is flexibility for the developers we're not we're not um boxing in any of the Developers with all of that flexibility though um we require some sort of

standardization so because you can you can have a account contract that implements arbitrary smart contract logic how do we as the community know which account contract to kind of band around to use um so we need standardization so open Zeppelin has created a slew of Cairo code for us and it's really helpful for things like reference code I recommend you to go look at Cairo contracts Source open Zeppelin account that's the subdirectory tree there you can see how they implement it but why do we need standardization because all of this flexibility basically there needs to be some set of accounts that we all know that we're

we're using some set of call data layout that we know that we're passing some sort of Base signature implementation that we know we're signing with and then the last reason why we're implementing account abstraction is to empower the devs and this is really the most important thing we're dealing with a design space at this layer two level and eventually layer three level I hope you guys got to hear uh Ellie's talk this morning but some pretty incredible stuff happening in layer three empowered by recursion which went live today you guys got the alpha um so it's just a completely new design space so why would we limit the subset of designs for wallets for all these

things to just a single public private key pair um and a lot of the things we do at starkware is to empower the devs so one of the things that got me into the starkware ecosystem is the optionality that it gives you know you can start playing around with volitions validiums and now you have Smart con you can uh you can play with account abstraction you can play with Stark X Stark net um really if you have something in mind you can basically implement it in whatever way you want on starknap or circus so how to implement account abstraction quite a few of your devs so if you want to follow along you should be able to but we're going to look into some code here

signing so how do we sign and where do we send the signature so the basics assigning are um and again I'll preface this by saying this can be implemented in however the developer wants it to be implemented but what I'm going to walk through is the basic implementation and probably what you're going to see as the default implementation in a lot of smart contracts especially those open Zeppelin reference code smart contracts so off chain you're going to Hash the transaction data probably with the Peterson hash the Peterson hash is a stark friendly hash which we use all over the protocol you're going to sign with a private key this is going to be a private key derived from the start curve and that start curve is the basis of

again a lot of the protocol um submit the transaction with the signature in the signature field array which is an array of Felts we don't need to dig too far into Felts but Felts are the base scalar type for uh for Cairo um and then verify with the stored public key of the of that ecdca signing signing pair that you generated for the account um and then on the right side you'll see a pretty stock implementation of a signature verification so when we pass the array of Felts into the smart contract we basically just say is this hash that we generated in the first

bullet point there valid um and then at the the code block below this is how you declare a storage variable in Cairo and starknet um and if you see on line 12 this is completely an implementation detail up to the developer to call this variable public key and store the public key in the in the smart contract storage of the smart contract and then again with the nonce so you are pretty much always going to see an ounce with an account contract so you don't get any duplicate signing but again this is an implementation detail that's up to the developer multi-call so we've seen signatures you can basically

apply that concept of signatures to everything I'm about to talk about the uh even though it's a it's an account contract and a lot of the stuff is is abstracted away you're always going to want to have some signature of some off-chain party with a private key um signifying that they did indeed agree to whatever the terms of that transaction is so multi-call multi-call you can batch contract calls and one invocation you can reduce the number of separate RPC calls and create Atomic invocations one of the cool things about Argent X specifically and a lot of the a lot of the account abstraction models on ethereum is that they actually by default Implement a multi-call and then if you only have one transaction that

you'd like to put on the network um you know it's still just a multi-call of one transaction so think back to the original hello account abstraction the simplest account abstraction we had what was passing in was a call data and we were calling a single address on starknet so if you look at this one online 107 we're passing in an array of arrays of call data so now we can pass in multiple calls for multiple contracts multi-sig we talked about multi-sig briefly um super important to the defy ecosystem you don't want to be trusting uh you don't want to be trusting a contract

with one person managing one private key so we have a share of security across signing entities and a lot of the different configs in the construction of multisig so two of three you know X of x a lot of this implementation is easier with account abstraction on the right is some of these functions are collapsed but it's kind of the the base of what a multi-call looks like and again this is just a few developers have this stubbed out if you have a different idea for a multi-call a different idea for a multi-call multi-sig architecture go build it um so Constructor Constructor is a call once function in Cairo and starknet it's

basically how we we init uh smart contract storage and smart contract configs so this this is an example of a two of three multi-sig and you can see that with the assertion of three owners and three public keys so if you have three uh three owners and three public keys and three signers one of the signers will submit a transaction at least two of the others will confirm it and then we can execute it and again you see the execute canonical entry point there at the bottom which is what you need to be an account contract so example of account abstraction um this was done at our Dev connect

hackathon in Amsterdam by some Community build doors brick and realms and if you think back to uh session keys this is what they they've implemented and again like like Ellie was saying you know the the ethos that we put out as a company is really you know not super heavy on marketing but super heavy on Shippy code that the community has basically inherited that so um we we you know this is a this is a feasibility and something that you can be done with account obstruction and guess what in I think two days this team went out and did it uh so the problem statement how can you play a high

frequency game on chain and the solution they came up with was ephemeral session keys they created a one-time key stored in browser and then for playing stardew Valley um this is the transaction makeup that they they decided to outline they needed um transaction data for the timestamp and for the X and Y coordinate of the plant that they planted in game so you can imagine how cumbersome that looks like if you need to sign every single plant that you plant well they have the one-time key stored in browser and that key has been authorized for a certain amount of time and now the user user signs only one transaction for this for the game

session some they did it and I think it was 24 hours it might have been 48 hours so they have some potential upgrades that they they can make to it a spend limit so so one key might have a certain amount that it's capped to spend function call restrictions so you can imagine a smart smart contract wallet that only allows certain keys to call certain functions you basically get into all these the slew of different configs for what can be whitelisted what can be permissioned uh or you could go with the base base level multi-call and more and so that that's kind of the point I want to drive home is that you know these smart contract account

abstractions enable you to do anything so here's the workshop um we're not able to live code but I I've taken some screenshots so hopefully if you do want to follow along feel free to check out starknet edu on GitHub and then this is the repo for starknet accounts um you should be able to code with but but if not no worries um if you're trying to catch up with this after this Workshop or after this talk there's a readme in starknet accounts that you can go through has all of the same information so you'll be able to hopefully go line by line and see how to run this Workshop so the environment for developing Cairo

looks like this in setup M if you need a python virtual environment and you need to activate it we're going to install the requirements so the requirements for this tutorial are the open Zeppelin Cairo contracts which we talked about a bit it is the Cairo language itself including starknet and the starknet devnet so for devs I recommend this Cadence and you've probably seen this Cadence otherwise uh in other places like like if you're a solidity developer but if you look at the bottom unit tests first then deployed a devnet then deploy to test net then deploy into mainnet so things like tesna and things like

mainnet even though they are fast and even though they are getting faster especially with certain implementations in Rust and certain other scalable scalability techniques we're employing they are still slower than running on your local machine so let's say your solidity Dev and you use ganache that is going to be comparable to the devnet so check out the devnet again it's in the readme but the typical development Cadence I would suggest is unit test devnet test net mainnet um so after we installed the requirements we initialize the devnet and the the line for that is there starknet devnet on Port 5000 with seed zero because we initialized the devnet with a zero seed we get a

deterministic pre-deployed accounts which means that we um know the public private keys and addresses for all of the devnet accounts which means we can start playing around with it the devnet pre-deployed accounts come with a certain amount of ethereum so we can start to pay gas fees with them like I mentioned any invocation transaction any transaction that alters state is going to cost money and it's going to be the L1 footprint of that invocation transaction so we do need that's why we need the seed zero is because we need some gas gas money basically okay so now we deploy the evaluator so

the way that these uh this workshop and actually all of our workshops in starknet edu are set up is there's an evaluator so um you you go out and deploy these incremental steps in this learning so multi-call multi-sig uh account abstraction with a with a secp 256 K1 signature so that's the ethereum signature um and we have an evaluator smart contract that goes through and says yes you did indeed sign a transaction how we specified you to sign it so on devnet this is how it looks you just do python3 evaluator.pi on devnet and if you did the previous

step of knitting the dev nut you should be good to go and you should see some smart contract addresses start populating for the registry erc20 and evaluator if you've done everything correct if you've implemented all of the smart contract logic as we've stated um then you will get paid in the erc20 that you see there which you know is not worth anything but it's fun uh okay so next step is to deploy the contracts so we've deployed the evaluator so we know if you've done the exercise correct or incorrect now we'll deploy the actual exercises so Python 3 hello hello Pi at the very top you'll

see the mission statement for what we're looking for you guys to do uh we're looking for you to deploy an account with a contract with a execute entry point like we've talked about fetch a random storage variable variable from the validator contract and then pass that random storage variable in call data to your account contract um so now we start working through basically all the steps that we talked about why why does the smart contract need to be funded to have gas to to cover your invocation fee how do we call another contract on Smart on starknet um yeah and if everything went right you'll see an emitted event for payday and you should get paid in absolutely worthless

erc20s um again just just a reminder at the bottom unit test devnet test net mainnet this should come as no surprise to developers you know you need to step through some sort of cadence before you feel comfortable deploying to either testnet or mainnet really um yeah one more thing I'll say is that there is a if you get confused about this repo if you are going to go through this accounts contract tutorial there's a branch of this repo called hints all so you know some of the requirements that we're looking for are a little esoteric they're gonna they're some of them require you to implement two lines of smart contract Cairo some

of them require you to to do about you know 30 or 40. um if you're confused if you want to look at some hints go to hints all and it'll show you what uh what a passable contract looks like so here are some useful links there was only a small subset of hands for Cairo so if you want to get started with Cairo check out starknet edu there's a couple other repositories in there for erc20s ERC 721s bridging so we're a layer two Stark net is the layer two um if you need to know how to bridge to and from ethyl one and then starklings I highly recommend starklings if you're getting started with Cairo some of you might be familiar

with rustlings but it's a dynamic test driven way to start learning the language resources so I recommend awesome starknet this is where you're going to find libraries sdks documentation tests uh you know I go there for when I'm basically looking for anything docs to chiralang.org forward slash docs is where the the docs are for Cairo and then docs.starknet.io for starknet specific so when you're writing a when you're writing a smart contract on Stark net you are writing a subset of Cairo so you can actually do a lot more things with raw Cairo and that's why those two documentation sites are are split out

from each other uh Community you know we'd love to have you guys involved we have a we have kind of a budding booming community so community.starknet.io come chat with us and yeah thanks appreciate your time