Skip to content

Commit 2493438

Browse files
committed
refactor: move SINGLE_SIGNER_VALIDATION_MODULE_ADDRESS out to configs and duplicate for acpContractClient.ts
1 parent ae4ec0c commit 2493438

4 files changed

Lines changed: 64 additions & 45 deletions

File tree

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ export const HTTP_STATUS_CODES = {
1919
OK: 200,
2020
PAYMENT_REQUIRED: 402,
2121
};
22+
23+
export const SINGLE_SIGNER_VALIDATION_MODULE_ADDRESS: Address = "0x00000000000099DE0BF6fA90dEB851E2A2df7d83";

src/contractClients/acpContractClient.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ class AcpContractClient extends BaseAcpContractClient {
7474
this.sessionKeyClient,
7575
this.publicClient
7676
);
77+
78+
const account = this.sessionKeyClient.account;
79+
const sessionSignerAddress: Address = await account.getSigner().getAddress();
80+
81+
if (!await account.isAccountDeployed()) {
82+
throw new AcpError(
83+
`ACP Contract Client validation failed: agent account ${this.agentWalletAddress} is not deployed on-chain`
84+
);
85+
}
86+
87+
await this.validateSessionKeyOnChain(sessionSignerAddress, sessionEntityKeyId);
88+
89+
console.log("Connected to ACP:", {
90+
agentWalletAddress: this.agentWalletAddress,
91+
whitelistedWalletAddress: sessionSignerAddress,
92+
entityId: sessionEntityKeyId,
93+
});
7794
}
7895

7996
getRandomNonce(bits = 152) {

src/contractClients/acpContractClientV2.ts

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
createModularAccountV2Client,
55
ModularAccountV2Client,
66
} from "@account-kit/smart-contracts";
7-
import { createPublicClient, decodeEventLog, http, zeroAddress } from "viem";
7+
import { createPublicClient, decodeEventLog, http } from "viem";
88
import { AcpContractConfig, baseAcpConfigV2 } from "../configs/acpConfigs";
99
import AcpError from "../acpError";
1010
import BaseAcpContractClient, {
@@ -20,14 +20,11 @@ import {
2020
X402PaymentResponse,
2121
} from "../interfaces";
2222
import { AcpX402 } from "../acpX402";
23-
import SINGLE_SIGNER_VALIDATION_MODULE_ABI from "../abis/singleSignerValidationModuleAbi";
2423

2524
class AcpContractClientV2 extends BaseAcpContractClient {
2625
private PRIORITY_FEE_MULTIPLIER = 2;
2726
private MAX_FEE_PER_GAS = 20000000;
2827
private MAX_PRIORITY_FEE_PER_GAS = 21000000;
29-
private readonly SINGLE_SIGNER_VALIDATION_MODULE_ADDRESS: Address =
30-
"0x00000000000099DE0BF6fA90dEB851E2A2df7d83" as Address;
3128

3229
private _sessionKeyClient: ModularAccountV2Client | undefined;
3330
private _acpX402: AcpX402 | undefined;
@@ -135,47 +132,6 @@ class AcpContractClientV2 extends BaseAcpContractClient {
135132
});
136133
}
137134

138-
private async validateSessionKeyOnChain(
139-
sessionSignerAddress: Address,
140-
sessionEntityKeyId: number
141-
): Promise<void> {
142-
const onChainSignerAddress = (await this.publicClient.readContract({
143-
address: this.SINGLE_SIGNER_VALIDATION_MODULE_ADDRESS,
144-
abi: SINGLE_SIGNER_VALIDATION_MODULE_ABI,
145-
functionName: "signers",
146-
args: [sessionEntityKeyId, this.agentWalletAddress],
147-
})) as Address;
148-
149-
if (!onChainSignerAddress || onChainSignerAddress.toLowerCase() === zeroAddress.toLowerCase()) {
150-
throw new AcpError(
151-
`ACP Contract Client validation failed:\n${JSON.stringify(
152-
{
153-
reason: "no whitelisted wallet registered on-chain for entity id",
154-
entityId: sessionEntityKeyId,
155-
agentWalletAddress: this.agentWalletAddress,
156-
},
157-
null,
158-
2
159-
)}`
160-
);
161-
}
162-
163-
if (onChainSignerAddress.toLowerCase() !== sessionSignerAddress.toLowerCase()) {
164-
throw new AcpError(
165-
`ACP Contract Client validation failed:\n${JSON.stringify(
166-
{
167-
agentWalletAddress: this.agentWalletAddress,
168-
entityId: sessionEntityKeyId,
169-
givenSessionSignerAddress: sessionSignerAddress,
170-
expectedSignerAddress: onChainSignerAddress,
171-
},
172-
null,
173-
2
174-
)}`
175-
);
176-
}
177-
}
178-
179135
getRandomNonce(bits = 152) {
180136
const bytes = bits / 8;
181137
const array = new Uint8Array(bytes);

src/contractClients/baseAcpContractClient.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
keccak256,
1010
toEventSignature,
1111
toHex,
12+
zeroAddress,
1213
} from "viem";
1314
import { AcpContractConfig, baseAcpConfig } from "../configs/acpConfigs";
1415
import ACP_V2_ABI from "../abis/acpAbiV2";
@@ -25,6 +26,8 @@ import {
2526
X402PaymentResponse,
2627
} from "../interfaces";
2728
import FIAT_TOKEN_V2_ABI from "../abis/fiatTokenV2Abi";
29+
import SINGLE_SIGNER_VALIDATION_MODULE_ABI from "../abis/singleSignerValidationModuleAbi";
30+
import { SINGLE_SIGNER_VALIDATION_MODULE_ADDRESS } from "../constants";
2831

2932
export enum MemoType {
3033
MESSAGE, // 0 - Text message
@@ -89,6 +92,47 @@ abstract class BaseAcpContractClient {
8992
});
9093
}
9194

95+
protected async validateSessionKeyOnChain(
96+
sessionSignerAddress: Address,
97+
sessionEntityKeyId: number
98+
): Promise<void> {
99+
const onChainSignerAddress = ((await this.publicClient.readContract({
100+
address: SINGLE_SIGNER_VALIDATION_MODULE_ADDRESS,
101+
abi: SINGLE_SIGNER_VALIDATION_MODULE_ABI,
102+
functionName: "signers",
103+
args: [sessionEntityKeyId, this.agentWalletAddress],
104+
})) as Address).toLowerCase();
105+
106+
if (onChainSignerAddress === zeroAddress.toLowerCase()) {
107+
throw new AcpError(
108+
`ACP Contract Client validation failed:\n${JSON.stringify(
109+
{
110+
reason: "no whitelisted wallet registered on-chain for entity id",
111+
entityId: sessionEntityKeyId,
112+
agentWalletAddress: this.agentWalletAddress,
113+
},
114+
null,
115+
2
116+
)}`
117+
);
118+
}
119+
120+
if (onChainSignerAddress !== sessionSignerAddress.toLowerCase()) {
121+
throw new AcpError(
122+
`ACP Contract Client validation failed:\n${JSON.stringify(
123+
{
124+
agentWalletAddress: this.agentWalletAddress,
125+
entityId: sessionEntityKeyId,
126+
givenWhitelistedWalletAddress: sessionSignerAddress,
127+
expectedWhitelistedWalletAddress: onChainSignerAddress,
128+
},
129+
null,
130+
2
131+
)}`
132+
);
133+
}
134+
}
135+
92136
abstract handleOperation(operations: OperationPayload[]): Promise<{ userOpHash: Address , txnHash: Address }>;
93137

94138
abstract getJobId(

0 commit comments

Comments
 (0)