Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add encode 1271 hash #58

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/account/api/encode1271Hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Address, Hex } from 'viem'
import { Account, ERC1271HashParams } from '../types'
import { getAccountImplementation } from './getAccountImplementation'

export const encode1271Hash = ({
account,
chainId,
validator,
hash,
}: {
account: Account
chainId: number
validator: Address
hash: Hex
}): Hex => {
const accountImplementation = getAccountImplementation({ account })
return accountImplementation.encode1271Hash({
account: account.address,
chainId,
validator,
hash,
})
}
16 changes: 11 additions & 5 deletions src/account/api/encode1271Signature.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { Hex } from 'viem'
import { Account, Signature1271Params } from '../types'
import { Address, Hex } from 'viem'
import { Account, ERC1271SignatureParams } from '../types'
import { getAccountImplementation } from './getAccountImplementation'

export const encode1271Signature = ({
account,
signatureParams,
validator,
signature,
}: {
account: Account
signatureParams: Signature1271Params
validator: Address
signature: Hex
}): Hex => {
const accountImplementation = getAccountImplementation({ account })
return accountImplementation.encode1271Signature(signatureParams)
return accountImplementation.encode1271Signature({
account: account.address,
validator,
signature,
})
}
1 change: 1 addition & 0 deletions src/account/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './installModule'
export * from './isModuleInstalled'
export * from './uninstallModule'
export * from './encode1271Signature'
export * from './encode1271Hash'
11 changes: 11 additions & 0 deletions src/account/erc7579-implementation/api/encode1271Hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ERC1271HashParams } from '../../types'
import { Hex } from 'viem'

export const encode1271Hash = ({
account,
chainId,
validator,
hash,
}: ERC1271HashParams): Hex => {
return hash
}
5 changes: 3 additions & 2 deletions src/account/erc7579-implementation/api/encode1271Signature.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Signature1271Params } from '../../types'
import { ERC1271SignatureParams } from '../../types'
import { encodePacked, Hex } from 'viem'

export const encode1271Signature = ({
account,
validator,
signature,
}: Signature1271Params): Hex => {
}: ERC1271SignatureParams): Hex => {
return encodePacked(['address', 'bytes'], [validator, signature])
}
22 changes: 19 additions & 3 deletions src/account/erc7579-implementation/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { Address, Hex, PublicClient } from 'viem'
import { Account, Execution, Signature1271Params } from '../types'
import {
Account,
Execution,
ERC1271SignatureParams,
ERC1271HashParams,
} from '../types'
import { Module, ModuleType } from '../../module/types'
import { getInstalledModules } from './api/getInstalledModules'
import { installModule } from './api/installModule'
import { isModuleInstalled } from './api/isModuleInstalled'
import { uninstallModule } from './api/uninstallModule'
import { encode1271Signature } from './api/encode1271Signature'
import { encode1271Hash } from './api/encode1271Hash'

export class ERC7579Implementation {
getInstalledModules = async ({
Expand Down Expand Up @@ -56,9 +62,19 @@ export class ERC7579Implementation {
}

encode1271Signature = ({
account,
validator,
signature,
}: Signature1271Params): Hex => {
return encode1271Signature({ validator, signature })
}: ERC1271SignatureParams): Hex => {
return encode1271Signature({ account, validator, signature })
}

encode1271Hash = ({
account,
chainId,
validator,
hash,
}: ERC1271HashParams): Hex => {
return encode1271Hash({ account, chainId, validator, hash })
}
}
1 change: 1 addition & 0 deletions src/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export {
isModuleInstalled,
uninstallModule,
encode1271Signature,
encode1271Hash,
} from './api'

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

export const encode1271Hash = ({
account,
chainId,
validator,
hash,
}: ERC1271HashParams): Hex => {
return hash
}
5 changes: 3 additions & 2 deletions src/account/kernel/api/encode1271Signature.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Signature1271Params } from '../../types'
import { ERC1271SignatureParams } from '../../types'
import { encodePacked, Hex } from 'viem'

export const encode1271Signature = ({
account,
validator,
signature,
}: Signature1271Params): Hex => {
}: ERC1271SignatureParams): Hex => {
return encodePacked(
['bytes1', 'address', 'bytes'],
['0x01', validator, signature],
Expand Down
22 changes: 19 additions & 3 deletions src/account/kernel/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { Address, Hex, PublicClient } from 'viem'
import { Account, Execution, Signature1271Params } from '../types'
import {
Account,
Execution,
ERC1271SignatureParams,
ERC1271HashParams,
} from '../types'
import { getInstalledModules } from './api/getInstalledModules'
import { installModule } from './api/installModule'
import { isModuleInstalled } from './api/isModuleInstalled'
import { uninstallModule } from './api/uninstallModule'
import { KernelModule, KernelModuleType } from './types'
import { encode1271Signature } from './api/encode1271Signature'
import { encode1271Hash } from './api/encode1271Hash'

export class KernelImplementation {
getInstalledModules = async ({
Expand Down Expand Up @@ -56,9 +62,19 @@ export class KernelImplementation {
}

encode1271Signature = ({
account,
validator,
signature,
}: Signature1271Params): Hex => {
return encode1271Signature({ validator, signature })
}: ERC1271SignatureParams): Hex => {
return encode1271Signature({ account, validator, signature })
}

encode1271Hash = ({
account,
chainId,
validator,
hash,
}: ERC1271HashParams): Hex => {
return encode1271Hash({ account, chainId, validator, hash })
}
}
11 changes: 11 additions & 0 deletions src/account/nexus/api/encode1271Hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ERC1271HashParams } from '../../types'
import { Hex } from 'viem'

export const encode1271Hash = ({
account,
chainId,
validator,
hash,
}: ERC1271HashParams): Hex => {
return hash
}
5 changes: 3 additions & 2 deletions src/account/nexus/api/encode1271Signature.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Signature1271Params } from '../../types'
import { ERC1271SignatureParams } from '../../types'
import { encodePacked, Hex } from 'viem'

export const encode1271Signature = ({
account,
validator,
signature,
}: Signature1271Params): Hex => {
}: ERC1271SignatureParams): Hex => {
return encodePacked(['address', 'bytes'], [validator, signature])
}
22 changes: 19 additions & 3 deletions src/account/nexus/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { Address, Hex, PublicClient } from 'viem'
import { Account, Execution, Signature1271Params } from '../types'
import {
Account,
Execution,
ERC1271SignatureParams,
ERC1271HashParams,
} from '../types'
import { Module, ModuleType } from '../../module/types'
import { getInstalledModules } from './api/getInstalledModules'
import { installModule } from './api/installModule'
import { isModuleInstalled } from './api/isModuleInstalled'
import { uninstallModule } from './api/uninstallModule'
import { encode1271Signature } from './api/encode1271Signature'
import { encode1271Hash } from './api/encode1271Hash'

export class NexusImplementation {
getInstalledModules = async ({
Expand Down Expand Up @@ -56,9 +62,19 @@ export class NexusImplementation {
}

encode1271Signature = ({
account,
validator,
signature,
}: Signature1271Params): Hex => {
return encode1271Signature({ validator, signature })
}: ERC1271SignatureParams): Hex => {
return encode1271Signature({ account, validator, signature })
}

encode1271Hash = ({
account,
chainId,
validator,
hash,
}: ERC1271HashParams): Hex => {
return encode1271Hash({ account, chainId, validator, hash })
}
}
26 changes: 26 additions & 0 deletions src/account/safe/api/encode1271Hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ERC1271HashParams } from '../../types'
import { hashTypedData, Hex } from 'viem'

export const encode1271Hash = ({
account,
chainId,
validator,
hash,
}: ERC1271HashParams): Hex => {
if (validator == account) {
return hashTypedData({
domain: {
chainId: chainId,
verifyingContract: account,
},
types: {
SafeMessage: [{ name: 'message', type: 'bytes' }],
},
primaryType: 'SafeMessage',
message: {
message: hash,
},
})
}
return hash
}
16 changes: 12 additions & 4 deletions src/account/safe/api/encode1271Signature.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { Signature1271Params } from '../../types'
import { encodePacked, Hex } from 'viem'
import { ERC1271SignatureParams } from '../../types'
import { concat, encodePacked, fromHex, Hex, slice, toHex } from 'viem'

export const encode1271Signature = ({
account,
validator,
signature,
}: Signature1271Params): Hex => {
return encodePacked(['address', 'bytes'], [validator, signature])
}: ERC1271SignatureParams): Hex => {
let formattedSignature = signature
if (account == validator) {
const v = fromHex(slice(signature, 64, 65), 'number')
if (v < 30) {
formattedSignature = concat([slice(signature, 0, 64), toHex(v + 4)])
}
}
return encodePacked(['address', 'bytes'], [validator, formattedSignature])
}
22 changes: 19 additions & 3 deletions src/account/safe/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { Address, Hex, PublicClient } from 'viem'
import { Account, Execution, Signature1271Params } from '../types'
import {
Account,
Execution,
ERC1271SignatureParams,
ERC1271HashParams,
} from '../types'
import { getInstalledModules } from './api/getInstalledModules'
import { installModule } from './api/installModule'
import { isModuleInstalled } from './api/isModuleInstalled'
import { uninstallModule } from './api/uninstallModule'
import { Module, ModuleType } from '../../module'
import { encode1271Signature } from './api/encode1271Signature'
import { encode1271Hash } from './api/encode1271Hash'

export class SafeImplementation {
getInstalledModules = async ({
Expand Down Expand Up @@ -56,9 +62,19 @@ export class SafeImplementation {
}

encode1271Signature = ({
account,
validator,
signature,
}: Signature1271Params): Hex => {
return encode1271Signature({ validator, signature })
}: ERC1271SignatureParams): Hex => {
return encode1271Signature({ account, validator, signature })
}

encode1271Hash = ({
account,
chainId,
validator,
hash,
}: ERC1271HashParams): Hex => {
return encode1271Hash({ account, chainId, validator, hash })
}
}
10 changes: 9 additions & 1 deletion src/account/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ export type InitialModules = {
fallbacks: Module[]
}

export type Signature1271Params = {
export type ERC1271SignatureParams = {
account: Address
validator: Address
signature: Hex
}

export type ERC1271HashParams = {
account: Address
chainId: number
validator: Address
hash: Hex
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
isModuleInstalled,
uninstallModule,
encode1271Signature,
encode1271Hash,
} from './account'
export type { Account, AccountType, Execution, InitialModules } from './account'

Expand Down
Loading