Skip to content
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
158 changes: 158 additions & 0 deletions aastar/src/account/account.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import { Test, TestingModule } from "@nestjs/testing";
import { ConfigService } from "@nestjs/config";
import { InternalServerErrorException } from "@nestjs/common";
import { AccountService } from "./account.service";
import { DatabaseService } from "../database/database.service";
import { YAAA_SERVER_CLIENT } from "../sdk/sdk.providers";

// `uuid` ships ESM-only and this project's jest transform doesn't cover node_modules;
// nothing under test generates an id, so a stub is enough.
jest.mock("uuid", () => ({ v4: () => "00000000-0000-4000-8000-000000000000" }));

const mockGetChainId = jest.fn();
const capturedWalletConfigs: any[] = [];

// Real viem elsewhere (resolveChain/defineChain/parseEther are all used for real);
// only the client factories are swapped so nothing hits the network and we can see
// exactly which `chain` the deployer wallet was built with.
jest.mock("viem", () => {
const actual = jest.requireActual("viem");
return {
...actual,
createPublicClient: jest.fn(() => ({ getChainId: mockGetChainId })),
createWalletClient: jest.fn((cfg: any) => {
capturedWalletConfigs.push(cfg);
return { ...cfg };
}),
};
});

const DEPLOYER_KEY = `0x${"a".repeat(64)}`;
const OWNER = "0x1111111111111111111111111111111111111111";
const FACTORY = "0x9999999999999999999999999999999999999999";

describe("AccountService — chain consistency (issue #439)", () => {
let service: AccountService;
const mockConfigGet = jest.fn();
const mockEnsureSigner = jest.fn();
const mockBuildGuardianAcceptanceHash = jest.fn();
const mockSubmitPreparedCreateAccount = jest.fn();

const buildService = async (chainId: unknown) => {
mockConfigGet.mockImplementation((key: string) => {
const cfg: Record<string, unknown> = {
ethRpcUrl: "http://localhost:8545",
deployerPrivateKey: DEPLOYER_KEY,
chainId,
};
return cfg[key];
});

const module: TestingModule = await Test.createTestingModule({
providers: [
AccountService,
{ provide: ConfigService, useValue: { get: mockConfigGet } },
{ provide: DatabaseService, useValue: { findUserById: jest.fn() } },
{
provide: YAAA_SERVER_CLIENT,
useValue: {
wallets: { ensureSigner: mockEnsureSigner },
ethereum: { getFactoryAddress: () => FACTORY },
accounts: {
buildGuardianAcceptanceHash: mockBuildGuardianAcceptanceHash,
submitPreparedCreateAccount: mockSubmitPreparedCreateAccount,
},
},
},
],
}).compile();

return module.get<AccountService>(AccountService);
};

beforeEach(() => {
capturedWalletConfigs.length = 0;
for (const m of [
mockGetChainId,
mockEnsureSigner,
mockBuildGuardianAcceptanceHash,
mockSubmitPreparedCreateAccount,
]) {
m.mockReset();
}
mockEnsureSigner.mockResolvedValue({ address: OWNER });
mockBuildGuardianAcceptanceHash.mockReturnValue("0xacceptance");
mockSubmitPreparedCreateAccount.mockResolvedValue({ address: OWNER, deployed: true });
});

// The defect: the deployer wallet hardcoded `sepolia` while the guardian acceptance
// hash and the CREATE_ACCOUNT digest are bound to the configured chainId. On any
// non-Sepolia config the user signs for one chain and the account is deployed on
// another. Same shape as PR #434 fixed in GuardianService.
describe.each([11155111, 10, 8453])("chainId=%i", chainId => {
beforeEach(async () => {
mockGetChainId.mockResolvedValue(chainId);
service = await buildService(chainId);
});

it("binds the guardian acceptance hash to the configured chain", async () => {
const prepared = await service.prepareGuardianSetup("user-1", {} as any);

expect(prepared.chainId).toBe(chainId);
// 4th positional arg of buildGuardianAcceptanceHash(owner, salt, factory, chainId, limit)
expect(mockBuildGuardianAcceptanceHash.mock.calls[0][3]).toBe(chainId);
expect(JSON.parse(prepared.qrPayload).chainId).toBe(chainId);
});

it("relays the deploy on that same chain", async () => {
await service.submitCreateWithPasskey("user-1", {
createId: "c-1",
challengeId: "ch-1",
credential: {},
} as any);

expect(capturedWalletConfigs).toHaveLength(1);
expect(capturedWalletConfigs[0].chain.id).toBe(chainId);
});
});

// viem does not check this for us: `assertCurrentChain` runs only for json-rpc
// accounts, and the deployer is a local (private-key) account, whose
// prepareTransactionRequest takes `chain.id` on faith without issuing eth_chainId.
describe("RPC/config chain mismatch", () => {
beforeEach(async () => {
mockGetChainId.mockResolvedValue(11155111); // RPC is on Sepolia…
service = await buildService(10); // …but CHAIN_ID says OP mainnet.
});

it("refuses to build an acceptance hash the guardian would sign for the wrong chain", async () => {
await expect(service.prepareGuardianSetup("user-1", {} as any)).rejects.toThrow(
/Chain mismatch/
);
expect(mockBuildGuardianAcceptanceHash).not.toHaveBeenCalled();
});

it("refuses to relay the deploy, before spending the one-time WebAuthn ceremony", async () => {
await expect(
service.submitCreateWithPasskey("user-1", {
createId: "c-1",
challengeId: "ch-1",
credential: {},
} as any)
).rejects.toThrow(/Chain mismatch/);
expect(mockSubmitPreparedCreateAccount).not.toHaveBeenCalled();
expect(capturedWalletConfigs).toHaveLength(0);
});
});

it("refuses an unusable chainId instead of falling back to a hardcoded default", async () => {
for (const bad of [undefined, 0, -1]) {
mockGetChainId.mockResolvedValue(11155111);
service = await buildService(bad);
await expect(service.prepareGuardianSetup("user-1", {} as any)).rejects.toThrow(
InternalServerErrorException
);
expect(mockBuildGuardianAcceptanceHash).not.toHaveBeenCalled();
}
});
});
63 changes: 58 additions & 5 deletions aastar/src/account/account.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Injectable, Inject, NotFoundException, BadRequestException, Logger } from "@nestjs/common";
import {
Injectable,
Inject,
NotFoundException,
BadRequestException,
InternalServerErrorException,
Logger,
} from "@nestjs/common";
import {
AirAccountServerClient as YAAAServerClient,
ALG_ECDSA,
Expand All @@ -18,9 +25,9 @@ import {
} from "./dto/guardian-setup.dto";
import { DatabaseService } from "../database/database.service";
import { ConfigService } from "@nestjs/config";
import { createWalletClient, http, parseEther, isAddress } from "viem";
import { createPublicClient, createWalletClient, http, parseEther, isAddress } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { sepolia } from "viem/chains";
import { assertRpcChain, assertValidChainId, resolveChain } from "../common/utils/chain.util";

@Injectable()
export class AccountService {
Expand All @@ -32,6 +39,37 @@ export class AccountService {
private configService: ConfigService
) {}

/**
* The single chain id this service operates on — it domain-separates the guardian
* acceptance hash and selects the chain the deploy is broadcast to. One source so
* the two cannot diverge (issue #439, same defect as PR #434 fixed in guardian).
*/
private getChainId(): number {
try {
return assertValidChainId(this.configService.get<number>("chainId"));
} catch (err) {
throw new InternalServerErrorException((err as Error).message);
}
}

/**
* Preflight: ETH_RPC_URL must actually serve `getChainId()`. viem will not check
* this for us — for a local (private-key) account it takes `chain.id` on faith and
* never issues `eth_chainId` — so a mismatch would otherwise surface only as an
* opaque `eth_sendRawTransaction` rejection, after the user's passkey ceremony.
*/
private async assertChainMatchesRpc(why: string): Promise<void> {
const rpcUrl = this.configService.get<string>("ethRpcUrl");
if (!rpcUrl) {
throw new InternalServerErrorException("ETH_RPC_URL is not configured");
}
try {
await assertRpcChain(createPublicClient({ transport: http(rpcUrl) }), this.getChainId(), why);
} catch (err) {
throw new InternalServerErrorException((err as Error).message);
}
}

/**
* Converts an ETH amount string (e.g. "1.0") to wei as bigint.
* Returns undefined when value is empty/zero (no guard enforcement).
Expand Down Expand Up @@ -117,9 +155,17 @@ export class AccountService {
// Resolve signer address (owner of the future account)
const { address: owner } = await this.client.wallets.ensureSigner(userId);

// The guardian signs an acceptance hash domain-separated with this chainId, so the
// RPC had better be that chain — otherwise the guardian approves for one chain and
// the account is created on another. The `|| 11155111` that used to sit here was
// dead (configuration.ts defaults chainId to 10) and only hid that divergence.
await this.assertChainMatchesRpc(
"The guardian acceptance hash would be signed for a chain the account is not created on."
);

// Pick factory + chainId from ethereum provider
const factoryAddress = this.client.ethereum.getFactoryAddress(version);
const chainId = this.configService.get<number>("chainId") || 11155111;
const chainId = this.getChainId();

// Determine salt (use provided or generate random)
const salt = dto.salt ?? Math.floor(Math.random() * 1_000_000);
Expand Down Expand Up @@ -395,6 +441,13 @@ export class AccountService {
// which requires the user's physical device. `userId` is therefore not a security check
// here (a strict createId↔userId assertion needs an SDK-exposed binding getter); it is
// recorded for audit. See PR #399 review H1.
// The CREATE_ACCOUNT digest the user's assertion signs is bound to a chain; the
// deploy must land on that same one. Checked before the relay so a misconfigured
// deployment fails here rather than burning the one-time WebAuthn ceremony.
await this.assertChainMatchesRpc(
"The signed CREATE_ACCOUNT digest would not match the chain the deploy is relayed to."
);
Comment on lines +447 to +449

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Add passkey preflight before preparing the digest

Putting the chain/RPC check only in submit still lets /account/prepare-create-with-passkey build the CREATE_ACCOUNT digest, create the pending challenge, and return publicKeyOptions; the frontend then runs navigator.credentials.get before this line can reject. When CHAIN_ID and ETH_RPC_URL are mismatched, users still spend the passkey ceremony and leave an unusable prepare session before seeing the configuration error. Please run the same assertChainMatchesRpc check at the start of prepareCreateWithPasskey, before the SDK issues the digest/challenge.

Useful? React with 👍 / 👎.


const deployerKey =
this.configService.get<string>("deployerPrivateKey") || process.env.DEPLOYER_PRIVATE_KEY;
if (!deployerKey) {
Expand All @@ -406,7 +459,7 @@ export class AccountService {
account: privateKeyToAccount(
(deployerKey.startsWith("0x") ? deployerKey : `0x${deployerKey}`) as `0x${string}`
),
chain: sepolia,
chain: resolveChain(this.getChainId()),
transport: http(this.configService.get<string>("ethRpcUrl")),
});

Expand Down
44 changes: 44 additions & 0 deletions aastar/src/common/utils/chain.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,47 @@ export function resolveChain(chainId: number): Chain {
rpcUrls: { default: { http: [] } },
});
}

/**
* Validates a `chainId` read from config. `configuration.ts` always supplies a value,
* so a non-number here means the config wiring itself is broken — worth saying so
* rather than silently falling back to a hardcoded default, which is exactly how the
* broadcast chain drifted away from the signature domain in the first place (#434).
*/
export function assertValidChainId(chainId: unknown): number {
if (typeof chainId !== "number" || !Number.isInteger(chainId) || chainId <= 0) {
throw new Error(
`CHAIN_ID is not configured correctly (got ${JSON.stringify(chainId)}). ` +
"It domain-separates signatures and selects the chain transactions are sent to."
);
}
return chainId;
}

/**
* Preflight: the configured chain id must be what the RPC endpoint actually serves.
*
* `resolveChain` alone cannot give this guarantee — see its note on viem skipping
* `assertCurrentChain` for local accounts. Anything that signs for, or broadcasts to,
* a specific chain should call this first so a CHAIN_ID/ETH_RPC_URL mismatch fails
* immediately instead of after a user-visible signing ceremony.
*
* `why` is appended to the error to say what the mismatch would have broken.
*/
export async function assertRpcChain(
client: { getChainId: () => Promise<number> },
expected: number,
why: string
): Promise<void> {
let actual: number;
try {
actual = await client.getChainId();
} catch (err) {
throw new Error(`Could not read the chain id from ETH_RPC_URL: ${(err as Error).message}`);
}
if (actual !== expected) {
throw new Error(
`Chain mismatch: CHAIN_ID is ${expected} but ETH_RPC_URL serves chain ${actual}. ${why}`
);
}
}
Loading