Skip to content

Commit

Permalink
support network based api keys (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmaayan authored Dec 3, 2024
1 parent a307fe5 commit 80a3c8a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/five-lies-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@aptos-labs/wallet-adapter-core": minor
"@aptos-labs/wallet-adapter-nextjs-example": minor
---

Support network based API key
7 changes: 5 additions & 2 deletions apps/nextjs-example/src/components/WalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { TrustWallet } from "@trustwallet/aptos-wallet-adapter";
import { FewchaWallet } from "fewcha-plugin-wallet-adapter";
import { PropsWithChildren } from "react";
import { Network } from "@aptos-labs/ts-sdk";
import { useClaimSecretKey } from '@/hooks/useClaimSecretKey';
import { useClaimSecretKey } from "@/hooks/useClaimSecretKey";
import { useAutoConnect } from "./AutoConnectProvider";
import { useToast } from "./ui/use-toast";

Expand All @@ -37,7 +37,10 @@ export const WalletProvider = ({ children }: PropsWithChildren) => {
autoConnect={autoConnect}
dappConfig={{
network: Network.TESTNET,
aptosApiKey: process.env.NEXT_PUBLIC_APTOS_API_KEY,
aptosApiKeys: {
testnet: process.env.NEXT_PUBLIC_APTOS_API_KEY_TESNET,
devnet: process.env.NEXT_PUBLIC_APTOS_API_KEY_DEVNET,
},
aptosConnect: {
claimSecretKey,
dappId: "57fa42a9-29c6-4f1e-939c-4eefa36d9ff5",
Expand Down
4 changes: 3 additions & 1 deletion packages/wallet-adapter-core/src/WalletCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ export type IAptosWallet = AptosStandardWallet & Wallet;
* Interface for dapp configuration
*
* @network The network the dapp is working with
* @aptosApiKey An Api Key generated with {@link https://developers.aptoslabs.com/docs/api-access}
* @aptosApiKeys A map of Network<>Api Key generated with {@link https://developers.aptoslabs.com/docs/api-access}
* @aptosConnect Config used to initialize the AptosConnect wallet provider
* @mizuwallet Config used to initialize the Mizu wallet provider
*/
export interface DappConfig {
network: Network;
aptosApiKeys?: Partial<Record<Network, string>>;
/** @deprecated */
aptosApiKey?: string;
/** @deprecated */
aptosConnectDappId?: string;
Expand Down
22 changes: 19 additions & 3 deletions packages/wallet-adapter-core/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,24 @@ export const getAptosConfig = (
if (!networkInfo) {
throw new Error("Undefined network");
}
const currentNetwork = convertNetwork(networkInfo);

if (isAptosLiveNetwork(currentNetwork)) {
const apiKey = dappConfig?.aptosApiKeys;
return new AptosConfig({
network: currentNetwork,
clientConfig: { API_KEY: apiKey ? apiKey[currentNetwork] : undefined },
});
}

if (isAptosNetwork(networkInfo)) {
return new AptosConfig({
network: convertNetwork(networkInfo),
clientConfig: { API_KEY: dappConfig?.aptosApiKey },
network: currentNetwork,
});
}
return new AptosConfig({
network: Network.CUSTOM,
fullnode: networkInfo.url,
clientConfig: { API_KEY: dappConfig?.aptosApiKey },
});
};

Expand All @@ -108,6 +116,14 @@ export const isAptosNetwork = (
return NetworkToNodeAPI[networkInfo.name] !== undefined;
};

export const isAptosLiveNetwork = (networkInfo: Network): boolean => {
return (
networkInfo === "devnet" ||
networkInfo === "testnet" ||
networkInfo === "mainnet"
);
};

/**
* Helper function to fetch Devnet chain id
*/
Expand Down
6 changes: 5 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"$schema": "https://turbo.build/schema.json",
"globalEnv": ["GAID", "NEXT_PUBLIC_APTOS_API_KEY"],
"globalEnv": [
"GAID",
"NEXT_PUBLIC_APTOS_API_KEY_TESNET",
"NEXT_PUBLIC_APTOS_API_KEY_DEVNET"
],
"tasks": {
"build": {
"dependsOn": ["^build"],
Expand Down

0 comments on commit 80a3c8a

Please sign in to comment.