Skip to content

Commit

Permalink
chore: continued
Browse files Browse the repository at this point in the history
  • Loading branch information
joepegler committed Dec 30, 2024
1 parent 186e759 commit c4f458a
Show file tree
Hide file tree
Showing 18 changed files with 489 additions and 256 deletions.
51 changes: 10 additions & 41 deletions src/sdk/account/toNexusAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
} from "../constants"
// Constants
import { EntrypointAbi } from "../constants/abi"
import { getCounterFactualAddress as getCounterFactualAddress_ } from "./utils/getCounterFactualAddress"

// Modules
import { toK1Validator } from "../modules/k1Validator/toK1Validator"
Expand Down Expand Up @@ -251,47 +252,15 @@ export const toNexusAccount = async (
}
}

const addressFromFactory = (await publicClient.readContract({
address: factoryAddress,
abi: [
{
inputs: [
{
internalType: "address",
name: "eoaOwner",
type: "address"
},
{
internalType: "uint256",
name: "index",
type: "uint256"
},
{
internalType: "address[]",
name: "attesters",
type: "address[]"
},
{
internalType: "uint8",
name: "threshold",
type: "uint8"
}
],
name: "computeAccountAddress",
outputs: [
{
internalType: "address payable",
name: "expectedAddress",
type: "address"
}
],
stateMutability: "view",
type: "function"
}
],
functionName: "computeAccountAddress",
args: [signerAddress, index, attesters_, attesterThreshold]
})) as Address
const addressFromFactory = await getCounterFactualAddress_(
publicClient,
signerAddress,
false,
index,
attesters_,
attesterThreshold,
factoryAddress
)

if (!addressEquals(addressFromFactory, zeroAddress)) {
_accountAddress = addressFromFactory
Expand Down
5 changes: 3 additions & 2 deletions src/sdk/clients/createNexusSessionClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe("nexus.session.client", async () => {
nexusClient.account.getCounterFactualAddress()

const createSessionsResponse =
await nexusSessionClient.grantPermissionAdvanced({
await nexusSessionClient.grantPermissionInAdvance({
sessionRequestedInfo
})

Expand All @@ -145,7 +145,8 @@ describe("nexus.session.client", async () => {
moduleData: {
permissionIds: createSessionsResponse.permissionIds,
action: createSessionsResponse.action,
mode: SmartSessionMode.USE
mode: SmartSessionMode.USE,
sessions: createSessionsResponse.sessions
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/sdk/modules/ownableValidator/toOwnableValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
ModuleParameters
} from "../utils/Types"
import { toModule } from "../utils/toModule"
import { getOwnableValidator } from "@rhinestone/module-sdk/module"

/**
* Parameters for creating an Ownable module.
Expand Down Expand Up @@ -130,7 +131,10 @@ export const toOwnableValidator = (
type: "nexus"
})

const moduleInitData = getOwnablesModuleInitData(moduleInitArgs_)
const moduleInitData = getOwnableValidator({
threshold: moduleInitArgs_.threshold,
owners: moduleInitArgs_.owners
})
const initData = initData_ ?? getOwnablesInitData(initArgs_)

return toModule({
Expand Down
16 changes: 11 additions & 5 deletions src/sdk/modules/smartSessionsValidator/Types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { Abi, AbiFunction, Address, Hex, OneOf } from "viem"
import type { EnableSessionData, SmartSessionMode } from "../../constants"
import type {
EnableSessionData,
Session,
SmartSessionMode
} from "../../constants"
import type { AnyReferenceValue } from "../utils/Helpers"
import type { Execution } from "../utils/Types"

Expand All @@ -26,20 +30,22 @@ export type SessionData = {
description?: string
}

export type GrantPermissionAdvancedActionReturnParams = {
export type PreparePermissionResponse = {
/** Array of permission IDs for the created sessions. */
permissionIds: Hex[]
/** The execution object for the action. */
action: Execution
/** The sessions that were created. */
sessions: Session[]
}

/**
* Represents the response for creating sessions.
*/
export type GrantPermissionAdvancedResponse = {
export type GrantPermissionInAdvanceResponse = {
/** The hash of the user operation. */
userOpHash: Hex
} & GrantPermissionAdvancedActionReturnParams
} & PreparePermissionResponse

/**
* Represents the possible modes for a smart session.
Expand All @@ -57,7 +63,7 @@ export type UsePermissionModuleData = {
enableSessionData?: EnableSessionData
/** The index of the permission ID to use for the session. Defaults to 0. */
permissionIdIndex?: number
} & GrantPermissionAdvancedActionReturnParams
} & PreparePermissionResponse

type OptionalSessionKeyData = OneOf<
| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import type { Chain, Client, Hex, PublicClient, Transport } from "viem"
import { sendUserOperation } from "viem/account-abstraction"
import type { Chain, Client, PublicClient, Transport } from "viem"
import { getAction, parseAccount } from "viem/utils"
import { AccountNotFoundError } from "../../../account/utils/AccountNotFound"
import type { AnyData, ModularSmartAccount } from "../../utils/Types"
import type { CreateSessionDataParams } from "../Types"
import type { ModularSmartAccount } from "../../utils/Types"
import type {
CreateSessionDataParams,
PreparePermissionResponse,
SessionData
} from "../Types"
import { preparePermission } from "./preparePermission"
import {
getAccount,
getEnableSessionDetails,
MAINNET_ADDRESS_K1_VALIDATOR_ADDRESS,
SmartSessionMode
} from "../../../constants"

/**
* Parameters for creating sessions in a modular smart account.
Expand All @@ -16,21 +25,13 @@ export type GrantPermissionParameters<
> = {
/** Array of session data parameters for creating multiple sessions. */
sessionRequestedInfo: CreateSessionDataParams[]
/** The maximum fee per gas unit the transaction is willing to pay. */
maxFeePerGas?: bigint
/** The maximum priority fee per gas unit the transaction is willing to pay. */
maxPriorityFeePerGas?: bigint
/** The nonce of the transaction. If not provided, it will be determined automatically. */
nonce?: bigint
/** Optional public client for blockchain interactions. */
publicClient?: PublicClient
/** The modular smart account to create sessions for. If not provided, the client's account will be used. */
account?: TModularSmartAccount
/** Optional attesters to trust. */
attesters?: Hex[]
}

export type GrantPermissionResponse = AnyData
export type GrantPermissionResponse = SessionData["moduleData"]
/**
* Adds multiple sessions to the SmartSessionValidator module of a given smart account.
*
Expand Down Expand Up @@ -81,12 +82,7 @@ export async function grantPermission<
client: Client<Transport, Chain | undefined, TModularSmartAccount>,
parameters: GrantPermissionParameters<TModularSmartAccount>
): Promise<GrantPermissionResponse> {
const {
account: account_ = client.account,
maxFeePerGas,
maxPriorityFeePerGas,
nonce
} = parameters
const { account: account_ = client.account } = parameters

if (!account_) {
throw new AccountNotFoundError({
Expand All @@ -95,6 +91,8 @@ export async function grantPermission<
}

const account = parseAccount(account_) as ModularSmartAccount
const publicClient = account?.client as PublicClient

if (!account || !account.address) {
throw new Error("Account not found")
}
Expand All @@ -105,20 +103,30 @@ export async function grantPermission<
"preparePermission"
)(parameters)

const userOpHash = await getAction(
client,
sendUserOperation,
"sendUserOperation"
)({
calls: preparedPermission.calls,
maxFeePerGas,
maxPriorityFeePerGas,
nonce,
account
const nexusAccount = getAccount({
address: account.address,
type: "nexus"
})

const sessionDetailsWithPermissionEnableHash = await getEnableSessionDetails({
enableMode: SmartSessionMode.UNSAFE_ENABLE,
sessions: preparedPermission.sessions,
account: nexusAccount,
clients: [publicClient],
enableValidatorAddress: MAINNET_ADDRESS_K1_VALIDATOR_ADDRESS
})

const { permissionEnableHash, ...sessionDetails } =
sessionDetailsWithPermissionEnableHash

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

return {
userOpHash,
...preparedPermission
permissionIds: preparedPermission.permissionIds,
action: preparedPermission.action,
mode: SmartSessionMode.UNSAFE_ENABLE,
sessions: preparedPermission.sessions,
enableSessionData: sessionDetails.enableSessionData
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import { AccountNotFoundError } from "../../../account/utils/AccountNotFound"
import type { ModularSmartAccount } from "../../utils/Types"
import type {
CreateSessionDataParams,
GrantPermissionAdvancedResponse
GrantPermissionInAdvanceResponse
} from "../Types"
import { preparePermission } from "./preparePermission"
import type { Call } from "../../../account/utils/Types"

/**
* Parameters for creating sessions in a modular smart account.
*
* @template TModularSmartAccount - Type of the modular smart account, extending ModularSmartAccount or undefined.
*/
export type GrantPermissionAdvancedParameters<
export type GrantPermissionInAdvanceParameters<
TModularSmartAccount extends ModularSmartAccount | undefined
> = {
/** Array of session data parameters for creating multiple sessions. */
Expand All @@ -31,6 +32,8 @@ export type GrantPermissionAdvancedParameters<
account?: TModularSmartAccount
/** Optional attesters to trust. */
attesters?: Hex[]
/** Additional calls to be included in the user operation. */
calls?: Call[]
}

/**
Expand All @@ -50,9 +53,9 @@ export type GrantPermissionAdvancedParameters<
*
* @example
* ```typescript
* import { grantPermissionAdvanced } from '@biconomy/sdk'
* import { grantPermissionInAdvance } from '@biconomy/sdk'
*
* const result = await grantPermissionAdvanced(nexusClient, {
* const result = await grantPermissionInAdvance(nexusClient, {
* sessionRequestedInfo: [
* {
* sessionKeyData: '0x...',
Expand All @@ -77,17 +80,18 @@ export type GrantPermissionAdvancedParameters<
* - The number of sessions created is determined by the length of the `sessionRequestedInfo` array.
* - Each session's policies and permissions are determined by the `actionPoliciesInfo` provided.
*/
export async function grantPermissionAdvanced<
export async function grantPermissionInAdvance<
TModularSmartAccount extends ModularSmartAccount | undefined
>(
client: Client<Transport, Chain | undefined, TModularSmartAccount>,
parameters: GrantPermissionAdvancedParameters<TModularSmartAccount>
): Promise<GrantPermissionAdvancedResponse> {
parameters: GrantPermissionInAdvanceParameters<TModularSmartAccount>
): Promise<GrantPermissionInAdvanceResponse> {
const {
account: account_ = client.account,
maxFeePerGas,
maxPriorityFeePerGas,
nonce
nonce,
calls: calls_
} = parameters

if (!account_) {
Expand All @@ -112,7 +116,13 @@ export async function grantPermissionAdvanced<
sendUserOperation,
"sendUserOperation"
)({
calls: preparedPermission.calls,
calls: [
{
to: preparedPermission.action.target,
data: preparedPermission.action.callData
},
...(calls_ || [])
],
maxFeePerGas,
maxPriorityFeePerGas,
nonce,
Expand Down
Loading

0 comments on commit c4f458a

Please sign in to comment.