Skip to content

Commit

Permalink
Merge pull request rhinestonewtf#80 from rhinestonewtf/feat/encode-va…
Browse files Browse the repository at this point in the history
…lidator-nonce

feat: add encode validator nonce helper
  • Loading branch information
kopy-kat authored Oct 9, 2024
2 parents 2f21dfd + a6fd96a commit fa8c219
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/account/api/encodeValidatorNonce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Hex, PublicClient } from 'viem'
import { Account } from '../types'
import { getAccountImplementation } from './getAccountImplementation'
import { Module } from '../../module/types'

export const encodeValidatorNonce = ({
account,
validator,
}: {
account: Account
validator: Module
}): bigint => {
const accountImplementation = getAccountImplementation({ account })
return accountImplementation.encodeValidatorNonce({
validator,
})
}
1 change: 1 addition & 0 deletions src/account/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './encode1271Signature'
export * from './encode1271Hash'
export * from './encodeModuleInstallationData'
export * from './encodeModuleUninstallationData'
export * from './encodeValidatorNonce'
11 changes: 11 additions & 0 deletions src/account/erc7579-implementation/api/encodeValidatorNonce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { pad } from 'viem'
import { Module } from '../../../module/types'

export const encodeValidatorNonce = ({ validator }: { validator: Module }) => {
return BigInt(
pad(validator.address, {
dir: 'right',
size: 24,
}),
)
}
5 changes: 5 additions & 0 deletions src/account/erc7579-implementation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { encode1271Signature } from './api/encode1271Signature'
import { encode1271Hash } from './api/encode1271Hash'
import { encodeModuleInstallationData } from './api/encodeModuleInstallationData'
import { encodeModuleUninstallationData } from './api/encodeModuleUninstallationData'
import { encodeValidatorNonce } from './api/encodeValidatorNonce'

export class ERC7579Implementation {
getInstalledModules = async ({
Expand Down Expand Up @@ -101,4 +102,8 @@ export class ERC7579Implementation {
}): Promise<Hex> => {
return await encodeModuleUninstallationData({ client, account, module })
}

encodeValidatorNonce = ({ validator }: { validator: Module }): bigint => {
return encodeValidatorNonce({ validator })
}
}
1 change: 1 addition & 0 deletions src/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export {
encode1271Hash,
encodeModuleInstallationData,
encodeModuleUninstallationData,
encodeValidatorNonce,
} from './api'

export type { Account, AccountType, Execution, InitialModules } from './types'
21 changes: 21 additions & 0 deletions src/account/kernel/api/encodeValidatorNonce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { pad, encodePacked } from 'viem'
import { KernelModule } from '../types'

export const encodeValidatorNonce = ({
validator,
}: {
validator: KernelModule
}) => {
return BigInt(
pad(
encodePacked(
['bytes1', 'bytes1', 'address'],
['0x00', '0x00', validator.address],
),
{
dir: 'right',
size: 24,
},
),
)
}
9 changes: 9 additions & 0 deletions src/account/kernel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { encode1271Signature } from './api/encode1271Signature'
import { encode1271Hash } from './api/encode1271Hash'
import { encodeModuleInstallationData } from './api/encodeModuleInstallationData'
import { encodeModuleUninstallationData } from './api/encodeModuleUninstallationData'
import { encodeValidatorNonce } from './api/encodeValidatorNonce'

export class KernelImplementation {
getInstalledModules = async ({
Expand Down Expand Up @@ -101,4 +102,12 @@ export class KernelImplementation {
}): Promise<Hex> => {
return await encodeModuleUninstallationData({ client, account, module })
}

encodeValidatorNonce = ({
validator,
}: {
validator: KernelModule
}): bigint => {
return encodeValidatorNonce({ validator })
}
}
1 change: 1 addition & 0 deletions src/account/kernel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CallType } from '../../module/types'
import { Address, Hex } from 'viem'

export type KernelModule = {
address: Address
module: Address
initData: Hex
deInitData: Hex
Expand Down
11 changes: 11 additions & 0 deletions src/account/nexus/api/encodeValidatorNonce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { pad } from 'viem'
import { Module } from '../../../module/types'

export const encodeValidatorNonce = ({ validator }: { validator: Module }) => {
return BigInt(
pad(validator.address, {
dir: 'right',
size: 24,
}),
)
}
5 changes: 5 additions & 0 deletions src/account/nexus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { encode1271Signature } from './api/encode1271Signature'
import { encode1271Hash } from './api/encode1271Hash'
import { encodeModuleInstallationData } from './api/encodeModuleInstallationData'
import { encodeModuleUninstallationData } from './api/encodeModuleUninstallationData'
import { encodeValidatorNonce } from './api/encodeValidatorNonce'

export class NexusImplementation {
getInstalledModules = async ({
Expand Down Expand Up @@ -101,4 +102,8 @@ export class NexusImplementation {
}): Promise<Hex> => {
return await encodeModuleUninstallationData({ client, account, module })
}

encodeValidatorNonce = ({ validator }: { validator: Module }): bigint => {
return encodeValidatorNonce({ validator })
}
}
11 changes: 11 additions & 0 deletions src/account/safe/api/encodeValidatorNonce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { pad } from 'viem'
import { Module } from '../../../module/types'

export const encodeValidatorNonce = ({ validator }: { validator: Module }) => {
return BigInt(
pad(validator.address, {
dir: 'right',
size: 24,
}),
)
}
5 changes: 5 additions & 0 deletions src/account/safe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { encode1271Signature } from './api/encode1271Signature'
import { encode1271Hash } from './api/encode1271Hash'
import { encodeModuleInstallationData } from './api/encodeModuleInstallationData'
import { encodeModuleUninstallationData } from './api/encodeModuleUninstallationData'
import { encodeValidatorNonce } from './api/encodeValidatorNonce'

export class SafeImplementation {
getInstalledModules = async ({
Expand Down Expand Up @@ -101,4 +102,8 @@ export class SafeImplementation {
}): Promise<Hex> => {
return await encodeModuleUninstallationData({ client, account, module })
}

encodeValidatorNonce = ({ validator }: { validator: Module }): bigint => {
return encodeValidatorNonce({ validator })
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {
encode1271Hash,
encodeModuleInstallationData,
encodeModuleUninstallationData,
encodeValidatorNonce,
} from './account'
export type { Account, AccountType, Execution, InitialModules } from './account'

Expand Down

0 comments on commit fa8c219

Please sign in to comment.