Aspect Case: Enable Session Keys for Your dApp
Mar 2, 2024
Technical
We've implemented an Aspect case — the Session Key Aspect! With it, Artela users can bypass the manual signing process when interacting with specific dApps, using the Session Key to interact automatically and securely!
We've applied it to Artela's DEX and Fully on-chain game applications, achieving one-click trading functionality and enhancing the user experience in high-frequency DeFi transactions!
In this blog, we'll detail how to utilize Aspect's features to implement native extensions for EoA accounts step by step.
What is Session Key?
Session key allows users to generate temporary sub-keys for their EoA wallets, enabling interactions with specific applications during a set timeframe. Normally, interacting with crypto applications involves manual transaction signing via the wallet. However, with a session key, transactions are automatically signed, removing the need for manual wallet signature confirmations.
In high-frequency trading context, this significantly improves the user experience. For instance, in perp markets where high-frequency trading are crucial, the typical wallet signature process can take upwards of twenty seconds, during which prices may significantly change. If the session key allows for transaction signatures within milliseconds of a trade request, it greatly enhances the trading experience.
Similarly, in fully on-chain gaming, every character movement requires a smart contract transaction. Manual signing for each move severely hampers playability. With session keys, a click to move can instantly generate and send a transaction to the blockchain, vastly improving game interaction.
This concept parallels the session idea in Web2, where users log in once and don't repeatedly enter passwords for subsequent actions within the session. Session keys bring this streamlined experience to Web3 applications, enhancing operational efficiency and user satisfaction in environments requiring frequent interactions.
Implemented by Aspect
In typical blockchain systems, the transaction processing flow is fixed and can't be altered by applications. For instance, in Ethereum, transactions are signed by an EoA using a specific signature method bound to the EoA's private key. The signature is then verified during the consensus process before execution.
In Artela, however, applications can modify the transaction execution flow through Aspects. During the transaction verification stage, Artela triggers the execution of Aspects, which can contain custom transaction validation logic, effectively replacing the default verification process.
Thus, on Artela, applications can tailor their transaction execution flows! In scenarios requiring high-frequency trading, an application could implement a session key Aspect. Here, an EoA would register its session key, and the application's Aspect, when verifying transactions, would recognize transactions signed by the session key as valid interactions from the EoA.
Procedure overview
The Session Key Aspect encompasses three main processes: EoA creation of the session key, using it to sign transactions, and the blockchain's verification of the transaction signatures.
Procedure 1. EoA registers session key to Aspect
Users generate a random, temporary key pair and use their EoA to sign a transaction that calls the Aspect. This transaction includes the session key's fundamental information: the session key's address, the address of the contract it can call, the methods of the contract it can invoke, and the key's expiration time. The Aspect will then store this session key information in its state.
Procedure 2. Use session key to sign transaction
The session key can sign transactions in place of the EoA. For transactions signed with a session key, the 'from' value is the address of the EoA, but the signature itself is from the session key.
Procedure 3. Artela verify the transaction
When Artela reaches the tx verify join point, it invokes the Session Key Aspect to perform transaction verification. The Session Key Aspect verifies whether the EoA has registered this session key. If it does, the Aspect continues to verify the legitimacy of the signature and whether the contract and method the session key is attempting to call are permitted.
Aspect Development
Once the process design is complete, development is carried out using the Aspect SDK.
We need to write an Aspect that implements two interfaces: ITransactionVerifier
and IAspectOperation
.
ITransactionVerifier
is an interface for a join point. Implementing this interface means that instead of using the default behavior, Artela will call this interface to complete the transaction verification.IAspectOperation
is a management interface for Aspects. Implementing this interface implies that an EoA can interact with the Aspect similarly to how it would with a smart contract. This interface is used to facilitate users in registering their Session keys.
IAspectOperation Interface
Operation acts as a unified management entry point, taking a byte array as its parameter. We'll design this parameter to have multiple sub-interfaces. The first two bytes of the byte array identify the sub-interface, while the subsequent bytes are the parameters for that sub-interface.
We'll designate 0x0001
as the registration interface and 0x1001
as the query interface.
The implementation of the registerSessionKey
registration interface is as follows:
Decode the byte array to obtain the required session key.
This involves a simple encoding where the first 20 bytes represent the address of the session key, the second set of 20 bytes denote the contract that the session key can call, followed by a variable-length byte array for the list of contract methods the session key can invoke, and finally, the last 8 bytes for the expiration block height of the session key.
Write the session key into the state of the Aspect.
Once written into the state, the session key can be retrieved from the state during the tx verify phase.
ITransactionVerifier Interface
The main steps of implementing this interface are as follows:
Decode the
validationData
of the transaction: This field contains the actual signature information. We need to verify the legitimacy of the transaction based on the information in this field.from
is the address of the EoA.r
,s
,v
are the signature of the session key.
Verify the transaction signature through ecRecover: This step involves verifying the transaction's signature to obtain the actual signer.
Access the original transaction data in the Aspect runtime context by the
sys
API .Use the sys's ecRecover API to verify the signature and obtain the actual signer's address, which is the address of the session key.
Verify the registered session key:
First, obtain the contract address that the current transaction intends to call from the join point's
input
. Then, match the registered session key with the EoA address, contract address, and session key address.If a match is found, it means the EoA has registered a session key authorized to call this contract.
Next, verify whether the method being called by the current transaction is within the session key's authorized access. Finally, access the current consensus block height in the Aspect runtime context and determine if the session key has expired at this height.
4. Return and Notify: After passing the above verifications, return the from value to the base layer, indicating that the transaction has been validated and the from value resolved. At this point, the session key has used its signature to represent the EoA in completing the transaction signing.
Enabling the Aspect for dApps
That concludes the general implementation process for the Session Key!
After deployment, the Aspect doesn't automatically activate. It requires a smart contract to actively bind it, only then will it affect transactions calling that contract. Thus, the Session Key Aspect is a universally applicable module that any smart contract can opt to activate, enabling session key functionality for its users!
Integrate the Session Key Aspect into your contract with these two steps:
Bind your contract to this Aspect. For a binding guide, click here: guide.
Integrate the Aspect's JavaScript client into your dApp's frontend. For a development guide, click here: guide.
For a reference to an integrated Demo project, click here: Session key integration demo.
Experience the Session Key Aspect
We've also deployed a demo project that has integrated the Session Key Aspect, offering a quick experience of interactive-free web3 applications! We plan to promote and deploy this Aspect on Artela Testnet as a public good, so stay tuned!
Visit the repository for detailed implementation details here: https://github.com/artela-network/session-key-aspect.
Experience it through the Demo project here: Demo Project.
If YOU’RE interested in building the next generation of DeFi with Artela (which, if you’re still reading, you totally are), join our Developer Portal, Discord, X, and send us an email at info@artela.network. We can’t wait to work with you!