EcosystemWallet.CreateSessionKey

Creates a session key for the user wallet with EIP-7702 functionality. Session keys allow delegated access to the wallet with specific permissions and time constraints. This method is only supported for EIP7702 and EIP7702Sponsored execution modes.

How it works: The method creates a SessionSpec with your parameters, signs it using EIP-712, and calls createSessionWithSig on the EIP-7702 account contract to register the session key on-chain.

Usage

// Create a session key for 24 hours with full permissions
var receipt = await ecosystemWallet.CreateSessionKey(
chainId: 1,
signerAddress: await sessionSigner.GetAddress(),
durationInSeconds: 86400, // 24 hours
grantFullPermissions: true
);
// Create a session key with specific call and transfer policies
var callPolicies = new List<CallSpec>
{
new CallSpec
{
Target = "0x1234567890123456789012345678901234567890",
Selector = new byte[] { 0xa9, 0x05, 0x9c, 0xbb }, // transfer(address,uint256)
MaxValuePerUse = BigInteger.Parse("100000000000000000"), // 0.1 ETH max per call
ValueLimit = new UsageLimit
{
LimitType = 1, // Lifetime limit
Limit = BigInteger.Parse("1000000000000000000"), // 1 ETH total
Period = 86400 // 1 day period
},
Constraints = new List<Constraint>
{
new Constraint
{
Condition = 1, // Equal to (not 0)
Index = 0, // First parameter (to address)
RefValue = new byte[32], // Reference value for validation
Limit = new UsageLimit
{
LimitType = 2, // Allowance (not 0)
Limit = BigInteger.Parse("500000000000000000"), // 0.5 ETH per period
Period = 3600 // 1 hour
}
}
}
}
};
var transferPolicies = new List<TransferSpec>
{
new TransferSpec
{
Target = "0x0000000000000000000000000000000000000000", // ETH transfers
MaxValuePerUse = BigInteger.Parse("100000000000000000"), // 0.1 ETH max per transfer
ValueLimit = new UsageLimit
{
LimitType = 1, // Lifetime limit
Limit = BigInteger.Parse("1000000000000000000"), // 1 ETH total
Period = 86400 // 1 day
}
},
new TransferSpec
{
Target = "0xA0b86a33E6411a3bb4CC4C7b9C5C5C7C7C8C9C0c", // Specific token contract
MaxValuePerUse = BigInteger.Parse("1000000000000000000000"), // 1000 tokens max per transfer
ValueLimit = new UsageLimit
{
LimitType = 2, // Allowance (resets every period)
Limit = BigInteger.Parse("10000000000000000000000"), // 10,000 tokens per period
Period = 3600 // 1 hour period
}
}
};
var receipt = await ecosystemWallet.CreateSessionKey(
chainId: 1,
signerAddress: await sessionSigner.GetAddress(),
durationInSeconds: 3600, // 1 hour
grantFullPermissions: false,
callPolicies: callPolicies,
transferPolicies: transferPolicies,
uid: System.Text.Encoding.UTF8.GetBytes("unique-session-id") // Custom unique identifier
);
// Create a wildcard session key (works with any address)
var wildcardReceipt = await ecosystemWallet.CreateSessionKey(
chainId: 1,
signerAddress: "0x0000000000000000000000000000000000000000", // Wildcard address
durationInSeconds: 7200, // 2 hours
grantFullPermissions: true
);

Common Use Cases

Gaming Session Key

// Allow spending game tokens with daily limits
var gameTokenPolicy = new List<CallSpec>
{
new CallSpec
{
Target = "0xGameTokenContract...",
Selector = new byte[] { 0xa9, 0x05, 0x9c, 0xbb }, // transfer(address,uint256)
MaxValuePerUse = BigInteger.Parse("1000000000000000000000"), // 1000 tokens per transaction
ValueLimit = new UsageLimit
{
LimitType = 2, // Allowance (resets daily)
Limit = BigInteger.Parse("10000000000000000000000"), // 10,000 tokens per day
Period = 86400 // 1 day
}
}
};

DeFi Trading Session Key

// Allow DEX trades with specific token constraints
var dexTradingPolicy = new List<CallSpec>
{
new CallSpec
{
Target = "0xUniswapV3Router...",
Selector = new byte[] { 0x41, 0x4b, 0xf3, 0x89 }, // exactInputSingle
MaxValuePerUse = BigInteger.Parse("100000000000000000"), // 0.1 ETH max per trade
ValueLimit = new UsageLimit
{
LimitType = 2, // Daily allowance
Limit = BigInteger.Parse("1000000000000000000"), // 1 ETH per day
Period = 86400
},
Constraints = new List<Constraint>
{
new Constraint
{
Condition = 1, // Equal - only allow specific token
Index = 0, // First parameter (tokenIn)
RefValue = new byte[32], // USDC token address (padded to 32 bytes)
Limit = new UsageLimit { LimitType = 0 } // Unlimited usage of this constraint
}
}
}
};

Subscription Payment Session Key

// Allow recurring payments to specific service
var subscriptionPolicy = new List<CallSpec>
{
new CallSpec
{
Target = "0xSubscriptionContract...",
Selector = new byte[] { 0x12, 0x34, 0x56, 0x78 }, // paySubscription()
MaxValuePerUse = BigInteger.Parse("50000000000000000000"), // $50 worth of tokens
ValueLimit = new UsageLimit
{
LimitType = 2, // Monthly allowance
Limit = BigInteger.Parse("50000000000000000000"), // $50 per month
Period = 2592000 // 30 days
},
Constraints = new List<Constraint>
{
new Constraint
{
Condition = 1, // Equal - only allow payments to specific service
Index = 0, // Service ID parameter
RefValue = BitConverter.GetBytes(12345).Concat(new byte[28]).ToArray(), // Service ID 12345
Limit = new UsageLimit { LimitType = 0 }
}
}
}
};

Emergency Withdrawal Session Key

// Allow emergency withdrawals with strict limits
var emergencyPolicy = new List<TransferSpec>
{
new TransferSpec
{
Target = "0x0000000000000000000000000000000000000000", // ETH
MaxValuePerUse = BigInteger.Parse("100000000000000000"), // 0.1 ETH per withdrawal
ValueLimit = new UsageLimit
{
LimitType = 1, // Lifetime limit
Limit = BigInteger.Parse("500000000000000000"), // 0.5 ETH total
Period = 0 // Not used for lifetime limits
}
}
};
var emergencyReceipt = await ecosystemWallet.CreateSessionKey(
chainId: 1,
signerAddress: await emergencyWallet.GetAddress(),
durationInSeconds: 604800, // 1 week
grantFullPermissions: false,
callPolicies: null,
transferPolicies: emergencyPolicy
);