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

feat: storing network alongside namespace for hub localstorage #965

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions wallets/core/namespaces/common/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@rango-dev/wallets-core/namespaces/common",
"type": "module",
"main": "../../dist/namespaces/common/mod.js",
"module": "../../dist/namespaces/common/mod.js",
"types": "../../dist/namespaces/common/mod.d.ts",
"sideEffects": false
}
8 changes: 8 additions & 0 deletions wallets/core/namespaces/evm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@rango-dev/wallets-core/namespaces/evm",
"type": "module",
"main": "../../dist/namespaces/evm/mod.js",
"module": "../../dist/namespaces/evm/mod.js",
"types": "../../dist/namespaces/evm/mod.d.ts",
"sideEffects": false
}
8 changes: 8 additions & 0 deletions wallets/core/namespaces/solana/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@rango-dev/wallets-core/namespaces/solana",
"type": "module",
"main": "../../dist/namespaces/solana/mod.js",
"module": "../../dist/namespaces/solana/mod.js",
"types": "../../dist/namespaces/solana/mod.d.ts",
"sideEffects": false
}
6 changes: 1 addition & 5 deletions wallets/core/src/legacy/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ export type {
NamespaceInputWithDiscoverMode as LegacyNamespaceInputWithDiscoverMode,
} from './types.js';

export {
Events as LegacyEvents,
Namespace as LegacyNamespace,
Networks as LegacyNetworks,
} from './types.js';
export { Events as LegacyEvents, Networks as LegacyNetworks } from './types.js';

export { Persistor } from './persistor.js';
export {
Expand Down
19 changes: 2 additions & 17 deletions wallets/core/src/legacy/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { State as WalletState } from './wallet.js';
import type { Namespace } from '../namespaces/common/mod.js';
import type { BlockchainMeta, SignerFactory } from 'rango-types';

export enum Networks {
Expand Down Expand Up @@ -64,15 +65,6 @@ export enum Networks {
Unknown = 'Unkown',
}

export enum Namespace {
Solana = 'Solana',
Evm = 'EVM',
Cosmos = 'Cosmos',
Utxo = 'UTXO',
Starknet = 'Starknet',
Tron = 'Tron',
}

export type NamespaceData = {
namespace: Namespace;
derivationPath?: string;
Expand Down Expand Up @@ -248,13 +240,6 @@ export type ProviderInterface = { config: WalletConfig } & WalletActions;
// it comes from wallets.ts and `connect`
type NetworkTypeFromLegacyConnect = Network | undefined;

export type NetworkTypeForNamespace<T extends NamespacesWithDiscoverMode> =
T extends 'DISCOVER_MODE'
? string
: T extends Namespace
? NetworkTypeFromLegacyConnect
: never;

export type NamespacesWithDiscoverMode = Namespace | 'DISCOVER_MODE';

export type NamespaceInputWithDiscoverMode = {
Expand All @@ -273,7 +258,7 @@ export type NamespaceInputForConnect<T extends Namespace = Namespace> =
/**
* In some cases, we need to connect a specific network on a namespace. e.g. Polygon on EVM.
*/
network: NetworkTypeForNamespace<T>;
network: NetworkTypeFromLegacyConnect;
derivationPath?: string;
}
| NamespaceInputWithDiscoverMode;
6 changes: 2 additions & 4 deletions wallets/core/src/legacy/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import type {
NamespaceInputWithDiscoverMode,
} from './types.js';

import { Namespace } from './types.js';

export async function eagerConnectHandler<R = unknown>(params: {
canEagerConnect: () => Promise<boolean>;
connectHandler: () => Promise<R>;
Expand All @@ -26,6 +24,6 @@ export function isNamespaceDiscoverMode(

export function isEvmNamespace(
namespace: NamespaceInputForConnect
): namespace is NamespaceInputForConnect<Namespace.Evm> {
return namespace.namespace === Namespace.Evm;
): namespace is NamespaceInputForConnect<'EVM'> {
return namespace.namespace === 'EVM';
}
2 changes: 2 additions & 0 deletions wallets/core/src/namespaces/common/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ export type {
Accounts,
AccountsWithActiveChain,
} from '../../types/accounts.js';

export type { Namespace } from './types.js';
16 changes: 16 additions & 0 deletions wallets/core/src/namespaces/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* These are supported namespaces in Rango that we want to officially support.
* This should be private and don't make it public since core can support more namespaces and should be extendable.
*/
type RangoNamespace =
| 'EVM'
| 'Solana'
| 'Cosmos'
| 'UTXO'
| 'Starknet'
| 'Tron'
| 'Ton';

// eslint-disable-next-line @typescript-eslint/ban-types
export type Namespace = RangoNamespace | (string & {});

export interface CommonActions {
init: () => void;
}
Expand Down
8 changes: 4 additions & 4 deletions wallets/provider-ledger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
WalletInfo,
} from '@rango-dev/wallets-shared';

import { Namespace, Networks, WalletTypes } from '@rango-dev/wallets-shared';
import { Networks, WalletTypes } from '@rango-dev/wallets-shared';
import { type BlockchainMeta, type SignerFactory } from 'rango-types';

import {
Expand All @@ -26,10 +26,10 @@ export const connect: Connect = async ({ namespaces }) => {
const results: ProviderConnectResult[] = [];

const solanaNamespace = namespaces?.find(
(namespaceItem) => namespaceItem.namespace === Namespace.Solana
(namespaceItem) => namespaceItem.namespace === 'Solana'
);
const evmNamespace = namespaces?.find(
(namespaceItem) => namespaceItem.namespace === Namespace.Evm
(namespaceItem) => namespaceItem.namespace === 'EVM'
);

if (solanaNamespace) {
Expand Down Expand Up @@ -93,7 +93,7 @@ export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = (
},
color: 'black',
supportedChains,
namespaces: [Namespace.Evm, Namespace.Solana],
namespaces: ['EVM', 'Solana'],
singleNamespace: true,
showOnMobile: false,
needsDerivationPath: true,
Expand Down
6 changes: 3 additions & 3 deletions wallets/provider-trezor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
WalletInfo,
} from '@rango-dev/wallets-shared';

import { Namespace, Networks, WalletTypes } from '@rango-dev/wallets-shared';
import { Networks, WalletTypes } from '@rango-dev/wallets-shared';
import { type BlockchainMeta, type SignerFactory } from 'rango-types';

import {
Expand Down Expand Up @@ -40,7 +40,7 @@ export const connect: Connect = async ({ namespaces }) => {
const TrezorConnect = await getTrezorModule();

const evmNamespace = namespaces?.find(
(namespaceItem) => namespaceItem.namespace === Namespace.Evm
(namespaceItem) => namespaceItem.namespace === 'EVM'
);

if (evmNamespace) {
Expand Down Expand Up @@ -95,7 +95,7 @@ export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = (
},
color: 'black',
supportedChains,
namespaces: [Namespace.Evm],
namespaces: ['EVM'],
singleNamespace: true,
showOnMobile: false,
needsDerivationPath: true,
Expand Down
6 changes: 3 additions & 3 deletions wallets/react/src/hub/autoConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { Hub } from '@rango-dev/wallets-core';
import type {
LegacyNamespaceInputForConnect,
LegacyProviderInterface,
LegacyNamespace as Namespace,
} from '@rango-dev/wallets-core/legacy';
import type { Namespace } from '@rango-dev/wallets-core/namespaces/common';

import {
legacyEagerConnectHandler,
Expand Down Expand Up @@ -131,8 +131,8 @@ export async function autoConnect(deps: {
const namespaces: LegacyNamespaceInputForConnect[] = lastConnectedWallets[
providerName
].map((namespace) => ({
namespace: namespace as Namespace,
network: undefined,
namespace: namespace.namsepace,
network: namespace.network,
}));

const canEagerConnect = async () => {
Expand Down
13 changes: 10 additions & 3 deletions wallets/react/src/hub/lastConnectedWallets.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import type { Namespace } from '@rango-dev/wallets-core/namespaces/common';

import { Persistor } from '@rango-dev/wallets-core/legacy';

import {
HUB_LAST_CONNECTED_WALLETS,
LEGACY_LAST_CONNECTED_WALLETS,
} from './constants.js';

interface NamespaceInput {
namsepace: Namespace;
network: string | undefined;
}

export interface LastConnectedWalletsStorage {
[providerId: string]: string[];
[providerId: string]: NamespaceInput[];
}

export type LegacyLastConnectedWalletsStorage = string[];
Expand All @@ -22,7 +29,7 @@ export class LastConnectedWalletsFromStorage {
this.#storageKey = storageKey;
}

addWallet(providerId: string, namespaces: string[]): void {
addWallet(providerId: string, namespaces: NamespaceInput[]): void {
if (this.#storageKey === HUB_LAST_CONNECTED_WALLETS) {
return this.#addWalletToHub(providerId, namespaces);
} else if (this.#storageKey === LEGACY_LAST_CONNECTED_WALLETS) {
Expand Down Expand Up @@ -64,7 +71,7 @@ export class LastConnectedWalletsFromStorage {
persistor.getItem(HUB_LAST_CONNECTED_WALLETS) || {};
return lastConnectedWallets;
}
#addWalletToHub(providerId: string, namespaces: string[]): void {
#addWalletToHub(providerId: string, namespaces: NamespaceInput[]): void {
const storage = new Persistor<LastConnectedWalletsStorage>();
const data = storage.getItem(this.#storageKey) || {};

Expand Down
16 changes: 9 additions & 7 deletions wallets/react/src/hub/useHubAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { AllProxiedNamespaces, ExtensionLink } from './types.js';
import type { Providers } from '../index.js';
import type { Provider } from '@rango-dev/wallets-core';
import type {
LegacyNamespaceInputForConnect,
LegacyNamespace as Namespace,
} from '@rango-dev/wallets-core/legacy';
import type { LegacyNamespaceInputForConnect } from '@rango-dev/wallets-core/legacy';
import type { Namespace } from '@rango-dev/wallets-core/namespaces/common';
import type { VersionedProviders } from '@rango-dev/wallets-core/utils';

import { legacyIsNamespaceDiscoverMode } from '@rango-dev/wallets-core/legacy';
Expand Down Expand Up @@ -200,11 +198,15 @@ export function useHubAdapter(params: UseAdapterParams): ProviderContext {
params.allVersionedProviders,
type
);

if (legacyProvider.canEagerConnect) {
const namespaces = targetNamespaces.map(
(targetNamespace) => targetNamespace[0].namespace
lastConnectedWalletsFromStorage.addWallet(
type,
namespaces.map((ns) => ({
namsepace: ns.namespace,
network: ns.network,
}))
);
lastConnectedWalletsFromStorage.addWallet(type, namespaces);
}

return connectResultWithLegacyFormat;
Expand Down
17 changes: 8 additions & 9 deletions wallets/react/src/hub/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import type {
LegacyProviderInterface,
LegacyEventHandler as WalletEventHandler,
} from '@rango-dev/wallets-core/legacy';
import type { Namespace } from '@rango-dev/wallets-core/namespaces/common';

import {
guessProviderStateSelector,
namespaceStateSelector,
} from '@rango-dev/wallets-core';
import {
LegacyEvents as Events,
LegacyNamespace as Namespace,
} from '@rango-dev/wallets-core/legacy';
import { LegacyEvents as Events } from '@rango-dev/wallets-core/legacy';
import {
generateStoreId,
type VersionedProviders,
Expand Down Expand Up @@ -269,7 +267,7 @@ export function discoverNamespace(network: string): Namespace {
case Networks.TERRA:
case Networks.THORCHAIN:
case Networks.UMEE:
return Namespace.Cosmos;
return 'Cosmos';
case Networks.AVAX_CCHAIN:
case Networks.ARBITRUM:
case Networks.BOBA:
Expand All @@ -284,17 +282,18 @@ export function discoverNamespace(network: string): Namespace {
case Networks.OPTIMISM:
case Networks.POLYGON:
case Networks.STARKNET:
return Namespace.Evm;
return 'Evm';
case Networks.SOLANA:
return Namespace.Solana;
return 'Solana';
case Networks.BTC:
case Networks.BCH:
case Networks.DOGE:
case Networks.LTC:
case Networks.TRON:
return Namespace.Utxo;
case Networks.POLKADOT:
return 'UTXO';
case Networks.TON:
return 'Ton';
case Networks.POLKADOT:
case Networks.AXELAR:
case Networks.MARS:
case Networks.MAYA:
Expand Down
Loading
Loading