From 0e621b576158c4366ed22fc02cbcd6717a3b886c Mon Sep 17 00:00:00 2001 From: Joe Pegler Date: Mon, 6 Jan 2025 09:38:59 +0000 Subject: [PATCH] chore: addresses check --- .../account/toNexusAccount.addresses.test.ts | 111 ++++++++++++++++++ src/sdk/account/toNexusAccount.test.ts | 12 -- 2 files changed, 111 insertions(+), 12 deletions(-) create mode 100644 src/sdk/account/toNexusAccount.addresses.test.ts diff --git a/src/sdk/account/toNexusAccount.addresses.test.ts b/src/sdk/account/toNexusAccount.addresses.test.ts new file mode 100644 index 00000000..1c02e774 --- /dev/null +++ b/src/sdk/account/toNexusAccount.addresses.test.ts @@ -0,0 +1,111 @@ +import { + http, + type Address, + type Chain, + type LocalAccount, + type PublicClient, + type WalletClient, + createWalletClient +} from "viem" +import { afterAll, beforeAll, describe, expect, test } from "vitest" +import { toNetwork } from "../../test/testSetup" +import { + fundAndDeployClients, + getTestAccount, + killNetwork, + toTestClient +} from "../../test/testUtils" +import type { MasterClient, NetworkConfig } from "../../test/testUtils" +import type { NexusAccount } from "./toNexusAccount" +import { getCounterFactualAddress } from "./utils" +import { createSmartAccountClient } from "../clients/createSmartAccountClient" + +describe("nexus.account.addresses", async () => { + let network: NetworkConfig + let chain: Chain + let bundlerUrl: string + + // Test utils + let testClient: MasterClient + let eoaAccount: LocalAccount + let userTwo: LocalAccount + let nexusAccountAddress: Address + let nexusClient: NexusClient + let nexusAccount: NexusAccount + let walletClient: WalletClient + + beforeAll(async () => { + network = await toNetwork() + + chain = network.chain + bundlerUrl = network.bundlerUrl + eoaAccount = getTestAccount(0) + userTwo = getTestAccount(1) + testClient = toTestClient(chain, getTestAccount(5)) + + walletClient = createWalletClient({ + account: eoaAccount, + chain, + transport: http() + }) + + nexusClient = await createSmartAccountClient({ + signer: eoaAccount, + chain, + transport: http(), + bundlerTransport: http(bundlerUrl) + }) + + nexusAccount = nexusClient.account + }) + afterAll(async () => { + await killNetwork([network?.rpcPort, network?.bundlerPort]) + }) + + test("should check account address", async () => { + nexusAccountAddress = await nexusClient.account.getCounterFactualAddress() + const counterfactualAddressFromHelper = await getCounterFactualAddress( + testClient as unknown as PublicClient, + eoaAccount.address, + true + ) + const gottenAddress = await nexusClient.account.getAddress() + expect(counterfactualAddressFromHelper).toBe(nexusAccountAddress) + expect(nexusAccount.address).toBe(nexusAccountAddress) + expect(nexusAccount.address).toBe(counterfactualAddressFromHelper) + expect(gottenAddress).toBe(nexusAccountAddress) + }) + + test("should check addresses after fund and deploy", async () => { + await fundAndDeployClients(testClient, [nexusClient]) + const counterfactualAddressFromHelper = await getCounterFactualAddress( + testClient as unknown as PublicClient, + eoaAccount.address, + true + ) + const gottenAddress = await nexusClient.account.getAddress() + expect(counterfactualAddressFromHelper).toBe(nexusAccountAddress) + expect(nexusAccount.address).toBe(nexusAccountAddress) + expect(nexusAccount.address).toBe(counterfactualAddressFromHelper) + expect(gottenAddress).toBe(nexusAccountAddress) + }) + + test("should override account address", async () => { + const someoneElsesNexusAddress = + "0xf0479e036343bC66dc49dd374aFAF98402D0Ae5f" + const newNexusClient = await createSmartAccountClient({ + chain, + transport: http(), + bundlerTransport: http(bundlerUrl), + accountAddress: someoneElsesNexusAddress, + signer: eoaAccount + }) + const accountAddress = await newNexusClient.account.getAddress() + const someoneElseCounterfactualAddress = + await newNexusClient.account.getCounterFactualAddress() + expect(newNexusClient.account.address).toBe( + someoneElseCounterfactualAddress + ) + expect(accountAddress).toBe(someoneElsesNexusAddress) + }) +}) diff --git a/src/sdk/account/toNexusAccount.test.ts b/src/sdk/account/toNexusAccount.test.ts index be334fcb..c3664802 100644 --- a/src/sdk/account/toNexusAccount.test.ts +++ b/src/sdk/account/toNexusAccount.test.ts @@ -106,18 +106,6 @@ describe("nexus.account", async () => { await killNetwork([network?.rpcPort, network?.bundlerPort]) }) - test("should override account address", async () => { - const newNexusClient = await createSmartAccountClient({ - chain, - transport: http(), - bundlerTransport: http(bundlerUrl), - accountAddress: "0xf0479e036343bC66dc49dd374aFAF98402D0Ae5f", - signer: eoaAccount - }) - const accountAddress = await newNexusClient.account.getAddress() - expect(accountAddress).toBe("0xf0479e036343bC66dc49dd374aFAF98402D0Ae5f") - }) - test("should check isValidSignature PersonalSign is valid", async () => { const meta = await getAccountMeta(testClient, nexusAccountAddress)