Skip to content

Commit

Permalink
fix review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
YasseinBilal committed Sep 10, 2024
1 parent b33465f commit 16799eb
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 33 deletions.
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ export {
getSpendingLimitsPolicy,
getSudoPolicy,
getUniversalActionPolicy,
encodeUseSmartSessionSignature,
encodeEnableSmartSessionSignature,
encodeSmartSessionSignature,
encodeUseOrEnableSmartSessionSignature,
isSessionEnabled,
hashChainSessions,
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 @@ -130,9 +130,10 @@ export {
getSpendingLimitsPolicy,
getSudoPolicy,
getUniversalActionPolicy,
encodeUseSmartSessionSignature,
encodeEnableSmartSessionSignature,
encodeSmartSessionSignature,
encodeUseOrEnableSmartSessionSignature,
isSessionEnabled,
hashChainSessions,
SmartSessionMode,
Session,
SessionEIP712,
Expand Down
111 changes: 82 additions & 29 deletions src/module/smart-sessions/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,51 +104,104 @@ export const getSessionDigest = async ({
})) as Hex
}

export const encodeUseSmartSessionSignature = ({
export const encodeSmartSessionSignature = ({
mode,
permissionId,
signature,
enableSessionData,
}: {
mode: SmartSessionModeType
permissionId: Hex
signature: Hex
enableSessionData?: EnableSessionData
}) => {
return encodePacked(
['bytes1', 'bytes32', 'bytes'],
[
SmartSessionMode.USE,
permissionId,
LibZip.flzCompress(
encodeAbiParameters(
[
{
type: 'bytes',
},
],
[signature],
),
) as Hex,
],
)
switch (mode) {
case SmartSessionMode.USE:
return encodePacked(
['bytes1', 'bytes32', 'bytes'],
[
mode,
permissionId,
LibZip.flzCompress(
encodeAbiParameters(
[
{
type: 'bytes',
},
],
[signature],
),
) as Hex,
],
)
case SmartSessionMode.ENABLE:
case SmartSessionMode.UNSAFE_ENABLE:
if (!enableSessionData) {
throw new Error('enableSession is required for ENABLE mode')
}

return encodePacked(
['bytes1', 'bytes32', 'bytes'],
[
mode,
permissionId,
LibZip.flzCompress(
encodeEnableSessionSignature({ enableSessionData, signature }),
) as Hex,
],
)
default:
throw new Error(`Unknown mode ${mode}`)
}
}

export const encodeEnableSmartSessionSignature = ({
export const encodeUseOrEnableSmartSessionSignature = async ({
account,
client,
permissionId,
signature,
enableSessionData,
}: {
account: Account
client: PublicClient
permissionId: Hex
signature: Hex
enableSessionData: EnableSessionData
}) => {
return encodePacked(
['bytes1', 'bytes32', 'bytes'],
[
SmartSessionMode.ENABLE,
permissionId,
LibZip.flzCompress(
encodeEnableSessionSignature({ enableSessionData, signature }),
) as Hex,
],
)
const sessionEnabled = await isSessionEnabled({
account,
client,
permissionId,
})

return sessionEnabled
? encodePacked(
['bytes1', 'bytes32', 'bytes'],
[
SmartSessionMode.USE,
permissionId,
LibZip.flzCompress(
encodeAbiParameters(
[
{
type: 'bytes',
},
],
[signature],
),
) as Hex,
],
)
: encodePacked(
['bytes1', 'bytes32', 'bytes'],
[
SmartSessionMode.ENABLE,
permissionId,
LibZip.flzCompress(
encodeEnableSessionSignature({ enableSessionData, signature }),
) as Hex,
],
)
}

export const hashChainSessions = (chainSessions: ChainSession[]): Hex => {
Expand Down

0 comments on commit 16799eb

Please sign in to comment.