Skip to content

Commit

Permalink
Merge pull request #77 from rhinestonewtf/feat/add-encode-module-data
Browse files Browse the repository at this point in the history
feat: add helpers to encode module data
  • Loading branch information
kopy-kat authored Oct 8, 2024
2 parents 1de63fa + 4b20163 commit f811187
Show file tree
Hide file tree
Showing 20 changed files with 436 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{
"name": "module-sdk (minimal surface - tree-shaking)",
"path": "./src/_esm/index.js",
"limit": "14 kB",
"limit": "15 kB",
"import": "{ installModule }"
}
]
18 changes: 18 additions & 0 deletions src/account/api/encodeModuleInstallationData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Hex } from 'viem'
import { Account } from '../types'
import { getAccountImplementation } from './getAccountImplementation'
import { Module } from '../../module/types'

export const encodeModuleInstallationData = ({
account,
module,
}: {
account: Account
module: Module
}): Hex => {
const accountImplementation = getAccountImplementation({ account })
return accountImplementation.encodeModuleInstallationData({
account,
module,
})
}
21 changes: 21 additions & 0 deletions src/account/api/encodeModuleUninstallationData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Hex, PublicClient } from 'viem'
import { Account } from '../types'
import { getAccountImplementation } from './getAccountImplementation'
import { Module } from '../../module/types'

export const encodeModuleUninstallationData = async ({
client,
account,
module,
}: {
client: PublicClient
account: Account
module: Module
}): Promise<Hex> => {
const accountImplementation = getAccountImplementation({ account })
return await accountImplementation.encodeModuleUninstallationData({
client,
account,
module,
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Account } from '../../types'
import { Hex, encodePacked } from 'viem'
import { Module, CallType } from '../../../module/types'

export const encodeModuleInstallationData = ({
account,
module,
}: {
account: Account
module: Module
}): Hex => {
switch (module.type) {
case 'validator':
case 'executor':
case 'hook':
return module.initData
case 'fallback':
return encodePacked(
['bytes4', 'bytes1', 'bytes'],
[
module.selector!,
module.callType ?? CallType.CALLTYPE_SINGLE,
module.initData,
],
)
default:
throw new Error(`Unknown module type ${module.type}`)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Account } from '../../types'
import { Hex, PublicClient, encodePacked, encodeAbiParameters } from 'viem'
import { Module, CallType } from '../../../module/types'
import { getPreviousModule } from '../../../common'

export const encodeModuleUninstallationData = async ({
client,
account,
module,
}: {
client: PublicClient
account: Account
module: Module
}): Promise<Hex> => {
switch (module.type) {
case 'validator':
case 'executor':
const prev = await getPreviousModule({ client, account, module })
return encodeAbiParameters(
[
{ name: 'prev', type: 'address' },
{ name: 'disableModuleData', type: 'bytes' },
],
[prev, module.deInitData],
)

case 'hook':
return module.deInitData
case 'fallback':
return encodePacked(
['bytes4', 'bytes'],
[module.selector!, module.deInitData],
)
default:
throw new Error(`Unknown module type ${module.type}`)
}
}
4 changes: 2 additions & 2 deletions src/account/erc7579-implementation/api/uninstallModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const _uninstallModule = async ({
const isInstalled = await isModuleInstalled({ client, account, module })

if (isInstalled) {
let moduleData = module.initData || '0x'
let moduleData = module.deInitData || '0x'
if (module.type === 'validator' || module.type === 'executor') {
const prev = await getPreviousModule({ client, account, module })
moduleData = encodeAbiParameters(
Expand Down Expand Up @@ -111,7 +111,7 @@ const _uninstallFallback = async ({
module.module,
encodePacked(
['bytes4', 'bytes'],
[module.selector!, module.initData ?? '0x'],
[module.selector!, module.deInitData],
),
],
})
Expand Down
24 changes: 24 additions & 0 deletions src/account/erc7579-implementation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { isModuleInstalled } from './api/isModuleInstalled'
import { uninstallModule } from './api/uninstallModule'
import { encode1271Signature } from './api/encode1271Signature'
import { encode1271Hash } from './api/encode1271Hash'
import { encodeModuleInstallationData } from './api/encodeModuleInstallationData'
import { encodeModuleUninstallationData } from './api/encodeModuleUninstallationData'

export class ERC7579Implementation {
getInstalledModules = async ({
Expand Down Expand Up @@ -77,4 +79,26 @@ export class ERC7579Implementation {
}: ERC1271HashParams): Hex => {
return encode1271Hash({ account, chainId, validator, hash })
}

encodeModuleInstallationData = ({
account,
module,
}: {
account: Account
module: Module
}): Hex => {
return encodeModuleInstallationData({ account, module })
}

encodeModuleUninstallationData = async ({
client,
account,
module,
}: {
client: PublicClient
account: Account
module: Module
}): Promise<Hex> => {
return await encodeModuleUninstallationData({ client, account, module })
}
}
48 changes: 48 additions & 0 deletions src/account/kernel/api/encodeModuleInstallationData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Account } from '../../types'
import { Hex, encodePacked, encodeAbiParameters } from 'viem'
import { KernelModule } from '../types'

export const encodeModuleInstallationData = ({
account,
module,
}: {
account: Account
module: KernelModule
}): Hex => {
switch (module.type) {
case 'validator':
case 'executor':
return encodePacked(
['address', 'bytes'],
[
module.hook!,
encodeAbiParameters(
[{ type: 'bytes' }, { type: 'bytes' }],
[module.initData, '0x'],
),
],
)
case 'hook':
return module.initData
case 'fallback':
return encodePacked(
['bytes4', 'address', 'bytes'],
[
module.selector!,
module.hook!,
encodeAbiParameters(
[{ type: 'bytes' }, { type: 'bytes' }],
[
encodePacked(
['bytes1', 'bytes'],
[module.callType!, module.initData],
),
'0x',
],
),
],
)
default:
throw new Error(`Unknown module type ${module.type}`)
}
}
27 changes: 27 additions & 0 deletions src/account/kernel/api/encodeModuleUninstallationData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Account } from '../../types'
import { Hex, PublicClient, encodePacked } from 'viem'
import { KernelModule } from '../types'

export const encodeModuleUninstallationData = async ({
client,
account,
module,
}: {
client: PublicClient
account: Account
module: KernelModule
}): Promise<Hex> => {
switch (module.type) {
case 'validator':
case 'executor':
case 'hook':
return module.deInitData
case 'fallback':
return encodePacked(
['bytes4', 'bytes'],
[module.selector!, module.deInitData],
)
default:
throw new Error(`Unknown module type ${module.type}`)
}
}
4 changes: 2 additions & 2 deletions src/account/kernel/api/uninstallModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const _uninstallModule = async ({
args: [
BigInt(kernelModuleTypeIds[module.type]),
module.module,
module.initData || '0x',
module.deInitData,
],
})

Expand Down Expand Up @@ -100,7 +100,7 @@ const _uninstallFallback = async ({
module.module,
encodePacked(
['bytes4', 'bytes'],
[module.selector!, module.initData || '0x'],
[module.selector!, module.deInitData],
),
],
})
Expand Down
24 changes: 24 additions & 0 deletions src/account/kernel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { uninstallModule } from './api/uninstallModule'
import { KernelModule, KernelModuleType } from './types'
import { encode1271Signature } from './api/encode1271Signature'
import { encode1271Hash } from './api/encode1271Hash'
import { encodeModuleInstallationData } from './api/encodeModuleInstallationData'
import { encodeModuleUninstallationData } from './api/encodeModuleUninstallationData'

export class KernelImplementation {
getInstalledModules = async ({
Expand Down Expand Up @@ -77,4 +79,26 @@ export class KernelImplementation {
}: ERC1271HashParams): Hex => {
return encode1271Hash({ account, chainId, validator, hash })
}

encodeModuleInstallationData = ({
account,
module,
}: {
account: Account
module: KernelModule
}): Hex => {
return encodeModuleInstallationData({ account, module })
}

encodeModuleUninstallationData = async ({
client,
account,
module,
}: {
client: PublicClient
account: Account
module: KernelModule
}): Promise<Hex> => {
return await encodeModuleUninstallationData({ client, account, module })
}
}
5 changes: 3 additions & 2 deletions src/account/kernel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { Address, Hex } from 'viem'

export type KernelModule = {
module: Address
initData?: Hex
additionalContext?: Hex
initData: Hex
deInitData: Hex
additionalContext: Hex
type: KernelModuleType
hook?: Address
selector?: Hex
Expand Down
29 changes: 29 additions & 0 deletions src/account/nexus/api/encodeModuleInstallationData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Account } from '../../types'
import { Hex, encodePacked } from 'viem'
import { Module, CallType } from '../../../module/types'

export const encodeModuleInstallationData = ({
account,
module,
}: {
account: Account
module: Module
}): Hex => {
switch (module.type) {
case 'validator':
case 'executor':
case 'hook':
return module.initData
case 'fallback':
return encodePacked(
['bytes4', 'bytes1', 'bytes'],
[
module.selector!,
module.callType ?? CallType.CALLTYPE_SINGLE,
module.initData,
],
)
default:
throw new Error(`Unknown module type ${module.type}`)
}
}
37 changes: 37 additions & 0 deletions src/account/nexus/api/encodeModuleUninstallationData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Account } from '../../types'
import { Hex, PublicClient, encodePacked, encodeAbiParameters } from 'viem'
import { Module, CallType } from '../../../module/types'
import { getPreviousModule } from '../../../common'

export const encodeModuleUninstallationData = async ({
client,
account,
module,
}: {
client: PublicClient
account: Account
module: Module
}): Promise<Hex> => {
switch (module.type) {
case 'validator':
case 'executor':
const prev = await getPreviousModule({ client, account, module })
return encodeAbiParameters(
[
{ name: 'prev', type: 'address' },
{ name: 'disableModuleData', type: 'bytes' },
],
[prev, module.deInitData],
)

case 'hook':
return module.deInitData
case 'fallback':
return encodePacked(
['bytes4', 'bytes'],
[module.selector!, module.deInitData],
)
default:
throw new Error(`Unknown module type ${module.type}`)
}
}
Loading

0 comments on commit f811187

Please sign in to comment.