Skip to content

Commit

Permalink
feat: init smart sessions simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
kopy-kat committed Oct 14, 2024
1 parent 6cdb06f commit 06fe448
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 8 deletions.
4 changes: 2 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 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',
}
}
2 changes: 2 additions & 0 deletions src/module/smart-sessions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ 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[]
Expand Down
76 changes: 74 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,75 @@ export const getDisableActionPolicies = ({
data,
}
}

// todo: make session an array
export const getSessionDetails = async ({
session,
chainId,
mode,
account,
client,
}: {
session: Session
chainId: bigint
mode: SmartSessionModeType
account: Account
client: PublicClient
}) => {
const permissionId = getPermissionId({
session,
})

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

const sessionDigest = await getSessionDigest({
client,
account,
session,
mode,
permissionId,
})

const chainDigests = [
{
chainId: chainId,
sessionDigest,
},
]

const chainSessions: ChainSession[] = [
{
chainId: chainId,
session: {
...session,
account: account.address,
smartSession: SMART_SESSIONS_ADDRESS,
mode,
nonce: sessionNonce,
},
},
]

const permissionEnableHash = hashChainSessions(chainSessions)

return {
permissionEnableHash,
mode,
permissionId,
signature: '0x' as Hex,
enableSessionData: {
enableSession: {
chainDigestIndex: 0,
hashesAndChainIds: chainDigests,
sessionToEnable: session,
permissionEnableSig: '0x' as Hex,
},
validator: session.sessionValidator,
accountType: account.type,
},
}
}

0 comments on commit 06fe448

Please sign in to comment.