Skip to content

Commit

Permalink
chore: move namespaces info to provider and remove the hardcoded mapp…
Browse files Browse the repository at this point in the history
…ing in wallets-shared
  • Loading branch information
yeager-eren committed Dec 15, 2024
1 parent 52953ea commit a6d4c11
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 37 deletions.
4 changes: 2 additions & 2 deletions wallets/core/src/hub/store/providers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Namespace } from '../../namespaces/common/types.js';
import type { State as InternalProviderState } from '../provider/mod.js';
import type { CommonNamespaceKeys } from '../provider/types.js';
import type { StateCreator } from 'zustand';

import { produce } from 'immer';
Expand All @@ -8,7 +8,7 @@ import { guessProviderStateSelector, type State } from './mod.js';

type Browsers = 'firefox' | 'chrome' | 'edge' | 'brave' | 'homepage';
type Property<N extends string, V> = { name: N; value: V };
type DetachedInstances = Property<'detached', CommonNamespaceKeys[]>;
type DetachedInstances = Property<'detached', Namespace[]>;

export type ProviderInfo = {
name: string;
Expand Down
24 changes: 21 additions & 3 deletions wallets/core/src/legacy/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ export type InstallObjects = {
DEFAULT: string;
};

interface NeedsNamespace {
selection: 'single' | 'multiple';
data: {
label: string;
id: string;
value: Namespace;
}[];
}

interface NeedsDerivationPath {
data: {
id: string;
label: string;
namespace: Namespace;
generateDerivationPath: (index: string) => string;
}[];
}

export type WalletInfo = {
name: string;
img: string;
Expand All @@ -93,9 +111,9 @@ export type WalletInfo = {
showOnMobile?: boolean;
isContractWallet?: boolean;
mobileWallet?: boolean;
namespaces?: Namespace[];
singleNamespace?: boolean;
needsDerivationPath?: boolean;

needsDerivationPath?: NeedsDerivationPath;
needsNamespace?: NeedsNamespace;
};

export type State = {
Expand Down
53 changes: 50 additions & 3 deletions wallets/provider-ledger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,56 @@ export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = (
},
color: 'black',
supportedChains,
namespaces: ['EVM', 'Solana'],
singleNamespace: true,
showOnMobile: false,
needsDerivationPath: true,
needsDerivationPath: {
data: [
{
id: 'metamask',
label: `Metamask (m/44'/60'/0'/0/index)`,
namespace: 'EVM',
generateDerivationPath: (index: string) => `44'/60'/0'/0/${index}`,
},
{
id: 'ledgerLive',
label: `LedgerLive (m/44'/60'/index'/0/0)`,
namespace: 'EVM',
generateDerivationPath: (index: string) => `44'/60'/${index}'/0/0`,
},
{
id: 'legacy',
label: `Legacy (m/44'/60'/0'/index)`,
namespace: 'EVM',
generateDerivationPath: (index: string) => `44'/60'/0'/${index}`,
},
{
id: `(m/44'/501'/index')`,
label: `(m/44'/501'/index')`,
namespace: 'Solana',
generateDerivationPath: (index: string) => `44'/501'/${index}'`,
},
{
id: `(m/44'/501'/0'/index)`,
label: `(m/44'/501'/0'/index)`,
namespace: 'Solana',
generateDerivationPath: (index: string) => `44'/501'/0'/${index}`,
},
],
},

needsNamespace: {
selection: 'single',
data: [
{
label: 'EVM',
value: 'EVM',
id: 'Ethereum',
},
{
label: 'Solana',
value: 'Solana',
id: 'Solana',
},
],
},
};
};
2 changes: 1 addition & 1 deletion wallets/provider-phantom/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const info: ProviderInfo = {
{
name: 'detached',
// if you are adding a new namespace, don't forget to also update `getWalletInfo`
value: ['solana', 'evm'],
value: ['Solana', 'EVM'],
},
],
};
16 changes: 16 additions & 0 deletions wallets/provider-phantom/src/legacy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = (
EVM_SUPPORTED_CHAINS.includes(chain.name as Networks)
),
],

needsNamespace: {
selection: 'single',
data: [
{
label: 'EVM',
value: 'EVM',
id: 'Ethereum',
},
{
label: 'Solana',
value: 'Solana',
id: 'Solana',
},
],
},
};
};

Expand Down
49 changes: 46 additions & 3 deletions wallets/provider-trezor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = (
if (ethereumBlockchain) {
supportedChains.push(ethereumBlockchain);
}

return {
name: 'Trezor',
img: 'https://raw.githubusercontent.com/rango-exchange/assets/main/wallets/trezor/icon.svg',
Expand All @@ -95,9 +96,51 @@ export const getWalletInfo: (allBlockChains: BlockchainMeta[]) => WalletInfo = (
},
color: 'black',
supportedChains,
namespaces: ['EVM'],
singleNamespace: true,
showOnMobile: false,
needsDerivationPath: true,

needsNamespace: {
selection: 'single',
data: [
{
id: 'EVM',
value: 'EVM',
label: 'Ethereum',
},
],
},
needsDerivationPath: {
data: [
{
id: 'metamask',
label: `Metamask (m/44'/60'/0'/0/index)`,
namespace: 'EVM',
generateDerivationPath: (index: string) => `44'/60'/0'/0/${index}`,
},
{
id: 'ledgerLive',
label: `LedgerLive (m/44'/60'/index'/0/0)`,
namespace: 'EVM',
generateDerivationPath: (index: string) => `44'/60'/${index}'/0/0`,
},
{
id: 'legacy',
label: `Legacy (m/44'/60'/0'/index)`,
namespace: 'EVM',
generateDerivationPath: (index: string) => `44'/60'/0'/${index}`,
},
{
id: `(m/44'/501'/index')`,
label: `(m/44'/501'/index')`,
namespace: 'Solana',
generateDerivationPath: (index: string) => `44'/501'/${index}'`,
},
{
id: `(m/44'/501'/0'/index)`,
label: `(m/44'/501'/0'/index)`,
namespace: 'Solana',
generateDerivationPath: (index: string) => `44'/501'/0'/${index}`,
},
],
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export function WalletList(props: PropTypes) {
const isDisconnected = wallet.state === WalletState.DISCONNECTED;
const isConnectedButDifferentThanTargetNamespace = wallet.isHub
? !conciseAddress
: !!wallet.namespaces && !conciseAddress;
: !!wallet.needsNamespace && !conciseAddress;

if (isDisconnected) {
setSelectedWalletToConnect(wallet);
Expand Down
36 changes: 22 additions & 14 deletions widget/embedded/src/hooks/useStatefulConnect/useStatefulConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { WalletState } from '@rango-dev/ui';
import { useWallets } from '@rango-dev/wallets-react';
import { useReducer } from 'react';

import { convertCommonNamespacesKeysToLegacyNamespace } from '../../utils/hub';

import {
isStateOnDerivationPathStep,
isStateOnNamespace,
Expand Down Expand Up @@ -99,10 +97,7 @@ export function useStatefulConnect(): UseStatefulConnect {
detachedInstances && wallet.state !== 'connected';

if (needsNamespace) {
const availableNamespaces =
convertCommonNamespacesKeysToLegacyNamespace(
detachedInstances.value
);
const availableNamespaces = detachedInstances.value;

dispatch({
type: 'needsNamespace',
Expand All @@ -117,12 +112,19 @@ export function useStatefulConnect(): UseStatefulConnect {
}
}

// Legacy
if (!wallet.namespaces) {
/*
* Legacy
*
* For legacy there are 3 states:
* 1. a wallet doesn't have any namespace defined, we call the connect.
* 2. a wallet has more than two namespaces, we should show namepsace modal, and in that place user will be checked to needs derivation path or not.
* 3. a wallet has exactly one namespacesape, in this case we check if that needs derivation or not, if it needs we will do it here by dispatching the action accordingly.
*/
if (!wallet.needsNamespace) {
return await runConnect(wallet.type, undefined, options);
}

const needsNamespace = wallet.namespaces.length > 1;
const needsNamespace = wallet.needsNamespace.data.length > 1;
const needsDerivationPath = wallet.needsDerivationPath;

if (needsNamespace) {
Expand All @@ -131,26 +133,32 @@ export function useStatefulConnect(): UseStatefulConnect {
payload: {
providerType: wallet.type,
providerImage: wallet.image,
availableNamespaces: wallet.namespaces,
singleNamespace: wallet.singleNamespace,
availableNamespaces: wallet.needsNamespace.data.map(
(ns) => ns.value
),
singleNamespace: wallet.needsNamespace.selection === 'single',
},
});
return { status: ResultStatus.Namespace };
} else if (needsDerivationPath) {
const namespace = wallet.needsNamespace.data[0];

dispatch({
type: 'needsDerivationPath',
payload: {
providerType: wallet.type,
providerImage: wallet.image,
namespace: wallet.namespaces[0],
namespace: namespace.value,
},
});
return { status: ResultStatus.DerivationPath };
}

return await runConnect(
wallet.type,
wallet.namespaces.map((namespace) => ({ namespace })),
wallet.needsNamespace?.data.map((namespace) => ({
namespace: namespace.value,
})),
options
);
}
Expand All @@ -167,7 +175,7 @@ export function useStatefulConnect(): UseStatefulConnect {
wallet: WalletInfoWithExtra,
selectedNamespaces: Namespace[]
): Promise<Result> => {
const isSingleNamespace = wallet.singleNamespace;
const isSingleNamespace = wallet.needsNamespace?.selection === 'single';
const needsDerivationPath = wallet.needsDerivationPath;
const firstSelectedNamespace = selectedNamespaces[0];

Expand Down
7 changes: 1 addition & 6 deletions widget/embedded/src/types/wallets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { WalletInfo } from '@rango-dev/ui';
import type { Namespace } from '@rango-dev/wallets-core/namespaces/common';
import type { WalletType } from '@rango-dev/wallets-shared';

export interface Wallet {
Expand All @@ -25,8 +24,4 @@ export type TokensBalance = {
[key: TokenHash]: Balance;
};

export type WalletInfoWithExtra = WalletInfo & {
namespaces?: Namespace[];
singleNamespace?: boolean;
needsDerivationPath?: boolean;
};
export type WalletInfoWithExtra = WalletInfo;
6 changes: 2 additions & 4 deletions widget/embedded/src/utils/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ export function mapWalletTypesToWalletInfo(
img: image,
installLink,
showOnMobile,
namespaces,
singleNamespace,
needsNamespace,
supportedChains,
needsDerivationPath,
properties,
Expand All @@ -106,8 +105,7 @@ export function mapWalletTypesToWalletInfo(
state,
type,
showOnMobile,
namespaces,
singleNamespace,
needsNamespace,
blockchainTypes,
needsDerivationPath,
properties,
Expand Down
3 changes: 3 additions & 0 deletions widget/ui/src/components/Wallet/Wallet.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { LegacyWalletInfo } from '@rango-dev/wallets-core/legacy';
import type { InstallObjects, WalletType } from '@rango-dev/wallets-shared';
import type { TransactionType } from 'rango-types';

Expand All @@ -16,6 +17,8 @@ export type WalletInfo = {
type: string;
showOnMobile?: boolean;
blockchainTypes: TransactionType[];
needsNamespace?: LegacyWalletInfo['needsNamespace'];
needsDerivationPath?: LegacyWalletInfo['needsDerivationPath'];
};

export interface Info {
Expand Down

0 comments on commit a6d4c11

Please sign in to comment.