Skip to content

Commit 5550437

Browse files
committed
refactor: update createSessionKey function to remove contract parameter
- Remove contract input parameter from createSessionKey function - Recreate contract internally using account.address and MinimalAccount ABI - Update function signature to take client and chain directly - Update tests to use new function signature - Set ABI to MinimalAccount contract ABI as specified
1 parent bd395a3 commit 5550437

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

packages/thirdweb/src/extensions/erc7702/account/createSessionKey.ts

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import type { BaseTransactionOptions } from "../../../transaction/types.js";
1+
import type { ThirdwebClient } from "../../../client/client.js";
2+
import type { Chain } from "../../../chains/types.js";
23
import { randomBytesHex } from "../../../utils/random.js";
34
import type { Account } from "../../../wallets/interfaces/wallet.js";
5+
import { getContract } from "../../../contract/contract.js";
46
import {
57
createSessionWithSig,
68
isCreateSessionWithSigSupported,
@@ -15,10 +17,47 @@ import {
1517
UsageLimitRequest,
1618
} from "./types.js";
1719

20+
// MinimalAccount ABI - using the ABI definition from the JSON file
21+
const MinimalAccountAbi = [
22+
"error AllowanceExceeded(uint256 allowanceUsage, uint256 limit, uint64 period)",
23+
"error CallPolicyViolated(address target, bytes4 selector)",
24+
"error CallReverted()",
25+
"error ConditionFailed(bytes32 param, bytes32 refValue, uint8 condition)",
26+
"error InvalidDataLength(uint256 actualLength, uint256 expectedLength)",
27+
"error InvalidSignature(address msgSender, address thisAddress)",
28+
"error LifetimeUsageExceeded(uint256 lifetimeUsage, uint256 limit)",
29+
"error MaxValueExceeded(uint256 value, uint256 maxValuePerUse)",
30+
"error NoCallsToExecute()",
31+
"error SessionExpired()",
32+
"error SessionExpiresTooSoon()",
33+
"error SessionZeroSigner()",
34+
"error TransferPolicyViolated(address target)",
35+
"error UIDAlreadyProcessed()",
36+
"event Executed(address indexed to, uint256 value, bytes data)",
37+
"event SessionCreated(address indexed signer, (address signer, bool isWildcard, uint256 expiresAt, (address target, bytes4 selector, uint256 maxValuePerUse, (uint8 limitType, uint256 limit, uint256 period) valueLimit, (uint8 condition, uint64 index, bytes32 refValue, (uint8 limitType, uint256 limit, uint256 period) limit)[] constraints)[] callPolicies, (address target, uint256 maxValuePerUse, (uint8 limitType, uint256 limit, uint256 period) valueLimit)[] transferPolicies, bytes32 uid) sessionSpec)",
38+
"function createSessionWithSig((address signer, bool isWildcard, uint256 expiresAt, (address target, bytes4 selector, uint256 maxValuePerUse, (uint8 limitType, uint256 limit, uint256 period) valueLimit, (uint8 condition, uint64 index, bytes32 refValue, (uint8 limitType, uint256 limit, uint256 period) limit)[] constraints)[] callPolicies, (address target, uint256 maxValuePerUse, (uint8 limitType, uint256 limit, uint256 period) valueLimit)[] transferPolicies, bytes32 uid) sessionSpec, bytes signature)",
39+
"function eip712Domain() view returns (bytes1 fields, string name, string version, uint256 chainId, address verifyingContract, bytes32 salt, uint256[] extensions)",
40+
"function execute((address target, uint256 value, bytes data)[] calls) payable",
41+
"function executeWithSig(((address target, uint256 value, bytes data)[] calls, bytes32 uid) wrappedCalls, bytes signature) payable",
42+
"function getCallPoliciesForSigner(address signer) view returns ((address target, bytes4 selector, uint256 maxValuePerUse, (uint8 limitType, uint256 limit, uint256 period) valueLimit, (uint8 condition, uint64 index, bytes32 refValue, (uint8 limitType, uint256 limit, uint256 period) limit)[] constraints)[])",
43+
"function getSessionExpirationForSigner(address signer) view returns (uint256)",
44+
"function getSessionStateForSigner(address signer) view returns (((uint256 remaining, address target, bytes4 selector, uint256 index)[] transferValue, (uint256 remaining, address target, bytes4 selector, uint256 index)[] callValue, (uint256 remaining, address target, bytes4 selector, uint256 index)[] callParams))",
45+
"function getTransferPoliciesForSigner(address signer) view returns ((address target, uint256 maxValuePerUse, (uint8 limitType, uint256 limit, uint256 period) valueLimit)[])",
46+
"function isWildcardSigner(address signer) view returns (bool)",
47+
] as const;
48+
1849
/**
1950
* @extension ERC7702
2051
*/
2152
export type CreateSessionKeyOptions = {
53+
/**
54+
* The Thirdweb client
55+
*/
56+
client: ThirdwebClient;
57+
/**
58+
* The chain to create the session key on
59+
*/
60+
chain: Chain;
2261
/**
2362
* The admin account that will perform the operation.
2463
*/
@@ -48,16 +87,16 @@ export type CreateSessionKeyOptions = {
4887
/**
4988
* Creates session key permissions for a specified address.
5089
* @param options - The options for the createSessionKey function.
51-
* @param {Contract} options.contract - The EIP-7702 smart EOA contract to create the session key from
5290
* @returns The transaction object to be sent.
5391
* @example
5492
* ```ts
5593
* import { createSessionKey } from 'thirdweb/wallets/in-app';
5694
* import { sendTransaction } from 'thirdweb';
5795
*
5896
* const transaction = createSessionKey({
97+
* client,
98+
* chain,
5999
* account: account,
60-
* contract: accountContract,
61100
* sessionKeyAddress: TEST_ACCOUNT_A.address,
62101
* durationInSeconds: 86400, // 1 day
63102
* grantFullPermissions: true
@@ -67,11 +106,10 @@ export type CreateSessionKeyOptions = {
67106
* ```
68107
* @extension ERC7702
69108
*/
70-
export function createSessionKey(
71-
options: BaseTransactionOptions<CreateSessionKeyOptions>,
72-
) {
109+
export function createSessionKey(options: CreateSessionKeyOptions) {
73110
const {
74-
contract,
111+
client,
112+
chain,
75113
account,
76114
sessionKeyAddress,
77115
durationInSeconds,
@@ -84,6 +122,14 @@ export function createSessionKey(
84122
throw new Error("durationInSeconds must be positive");
85123
}
86124

125+
// Create contract from account.address with MinimalAccount ABI
126+
const contract = getContract({
127+
address: account.address,
128+
chain,
129+
client,
130+
abi: MinimalAccountAbi,
131+
});
132+
87133
return createSessionWithSig({
88134
async asyncParams() {
89135
const req = {

packages/thirdweb/src/extensions/erc7702/account/sessionkey.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ describe.runIf(process.env.TW_SECRET_KEY)(
6464
const receipt = await sendAndConfirmTransaction({
6565
account: account,
6666
transaction: createSessionKey({
67+
client: TEST_CLIENT,
68+
chain: defineChain(chainId),
6769
account: account,
68-
contract: accountContract,
6970
durationInSeconds: 86400,
7071
grantFullPermissions: true, // 1 day
7172
sessionKeyAddress: TEST_ACCOUNT_A.address,
@@ -82,6 +83,8 @@ describe.runIf(process.env.TW_SECRET_KEY)(
8283
const receipt = await sendAndConfirmTransaction({
8384
account: account,
8485
transaction: createSessionKey({
86+
client: TEST_CLIENT,
87+
chain: defineChain(chainId),
8588
account: account,
8689
callPolicies: [
8790
{
@@ -103,7 +106,6 @@ describe.runIf(process.env.TW_SECRET_KEY)(
103106
},
104107
},
105108
],
106-
contract: accountContract,
107109
durationInSeconds: 86400, // 1 day
108110
grantFullPermissions: false,
109111
sessionKeyAddress: TEST_ACCOUNT_A.address,

0 commit comments

Comments
 (0)