diff --git a/package.json b/package.json index f00aeea09..86aa46819 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,5 @@ "./packages/*/*" ], "dependencies": { - "assert-never": "^1.2.1" } } diff --git a/packages/batcher/address-validator/package.json b/packages/batcher/address-validator/package.json index b37299146..032b5e7da 100644 --- a/packages/batcher/address-validator/package.json +++ b/packages/batcher/address-validator/package.json @@ -11,7 +11,8 @@ }, "dependencies": { "pg": "^8.7.3", - "web3": "1.10.0" + "web3": "1.10.0", + "assert-never": "^1.2.1" }, "devDependencies": { "@types/pg": "^8.6.5" diff --git a/packages/batcher/utils/package.json b/packages/batcher/utils/package.json index 6df26a2c2..afab2fb5f 100644 --- a/packages/batcher/utils/package.json +++ b/packages/batcher/utils/package.json @@ -11,6 +11,7 @@ }, "dependencies": { "@truffle/hdwallet-provider": "^2.1.15", - "web3": "1.10.0" + "web3": "1.10.0", + "assert-never": "^1.2.1" } } diff --git a/packages/engine/paima-funnel/package.json b/packages/engine/paima-funnel/package.json index bdce22a7c..394c97860 100644 --- a/packages/engine/paima-funnel/package.json +++ b/packages/engine/paima-funnel/package.json @@ -17,5 +17,6 @@ "typescript": "^5.2.2" }, "dependencies": { + "assert-never": "^1.2.1" } } diff --git a/packages/engine/paima-runtime/package.json b/packages/engine/paima-runtime/package.json index 979635e1a..9bcfcef78 100644 --- a/packages/engine/paima-runtime/package.json +++ b/packages/engine/paima-runtime/package.json @@ -17,7 +17,8 @@ "express": "^4.18.1", "fnv-plus": "^1.3.1", "json-stable-stringify": "^1.0.2", - "yaml": "^2.3.1" + "yaml": "^2.3.1", + "assert-never": "^1.2.1" }, "devDependencies": { "@types/cors": "^2.8.12", diff --git a/packages/engine/paima-sm/package.json b/packages/engine/paima-sm/package.json index 12d05ea07..fe82ccf6b 100644 --- a/packages/engine/paima-sm/package.json +++ b/packages/engine/paima-sm/package.json @@ -14,5 +14,6 @@ }, "author": "Paima Studios", "dependencies": { + "assert-never": "^1.2.1" } } diff --git a/packages/paima-sdk/paima-mw-core/package.json b/packages/paima-sdk/paima-mw-core/package.json index 53517593d..5bfe9fd76 100644 --- a/packages/paima-sdk/paima-mw-core/package.json +++ b/packages/paima-sdk/paima-mw-core/package.json @@ -40,6 +40,7 @@ "@paima/utils": "1.1.5", "@paima/providers": "1.1.5", "@paima/concise": "1.1.5", - "@paima/prando": "1.1.5" + "@paima/prando": "1.1.5", + "assert-never": "^1.2.1" } } diff --git a/packages/paima-sdk/paima-mw-core/src/endpoints/accounts.ts b/packages/paima-sdk/paima-mw-core/src/endpoints/accounts.ts index 5da8f980c..bbe328c70 100644 --- a/packages/paima-sdk/paima-mw-core/src/endpoints/accounts.ts +++ b/packages/paima-sdk/paima-mw-core/src/endpoints/accounts.ts @@ -5,7 +5,7 @@ import { } from '../helpers/auxiliary-queries'; import { checkCardanoWalletStatus } from '../wallets/cardano'; import { checkEthWalletStatus } from '../wallets/evm'; -import { specificWalletLogin, stringToWalletMode } from '../wallets/wallets'; +import { specificWalletLogin } from '../wallets/wallets'; import { getEmulatedBlocksActive, getPostingMode, @@ -14,6 +14,7 @@ import { setEmulatedBlocksInactive, } from '../state'; import type { Result, OldResult, Wallet } from '../types'; +import type { LoginInfo } from '../wallets/wallet-modes'; /** * Wrapper function for all wallet status checking functions @@ -39,15 +40,10 @@ async function checkWalletStatus(): Promise { * thus allowing the game to get past the login screen. * @param preferBatchedMode - If true (or truthy value) even EVM wallet inputs will be batched. */ -async function userWalletLogin( - loginType: string, - preferBatchedMode: boolean = false -): Promise> { +async function userWalletLogin(loginInfo: LoginInfo): Promise> { const errorFxn = buildEndpointErrorFxn('userWalletLogin'); - const walletMode = stringToWalletMode(loginType); - // Unity bridge uses 0|1 instead of booleans - const response = await specificWalletLogin(walletMode, !!preferBatchedMode); + const response = await specificWalletLogin(loginInfo); if (!response.success) { return response; } diff --git a/packages/paima-sdk/paima-mw-core/src/endpoints/internal.ts b/packages/paima-sdk/paima-mw-core/src/endpoints/internal.ts index 5ea3c98b7..9ca821eeb 100644 --- a/packages/paima-sdk/paima-mw-core/src/endpoints/internal.ts +++ b/packages/paima-sdk/paima-mw-core/src/endpoints/internal.ts @@ -3,7 +3,6 @@ import { buildEndpointErrorFxn, PaimaMiddlewareErrorCode } from '../errors'; import { getActiveAddress, getChainUri, - getGameName, getPostingInfo, setAutomaticMode, setBackendUri, @@ -15,17 +14,14 @@ import { setUnbatchedMode, } from '../state'; import type { PostingInfo, PostingModeSwitchResult, Result, Wallet } from '../types'; -import { specificWalletLogin, stringToWalletMode } from '../wallets/wallets'; +import { specificWalletLogin } from '../wallets/wallets'; import { emulatedBlocksActiveOnBackend } from '../helpers/auxiliary-queries'; import { TruffleConnector } from '@paima/providers'; import HDWalletProvider from '@truffle/hdwallet-provider'; +import type { LoginInfo } from '../wallets/wallet-modes'; -export async function userWalletLoginWithoutChecks( - loginType: string, - preferBatchedMode = false -): Promise> { - const walletMode = stringToWalletMode(loginType); - return await specificWalletLogin(walletMode, preferBatchedMode); +export async function userWalletLoginWithoutChecks(loginInfo: LoginInfo): Promise> { + return await specificWalletLogin(loginInfo); } export async function automaticWalletLogin(privateKey: string): Promise> { diff --git a/packages/paima-sdk/paima-mw-core/src/index.ts b/packages/paima-sdk/paima-mw-core/src/index.ts index 30ca590d6..4f2b77a0f 100644 --- a/packages/paima-sdk/paima-mw-core/src/index.ts +++ b/packages/paima-sdk/paima-mw-core/src/index.ts @@ -58,6 +58,7 @@ export type * from './errors'; // Only for use in game-specific middleware: export * from './types'; export type * from './types'; +export { WalletMode } from './wallets/wallet-modes'; export { paimaEndpoints, getBlockNumber, diff --git a/packages/paima-sdk/paima-mw-core/src/types.ts b/packages/paima-sdk/paima-mw-core/src/types.ts index d8ea3fe38..9d05a4145 100644 --- a/packages/paima-sdk/paima-mw-core/src/types.ts +++ b/packages/paima-sdk/paima-mw-core/src/types.ts @@ -1,4 +1,5 @@ import type { Hash, WalletAddress, UserSignature } from '@paima/utils'; +export type * from './wallets/wallet-modes'; export interface PostingInfo { address: WalletAddress; 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 e3f61277c..a95289b68 100644 --- a/packages/paima-sdk/paima-mw-core/src/wallets/algorand.ts +++ b/packages/paima-sdk/paima-mw-core/src/wallets/algorand.ts @@ -1,44 +1,34 @@ -import type { Result, Wallet } from '../types'; +import type { LoginInfoMap, Result, Wallet } from '../types'; import { PaimaMiddlewareErrorCode, buildEndpointErrorFxn } from '../errors'; import { AlgorandConnector } from '@paima/providers'; import { getGameName } from '../state'; -import { WalletMode } from './wallet-modes'; +import type { WalletMode } from './wallet-modes'; +import { connectWallet } from './wallet-modes'; -// TODO: the whole concept of converting wallet mode to names should be removed -function algorandWalletModeToName(walletMode: WalletMode): string { - switch (walletMode) { - case WalletMode.ALGORAND_PERA: - return 'pera'; - default: - return ''; - } -} - -export async function algorandLoginWrapper(walletMode: WalletMode): Promise> { +export async function algorandLoginWrapper( + loginInfo: LoginInfoMap[WalletMode.ALGORAND] +): Promise> { const errorFxn = buildEndpointErrorFxn('algorandLoginWrapper'); - const walletName = algorandWalletModeToName(walletMode); - try { - const provider = - walletMode === WalletMode.ALGORAND - ? await AlgorandConnector.instance().connectSimple({ - gameName: getGameName(), - gameChainId: undefined, // Not needed because of batcher - }) - : await AlgorandConnector.instance().connectNamed( - { - gameName: getGameName(), - gameChainId: undefined, // Not needed because of batcher - }, - walletName - ); - return { - success: true, - result: { - walletAddress: provider.getAddress(), - }, - }; - } catch (err) { - return errorFxn(PaimaMiddlewareErrorCode.ALGORAND_LOGIN, err); + const gameInfo = { + gameName: getGameName(), + gameChainId: undefined, // Not needed because of batcher + }; + const loginResult = await connectWallet( + 'algorandLoginWrapper', + errorFxn, + PaimaMiddlewareErrorCode.ALGORAND_LOGIN, + loginInfo, + AlgorandConnector.instance(), + gameInfo + ); + if (loginResult.success === false) { + return loginResult; } + return { + success: true, + result: { + walletAddress: loginResult.result.getAddress().toLocaleLowerCase(), + }, + }; } 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 50fd6238b..ada4e35df 100644 --- a/packages/paima-sdk/paima-mw-core/src/wallets/cardano.ts +++ b/packages/paima-sdk/paima-mw-core/src/wallets/cardano.ts @@ -1,11 +1,8 @@ -import type { OldResult, Result, Wallet } from '../types'; -import { - buildEndpointErrorFxn, - PaimaMiddlewareErrorCode, - FE_ERR_SPECIFIC_WALLET_NOT_INSTALLED, -} from '../errors'; -import { WalletMode } from './wallet-modes'; -import { CardanoConnector, UnsupportedWallet, WalletNotFound } from '@paima/providers'; +import type { LoginInfoMap, OldResult, Result, Wallet } from '../types'; +import { buildEndpointErrorFxn, PaimaMiddlewareErrorCode } from '../errors'; +import type { WalletMode } from './wallet-modes'; +import { connectWallet } from './wallet-modes'; +import { CardanoConnector } from '@paima/providers'; import { getGameName } from '../state'; export async function checkCardanoWalletStatus(): Promise { @@ -21,63 +18,30 @@ export async function checkCardanoWalletStatus(): Promise { return { success: true, message: '' }; } -function cardanoWalletModeToName(walletMode: WalletMode): string { - switch (walletMode) { - case WalletMode.CARDANO_FLINT: - return 'flint'; - case WalletMode.CARDANO_NUFI: - return 'nufi'; - case WalletMode.CARDANO_NAMI: - return 'nami'; - case WalletMode.CARDANO_ETERNL: - return 'eternl'; - default: - return ''; - } -} - -export async function cardanoLoginWrapper(walletMode: WalletMode): Promise> { +export async function cardanoLoginWrapper( + loginInfo: LoginInfoMap[WalletMode.CARDANO] +): Promise> { const errorFxn = buildEndpointErrorFxn('cardanoLoginWrapper'); - console.log('[cardanoLoginWrapper] window.cardano:', (window as any).cardano); - - let specificWalletName: string | undefined = undefined; - if (walletMode !== WalletMode.CARDANO) { - console.log(`[cardanoLoginWrapper] Attempting to log into ${specificWalletName}`); - specificWalletName = cardanoWalletModeToName(walletMode); - if (!specificWalletName) { - return errorFxn(PaimaMiddlewareErrorCode.CARDANO_WALLET_NOT_INSTALLED); - } - } else { - console.log(`[cardanoLoginWrapper] Attempting to log into any Cardano wallet`); - } - try { - const gameInfo = { - gameName: getGameName(), - gameChainId: undefined, - }; - const provider = - specificWalletName == null - ? await CardanoConnector.instance().connectSimple(gameInfo) - : await CardanoConnector.instance().connectNamed(gameInfo, specificWalletName); - return { - success: true, - result: { - walletAddress: provider.getAddress().toLocaleLowerCase(), - }, - }; - } catch (err) { - if (err instanceof WalletNotFound || err instanceof UnsupportedWallet) { - return errorFxn( - PaimaMiddlewareErrorCode.CARDANO_WALLET_NOT_INSTALLED, - undefined, - FE_ERR_SPECIFIC_WALLET_NOT_INSTALLED - ); - } - console.log( - `[cardanoLoginWrapper] Error while logging into wallet ${specificWalletName ?? 'Cardano'}` - ); - return errorFxn(PaimaMiddlewareErrorCode.CARDANO_LOGIN, err); - // TODO: improve error differentiation + const gameInfo = { + gameName: getGameName(), + gameChainId: undefined, // Not needed because of batcher + }; + const loginResult = await connectWallet( + 'cardanoLoginWrapper', + errorFxn, + PaimaMiddlewareErrorCode.CARDANO_LOGIN, + loginInfo, + CardanoConnector.instance(), + gameInfo + ); + if (loginResult.success === false) { + return loginResult; } + return { + success: true, + result: { + walletAddress: loginResult.result.getAddress().toLocaleLowerCase(), + }, + }; } diff --git a/packages/paima-sdk/paima-mw-core/src/wallets/evm.ts b/packages/paima-sdk/paima-mw-core/src/wallets/evm.ts index 0f2ff024a..0a1074003 100644 --- a/packages/paima-sdk/paima-mw-core/src/wallets/evm.ts +++ b/packages/paima-sdk/paima-mw-core/src/wallets/evm.ts @@ -1,8 +1,4 @@ -import { - buildEndpointErrorFxn, - PaimaMiddlewareErrorCode, - FE_ERR_SPECIFIC_WALLET_NOT_INSTALLED, -} from '../errors'; +import { buildEndpointErrorFxn, PaimaMiddlewareErrorCode } from '../errors'; import { getChainCurrencyDecimals, getChainCurrencyName, @@ -13,11 +9,12 @@ import { getChainUri, getGameName, } from '../state'; -import type { OldResult, Result, Wallet } from '../types'; +import type { LoginInfoMap, OldResult, Result, Wallet } from '../types'; import { updateFee } from '../helpers/posting'; -import { WalletMode } from './wallet-modes'; -import { EvmConnector, UnsupportedWallet, WalletNotFound } from '@paima/providers'; +import { connectWallet } from './wallet-modes'; +import type { WalletMode } from './wallet-modes'; +import { EvmConnector } from '@paima/providers'; interface SwitchError { code: number; @@ -86,51 +83,25 @@ export async function checkEthWalletStatus(): Promise { return { success: true, message: '' }; } -function evmWalletModeToName(walletMode: WalletMode): string { - switch (walletMode) { - case WalletMode.METAMASK: - return 'metamask'; - case WalletMode.EVM_FLINT: - return 'flint'; - default: - return ''; - } -} - -export async function evmLoginWrapper(walletMode: WalletMode): Promise> { +export async function evmLoginWrapper( + loginInfo: LoginInfoMap[WalletMode.EVM] +): Promise> { const errorFxn = buildEndpointErrorFxn('evmLoginWrapper'); - const walletName = evmWalletModeToName(walletMode); - try { - if (walletMode === WalletMode.EVM) { - await EvmConnector.instance().connectSimple({ - gameName: getGameName(), - gameChainId: '0x' + getChainId().toString(16), - }); - } else { - if (!walletName) { - return errorFxn(PaimaMiddlewareErrorCode.WALLET_NOT_SUPPORTED); - } - console.log(`[evmLoginWrapper] Attempting to log into ${walletName}`); - await EvmConnector.instance().connectNamed( - { - gameName: getGameName(), - gameChainId: '0x' + getChainId().toString(16), - }, - walletName - ); - } - } catch (err) { - if (err instanceof WalletNotFound || err instanceof UnsupportedWallet) { - return errorFxn( - PaimaMiddlewareErrorCode.CARDANO_WALLET_NOT_INSTALLED, - undefined, - FE_ERR_SPECIFIC_WALLET_NOT_INSTALLED - ); - } - console.log(`[evmLoginWrapper] Error while logging into wallet ${walletName ?? 'simple'}`); - - return errorFxn(PaimaMiddlewareErrorCode.EVM_LOGIN, err); + const gameInfo = { + gameName: getGameName(), + gameChainId: '0x' + getChainId().toString(16), + }; + const loginResult = await connectWallet( + 'evmLoginWrapper', + errorFxn, + PaimaMiddlewareErrorCode.EVM_LOGIN, + loginInfo, + EvmConnector.instance(), + gameInfo + ); + if (loginResult.success === false) { + return loginResult; } try { 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 a58769b7b..510cd8d55 100644 --- a/packages/paima-sdk/paima-mw-core/src/wallets/polkadot.ts +++ b/packages/paima-sdk/paima-mw-core/src/wallets/polkadot.ts @@ -1,55 +1,34 @@ -import { - buildEndpointErrorFxn, - PaimaMiddlewareErrorCode, - FE_ERR_SPECIFIC_WALLET_NOT_INSTALLED, -} from '../errors'; +import { buildEndpointErrorFxn, PaimaMiddlewareErrorCode } from '../errors'; import { getGameName } from '../state'; -import type { Result, Wallet } from '../types'; -import { UnsupportedWallet, WalletNotFound } from '@paima/providers'; +import type { LoginInfoMap, Result, Wallet } from '../types'; import { PolkadotConnector } from '@paima/providers'; -import { WalletMode } from './wallet-modes'; +import type { WalletMode } from './wallet-modes'; +import { connectWallet } from './wallet-modes'; -// TODO: the whole concept of converting wallet mode to names should be removed -function polkadotWalletModeToName(walletMode: WalletMode): string { - switch (walletMode) { - default: - return ''; - } -} - -export async function polkadotLoginWrapper(walletMode: WalletMode): Promise> { +export async function polkadotLoginWrapper( + loginInfo: LoginInfoMap[WalletMode.POLKADOT] +): Promise> { const errorFxn = buildEndpointErrorFxn('polkadotLoginWrapper'); - const walletName = polkadotWalletModeToName(walletMode); - try { - const provider = - walletMode === WalletMode.POLKADOT - ? await PolkadotConnector.instance().connectSimple({ - gameName: getGameName(), - gameChainId: undefined, // Not needed because of batcher - }) - : await PolkadotConnector.instance().connectNamed( - { - gameName: getGameName(), - gameChainId: undefined, // Not needed because of batcher - }, - walletName - ); - return { - success: true, - result: { - walletAddress: provider.getAddress(), - }, - }; - } catch (err) { - if (err instanceof WalletNotFound || err instanceof UnsupportedWallet) { - return errorFxn( - PaimaMiddlewareErrorCode.POLKADOT_WALLET_NOT_INSTALLED, - undefined, - FE_ERR_SPECIFIC_WALLET_NOT_INSTALLED - ); - } - console.log(`[polkadotLoginWrapper] Error while logging into wallet`); - return errorFxn(PaimaMiddlewareErrorCode.POLKADOT_LOGIN, err); + const gameInfo = { + gameName: getGameName(), + gameChainId: undefined, // Not needed because of batcher + }; + const loginResult = await connectWallet( + 'polkadotLoginWrapper', + errorFxn, + PaimaMiddlewareErrorCode.POLKADOT_LOGIN, + loginInfo, + PolkadotConnector.instance(), + gameInfo + ); + if (loginResult.success === false) { + return loginResult; } + return { + success: true, + result: { + walletAddress: loginResult.result.getAddress().toLocaleLowerCase(), + }, + }; } 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 6886fd661..01bfcadf3 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 @@ -1,14 +1,103 @@ -export enum WalletMode { +import type { + ActiveConnection, + EvmApi, + CardanoApi, + AlgorandApi, + PolkadotApi, + IConnector, + IProvider, + GameInfo, +} from '@paima/providers'; +import { WalletNotFound, UnsupportedWallet } 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 { NO_WALLET, EVM, - METAMASK, - EVM_FLINT, CARDANO, - CARDANO_FLINT, - CARDANO_NUFI, - CARDANO_NAMI, - CARDANO_ETERNL, POLKADOT, ALGORAND, - ALGORAND_PERA, +} + +export type Preference = + | { + name: string; + } + | { + connection: ActiveConnection; + }; + +export type BaseLoginInfo = { + preference?: Preference; +}; +export type LoginInfoMap = { + [WalletMode.EVM]: BaseLoginInfo & { preferBatchedMode: boolean }; + [WalletMode.CARDANO]: BaseLoginInfo; + [WalletMode.POLKADOT]: BaseLoginInfo; + [WalletMode.ALGORAND]: BaseLoginInfo; + [WalletMode.NO_WALLET]: void; +}; + +type ToUnion = { + [K in keyof T]: { mode: K } & T[K]; +}[keyof T]; + +export type LoginInfo = ToUnion; + +function getWalletName(info: BaseLoginInfo): undefined | string { + if (info.preference == null) return undefined; + if ('name' in info.preference) { + return info.preference.name; + } + return info.preference.connection.metadata.name; +} +export async function connectWallet( + typeName: string, + errorFxn: EndpointErrorFxn, + errorCode: PaimaMiddlewareErrorCode, + loginInfo: BaseLoginInfo, + connector: IConnector, + 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); + } + } catch (err) { + if (err instanceof WalletNotFound || err instanceof UnsupportedWallet) { + return errorFxn(errorCode, undefined, FE_ERR_SPECIFIC_WALLET_NOT_INSTALLED); + } + console.log( + `${typeName} Error while logging into wallet ${getWalletName(loginInfo) ?? 'simple'}` + ); + + return errorFxn(errorCode, err); + } } diff --git a/packages/paima-sdk/paima-mw-core/src/wallets/wallets.ts b/packages/paima-sdk/paima-mw-core/src/wallets/wallets.ts index b81c782ed..1fb5989e7 100644 --- a/packages/paima-sdk/paima-mw-core/src/wallets/wallets.ts +++ b/packages/paima-sdk/paima-mw-core/src/wallets/wallets.ts @@ -7,78 +7,37 @@ import { setBatchedPolkadotMode, setUnbatchedMode, } from '../state'; -import type { Result, Wallet } from '../types'; +import type { LoginInfo, Result, Wallet } from '../types'; import { algorandLoginWrapper } from './algorand'; import { cardanoLoginWrapper } from './cardano'; import { evmLoginWrapper } from './evm'; import { polkadotLoginWrapper } from './polkadot'; import { WalletMode } from './wallet-modes'; -export function stringToWalletMode(loginType: string): WalletMode { - // TODO: this function has a bunch of magic strings in it which is not great - // some of these are also repeated in other places (ex: evmWalletModeToName) - switch (loginType) { - case 'evm': - return WalletMode.EVM; - case 'metamask': - return WalletMode.METAMASK; - case 'evm-flint': - return WalletMode.EVM_FLINT; - case 'cardano': - return WalletMode.CARDANO; - case 'flint': - return WalletMode.CARDANO_FLINT; - case 'nufi': - return WalletMode.CARDANO_NUFI; - case 'nami': - return WalletMode.CARDANO_NAMI; - case 'eternl': - return WalletMode.CARDANO_ETERNL; - case 'polkadot': - return WalletMode.POLKADOT; - case 'pera': - return WalletMode.ALGORAND_PERA; - case 'algorand': - return WalletMode.ALGORAND; - default: - return WalletMode.NO_WALLET; - } -} - -export async function specificWalletLogin( - walletMode: WalletMode, - preferBatchedMode: boolean -): Promise> { +export async function specificWalletLogin(loginInfo: LoginInfo): Promise> { const errorFxn = buildEndpointErrorFxn('specificWalletLogin'); - switch (walletMode) { + switch (loginInfo.mode) { case WalletMode.EVM: - case WalletMode.METAMASK: - case WalletMode.EVM_FLINT: - if (preferBatchedMode) { + if (loginInfo.preferBatchedMode) { setBatchedEthMode(); } else { setUnbatchedMode(); } - return await evmLoginWrapper(walletMode); + return await evmLoginWrapper(loginInfo); case WalletMode.CARDANO: - case WalletMode.CARDANO_FLINT: - case WalletMode.CARDANO_NUFI: - case WalletMode.CARDANO_NAMI: - case WalletMode.CARDANO_ETERNL: setBatchedCardanoMode(); - return await cardanoLoginWrapper(walletMode); + return await cardanoLoginWrapper(loginInfo); case WalletMode.POLKADOT: setBatchedPolkadotMode(); - return await polkadotLoginWrapper(walletMode); + return await polkadotLoginWrapper(loginInfo); case WalletMode.ALGORAND: - case WalletMode.ALGORAND_PERA: setBatchedAlgorandMode(); - return await algorandLoginWrapper(walletMode); + return await algorandLoginWrapper(loginInfo); case WalletMode.NO_WALLET: return errorFxn(FE_ERR_SPECIFIC_WALLET_NOT_INSTALLED); default: - assertNever(walletMode, true); + assertNever(loginInfo, true); return errorFxn(FE_ERR_SPECIFIC_WALLET_NOT_INSTALLED); } }