Skip to content
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

feat: integratorId check #33

Merged
merged 3 commits into from
Oct 2, 2024
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/grumpy-swans-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@across-protocol/integrator-sdk": patch
---

stricter integrator id check
1 change: 0 additions & 1 deletion apps/example/app/ethers/components/Bridge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const chains = [mainnet, arbitrum];
const sdk = AcrossClient.create({
chains,
useTestnet: false,
integratorId: "TEST",
logLevel: "DEBUG",
});

Expand Down
1 change: 0 additions & 1 deletion apps/example/lib/across.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const SUPPORTED_CHAINS = MAINNET_SUPPORTED_CHAINS;
const sdk = AcrossClient.create({
chains: [...SUPPORTED_CHAINS],
useTestnet: false,
integratorId: "TEST",
logLevel: "DEBUG",
});

Expand Down
1 change: 0 additions & 1 deletion apps/example/scripts/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ async function main() {
const client = AcrossClient.create({
chains,
useTestnet: false,
integratorId: "TEST",
logLevel: "DEBUG",
walletClient,
tenderly: {
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/src/actions/executeQuote.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Address,
Hash,
Hex,
parseAbi,
SimulateContractReturnType,
TransactionReceipt,
Expand Down Expand Up @@ -112,7 +113,7 @@ export type ExecuteQuoteParams = {
/**
* An identifier for the integrator.
*/
integratorId: string;
integratorId: Hex;
/**
* The deposit to execute. Should be taken from return value of {@link getQuote}.
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/src/actions/simulateDepositTx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Hex,
PublicClient,
SimulateContractReturnType,
WalletClient,
Expand All @@ -18,7 +19,7 @@ export type SimulateDepositTxParams = {
deposit: Quote["deposit"] & {
fillDeadline?: number;
};
integratorId: string;
integratorId: Hex;
logger?: LoggerT;
};

Expand Down
14 changes: 9 additions & 5 deletions packages/sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Chain,
ContractFunctionExecutionError,
encodeFunctionData,
Hex,
} from "viem";
import {
getAvailableRoutes,
Expand Down Expand Up @@ -43,6 +44,7 @@ import {
getSupportedChains,
simulateTxOnTenderly,
TenderlySimulateTxParams,
assertValidIntegratorId,
} from "./utils";
import {
AcrossApiSimulationError,
Expand All @@ -53,12 +55,11 @@ import {
ConfiguredPublicClient,
ConfiguredPublicClientMap,
ConfiguredWalletClient,
Deposit,
} from "./types";

const CLIENT_DEFAULTS = {
pollingInterval: 3_000,
integratorId: "INTEGRATOR_SDK",
integratorId: "0xdead",
logLevel: "ERROR",
} as const;

Expand All @@ -67,7 +68,7 @@ export type AcrossClientOptions = {
/**
* An identifier representing the integrator.
*/
integratorId?: string;
integratorId?: Hex;
/**
* The chains to use for the Across API. Should be imported from `viem/chains`.
*/
Expand Down Expand Up @@ -135,7 +136,7 @@ export type AcrossClientOptions = {
export class AcrossClient {
private static instance: AcrossClient | null = null;

private integratorId: string;
private integratorId: Hex;
private publicClients: ConfiguredPublicClientMap;
private walletClient?: ConfiguredWalletClient;
private apiUrl: string;
Expand All @@ -159,7 +160,10 @@ export class AcrossClient {
}

private constructor(args: AcrossClientOptions) {
this.integratorId = args?.integratorId ?? CLIENT_DEFAULTS.integratorId;
const integratorId = args?.integratorId ?? CLIENT_DEFAULTS.integratorId;
assertValidIntegratorId(integratorId);

this.integratorId = integratorId;
this.walletClient = args?.walletClient;
this.publicClients = configurePublicClients(
args.chains,
Expand Down
34 changes: 29 additions & 5 deletions packages/sdk/src/utils/hex.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
import { concat, Hex, toHex } from "viem";
import { concat, Hex, isHex } from "viem";

export const DOMAIN_CALLDATA_DELIMITER = "0x1dc0de";

export function tagIntegratorId(integratorId: string, txData: Hex) {
return concat([txData, DOMAIN_CALLDATA_DELIMITER, toHex(integratorId)]);
export function tagIntegratorId(integratorId: Hex, txData: Hex) {
assertValidIntegratorId(integratorId);

return concat([txData, DOMAIN_CALLDATA_DELIMITER, integratorId]);
}

export function getIntegratorDataSuffix(integratorId: Hex) {
assertValidIntegratorId(integratorId);

return concat([DOMAIN_CALLDATA_DELIMITER, integratorId]);
}

export function getIntegratorDataSuffix(integratorId: string) {
return concat([DOMAIN_CALLDATA_DELIMITER, toHex(integratorId)]);
export function isValidIntegratorId(integratorId: string) {
return (
isHex(integratorId) &&
// "0x" + 2 bytes = 6 hex characters
integratorId.length === 6
);
}

export function assertValidIntegratorId(
integratorId: string,
): integratorId is Hex {
if (!isValidIntegratorId(integratorId)) {
throw new Error(
`Invalid integrator ID: ${integratorId}. Needs to be 2 bytes hex string.`,
);
}

return true;
}
1 change: 0 additions & 1 deletion packages/sdk/test/utils/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { hardhat } from "viem/chains";

const client = AcrossClient.create({
useTestnet: true,
integratorId: "TEST_ID",
logLevel: "WARN",
chains: [hardhat],
});
Expand Down