-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from rhinestonewtf/feat/add-encode-module-data
feat: add helpers to encode module data
- Loading branch information
Showing
20 changed files
with
436 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
29 changes: 29 additions & 0 deletions
29
src/account/erc7579-implementation/api/encodeModuleInstallationData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
src/account/erc7579-implementation/api/encodeModuleUninstallationData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
} |
Oops, something went wrong.