bcnmy / scw-contracts

SCW contracts for Biconomy Smart Account
MIT License
115 stars 82 forks source link

SMA-392: Session Keys V2 #180

Closed ankurdubey521 closed 7 months ago

ankurdubey521 commented 10 months ago

Introduction

Introduces a new SessionKeyManagerHybrid Module to support Session Keys infrastructure with the following enhancements:

Other notes:

Usage

Definitions

struct SessionData {
    uint48 validUntil;
    uint48 validAfter;
    address sessionValidationModule;
    bytes sessionKeyData;
}

MODE_SESSION_KEY_PRE_ENABLED = 0
MODE_SESSION_KEY_ENABLE_AND_USE = 1

Enabling a Session Manually

/**
 * @dev creates a session for a smart account
 * @param sessionData session data
 */
function enableSession(SessionData calldata sessionData) external;

Disabling a Session Manually

Normally a session will expire automatically based on (validUntil, validAfter), however an option to disable the session manually is available.

/**
 * @notice Explicitly disable a session. Can be useful in situations where a session
 *         needs to be disabled before it expires.
 * @param _sessionDigest digest of session key data
 */
function disableSession(bytes32 _sessionDigest) external;

Single Execute

Enable And Use Session

Prepare a User Operation where op.calldata[0:4] is SmartAccount.execute.selector or SmartAccount.execute_ncC.selector.

Prepare one or more SessionData as described above. Calculate

Prepare bytes sessionEnableData as

 * Offset (in bytes)    | Length (in bytes) | Contents
 * 0x0                  | 0x1               | No of session keys enabled
 * 0x1                  | 0x8 x count       | Chain IDs
 * 0x1 + 0x8 x count    | 0x20 x count      | Session Data Hash

Sign sessionEnableData using EIP1271 to produce sessionEnableSignature

Prepare moduleSignature as

 * Offset (in bytes)    | Length (in bytes) | Contents
 * 0x0                  | 0x1               | MODE_SESSION_KEY_ENABLE_AND_USE
 * 0x1                  | 0x1               | Index of Session Key in Session Enable Data
 * 0x2                  | 0x6               | Valid Until
 * 0x8                  | 0x6               | Valid After
 * 0xe                  | 0x14              | Session Validation Module Address
 * 0x22                 | --                | abi.encode(sessionKeyData, sessionEnableData,
 *                      |                   |   sessionEnableSignature, sessionKeySignature)

Use Pre-Enabled Session

Prepare a User Operation where op.calldata[0:4] is SmartAccount.execute.selector or SmartAccount.execute_ncC.selector.

No sessionEnableData is needed in this case. Simply prepare moduleSignature as

 * Session Data Pre Enabled Signature Layout
 * Offset (in bytes)    | Length (in bytes) | Contents
 * 0x0                  | 0x1               | MODE_SESSION_KEY_PRE_ENABLED
 * 0x1                  | --                | abi.encode(bytes32 sessionDataDigest, sessionKeySignature)

Batch Execute

Prepare a User Operation where op.calldata[0:4] is SmartAccount.executeBatch.selector or SmartAccount.executeBatch_y6U.selector.

Here, for each item of the batch independently follow either the enable-and-use or the pre-enabled flow. The global moduleSignature structure is as follows:

abi.encode(
    bytes[] sessionEnableDataList, 
    bytes[] sessionEnableSignatureList, 
    bytes[] sessionInfo, 
    bytes sessionKeySignature
)

First some invariants:

  1. sessionInfo.length == executeBatch-operations.length
  2. sessionEnableDataList.length == sessionEnableSignatureList.length >= 0

Each item in sessionInfo corresponds to an operation in the batch. If any session wants to leverage the enable-and-use flow, it can refer to one of the enableData-signature pair. If no sessions use these flows, the first two lists are empty.

Session Enable and Signature List

Follows the same structure as described in the Single Execute Section. This is effectively a 2D array of (sessionDataHash, chainID). Assuming that batch operations A, B and C reference sessions SA, SB and SC, assuming the following Session Enable Data:

sessionEnableDataList = [
    SED1: [ (KECCAK(SA), 137), (KECCAK(SB), 137) ] ,
    SED2: [ (KECCAK(SC), 137)] 
]
  1. sessionInfo[0] can refer to it's corresponding SED entry as [0, 0]
  2. sessionInfo[1] can refer to it's corresponding SED entry as [0, 1]
  3. sessionInfo[2] can refer to it's corresponding SED entry as [1, 0]

Session Info Structure - Enable And Use

* Offset (in bytes)    | Length (in bytes) | Contents
* 0x0                  | 0x1               | MODE_SESSION_KEY_ENABLE_AND_USE
* 0x1                  | 0x1               | Index of Session Enable Data in Session Enable Data List
* 0x2                  | 0x1               | Index of Session Key in Session Enable Data
* 0x3                  | 0x6               | Valid Until
* 0x9                  | 0x6               | Valid After
* 0xf                  | 0x14              | Session Validation Module Address
* 0x23                 | --                | abi.encode(sessionKeyData, callSpecificData)

Session Info Structure - Use Pre-Enabled Session

 * Session Data Pre Enabled Signature Layout
 * Offset (in bytes)    | Length (in bytes) | Contents
 * 0x0                  | 0x1               | MODE_SESSION_KEY_PRE_ENABLED
 * 0x1                  | 0x20              | bytes32 sessionDataDigest
 * 0x21                 | ---               | abi.encode(callSpecificData)

Change Type

Checklist

linear[bot] commented 10 months ago

SMA-392 Session Keys V2 - Onchain

livingrockrises commented 9 months ago

resolve conflicts

ankurdubey521 commented 9 months ago

PR is ready to be merged. I think we should merge this post the "all-encompassing audit"

tomarsachin2271 commented 8 months ago

Looks great 🚀 LGTM

livingrockrises commented 8 months ago

Should we merge this or Not?

ankurdubey521 commented 8 months ago

@Aboudjem please let us know when this can be safely merged.

Aboudjem commented 8 months ago

@Aboudjem please let us know when this can be safely merged.

Sure ! let's discuss it next week

ankurdubey521 commented 8 months ago

@Aboudjem Any update on this?

Aboudjem commented 8 months ago

@Aboudjem Any update on this?

I thought we had agreed to proceed with the merging after completing the audits, if I'm not wrong

github-actions[bot] commented 7 months ago

This PR has been inactive for 30 days. If it's waiting for a review, please reach out to the team. Otherwise, please update the PR or it will be closed due to inactivity.

ankurdubey521 commented 7 months ago

@Aboudjem Any update on this?

I thought we had agreed to proceed with the merging after completing the audits, if I'm not wrong

We have completed 1 audit for the module, I believe it can be safely merged now.