Skip to content

Commit

Permalink
chore: add Kernel smart account grant permissions request
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaisailovic committed Jun 26, 2024
1 parent 6a94d64 commit a898f9b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import {
Transport,
TypedDataDefinition,
validateTypedData,
WalletGrantPermissionsParameters,
WalletGrantPermissionsReturnType,
zeroAddress
} from 'viem'
import { privateKeyToAccount, signMessage } from 'viem/accounts'
import { privateKeyToAccount, publicKeyToAddress, signMessage } from 'viem/accounts'
import { EIP155Wallet } from '../EIP155Lib'
import { JsonRpcProvider } from '@ethersproject/providers'
import { KernelValidator, signerToEcdsaValidator } from '@zerodev/ecdsa-validator'
Expand Down Expand Up @@ -44,14 +46,23 @@ import {
SECP256K1_SIGNATURE_VALIDATOR_ADDRESS
} from '@/utils/permissionValidatorUtils/constants'
import { executeAbi } from '@/utils/safe7579AccountUtils/abis/Account'
import { ENTRYPOINT_ADDRESS_V07_TYPE } from 'permissionless/_types/types'
import {
getPermissionScopeData,
PermissionContext,
SingleSignerPermission
} from '@/utils/permissionValidatorUtils'
import { KERNEL_V2_4, KERNEL_V3_1 } from '@zerodev/sdk/constants'
import { KERNEL_V2_VERSION_TYPE, KERNEL_V3_VERSION_TYPE } from '@zerodev/sdk/types'
import { decodeDIDToSECP256k1PublicKey } from '@/utils/HelperUtil'
import { KeySigner } from 'viem/_types/experimental/erc7715/types/signer'


type DonutPurchasePermissionData = {
target: string
abi: any
valueLimit: bigint
functionName: string
}

type SmartAccountLibOptions = {
privateKey: string
Expand Down Expand Up @@ -279,6 +290,70 @@ export class KernelSmartAccountLib implements EIP155Wallet {
return serializedSessionKey
}

async grantPermissions(
grantPermissionsRequestParams: WalletGrantPermissionsParameters
): Promise<WalletGrantPermissionsReturnType> {
if (!this.publicClient) {
throw new Error('Client not initialized')
}
console.log('grantPermissions', {grantPermissionsRequestParams});

const signer = grantPermissionsRequestParams.signer
// check if signer type is AccountSigner then it will have data.id
if (signer && !(signer.type === 'key')) {
throw Error('Currently only supporting KeySigner Type for permissions')
}

const typedSigner = signer as KeySigner
const id = typedSigner.data.id
const targetAddress = id.replace('did:ethr:','')
const emptySessionKeySigner = addressToEmptyAccount(targetAddress as `0x${string}`)


const permissions = grantPermissionsRequestParams.permissions
const zeroDevPermissions = []

for (const permission of permissions) {
if (permission.type === 'donut-purchase') {
const data = permission.data as DonutPurchasePermissionData
zeroDevPermissions.push({
target: data.target,
abi: data.abi,
valueLimit: data.valueLimit,
functionName: data.functionName
})

}
}
const sessionKeyValidator = await signerToSessionKeyValidator(this.publicClient, {
signer: emptySessionKeySigner,
validatorData: {
// @ts-ignore
permissions: zeroDevPermissions
},
kernelVersion: this.kernelVersion,
entryPoint: this.entryPoint
})
const sessionKeyAccount = await createKernelAccount(this.publicClient, {
plugins: {
sudo: this.validator,
regular: sessionKeyValidator
},
entryPoint: this.entryPoint,
kernelVersion: this.kernelVersion
})

const serializedSessionKey = await serializeSessionKeyAccount(sessionKeyAccount)


return {
permissionsContext: serializedSessionKey,
grantedPermissions: grantPermissionsRequestParams.permissions,
expiry: grantPermissionsRequestParams.expiry
} as WalletGrantPermissionsReturnType

}

async updateCoSigners(signers: `0x${string}`[]) {
if (!this.client || !this.publicClient || !this.client.account) {
throw new Error('Client not initialized')
Expand Down Expand Up @@ -329,6 +404,8 @@ export class KernelSmartAccountLib implements EIP155Wallet {
return currentNonce
}



async issuePermissionContext(
targetAddress: Address,
approvedPermissions: any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
import {
Address,
Hex,
WalletGrantPermissionsParameters,
WalletGrantPermissionsReturnType,
concatHex,
encodeFunctionData,
encodePacked,
Expand All @@ -31,7 +33,6 @@ import { Execution } from '@/utils/safe7579AccountUtils/userop'
import { isModuleInstalledAbi } from '@/utils/safe7579AccountUtils/abis/Account'
import { ethers } from 'ethers'
import { SAFE7579_USER_OPERATION_BUILDER_ADDRESS } from '@/utils/safe7579AccountUtils/constants'
import { GrantPermissionsParameters, GrantPermissionsReturnType } from 'viem/experimental'
import { KeySigner } from 'viem/_types/experimental/erc7715/types/signer'
import { decodeDIDToSECP256k1PublicKey } from '@/utils/HelperUtil'

Expand Down Expand Up @@ -105,8 +106,8 @@ export class SafeSmartAccountLib extends SmartAccountLib {
}

async grantPermissions(
grantPermissionsRequestParams: GrantPermissionsParameters
): Promise<GrantPermissionsReturnType> {
grantPermissionsRequestParams: WalletGrantPermissionsParameters
): Promise<WalletGrantPermissionsReturnType> {
if (!this.client?.account) {
throw new Error('Client not initialized')
}
Expand Down Expand Up @@ -153,7 +154,7 @@ export class SafeSmartAccountLib extends SmartAccountLib {
userOpBuilder: SAFE7579_USER_OPERATION_BUILDER_ADDRESS,
submitToAddress: this.client.account.address
}
} as GrantPermissionsReturnType
} as WalletGrantPermissionsReturnType
}

private async setupSafe7579(calls: Execution | Execution[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { SafeSmartAccountLib } from '@/lib/smart-accounts/SafeSmartAccountLib'
import { web3wallet } from './WalletConnectUtil'
import { smartAccountWallets } from './SmartAccountUtil'
import { GrantPermissionsParameters, GrantPermissionsReturnType } from 'viem/experimental'
import { KernelSmartAccountLib } from '@/lib/smart-accounts/KernelSmartAccountLib'
import { WalletGrantPermissionsParameters, WalletGrantPermissionsReturnType } from 'viem'
type RequestEventArgs = Omit<SignClientTypes.EventArguments['session_request'], 'verifyContext'>

function getSmartWalletAddressFromSession(requestSession: SessionTypes.Struct) {
Expand Down Expand Up @@ -43,12 +45,12 @@ export async function approveEIP7715Request(requestEvent: RequestEventArgs) {
switch (request.method) {
case EIP7715_METHOD.WALLET_GRANT_PERMISSIONS: {
const wallet = getSmartWalletAddressFromSession(requestSession)
let grantPermissionsRequestParams: GrantPermissionsParameters = request.params[0]
if (wallet instanceof SafeSmartAccountLib) {
const grantPermissionsResponse: GrantPermissionsReturnType = await wallet.grantPermissions(
let grantPermissionsRequestParams: WalletGrantPermissionsParameters = request.params[0]
if (wallet instanceof SafeSmartAccountLib || wallet instanceof KernelSmartAccountLib) {
const grantPermissionsResponse: WalletGrantPermissionsReturnType = await wallet.grantPermissions(
grantPermissionsRequestParams
)
return formatJsonRpcResult<GrantPermissionsReturnType>(id, grantPermissionsResponse)
return formatJsonRpcResult<WalletGrantPermissionsReturnType>(id, grantPermissionsResponse)
}

// for any other wallet instance return un_supported
Expand Down

0 comments on commit a898f9b

Please sign in to comment.