Skip to content

Commit

Permalink
Support an optional aptosConnectConfig user prop to set AptosConnec…
Browse files Browse the repository at this point in the history
…t configuration (#326)

* Support aptosConnectConfig user prop to set AptosConnect configuration

* address feedback
  • Loading branch information
0xmaayan authored Jun 17, 2024
1 parent 7eb058c commit 07ee265
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 30 deletions.
7 changes: 7 additions & 0 deletions .changeset/healthy-parrots-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@aptos-labs/wallet-adapter-react": minor
"@aptos-labs/wallet-adapter-core": minor
"@aptos-labs/wallet-adapter-nextjs-example": minor
---

Support dappConfig user prop to set SDK wallets configuration
2 changes: 2 additions & 0 deletions apps/nextjs-example/src/components/WalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PontemWallet } from "@pontem/wallet-adapter-plugin";
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 { useAutoConnect } from "./AutoConnectProvider";
import { useToast } from "./ui/use-toast";

Expand All @@ -30,6 +31,7 @@ export const WalletProvider = ({ children }: PropsWithChildren) => {
<AptosWalletAdapterProvider
plugins={wallets}
autoConnect={autoConnect}
dappConfig={{ network: Network.DEVNET }}
onError={(error) => {
toast({
variant: "destructive",
Expand Down
32 changes: 15 additions & 17 deletions packages/wallet-adapter-core/src/AIP62StandardWallets/sdkWallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@ import { AptosConnectWallet } from "@aptos-connect/wallet-adapter-plugin";
import { AptosStandardWallet } from "./WalletStandard";
import { Network } from "@aptos-labs/ts-sdk";

const isProd = process.env.NODE_ENV === "production";
export function getSDKWallets(dappConfig?: { network: Network }) {
const sdkWallets: AptosStandardWallet[] = [];
// Push production wallet if env is production, otherwise use dev wallet
if (dappConfig?.network === Network.MAINNET) {
// TODO twallet uses @aptos-labs/wallet-standard at version 0.0.11 while adapter uses
// a newer version (0.1.0) - this causes type mismatch. We should figure out how to handle it.
sdkWallets.push(new TWallet() as any);
} else {
sdkWallets.push(new DevTWallet() as any);
}

const sdkWallets: AptosStandardWallet[] = [];
// Need to check window is defined for AptosConnect
if (typeof window !== "undefined") {
sdkWallets.push(new AptosConnectWallet({ network: dappConfig?.network }));
}

// Push production wallet if env is production, otherwise use dev wallet
if (isProd) {
// TODO twallet uses @aptos-labs/wallet-standard at version 0.0.11 while adapter uses
// a newer version (0.1.0) - this causes type mismatch. We should figure out how to handle it.
sdkWallets.push(new TWallet() as any);
} else {
sdkWallets.push(new DevTWallet() as any);
return sdkWallets;
}

let aptosNetwork = isProd ? Network.MAINNET : Network.TESTNET;
// Need to check window is defined for AptosConnect
if (typeof window !== "undefined") {
sdkWallets.push(new AptosConnectWallet({ network: aptosNetwork }));
}

export default sdkWallets;
35 changes: 23 additions & 12 deletions packages/wallet-adapter-core/src/WalletCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
isWalletWithRequiredFeatureSet,
} from "@aptos-labs/wallet-standard";

import SDKWallets from "./AIP62StandardWallets/sdkWallets";
import { getSDKWallets } from "./AIP62StandardWallets/sdkWallets";
import { ChainIdToAnsSupportedNetworkMap, WalletReadyState } from "./constants";
import {
WalletAccountChangeError,
Expand Down Expand Up @@ -127,6 +127,12 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
// Google Analytics 4 module
private readonly ga4: GA4 = new GA4();

// JSON configuration for AptosConnect
private _dappConfig: { network: Network } | undefined;

// Local private variable to hold SDK wallets in the adapter
private readonly _sdkWallets: AptosStandardWallet[];

/**
* Core functionality constructor.
* For legacy wallet adapter v1 support we expect the dapp to pass in wallet plugins,
Expand All @@ -136,11 +142,14 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
*/
constructor(
plugins: ReadonlyArray<Wallet>,
optInWallets: ReadonlyArray<AvailableWallets>
optInWallets: ReadonlyArray<AvailableWallets>,
dappConfig?: { network: Network }
) {
super();
this._wallets = plugins;
this._optInWallets = optInWallets;
this._dappConfig = dappConfig;
this._sdkWallets = getSDKWallets(this._dappConfig);
// Strategy to detect legacy wallet adapter v1 wallet plugins
this.scopePollingDetectionStrategy();
// Strategy to detect AIP-62 standard compatible wallets (extension + SDK wallets)
Expand Down Expand Up @@ -224,17 +233,19 @@ export class WalletCore extends EventEmitter<WalletCoreEvents> {
private setWallets(extensionwWallets: readonly AptosWallet[]) {
const aptosStandardWallets: AptosStandardWallet[] = [];

[...SDKWallets, ...extensionwWallets].map((wallet: AptosStandardWallet) => {
if (this.excludeWallet(wallet.name)) {
return;
}
const isValid = isWalletWithRequiredFeatureSet(wallet);
if (isValid) {
wallet.readyState = WalletReadyState.Installed;
aptosStandardWallets.push(wallet);
this.standardizeStandardWalletToPluginWalletType(wallet);
[...this._sdkWallets, ...extensionwWallets].map(
(wallet: AptosStandardWallet) => {
if (this.excludeWallet(wallet.name)) {
return;
}
const isValid = isWalletWithRequiredFeatureSet(wallet);
if (isValid) {
wallet.readyState = WalletReadyState.Installed;
aptosStandardWallets.push(wallet);
this.standardizeStandardWalletToPluginWalletType(wallet);
}
}
});
);

this._standard_wallets = aptosStandardWallets;
// Append AIP-62 compatible wallets that are not detected on the user machine
Expand Down
5 changes: 4 additions & 1 deletion packages/wallet-adapter-react/src/WalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface AptosWalletProviderProps {
plugins?: ReadonlyArray<Wallet>;
optInWallets?: ReadonlyArray<AvailableWallets>;
autoConnect?: boolean;
dappConfig?: { network: Network };
onError?: (error: any) => void;
}

Expand All @@ -54,6 +55,7 @@ const initialState: {
* @param plugins Non AIP-62 supported wallet plugins array
* @param optInWallets AIP-62 supported wallet names array to only include in the adapter wallets
* @param autoConnect A boolean flag to indicate if the adapter should auto connect to a wallet
* @param dappConfig The dapp configurations to be used by SDK wallets to set their configurations
* @param onError A callback function to execute when there is an error in the adapter
*
*/
Expand All @@ -62,6 +64,7 @@ export const AptosWalletAdapterProvider: FC<AptosWalletProviderProps> = ({
plugins,
optInWallets,
autoConnect = false,
dappConfig,
onError,
}: AptosWalletProviderProps) => {
const [{ connected, account, network, wallet }, setState] =
Expand All @@ -72,7 +75,7 @@ export const AptosWalletAdapterProvider: FC<AptosWalletProviderProps> = ({
const [isLoading, setIsLoading] = useState<boolean>(true);

const walletCore = useMemo(
() => new WalletCore(plugins ?? [], optInWallets ?? []),
() => new WalletCore(plugins ?? [], optInWallets ?? [], dappConfig),
[]
);
const [wallets, setWallets] = useState<
Expand Down

0 comments on commit 07ee265

Please sign in to comment.