Skip to content

Commit

Permalink
chore: refactor signer -> holder
Browse files Browse the repository at this point in the history
  • Loading branch information
joepegler committed Sep 17, 2024
1 parent 3d093c9 commit 8ce67b1
Show file tree
Hide file tree
Showing 30 changed files with 212 additions and 714 deletions.
2 changes: 0 additions & 2 deletions packages/sdk/account/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export * from "./utils/index.js"
export * from "./signers/local-account.js"
export * from "./signers/wallet-client.js"
export * from "./toNexusAccount.js"
55 changes: 0 additions & 55 deletions packages/sdk/account/signers/local-account.ts

This file was deleted.

48 changes: 0 additions & 48 deletions packages/sdk/account/signers/wallet-client.ts

This file was deleted.

5 changes: 2 additions & 3 deletions packages/sdk/account/toNexusAccount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("nexus.account", async () => {
})

nexusClient = await createNexusClient({
owner: account,
holder: account,
chain,
transport: http(),
bundlerTransport: http(bundlerUrl)
Expand Down Expand Up @@ -275,7 +275,7 @@ describe("nexus.account", async () => {
expect(contractResponse).toBe(eip1271MagicValue)
})

test.skip("should sign using signTypedData SDK method", async () => {
test("should sign using signTypedData SDK method", async () => {
const appDomain = {
chainId: chain.id,
name: "TokenWithPermit",
Expand Down Expand Up @@ -348,7 +348,6 @@ describe("nexus.account", async () => {
})

const permitTokenResponse = await nexusClient.writeContract({
account: account,
address: mockAddresses.TokenWithPermit,
abi: TokenWithPermitAbi,
functionName: "permitWith1271",
Expand Down
67 changes: 34 additions & 33 deletions packages/sdk/account/toNexusAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
type RpcSchema,
type SignableMessage,
type Transport,
type TypedData,
type TypedDataDefinition,
type UnionPartialBy,
concat,
Expand Down Expand Up @@ -37,7 +38,6 @@ import {
getUserOperationHash,
toSmartAccount
} from "viem/account-abstraction"
import { parseAccount } from "viem/accounts"
import contracts from "../__contracts"
import { EntrypointAbi, K1ValidatorFactoryAbi } from "../__contracts/abi"
import type { Call, GetNonceArgs, UserOperationStruct } from "./utils/Types"
Expand All @@ -53,19 +53,20 @@ import {
import type { BaseExecutionModule } from "../modules/base/BaseExecutionModule"
import type { BaseValidationModule } from "../modules/base/BaseValidationModule"
import { K1ValidatorModule } from "../modules/validators/K1ValidatorModule"
import { WalletClientSigner } from "./signers/wallet-client"
import {
type TypedDataWith712,
eip712WrapHash,
getAccountDomainStructFields,
getTypesForEIP712Domain,
packUserOp,
typeToString
} from "./utils/Utils"
import { type UnknownHolder, toHolder } from "./utils/toHolder"

export type ToNexusSmartAccountParameters = {
chain: Chain
transport: ClientConfig["transport"]
owner: Account | Address
holder: UnknownHolder
index?: bigint | undefined
activeModule?: BaseValidationModule
executorModule?: BaseExecutionModule
Expand Down Expand Up @@ -123,7 +124,7 @@ export const toNexusAccount = async (
const {
chain,
transport,
owner,
holder: holder_,
index = 0n,
activeModule,
executorModule: _,
Expand All @@ -133,8 +134,10 @@ export const toNexusAccount = async (
name = "Nexus Account"
} = parameters

const holder = await toHolder({ holder: holder_ })

const masterClient = createWalletClient({
account: parseAccount(owner),
account: holder,
chain,
transport,
key,
Expand All @@ -143,7 +146,6 @@ export const toNexusAccount = async (
.extend(walletActions)
.extend(publicActions)

const moduleSigner = new WalletClientSigner(masterClient, "viem")
const signerAddress = masterClient.account.address
const entryPointContract = getContract({
address: contracts.entryPoint.address,
Expand All @@ -169,7 +171,7 @@ export const toNexusAccount = async (
context: signerAddress,
additionalContext: "0x"
},
moduleSigner
holder
)

let _accountAddress: Address
Expand Down Expand Up @@ -337,8 +339,8 @@ export const toNexusAccount = async (
message
}: { message: SignableMessage }): Promise<Hex> => {
const tempSignature = await defaultedActiveModule
.getSigner()
.signMessage(message)
.getHolder()
.signMessage({ message })

const signature = encodePacked(
["address", "bytes"],
Expand Down Expand Up @@ -370,33 +372,32 @@ export const toNexusAccount = async (
return accountIsDeployed ? signature : erc6492Signature
}

const signTypedData = async (
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
typedData: any
): Promise<Hex> => {
async function signTypedData<
const typedData extends TypedData | Record<string, unknown>,
primaryType extends keyof typedData | "EIP712Domain" = keyof typedData
>(parameters: TypedDataDefinition<typedData, primaryType>): Promise<Hex> {
const { message, primaryType, types: _types, domain } = parameters

if (!domain) throw new Error("Missing domain")
if (!message) throw new Error("Missing message")

const types = {
EIP712Domain: getTypesForEIP712Domain({
domain: typedData.domain
}),
...typedData.types
EIP712Domain: getTypesForEIP712Domain({ domain }),
..._types
}

// @ts-ignore: Comes from nexus parent typehash
const messageStuff: Hex = message.stuff

// @ts-ignore
validateTypedData({
domain: typedData.domain,
message: typedData.message,
primaryType: typedData.primaryType,
domain,
message,
primaryType,
types
} as TypedDataDefinition)

const appDomainSeparator = domainSeparator({
domain: {
name: typedData.domain.name,
version: typedData.domain.version,
chainId: typedData.domain.chainId,
verifyingContract: typedData.domain.verifyingContract
}
})

const appDomainSeparator = domainSeparator({ domain })
const accountDomainStructFields = await getAccountDomainStructFields(
masterClient as unknown as PublicClient,
await getAddress()
Expand All @@ -408,14 +409,14 @@ export const toNexusAccount = async (
[
encodeAbiParameters(parseAbiParameters(["bytes32, bytes32"]), [
keccak256(toBytes(PARENT_TYPEHASH)),
typedData.message.stuff
messageStuff
]),
accountDomainStructFields
]
)
)

const wrappedTypedHash = await eip712WrapHash(
const wrappedTypedHash = eip712WrapHash(
parentStructHash,
appDomainSeparator
)
Expand All @@ -424,12 +425,12 @@ export const toNexusAccount = async (
toBytes(wrappedTypedHash)
)

const contentsType = toBytes(typeToString(types)[1])
const contentsType = toBytes(typeToString(types as TypedDataWith712)[1])

const signatureData = concatHex([
signature,
appDomainSeparator,
typedData.message.stuff,
messageStuff,
toHex(contentsType),
toHex(contentsType.length, { size: 2 })
])
Expand Down
42 changes: 0 additions & 42 deletions packages/sdk/account/utils/EthersSigner.ts

This file was deleted.

Loading

0 comments on commit 8ce67b1

Please sign in to comment.