New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Safe 🛠 Building a Plug with the Safe{Core} Protocol

ETHGlobalWed, Oct 9, 2024, 12:00 AM

Join us for workshop titled, "Safe Building a Plug with the Safe{Core} Protocol" This workshop is part of ETHGlobal's flagship online event: ETHOnline 2023! ETHOnline 2023 has grown into one of the most exciting events on our calendar, anticipated by hackers all over the world. ETHOnline is a three week long online hackathon with over $200,000 USD in prizes as well as a series of curated summits, celebrating some of the most significant happenings & learnings from the past 12 months in web3. ----------------- Follow us: https://twitter.com/ETHGlobal​ Check us out: https://ethglobal.co​m Email us at: hello@ethglobal.com

Transcript

[Music] I'm uh working as the pr engineer at safe and today I want to share how to build a plugin with the safe core protocol so uh you may or may not know what the safe cor protocol is it was uh shared publicly a few months ago many uh new terms and Concepts were introduced like modules manager registry plugin um and now I would like to uh show you a little bit how uh what all of this is and specifically how to build plugins uh that can extend the functionality of uh smart accounts so before going uh deeper I would like to share first some issues we have with modular smart account accounts that uh they will fail if there's no interoperability no Discovery and no security by interoperability it means that currently there's a risk of bendor locking multiple uh projects working on tools and uh extensions for smart accounts that are not compatible creating a fragmented ecosystem also there's duplicate work we are trying to uh fix this issue with the safe core protocol by uh creating a protocol that is account agnostic and will allow to communicate uh the smart accounts with these uh extensions regarding Discovery there's no central place uh these days where uh to discover a smart account extensions so there's an issue for users to discover what kind of extensions they can uh connect or enableing their accounts to expand their functionality but also uh the other way around for the developers of them and regarding security um there's also an issue where users need to trust every single uh extension they want to use so uh to solve also uh Discovery and security we are introducing um Registries that are like created list of uh these components the modules where um the developers can submit their plugins to it and also users via manager contract they can uh trust this registry and then select the the modules or plugins they want to enable in their accounts so uh the estate of the safe C protocol today it is in still in Alpha version it's ready for hacking and playing around with the but uh not ready for use in production so expect changes and things to break it is designed to be a account agnostic but uh first we are uh focusing on the save specifically version 1.x to speed up the development it is open source everything is public and you can uh read the contracts and documentation about that and also and currently we are looking for Community feedback and uh now it is the best time to help us evolve this protocol so before uh sharing uh the overview I would recommend to check before um this uh or maybe later uh another Workshop that was uh presented at uh e New York 2023 on on E Global where I was showing uh like a big overview and diagrams more in detail on how the flows on this protocol work right now I will uh cover that quickly and then share some code examples to actually Implement a plugin but uh yeah let's go with the diagrams so uh at safe we have the safe account and then we have different um element that can be plugged into the accounts directly to extend the functionality for example modules are able to initiate transactions transaction guards are able to pass uh and check the transactions that are being executed and act on them maybe reject them if some conditions are not met and then fullback handlers are able to uh add extra functions to the core uh smart account with the SEC core protocol what we are doing is uh removing this uh the connection the direct connection of all these components and just enable a save protocol manager directly into your account this manager would be uh would have access to a registry where multiple uh modules are listed there's there would be an owner of this registry that is creating this list and then also the manager would allow uh to communicate would allow the communication between the accounts and the modules and there are different kind of modules like plugins hooks functional handlers and so on today we are going to be talking about pluggins specifically the ones that are able to initiate transactions that will be sent to the manager and then to the account so uh let's see how to add a plugin to our registry enable a plugin in an account and then how to execute the transaction via a plugin again to get more details on this I recommend checking the other worksh uh so to add a plugin to the registry basically uh we can call the function add module in the registry and just add it to the list this has to be done by the owner of course then if we want to enable a plug-in uh in an account uh if we are user then we would need to execute a transaction that tells the manager to enable a specific plug-in with a specific kind of permission then this manager will check if the plugin is added to the to the registry we ask the plugin what kind of permission uh does it need and then the manager will store in the storage okay this save has uh enabled this manager oh sorry this plugin with this kind of permission finally how are transactions executed via a pluging now here the the flow starts on the plugin Side by calling a function in the plugin let's say exec now the plugin will send this transaction to the manager depending on the kind of permission this plugin has uh one of these two functions can be executed execute root access or execute transaction where we can as the save where this transaction is going to be executed and the transaction itself and this transaction will also have some metadata to give some context to the to the transaction and and tell interfaces uh who is the plugin initiating them and we will see more of this later then the manager when it receives the transaction it will check if the plugin is enabled into the save it will also make sure that the plugin is added to the registry and it can be used correctly then the manager will ask uh the pluging what is the kind of permission it has we'll make sure and we we'll compare with the data that is stored in the manager's Storage storage and if everything is okay the manager will finally H send the transaction from the plugin to the safe so the safe can execute it this was very fast but now we are uh cover the plug-in and the manager uh sites a bit more in detail so basically for all these four components there are interfaces that anybody can Implement and deploy its own manager its own registry today we are going to see our own pling so the interface for a plugin it's uh simple it's small it has four functions name version metadata provider and requires root access that we need to implement and then on the manager side probably we don't need to implement a manager but we will need to call some functions depending on the on the permissions the plugin has we can call execute transaction or execute root access so let's see some code here I have a uh visual studio with some uh some folders some repos the safe C protocol where you can find the official uh implementations of the registry and the manager also check some interfaces uh then in the specs you can see some documentation and diagrams and flows and uh now we will work with the safe card protocol demo this folder has two projects inside one is uh web and the other is contract the web is a save app that we can see here this is the save web app I just created a save on girly and then if we go to app I added a custom app that is called save C protocol demo and this one it shows a registry in gy with multiple plugins that are listed there so we are going to add one here and enable it in our safe so that is the web and uh let's open the the contracts here in the contracts this is a hard heart repository with some examples that we can see here um well I have some tabs open also there are some useful scripts to deploy the plugin we Implement also to add it to our registry here we will be using a registry that is a test registry so anybody can add anything there just for testing purposes so let's start with U the different uh PLS this one let's create a very very simple base T plugin if you remember there's an interface that needs to be implemented the iafe protocol planning where we have a name a version requires root access it can be true or false and then the metadata provider function also if we want to execute transactions depending on the permissions the the plugin has we can have let's say a t Plugin or a test plugin with root access very very quickly root access means uh that the the kind of means uh that transactions for example if a plugin has root access it can execute delegate calls and also transactions where the destination is the safe or the manager so kind of admin transactions can be executed because if the destination is the safe or the manager uh maybe the signers of the safe can be uh can be replaced or maybe uh new plugins can be enabled or disabled that is kind of admin permission uh anything else is just not root access so with a with a plugin that has not Ro access we can Implement a function uh yes a function that is called execute from plugin for example that receives the manager where we are going to send the our our transaction we need to specify the save that will execute it and also the transaction that we uh that the save will execute and then this function can call the manager specifically the function execute transaction and pass the save and the transaction if we are talking about a plugin with root access then this is very similar we just need to make sure that the variable required root access is true and instead of calling the method execute transaction we can call the one called uh execute root access so now we can see uh talking about metadata because here when we send a transaction this safe root access transaction uh has some metadata not just the transaction itself this metadata can be something similar ilar to this like a name of the plugin the version if it requires shot access or not but also different things like a an app URL where there can be a an interface deployed that is able to interact with the with the plugin if there are some things to be done there on the interface maybe an icon for the URL or whatever now uh if we see a very basic implementation of that uh of this abstract contract this is an abstract contract as well but it has a con a Constructor where we can pass some metadata and we will start here uh some things that are required by the interface like the name version and requires sh access and also with the metadata we are going to encode it calculate The Meta data hash and store it there for later now this metadata can be handled in different ways there is a a an an where we can uh specify where this metadata will be uh stored for example maybe in ipfs or maybe in a URL in an API or maybe in the contract itself or maybe we can emit an event from the plin that has the metadata and that we can check later in the future so let's see uh these two last ones the contract and the event so here for example uh if we have a base planning with a stored metadata that is also a base plugin we will have a Constructor where we are passing the metadata and encoding it but also the interface requires to implement the metadata a provider function that Returns the provider type and the location the provider type will specify how this metadata is uh handled in the plugin in this case it says contract and then the location where to find it in this cases in this contract in the plug-in so the interface will read this information will know it is a story the contract in this contract and then it can call the retrieve metadata pass the metadata hash and uh receive the encoded metadata in return so now the uh interface will be will be able to display here like the name the interface and also a link pointing to the uh interface where the where we can interact with the planning in case we are uh using for example the events metadata we can implement this similarly but differently we can have a Constructor where we are passing the metadata and here in the Constructor we can emit the metadata event pass the metadata hash and the metadata and coded so later if the uh interface calls the metadata provider function it will receive that the provider type is an event it will know that the event was triggered by this uh contract meaning that the interface will know how to access that metadata let's see now a plugin that uh uses the event metadata for example an allow list plugin what is that what it does um is not relevant not very relevant for us right now but basically allows um let's say we have a save with three owners and then uh we want to interact with um with Unis swap smart contract to swap two tokens but we want to allow this owners to execute this transaction independently without the need of collecting the signatures from the other signers then these owners or let's say the the save would call the method add to allow list or remov from allow list and there they can at uh some destinations where these owners if they use the plugin they could interact with directly in this case as safe could add the Unis swap smart contract to do the swapping so now uh this contract is a base pluging with events metadata this means that in the Constructor uh the plugin metadata is emitted and it has a name it has a version uh it defines it requires root access or not and then it has some other properties let's add here some uh E online allow list luggin for example in version one uh and for for the app URL let's say HTTP example.com here ideally ideally no is mandatory we will have the URL where we can interact with this uh with this plugin but now uh that we have this plugin implemented well maybe it's interesting how the execution works here it basically uh this this method uh this function execute from plugin can be called from an owner of the safe passing the managers the save and the transaction as we saw before in the interface it will check that it only it can only be uh called by an owner and then it will check uh because we can pass an array of transactions it will check that the destinations of these transactions are added to the allow list and finally it will call the manager execute transaction because it does not have root access passing the save and the transaction with the knowns and the uh metadata so now let's uh deploy this contract on gy we need to install the project we need to build it as well okay and now we can deploy here Deploy on girly okay this will deploy the new plugin in gley we will see the address in a bit okay here it is now that it's deployed we want to add it to the registry in the real world this would be more bureaucratic talking with the owner of the of the registry meeting the security requirements like Audi need and one here in the test world we just need to yarn there's a script here in this project to add it to the test registry register plugin on girly as well and now if we go to the interface here we see that the pluging is not added yet now uh we see that the registry address is this one the plugin we are adding to the registry has this name it's online allow list lugging in this address and if we refresh the the registry here in the end we will see that there is a new uh a new plugin added to the registry with the name with the version and with our URL that was example.com but can be or must be a proper one so we can enable it in the save and if we see this generates a bat transaction with two options the first one is enabling a module this is uh telling the the safe account this one to enable a module this this address is the manager that we are enabling in the account and then we are enabling a plug so we call the the manager the ABD which is the same and we are telling uh to enable this plugin the one we just deployed with no root access so we can execute it and when this is ready in the settings we will see that now uh the safe has a module enabled okay transaction successful we can go to settings modules and now we see that this is the manager now that we have the manager enabled and also the the plugin enabled we could use the plugin we can also enable other plugins like this one for example the relay plugin we see that now there's only one transaction to uh call in the the manager the IBD uh enabling this plugin with no root access that we can execute so now we can add all the we can enable in our account all the plugins we see in the registry that we like also disable them of course now transaction was successful and here in the registry uh we need to reload it we see that the relay pluging now offers us the option to disable but we don't want to disable we want to use it so this uh plug-in one of the metadata properties for the interface is actually pointing to an existing interface where we need one transaction and this plugin will allow us to relay a transaction not just execute it uh with our save but uh a relay will pay the fees for us so we can create a transaction where we are sending some tokens to ourselves but we are not executing this just signing it making it ready so the Relay can later execute it okay now the transaction was added to the queue and now the interface of this uh relay plugin sees that there's a transaction ready to be executed and this interface will allow us to call the plugin and to execute the transaction by relaying it using gelato relay so now we just have to wait a little bit and the transaction will be executed and also the relay plugin will extract some funds uh according to the gas costs from the safe and send them to the uh relay takes a little bit but uh it will go through at some point okay so now the transaction uh was relayed using the the relay plugin so now uh we can see some uh resources uh it's basically the ones uh I just showed the the safe C protocol and the safe C protocol specs in the safe Global organization in gith if you want to check the demo that I used is saf protocol demo in the 58 which is an organization more for experimental things we are working on and then the the UI for the registry uh you can find it uh in the last URL you can uh contact us on Twitter we have some docs Discord a forum thank you very much for attending this Workshop I hope it was useful for

Automatic transcript — names and jargon may be misspelled.