Skip to content

Commit

Permalink
Merge pull request rhinestonewtf#86 from rhinestonewtf/feature/simpli…
Browse files Browse the repository at this point in the history
…fy-smart-sessions

feat: add getEnableSessionDetails
  • Loading branch information
kopy-kat authored Oct 15, 2024
2 parents 6cdb06f + b238db5 commit b115055
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ export {
getDisableUserOpPoliciesAction,
getEnableERC1271PoliciesAction,
getDisableERC1271PoliciesAction,
getEnableActionPolicies,
getDisableActionPolicies,
getEnableActionPoliciesAction,
getDisableActionPoliciesAction,
getPermissionId,
getActionId,
getSessionDigest,
Expand All @@ -123,6 +123,7 @@ export {
isSessionEnabled,
hashChainSessions,
getPermissions,
getEnableSessionDetails,
SmartSessionMode,
Session,
SessionEIP712,
Expand Down
5 changes: 3 additions & 2 deletions src/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ export {
getDisableUserOpPoliciesAction,
getEnableERC1271PoliciesAction,
getDisableERC1271PoliciesAction,
getEnableActionPolicies,
getDisableActionPolicies,
getEnableActionPoliciesAction,
getDisableActionPoliciesAction,
getPermissionId,
getActionId,
getSessionDigest,
Expand All @@ -137,6 +137,7 @@ export {
isSessionEnabled,
hashChainSessions,
getPermissions,
getEnableSessionDetails,
SmartSessionMode,
Session,
SessionEIP712,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ type Params = {

export const getSpendingLimitsPolicy = (params: Params): Policy => {
return {
policy: SPENDING_LIMITS_POLICY_ADDRESS,
address: SPENDING_LIMITS_POLICY_ADDRESS,
initData: encodeAbiParameters(
[{ type: 'address[]' }, { type: 'uint256[]' }],
[params.map(({ token }) => token), params.map(({ limit }) => limit)],
),
deInitData: '0x',
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Policy } from '../types'

export const getSudoPolicy = (): Policy => {
return {
policy: SUDO_POLICY_ADDRESS,
address: SUDO_POLICY_ADDRESS,
initData: '0x',
deInitData: '0x',
}
}
2 changes: 1 addition & 1 deletion src/module/smart-sessions/policies/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Hex, Address } from 'viem'

export type Policy = {
policy: Address
address: Address
initData: Hex
deInitData: Hex
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const getUniversalActionPolicy = (
}

return {
policy: UNIVERSAL_ACTION_POLICY_ADDRESS,
address: UNIVERSAL_ACTION_POLICY_ADDRESS,
initData: encodeAbiParameters(abi, [
{
Expand All @@ -22,6 +23,5 @@ export const getUniversalActionPolicy = (
},
},
]),
deInitData: '0x',
}
}
3 changes: 3 additions & 0 deletions src/module/smart-sessions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { Address, Hex } from 'viem'
export type Session = {
sessionValidator: Address
sessionValidatorInitData: Hex
// todo: make this optional with default 0
salt: Hex
// todo: make the below optional but require one of them to be defined
userOpPolicies: PolicyData[]
erc7739Policies: ERC7739Data
actions: ActionData[]
chainId: bigint
}

export type SessionEIP712 = {
Expand Down
78 changes: 76 additions & 2 deletions src/module/smart-sessions/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ export const getDisableERC1271PoliciesAction = ({
}
}

export const getEnableActionPolicies = ({
export const getEnableActionPoliciesAction = ({
permissionId,
actionPolicies,
}: {
Expand All @@ -563,7 +563,7 @@ export const getEnableActionPolicies = ({
}
}

export const getDisableActionPolicies = ({
export const getDisableActionPoliciesAction = ({
permissionId,
actionId,
policies,
Expand All @@ -586,3 +586,77 @@ export const getDisableActionPolicies = ({
data,
}
}

export const getEnableSessionDetails = async ({
sessions,
sessionIndex,
account,
client,
}: {
sessions: Session[]
sessionIndex?: number
account: Account
client: PublicClient
}) => {
const chainDigests = []
const chainSessions: ChainSession[] = []
for (const session of sessions) {
const permissionId = getPermissionId({
session,
})

const sessionNonce = await getSessionNonce({
client,
account,
permissionId,
})

const sessionDigest = await getSessionDigest({
client,
account,
session,
mode: SmartSessionMode.ENABLE,
permissionId,
})

chainDigests.push({
chainId: session.chainId,
sessionDigest,
})

chainSessions.push({
chainId: session.chainId,
session: {
...session,
account: account.address,
smartSession: SMART_SESSIONS_ADDRESS,
mode: 1,
nonce: sessionNonce,
},
})
}

const permissionEnableHash = hashChainSessions(chainSessions)

const sessionToEnable = sessions[sessionIndex || 0]
const permissionId = getPermissionId({
session: sessionToEnable,
})

return {
permissionEnableHash,
mode: SmartSessionMode.ENABLE,
permissionId,
signature: '0x' as Hex,
enableSessionData: {
enableSession: {
chainDigestIndex: sessionIndex || 0,
hashesAndChainIds: chainDigests,
sessionToEnable,
permissionEnableSig: '0x' as Hex,
},
validator: sessionToEnable.sessionValidator,
accountType: account.type,
},
}
}
3 changes: 2 additions & 1 deletion test/e2e/infra/installModuleActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export const getInstallModuleActions = async ({ account, client }: Params) => {
})

return [
// ...installSmartSessionsValidatorAction,
...installSmartSessionsValidatorAction,
...installOwnableValidatorAction,
...installWebAuthnValidatorAction,
...installOwnableExecutorAction,
Expand Down Expand Up @@ -309,6 +309,7 @@ export const getInstallModuleData = ({ account }: Pick<Params, 'account'>) => ({
],
},
],
chainId: BigInt(sepolia.id),
},
],
hook: zeroAddress,
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/infra/sendUserOp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const sendUserOp = async ({
nonce,
})

await publicClient.waitForTransactionReceipt({ hash })
const receipt = await publicClient.waitForTransactionReceipt({ hash })

return hash
return receipt
}
64 changes: 60 additions & 4 deletions test/e2e/modules/smartSessionsValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import {
} from 'src/module'
import {
encodeSmartSessionSignature,
getDisableActionPolicies,
getDisableActionPoliciesAction,
getDisableERC1271PoliciesAction,
getDisableUserOpPoliciesAction,
getEnableActionPolicies,
getEnableActionPoliciesAction,
getEnableERC1271PoliciesAction,
getEnableSessionsAction,
getEnableUserOpPoliciesAction,
getPermissionId,
getEnableSessionDetails,
getSessionDigest,
getSessionNonce,
hashChainSessions,
Expand Down Expand Up @@ -127,6 +128,53 @@ export const testSmartSessionsValidator = async ({
}, 30000)

it('should validate userOp using smart session using ENABLE flow', async () => {
const signer = privateKeyToAccount(process.env.PRIVATE_KEY as Hex)

const { smartSessions } = getInstallModuleData({ account })

const session: Session = {
...smartSessions.sessions[0],
salt: toHex(toBytes('3344433', { size: 32 })),
}

const sessionDetails = await getEnableSessionDetails({
sessions: [session],
account,
client: publicClient,
})

sessionDetails.enableSessionData.enableSession.permissionEnableSig =
await signer.signMessage({
message: { raw: sessionDetails.permissionEnableHash },
})

const receipt = await sendUserOp({
account,
actions: [
{
target: session.actions[0].actionTarget,
value: BigInt(0),
callData: session.actions[0].actionTargetSelector,
},
],
validator: SMART_SESSIONS_ADDRESS,
signUserOpHash: async (userOpHash) => {
sessionDetails.signature = await signer.signMessage({
message: { raw: userOpHash },
})
return encodeSmartSessionSignature(sessionDetails)
},
getDummySignature: async () => {
sessionDetails.signature = getOwnableValidatorMockSignature({
threshold: 1,
})
return encodeSmartSessionSignature(sessionDetails)
},
})

expect(receipt).toBeDefined()
expect(receipt.status).toBe('success')
}, 30000)

it('should validate userOp using smart session using ENABLE flow for safe', async () => {
if (account.type !== 'safe') {
Expand Down Expand Up @@ -254,6 +302,7 @@ export const testSmartSessionsValidator = async ({
})

expect(receipt).toBeDefined()
expect(receipt.status).toBe('success')
}, 30000)

it('should add new session for the account', async () => {
Expand All @@ -274,6 +323,7 @@ export const testSmartSessionsValidator = async ({
})

expect(receipt).toBeDefined()
expect(receipt.status).toBe('success')
}, 30000)

it('should enable userOp policy', async () => {
Expand Down Expand Up @@ -313,6 +363,7 @@ export const testSmartSessionsValidator = async ({
})

expect(receipt).toBeDefined()
expect(receipt.status).toBe('success')
}, 3000000)

it('should disable userOp policy', async () => {
Expand All @@ -333,6 +384,7 @@ export const testSmartSessionsValidator = async ({
})

expect(receipt).toBeDefined()
expect(receipt.status).toBe('success')
}, 3000000)

it('should enable action policy', async () => {
Expand All @@ -349,7 +401,7 @@ export const testSmartSessionsValidator = async ({
},
])

const enableActionPolicyAction = getEnableActionPolicies({
const enableActionPolicyAction = getEnableActionPoliciesAction({
permissionId,
actionPolicies: [
{
Expand All @@ -371,6 +423,7 @@ export const testSmartSessionsValidator = async ({
})

expect(receipt).toBeDefined()
expect(receipt.status).toBe('success')
}, 30000)

it('should disable action policy', async () => {
Expand All @@ -390,7 +443,7 @@ export const testSmartSessionsValidator = async ({
},
])

const disableActionPolicyAction = getDisableActionPolicies({
const disableActionPolicyAction = getDisableActionPoliciesAction({
permissionId,
actionId,
policies: [spendingLimitsPolicy.address],
Expand All @@ -402,6 +455,7 @@ export const testSmartSessionsValidator = async ({
})

expect(receipt).toBeDefined()
expect(receipt.status).toBe('success')
}, 30000)

it('should enable ERC1271 policy', async () => {
Expand Down Expand Up @@ -429,6 +483,7 @@ export const testSmartSessionsValidator = async ({
})

expect(receipt).toBeDefined()
expect(receipt.status).toBe('success')
}, 3000000)

it('should disable ERC1271 policy', async () => {
Expand All @@ -449,6 +504,7 @@ export const testSmartSessionsValidator = async ({
})

expect(receipt).toBeDefined()
expect(receipt.status).toBe('success')
}, 3000000)

it.skip('should return true when checking is valid signature', async () => {
Expand Down
3 changes: 0 additions & 3 deletions test/unit/module/smartSessions/smartSessionPolicies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ describe('Smart Sessions Polices', () => {
UNIVERSAL_ACTION_POLICY_ADDRESS,
)
expect(installUniversalPolicy.initData).toBeDefined()
expect(installUniversalPolicy.deInitData).toEqual('0x')
})

// -----------------------
Expand All @@ -48,7 +47,6 @@ describe('Smart Sessions Polices', () => {
const installSudoActionPolicy = getSudoPolicy()
expect(installSudoActionPolicy.address).toBeDefined()
expect(installSudoActionPolicy.initData).toEqual('0x')
expect(installSudoActionPolicy.deInitData).toEqual('0x')
})

// -----------------------
Expand All @@ -61,6 +59,5 @@ describe('Smart Sessions Polices', () => {

expect(installSpendingLimitPolicy.address).toBeDefined()
expect(installSpendingLimitPolicy.initData).toBeDefined()
expect(installSpendingLimitPolicy.deInitData).toEqual('0x')
})
})
Loading

0 comments on commit b115055

Please sign in to comment.