From 45dacb76c7a390f65d77e4caba53a1a0dd0c98de Mon Sep 17 00:00:00 2001 From: Sebastien Date: Mon, 30 Oct 2023 07:16:46 +0900 Subject: [PATCH] Broken: refactoring utility function location --- .../paima-mw-core/src/wallets/algorand.ts | 4 +- .../paima-mw-core/src/wallets/cardano.ts | 4 +- .../paima-mw-core/src/wallets/evm/injected.ts | 4 +- .../paima-mw-core/src/wallets/polkadot.ts | 4 +- .../paima-mw-core/src/wallets/wallet-modes.ts | 55 +++-------------- .../paima-sdk/paima-providers/package.json | 3 +- .../paima-sdk/paima-providers/src/index.ts | 2 + .../paima-sdk/paima-providers/src/utils.ts | 60 +++++++++++++++++++ 8 files changed, 81 insertions(+), 55 deletions(-) create mode 100644 packages/paima-sdk/paima-providers/src/utils.ts diff --git a/packages/paima-sdk/paima-mw-core/src/wallets/algorand.ts b/packages/paima-sdk/paima-mw-core/src/wallets/algorand.ts index 54b9140c3..e094110b6 100644 --- a/packages/paima-sdk/paima-mw-core/src/wallets/algorand.ts +++ b/packages/paima-sdk/paima-mw-core/src/wallets/algorand.ts @@ -3,7 +3,7 @@ import { PaimaMiddlewareErrorCode, buildEndpointErrorFxn } from '../errors'; import { AlgorandConnector } from '@paima/providers'; import { getGameName } from '../state'; import type { WalletMode } from './wallet-modes'; -import { connectInjectedWallet } from './wallet-modes'; +import { connectInjected } from './wallet-modes'; export async function algorandLoginWrapper( loginInfo: LoginInfoMap[WalletMode.Algorand] @@ -14,7 +14,7 @@ export async function algorandLoginWrapper( gameName: getGameName(), gameChainId: undefined, // Not needed because of batcher }; - const loginResult = await connectInjectedWallet( + const loginResult = await connectInjected( 'algorandLoginWrapper', errorFxn, PaimaMiddlewareErrorCode.ALGORAND_LOGIN, diff --git a/packages/paima-sdk/paima-mw-core/src/wallets/cardano.ts b/packages/paima-sdk/paima-mw-core/src/wallets/cardano.ts index 433b58250..357c65c05 100644 --- a/packages/paima-sdk/paima-mw-core/src/wallets/cardano.ts +++ b/packages/paima-sdk/paima-mw-core/src/wallets/cardano.ts @@ -1,7 +1,7 @@ import type { LoginInfoMap, OldResult, Result, Wallet } from '../types'; import { buildEndpointErrorFxn, PaimaMiddlewareErrorCode } from '../errors'; import type { WalletMode } from './wallet-modes'; -import { connectInjectedWallet } from './wallet-modes'; +import { connectInjected } from './wallet-modes'; import { CardanoConnector } from '@paima/providers'; import { getGameName } from '../state'; @@ -27,7 +27,7 @@ export async function cardanoLoginWrapper( gameName: getGameName(), gameChainId: undefined, // Not needed because of batcher }; - const loginResult = await connectInjectedWallet( + const loginResult = await connectInjected( 'cardanoLoginWrapper', errorFxn, PaimaMiddlewareErrorCode.CARDANO_LOGIN, diff --git a/packages/paima-sdk/paima-mw-core/src/wallets/evm/injected.ts b/packages/paima-sdk/paima-mw-core/src/wallets/evm/injected.ts index 8375cafa8..be46591f0 100644 --- a/packages/paima-sdk/paima-mw-core/src/wallets/evm/injected.ts +++ b/packages/paima-sdk/paima-mw-core/src/wallets/evm/injected.ts @@ -12,7 +12,7 @@ import { import type { LoginInfoMap, OldResult, Result, Wallet } from '../../types'; import { updateFee } from '../../helpers/posting'; -import { connectInjectedWallet } from '../wallet-modes'; +import { connectInjected } from '../wallet-modes'; import type { WalletMode } from '../wallet-modes'; import { EvmInjectedConnector } from '@paima/providers'; @@ -92,7 +92,7 @@ export async function evmLoginWrapper( gameName: getGameName(), gameChainId: '0x' + getChainId().toString(16), }; - const loginResult = await connectInjectedWallet( + const loginResult = await connectInjected( 'evmLoginWrapper', errorFxn, PaimaMiddlewareErrorCode.EVM_LOGIN, diff --git a/packages/paima-sdk/paima-mw-core/src/wallets/polkadot.ts b/packages/paima-sdk/paima-mw-core/src/wallets/polkadot.ts index be5eff3fb..1094af95e 100644 --- a/packages/paima-sdk/paima-mw-core/src/wallets/polkadot.ts +++ b/packages/paima-sdk/paima-mw-core/src/wallets/polkadot.ts @@ -3,7 +3,7 @@ import { getGameName } from '../state'; import type { LoginInfoMap, Result, Wallet } from '../types'; import { PolkadotConnector } from '@paima/providers'; import type { WalletMode } from './wallet-modes'; -import { connectInjectedWallet } from './wallet-modes'; +import { connectInjected } from './wallet-modes'; export async function polkadotLoginWrapper( loginInfo: LoginInfoMap[WalletMode.Polkadot] @@ -14,7 +14,7 @@ export async function polkadotLoginWrapper( gameName: getGameName(), gameChainId: undefined, // Not needed because of batcher }; - const loginResult = await connectInjectedWallet( + const loginResult = await connectInjected( 'polkadotLoginWrapper', errorFxn, PaimaMiddlewareErrorCode.POLKADOT_LOGIN, diff --git a/packages/paima-sdk/paima-mw-core/src/wallets/wallet-modes.ts b/packages/paima-sdk/paima-mw-core/src/wallets/wallet-modes.ts index d7da7b6df..98904e5d2 100644 --- a/packages/paima-sdk/paima-mw-core/src/wallets/wallet-modes.ts +++ b/packages/paima-sdk/paima-mw-core/src/wallets/wallet-modes.ts @@ -8,30 +8,14 @@ import type { IProvider, GameInfo, EthersApi, + InjectionPreference, + WalletMode, } from '@paima/providers'; -import { WalletNotFound, UnsupportedWallet } from '@paima/providers'; +import { WalletNotFound, UnsupportedWallet, connectInjectedWallet } from '@paima/providers'; import type { EndpointErrorFxn } from '../errors'; import type { Result } from '../types'; import type { PaimaMiddlewareErrorCode } from '../errors'; import { FE_ERR_SPECIFIC_WALLET_NOT_INSTALLED } from '../errors'; -import assertNever from 'assert-never'; - -export const enum WalletMode { - NoWallet, - EvmInjected, - EvmEthers, - Cardano, - Polkadot, - Algorand, -} - -export type InjectionPreference = - | { - name: string; - } - | { - connection: ActiveConnection; - }; export type BaseLoginInfo = { preference?: InjectionPreference; @@ -58,7 +42,7 @@ function getWalletName(info: BaseLoginInfo): undefined | string { } return info.preference.connection.metadata.name; } -export async function connectInjectedWallet( +export async function connectInjected( typeName: string, errorFxn: EndpointErrorFxn, errorCode: PaimaMiddlewareErrorCode, @@ -67,32 +51,11 @@ export async function connectInjectedWallet( gameInfo: GameInfo ): Promise>> { try { - if (loginInfo.preference == null) { - console.log(`${typeName} Attempting simple login`); - const provider = await connector.connectSimple(gameInfo); - return { - success: true, - result: provider, - }; - } else if ('name' in loginInfo.preference) { - const walletName = loginInfo.preference.name; - console.log(`${typeName} Attempting to log into ${walletName}`); - const provider = await connector.connectNamed(gameInfo, walletName); - return { - success: true, - result: provider, - }; - } else if ('connection' in loginInfo.preference) { - const walletName = loginInfo.preference.connection.metadata.name; - console.log(`${typeName} Attempting to log into ${walletName}`); - const provider = await connector.connectExternal(gameInfo, loginInfo.preference.connection); - return { - success: true, - result: provider, - }; - } else { - assertNever(loginInfo.preference); - } + const provider = await connectInjectedWallet(typeName, loginInfo.preference, connector, gameInfo); + return { + success: true, + result: provider + }; } catch (err) { if (err instanceof WalletNotFound || err instanceof UnsupportedWallet) { return errorFxn(errorCode, undefined, FE_ERR_SPECIFIC_WALLET_NOT_INSTALLED); diff --git a/packages/paima-sdk/paima-providers/package.json b/packages/paima-sdk/paima-providers/package.json index 5a9f51a26..b8151677b 100644 --- a/packages/paima-sdk/paima-providers/package.json +++ b/packages/paima-sdk/paima-providers/package.json @@ -33,6 +33,7 @@ "@truffle/hdwallet-provider": "^2.1.15", "bech32": "^2.0.0", "web3": "1.10.0", - "web3-utils": "1.10.0" + "web3-utils": "1.10.0", + "assert-never": "^1.2.1" } } diff --git a/packages/paima-sdk/paima-providers/src/index.ts b/packages/paima-sdk/paima-providers/src/index.ts index 88ed2c1df..5637f021e 100644 --- a/packages/paima-sdk/paima-providers/src/index.ts +++ b/packages/paima-sdk/paima-providers/src/index.ts @@ -3,8 +3,10 @@ export * from './cardano'; export * from './evm/index'; export * from './polkadot'; export * from './errors'; +export * from './utils'; export type * from './algorand'; export type * from './cardano'; export type * from './evm/index'; export type * from './polkadot'; export type * from './IProvider'; +export type * from './utils'; \ No newline at end of file diff --git a/packages/paima-sdk/paima-providers/src/utils.ts b/packages/paima-sdk/paima-providers/src/utils.ts new file mode 100644 index 000000000..9137d766a --- /dev/null +++ b/packages/paima-sdk/paima-providers/src/utils.ts @@ -0,0 +1,60 @@ +import type { ActiveConnection, GameInfo, IConnector, IProvider } from './IProvider'; +import assertNever from 'assert-never'; +import { AlgorandConnector } from './algorand'; +import { EvmInjectedConnector } from './evm'; +import { CardanoConnector } from './cardano'; +import { PolkadotConnector } from './polkadot'; + +export const enum WalletMode { + NoWallet, + EvmInjected, + EvmEthers, + Cardano, + Polkadot, + Algorand, +} + +export type InjectionPreference = + | { + name: string; + } + | { + connection: ActiveConnection; + }; + +export async function allInjectedWallets(gameInfo: GameInfo): Promise<{ + [WalletMode.EvmInjected]: ReturnType, + [WalletMode.Cardano]: ReturnType, + [WalletMode.Polkadot]: Awaited>, + [WalletMode.Algorand]: ReturnType, +}> { + return { + [WalletMode.EvmInjected]: EvmInjectedConnector.getWalletOptions(), + [WalletMode.Cardano]: CardanoConnector.getWalletOptions(), + [WalletMode.Polkadot]: await PolkadotConnector.getWalletOptions(gameInfo.gameName), + [WalletMode.Algorand]: AlgorandConnector.getWalletOptions(), + } +} +export async function connectInjectedWallet( + typeName: string, + preference: undefined | InjectionPreference, + connector: IConnector, + gameInfo: GameInfo +): Promise> { + if (preference == null) { + console.log(`${typeName} Attempting simple login`); + const provider = await connector.connectSimple(gameInfo); + return provider; + } else if ('name' in preference) { + const walletName = preference.name; + console.log(`${typeName} Attempting to log into ${walletName}`); + const provider = await connector.connectNamed(gameInfo, walletName); + return provider; + } else if ('connection' in preference) { + const walletName = preference.connection.metadata.name; + console.log(`${typeName} Attempting to log into ${walletName}`); + const provider = await connector.connectExternal(gameInfo, preference.connection); + return provider; + } + assertNever(preference); +}