Skip to content

[SDK] Feature: Adds getAdminAccount to ecosystem wallets and cleans up smart account creation #5829

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

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
5 changes: 5 additions & 0 deletions .changeset/afraid-mails-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": minor
---

Feature: Adds getAdminAccount to inAppWallet interface for AA ecosystem wallets
169 changes: 115 additions & 54 deletions packages/thirdweb/src/wallets/in-app/core/wallet/in-app-core.test.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,54 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { TEST_CLIENT } from "../../../../../test/src/test-clients.js";
import { TEST_ACCOUNT_A } from "../../../../../test/src/test-wallets.js";
import { baseSepolia } from "../../../../chains/chain-definitions/base-sepolia.js";
import { createThirdwebClient } from "../../../../client/client.js";
import { getEcosystemInfo } from "../../../ecosystem/get-ecosystem-wallet-auth-options.js";
import type { Account } from "../../../interfaces/wallet.js";
import { predictSmartAccountAddress } from "../../../smart/lib/calls.js";
import { DEFAULT_ACCOUNT_FACTORY_V0_6 } from "../../../smart/lib/constants.js";
import type { AuthLoginReturnType } from "../authentication/types.js";
import type { InAppConnector } from "../interfaces/connector.js";
import { createInAppWallet } from "./in-app-core.js";
import { autoConnectInAppWallet, connectInAppWallet } from "./index.js";
import * as InAppWallet from "./index.js";

vi.mock("../../../../analytics/track/connect.js", () => ({
trackConnect: vi.fn(),
}));

vi.mock("./index.js", () => ({
autoConnectInAppWallet: vi.fn(),
connectInAppWallet: vi.fn(),
}));

vi.spyOn(InAppWallet, "connectInAppWallet");
vi.spyOn(InAppWallet, "autoConnectInAppWallet");
vi.mock("../../../ecosystem/get-ecosystem-wallet-auth-options.js", () => ({
getEcosystemInfo: vi.fn(),
}));

describe("createInAppWallet", () => {
const mockClient = createThirdwebClient({
clientId: "test-client",
});
describe.runIf(process.env.TW_SECRET_KEY)("createInAppWallet", () => {
const mockClient = TEST_CLIENT;
const mockChain = baseSepolia;
const mockAccount = { address: "0x123" } as Account;
const mockAccount = TEST_ACCOUNT_A;
const mockUser = {
status: "Logged In, Wallet Initialized",
walletAddress: TEST_ACCOUNT_A.address,
authDetails: {
userWalletId: TEST_ACCOUNT_A.address,
recoveryShareManagement: "ENCLAVE",
email: "test@test.com",
},
account: mockAccount,
} as const;
const mockAuthResult: AuthLoginReturnType = {
user: mockUser,
};

const mockConnectorFactory = vi.fn(() =>
Promise.resolve({
connect: vi.fn(),
connect: vi.fn().mockResolvedValue(mockAuthResult),
logout: vi.fn(() => Promise.resolve({ success: true })),
authenticate: vi.fn(),
getAccounts: vi.fn(),
getAccount: vi.fn(),
getProfiles: vi.fn(),
getUser: vi.fn(),
getUser: vi.fn().mockResolvedValue(mockUser),
linkProfile: vi.fn(),
unlinkProfile: vi.fn(),
preAuthenticate: vi.fn(),
} as InAppConnector),
);
Expand All @@ -46,8 +58,6 @@ describe("createInAppWallet", () => {
});

it("should connect successfully", async () => {
vi.mocked(connectInAppWallet).mockResolvedValue([mockAccount, mockChain]);

const wallet = createInAppWallet({
connectorFactory: mockConnectorFactory,
});
Expand All @@ -61,7 +71,7 @@ describe("createInAppWallet", () => {
});

expect(result).toBe(mockAccount);
expect(connectInAppWallet).toHaveBeenCalledWith(
expect(InAppWallet.connectInAppWallet).toHaveBeenCalledWith(
expect.objectContaining({
client: mockClient,
chain: mockChain,
Expand All @@ -72,11 +82,6 @@ describe("createInAppWallet", () => {
});

it("should auto connect successfully", async () => {
vi.mocked(autoConnectInAppWallet).mockResolvedValue([
mockAccount,
mockChain,
]);

const wallet = createInAppWallet({
connectorFactory: mockConnectorFactory,
});
Expand All @@ -87,7 +92,7 @@ describe("createInAppWallet", () => {
});

expect(result).toBe(mockAccount);
expect(autoConnectInAppWallet).toHaveBeenCalledWith(
expect(InAppWallet.autoConnectInAppWallet).toHaveBeenCalledWith(
expect.objectContaining({
client: mockClient,
chain: mockChain,
Expand All @@ -102,15 +107,13 @@ describe("createInAppWallet", () => {
smartAccountOptions: {
defaultChainId: mockChain.id,
sponsorGas: true,
accountFactoryAddress: "0x456",
accountFactoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_6,
},
authOptions: [],
name: "hello world",
slug: "test-ecosystem",
});

vi.mocked(connectInAppWallet).mockResolvedValue([mockAccount, mockChain]);

const wallet = createInAppWallet({
connectorFactory: mockConnectorFactory,
ecosystem: { id: "ecosystem.test-ecosystem" },
Expand All @@ -124,8 +127,14 @@ describe("createInAppWallet", () => {
verificationCode: "",
});

expect(result).toBe(mockAccount);
expect(connectInAppWallet).toHaveBeenCalledWith(
const expectedSmartAccountAddress = await predictSmartAccountAddress({
factoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_6,
chain: mockChain,
adminAddress: TEST_ACCOUNT_A.address,
client: mockClient,
});
expect(result.address).toBe(expectedSmartAccountAddress);
expect(InAppWallet.connectInAppWallet).toHaveBeenCalledWith(
expect.objectContaining({
client: mockClient,
chain: mockChain,
Expand All @@ -134,7 +143,7 @@ describe("createInAppWallet", () => {
smartAccount: expect.objectContaining({
chain: mockChain,
sponsorGas: true,
factoryAddress: "0x456",
factoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_6,
}),
}),
expect.any(Object),
Expand All @@ -145,15 +154,13 @@ describe("createInAppWallet", () => {
smartAccountOptions: {
defaultChainId: mockChain.id,
sponsorGas: true,
accountFactoryAddress: "0x456",
accountFactoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_6,
},
authOptions: [],
name: "hello world",
slug: "test-ecosystem",
});

vi.mocked(connectInAppWallet).mockResolvedValue([mockAccount, mockChain]);

const wallet = createInAppWallet({
connectorFactory: mockConnectorFactory,
ecosystem: { id: "ecosystem.test-ecosystem" },
Expand All @@ -166,16 +173,22 @@ describe("createInAppWallet", () => {
verificationCode: "",
});

expect(result).toBe(mockAccount);
expect(connectInAppWallet).toHaveBeenCalledWith(
const expectedSmartAccountAddress = await predictSmartAccountAddress({
factoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_6,
chain: mockChain,
adminAddress: TEST_ACCOUNT_A.address,
client: mockClient,
});
expect(result.address).toBe(expectedSmartAccountAddress);
expect(InAppWallet.connectInAppWallet).toHaveBeenCalledWith(
expect.objectContaining({
client: mockClient,
}),
expect.objectContaining({
smartAccount: expect.objectContaining({
chain: mockChain,
sponsorGas: true,
factoryAddress: "0x456",
factoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_6,
}),
}),
expect.any(Object),
Expand All @@ -187,18 +200,13 @@ describe("createInAppWallet", () => {
smartAccountOptions: {
defaultChainId: mockChain.id,
sponsorGas: true,
accountFactoryAddress: "0x456",
accountFactoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_6,
},
authOptions: [],
name: "hello world",
slug: "test-ecosystem",
});

vi.mocked(autoConnectInAppWallet).mockResolvedValue([
mockAccount,
mockChain,
]);

const wallet = createInAppWallet({
connectorFactory: mockConnectorFactory,
ecosystem: { id: "ecosystem.test-ecosystem" },
Expand All @@ -209,8 +217,14 @@ describe("createInAppWallet", () => {
chain: mockChain,
});

expect(result).toBe(mockAccount);
expect(autoConnectInAppWallet).toHaveBeenCalledWith(
const expectedSmartAccountAddress = await predictSmartAccountAddress({
factoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_6,
chain: mockChain,
adminAddress: TEST_ACCOUNT_A.address,
client: mockClient,
});
expect(result.address).toBe(expectedSmartAccountAddress);
expect(InAppWallet.autoConnectInAppWallet).toHaveBeenCalledWith(
expect.objectContaining({
client: mockClient,
chain: mockChain,
Expand All @@ -219,7 +233,7 @@ describe("createInAppWallet", () => {
smartAccount: expect.objectContaining({
chain: mockChain,
sponsorGas: true,
factoryAddress: "0x456",
factoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_6,
}),
}),
expect.any(Object),
Expand All @@ -231,18 +245,13 @@ describe("createInAppWallet", () => {
smartAccountOptions: {
defaultChainId: mockChain.id,
sponsorGas: true,
accountFactoryAddress: "0x456",
accountFactoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_6,
},
authOptions: [],
name: "hello world",
slug: "test-ecosystem",
});

vi.mocked(autoConnectInAppWallet).mockResolvedValue([
mockAccount,
mockChain,
]);

const wallet = createInAppWallet({
connectorFactory: mockConnectorFactory,
ecosystem: { id: "ecosystem.test-ecosystem" },
Expand All @@ -252,19 +261,71 @@ describe("createInAppWallet", () => {
client: mockClient,
});

expect(result).toBe(mockAccount);
expect(autoConnectInAppWallet).toHaveBeenCalledWith(
const expectedSmartAccountAddress = await predictSmartAccountAddress({
factoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_6,
chain: mockChain,
adminAddress: TEST_ACCOUNT_A.address,
client: mockClient,
});
expect(result.address).toBe(expectedSmartAccountAddress);
expect(InAppWallet.autoConnectInAppWallet).toHaveBeenCalledWith(
expect.objectContaining({
client: mockClient,
}),
expect.objectContaining({
smartAccount: expect.objectContaining({
chain: mockChain,
sponsorGas: true,
factoryAddress: "0x456",
factoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_6,
}),
}),
expect.any(Object),
);
});

it("should return undefined for getAdminAccount if the account is not a smart account", () => {
const wallet = createInAppWallet({
connectorFactory: mockConnectorFactory,
});

expect(wallet.getAdminAccount?.()).toBeUndefined();
});

it("should return undefined if no account is connected", () => {
const wallet = createInAppWallet({
connectorFactory: mockConnectorFactory,
});

expect(wallet.getAdminAccount?.()).toBeUndefined();
});

it("should return the admin account for a smart account", async () => {
vi.unmock("./index.js");
vi.mocked(getEcosystemInfo).mockResolvedValue({
smartAccountOptions: {
defaultChainId: mockChain.id,
sponsorGas: true,
accountFactoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_6,
},
authOptions: [],
name: "hello world",
slug: "test-ecosystem",
});

const wallet = createInAppWallet({
connectorFactory: mockConnectorFactory,
ecosystem: { id: "ecosystem.test-ecosystem" },
});

const smartAccount = await wallet.connect({
client: mockClient,
strategy: "email",
email: "",
verificationCode: "",
});

const adminAccount = wallet.getAdminAccount?.();
expect(adminAccount).toBeDefined();
expect(adminAccount?.address).not.toBe(smartAccount.address);
});
});
Loading
Loading