Skip to content

Commit

Permalink
core: add legacy connector adapter (#533)
Browse files Browse the repository at this point in the history
## Context

This PR adds a new `LegacyInjectedConnector` to allow using (some)
methods from wallets that haven't upgraded to the new wallet api.
  • Loading branch information
fracek authored Nov 12, 2024
2 parents d162533 + 2b2c63d commit 17fce9b
Show file tree
Hide file tree
Showing 6 changed files with 373 additions and 4 deletions.
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

0 comments on commit 17fce9b

Please sign in to comment.