Skip to content

Commit

Permalink
feat: integratorId check (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
dohaki authored Oct 2, 2024
1 parent eded2f7 commit 790c7b9
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 16 deletions.
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

0 comments on commit 790c7b9

Please sign in to comment.