Implementation
Jumping straight into the code.
Let's open up the App.tsx file and see what's up.
Ignore the UI components defined below the App component, those are not relevant to the functionality of the workshop.
To be fair, you can ignore most of the code in this file since it's primarily UI code for us to track the states,
but the App component is where the logic happens.
Specifically, in these three functions:
As you might have guessed by the name of these functions, they are going to be used to:
- Sign an authorization to use the source code of our delegate contract.
- Remove an authorization signed by the embedded wallet.
- Execute a batch transaction that would
- Mint 100 MNT tokens to the embedded wallet.
- Send 10 MNT to the mock account.
Let's see how we can implement these functions.
1. onAuthorize
Quick brief:
- Getting the nonce is optional, but may be useful to understand.
- Hashing the authorization is also optional, but might be useful for debugging.
- Signing and storing the authorization is the most important part, without it the relay client won't be able call functions on our embedded wallet.
Note: the signAuthorization function is a helper function from Privy that allows to sign the authorization using the embedded wallet.
Also, obtained through the useSignAuthorization hook.
2. onExecute
Looks like a lot, but the majority of the code is optional. Here's the important stuff:
- We're using our
relayClientto call theexecutefunction on the embedded wallet. - During the tx, the embedded wallet is "enhanced" with the source code of the
DelegateContract, since we're specified theauthorizationListparameter. - The sequence of execution is as follows:
- The relay client will call the
executefunction on the embedded wallet. - The embedded wallet calls the
mintfunction on the MNT contract. - The embedded wallet calls the
transferfunction on the MNT contract.
- The relay client will call the
- All of this is also done gaslessly, since the gas is covered by the relayer (Abstracted for the end user)
3. onUnauthorize
Quick rundown:
- We're signing an authorization using the embedded wallet.
- We're sending a redundant tx to remove the source code of the embedded wallet immidiately.
Note: we're setting the address of the source code contract to be the zeroAddress,
which basically means removing the source code.
Also, the funny part is that for the modification to be reflected immidiately,
we have to send a redundant tx with the authorizationList parameter set to the signed authorization.
For the full implementation, check out the demo repo.