Skip to content

Commit

Permalink
chore: continued migration
Browse files Browse the repository at this point in the history
  • Loading branch information
joepegler committed Dec 31, 2024
1 parent 0e83bc4 commit 3fe6115
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 208 deletions.
1 change: 1 addition & 0 deletions src/sdk/account/utils/getChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const getChain = (chainId: number): Chain => {
return chain
}
}

throw new Error(
"Chain not found. Please add a customChain into your config using the getCustomChain(...) helper"
)
Expand Down
7 changes: 2 additions & 5 deletions src/sdk/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ import { ParamCondition } from "../modules/smartSessionsValidator/Types"

export * from "./abi"

export const SIMPLE_SESSION_VALIDATOR_ADDRESS: Hex =
"0xE7A6F1a02151E50b600BC3d06FeEd70C6c4B19Bd"
export const ENTRY_POINT_ADDRESS: Hex =
"0x0000000071727De22E5E9d8BAf0edAc6f37da032"
export const ENTRYPOINT_SIMULATIONS_ADDRESS: Hex =
"0x74Cb5e4eE81b86e70f9045036a1C5477de69eE87"
export const TIMEFRAME_POLICY_ADDRESS: Hex =
"0x0B7BB9bD65858593D97f12001FaDa94828307805"
export const NEXUS_BOOTSTRAP_ADDRESS: Hex =
"0x00000008c901d8871b6F6942De0B5D9cCf3873d3"

Expand Down Expand Up @@ -78,7 +74,8 @@ export {
getSpendingLimitsPolicy,
getUsageLimitPolicy,
getValueLimitPolicy,
getOwnableValidator
getOwnableValidator,
getUniversalActionPolicy
} from "@rhinestone/module-sdk"

// Rhinestone doesn't export the universal action policy address, so we need to get it from the policies
Expand Down
75 changes: 16 additions & 59 deletions src/sdk/modules/smartSessionsValidator/Helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ import {
type Address,
type Hex,
type PublicClient,
encodeAbiParameters,
encodePacked,
pad,
getAddress,
toBytes,
toFunctionSelector,
toHex
} from "viem"
import {
type ActionData,
OWNABLE_VALIDATOR_ADDRESS,
type PolicyData,
REGISTRY_ADDRESS,
SIMPLE_SESSION_VALIDATOR_ADDRESS,
SMART_SESSIONS_ADDRESS,
type Session,
TIMEFRAME_POLICY_ADDRESS,
UNIVERSAL_ACTION_POLICY_ADDRESS
encodeValidationData
} from "../../constants"
import { ERC7484RegistryAbi, UniActionPolicyAbi } from "../../constants/abi"
import { ERC7484RegistryAbi } from "../../constants/abi"
import { SmartSessionAbi } from "../../constants/abi/SmartSessionAbi"
import { parseReferenceValue } from "../utils/Helpers"
import type { AnyData } from "../utils/Types"
Expand All @@ -30,10 +27,10 @@ import type {
ActionPolicyInfo,
CreateSessionDataParams,
FullCreateSessionDataParams,
RawActionConfig,
ResolvedActionPolicyInfo,
Rule
} from "./Types"
import { ONE_YEAR_FROM_NOW_IN_SECONDS } from "./decorators/preparePermission"

export const MAX_RULES = 16

Expand Down Expand Up @@ -84,10 +81,16 @@ export const applyDefaults = (
...sessionInfo,
sessionKeyData,
sessionPublicKey,
sessionValidUntil: sessionInfo.sessionValidUntil ?? 0,
sessionValidUntil:
sessionInfo.sessionValidUntil ?? ONE_YEAR_FROM_NOW_IN_SECONDS,
sessionValidAfter: sessionInfo.sessionValidAfter ?? 0,
sessionValidatorAddress:
sessionInfo.sessionValidatorAddress ?? SIMPLE_SESSION_VALIDATOR_ADDRESS
sessionValidator: sessionInfo.sessionValidator ?? OWNABLE_VALIDATOR_ADDRESS,
sessionValidatorInitData:
sessionInfo.sessionValidatorInitData ??
encodeValidationData({
threshold: 1,
owners: [getAddress(sessionPublicKey)]
})
}
}

Expand Down Expand Up @@ -119,7 +122,7 @@ export const createActionData = (
* @param config - The ActionConfig to convert.
* @returns A RawActionConfig object.
*/
export const toActionConfig = (config: ActionConfig): RawActionConfig => {
export const toActionConfig = (config: ActionConfig) => {
// Ensure we always have 16 rules, filling with default values if necessary
const filledRules = [...config.paramRules.rules]

Expand All @@ -145,7 +148,7 @@ export const toActionConfig = (config: ActionConfig): RawActionConfig => {
const parsedRef = parseReferenceValue(rule.ref)
return {
condition: rule.condition,
offset: BigInt(rule.offsetIndex) * BigInt(32),
offset: rule.offsetIndex * 32,
isLimited: rule.isLimited,
ref: parsedRef,
usage: rule.usage
Expand Down Expand Up @@ -194,52 +197,6 @@ export const isPermissionEnabled = async ({
})
}

/**
* Converts an ActionConfig to a UniversalActionPolicy.
*
* @param actionConfig - The ActionConfig to convert.
* @returns A PolicyData object representing the UniversalActionPolicy.
*/
export const toUniversalActionPolicy = (
actionConfig: ActionConfig
): PolicyData => ({
policy: UNIVERSAL_ACTION_POLICY_ADDRESS,
initData: encodeAbiParameters(UniActionPolicyAbi, [
toActionConfig(actionConfig)
])
})

/**
* Creates a TimeRangePolicy.
*
* @param validUntil - The timestamp until which the policy is valid.
* @param validAfter - The timestamp after which the policy is valid.
* @returns A PolicyData object representing the TimeRangePolicy.
*/
export const toTimeRangePolicy = (
validUntil: number,
validAfter: number
): PolicyData => {
const validUntilBytes = pad(toBytes(BigInt(validUntil), { size: 16 }), {
dir: "right",
size: 16
})
const validAfterBytes = pad(toBytes(BigInt(validAfter), { size: 16 }), {
dir: "right",
size: 16
})
const packedData = encodePacked(
["bytes16", "bytes16"],
[toHex(validUntilBytes), toHex(validAfterBytes)]
)
const timeFramePolicyData: PolicyData = {
policy: TIMEFRAME_POLICY_ADDRESS,
// initData for TimeframePolicy
initData: packedData
}
return timeFramePolicyData
}

/**
* Stringifies an object, explicitly tagging BigInt values.
*
Expand Down
12 changes: 6 additions & 6 deletions src/sdk/modules/smartSessionsValidator/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ export type CreateSessionDataParams = OptionalSessionKeyData & {
/** Public key for the session. Required for K1 algorithm validators. */
sessionPublicKey?: Hex
/** Address of the session validator. */
sessionValidatorAddress?: Address
/** Type of the session validator. Usually "simple K1 validator". */
sessionValidatorType?: string
sessionValidator?: Address
/** Data for the session validator. */
sessionValidatorInitData?: Hex
/** Optional salt for the session. */
salt?: Hex
/** Timestamp until which the session is valid. */
Expand All @@ -102,9 +102,9 @@ export type FullCreateSessionDataParams = {
/** Public key for the session. Required for K1 algorithm validators. */
sessionPublicKey: Hex
/** Address of the session validator. */
sessionValidatorAddress: Address
/** Type of the session validator. Usually "simple K1 validator". */
sessionValidatorType?: string
sessionValidator?: Address
/** Data for the session validator. */
sessionValidatorInitData?: Hex
/** Data for the session key. */
sessionKeyData: Hex
/** Optional salt for the session. */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
getTimeFramePolicy,
getUniversalActionPolicy
} from "@rhinestone/module-sdk"
import type { Chain, Client, Hex, PublicClient, Transport } from "viem"
import { encodeFunctionData, parseAccount } from "viem/utils"
import { ERROR_MESSAGES } from "../../../account"
Expand All @@ -23,8 +27,7 @@ import {
createActionData,
generateSalt,
getPermissionId,
toTimeRangePolicy,
toUniversalActionPolicy
toActionConfig
} from "../Helpers"
import type {
CreateSessionDataParams,
Expand All @@ -33,6 +36,8 @@ import type {
ResolvedActionPolicyInfo
} from "../Types"

export const ONE_YEAR_FROM_NOW_IN_SECONDS = Date.now() + 60 * 60 * 24 * 365

/**
* Parameters for creating sessions in a modular smart account.
*
Expand Down Expand Up @@ -76,7 +81,7 @@ export const getPermissionAction = async ({

const resolvedPolicyInfo2ActionData = (
actionPolicyInfo: ResolvedActionPolicyInfo
): ActionData => {
) => {
const actionConfig = createActionConfig(
actionPolicyInfo.rules ?? [],
actionPolicyInfo.valueLimit
Expand All @@ -85,14 +90,16 @@ export const getPermissionAction = async ({
const policyData: PolicyData[] = []

// create uni action policy here..
const uniActionPolicyInfo = toUniversalActionPolicy(actionConfig)
const uniActionPolicyInfo = getUniversalActionPolicy(
toActionConfig(actionConfig)
)
policyData.push(uniActionPolicyInfo)

// create time frame policy here..
const timeFramePolicyData: PolicyData = toTimeRangePolicy(
actionPolicyInfo.validUntil ?? 0,
actionPolicyInfo.validAfter ?? 0
)
const timeFramePolicyData = getTimeFramePolicy({
validUntil: actionPolicyInfo.validUntil ?? ONE_YEAR_FROM_NOW_IN_SECONDS,
validAfter: actionPolicyInfo.validAfter ?? 0
})
policyData.push(timeFramePolicyData)

// create sudo policy here..
Expand Down Expand Up @@ -153,10 +160,10 @@ export const getPermissionAction = async ({
}
}

const userOpTimeFramePolicyData: PolicyData = toTimeRangePolicy(
sessionInfo.sessionValidUntil ?? 0,
sessionInfo.sessionValidAfter ?? 0
)
const userOpTimeFramePolicyData = getTimeFramePolicy({
validUntil: sessionInfo.sessionValidUntil ?? ONE_YEAR_FROM_NOW_IN_SECONDS,
validAfter: sessionInfo.sessionValidAfter ?? 0
})

const session: Session = {
chainId: BigInt(chainId),
Expand Down Expand Up @@ -276,8 +283,7 @@ export async function preparePermission<
throw new Error(ERROR_MESSAGES.CHAIN_NOT_FOUND)
}

const defaultedSessionRequestedInfo: FullCreateSessionDataParams[] =
sessionRequestedInfo.map(applyDefaults)
const defaultedSessionRequestedInfo = sessionRequestedInfo.map(applyDefaults)

const actionResponse = await getPermissionAction({
chainId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export type UsePermissionParameters<
account?: TModularSmartAccount
/** The signer to use for the session. Defaults to the signer of the client. */
signer?: Signer
/** The gas limit for the pre-verification phase. */
preVerificationGas?: bigint
/** The gas limit for the verification phase. */
verificationGasLimit?: bigint
/** The gas limit for the call phase. */
callGasLimit?: bigint
}

/**
Expand Down Expand Up @@ -79,5 +85,5 @@ export async function usePermission<
sendUserOperation,
"sendUserOperation"
// @ts-ignore
)({ ...parameters, account: account_ })
)(parameters)
}
Loading

0 comments on commit 3fe6115

Please sign in to comment.