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

core: add legacy connector adapter #533

Merged
merged 1 commit into from
Nov 12, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "core: add legacy connector adapter",
"packageName": "@starknet-react/core",
"email": "[email protected]",
"dependentChangeType": "patch"
}
1 change: 1 addition & 0 deletions docs/components/starknet/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function StarknetProvider({
includeRecommended: "always",
// Randomize the order of the connectors.
order: "alphabetical",
shimLegacyConnectors: ["okxwallet"],
});

return (
Expand Down
14 changes: 11 additions & 3 deletions packages/core/src/connectors/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { StarknetWindowObject } from "get-starknet-core";
import { useCallback, useEffect, useMemo, useState } from "react";

import type { Connector } from "./base";
import { injected } from "./helpers";
import { injected, legacyInjected } from "./helpers";

export type UseInjectedConnectorsProps = {
/** List of recommended connectors to display. */
Expand All @@ -11,6 +11,8 @@ export type UseInjectedConnectorsProps = {
includeRecommended?: "always" | "onlyIfNoConnectors";
/** How to order connectors. */
order?: "random" | "alphabetical";
/** Shim the following legacy connectors if they are detected. */
shimLegacyConnectors?: string[];
};

export type UseInjectedConnectorsResult = {
Expand All @@ -22,14 +24,20 @@ export function useInjectedConnectors({
recommended,
includeRecommended = "always",
order = "alphabetical",
shimLegacyConnectors = [],
}: UseInjectedConnectorsProps): UseInjectedConnectorsResult {
const [injectedConnectors, setInjectedConnectors] = useState<Connector[]>([]);

const refreshConnectors = useCallback(() => {
const wallets = scanObjectForWallets(window);
const connectors = wallets.map((wallet) => injected({ id: wallet.id }));
const connectors = wallets.map((wallet) => {
if (shimLegacyConnectors.includes(wallet.id)) {
return legacyInjected({ id: wallet.id });
}
return injected({ id: wallet.id });
});
setInjectedConnectors(connectors);
}, []);
}, [shimLegacyConnectors.includes]);

useEffect(() => {
refreshConnectors();
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/connectors/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { InjectedConnector } from "./injected";
import { LegacyInjectedConnector } from "./legacy";

export function argent(): InjectedConnector {
return new InjectedConnector({
Expand All @@ -25,3 +26,13 @@ export function injected({ id }: { id: string }): InjectedConnector {
},
});
}

export function legacyInjected({
id,
}: { id: string }): LegacyInjectedConnector {
return new LegacyInjectedConnector({
options: {
id,
},
});
}
6 changes: 5 additions & 1 deletion packages/core/src/connectors/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export { Connector, type ConnectArgs } from "./base";
export { InjectedConnector, type InjectedConnectorOptions } from "./injected";
export {
LegacyInjectedConnector,
type LegacyInjectedConnectorOptions,
} from "./legacy";
export {
type UseInjectedConnectorsProps,
type UseInjectedConnectorsResult,
Expand All @@ -10,4 +14,4 @@ export {
type MockConnectorAccounts,
type MockConnectorOptions,
} from "./mock";
export { argent, braavos, injected } from "./helpers";
export { argent, braavos, injected, legacyInjected } from "./helpers";
Loading
Loading