|
| 1 | +import { |
| 2 | + setupWalletSelector, |
| 3 | + VerifiedOwner, |
| 4 | + VerifyOwnerParams, |
| 5 | + Wallet, |
| 6 | +} from '@near-wallet-selector/core'; |
| 7 | +import { setupModal } from '@near-wallet-selector/modal-ui'; |
| 8 | +import { map, distinctUntilChanged, Subscription } from 'rxjs'; |
| 9 | + |
| 10 | +import { |
| 11 | + WALLET_CONNECTION_POLL_INTERVAL, |
| 12 | + WALLET_CONNECTION_TIMEOUT, |
| 13 | +} from './constants'; |
| 14 | + |
| 15 | +import type { |
| 16 | + WalletSelector, |
| 17 | + AccountState, |
| 18 | + WalletModuleFactory, |
| 19 | +} from '@near-wallet-selector/core'; |
| 20 | +import type { WalletSelectorModal } from '@near-wallet-selector/modal-ui'; |
| 21 | +import { SUPPORTED_NEAR_WALLETS } from './wallets.setup'; |
| 22 | +import { ERROR_MESSAGES } from './errorMessages'; |
| 23 | +import { mbjs } from '@mintbase-js/sdk'; |
| 24 | +import { setupBitteWallet } from '@mintbase-js/wallet'; |
| 25 | +import { ConnectionTimeoutError } from './wallet'; |
| 26 | + |
| 27 | +// mintbase SDK wallet functionality wraps |
| 28 | +// Near Wallet Selector lib, provided by NEAR Protocol |
| 29 | +// https://github.com/near/wallet-selector/ |
| 30 | + |
| 31 | +export type WalletSelectorComponents = { |
| 32 | + selector: WalletSelector; |
| 33 | + modal: WalletSelectorModal; |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +const walletUrls = { |
| 38 | + testnet: 'https://testnet.wallet.bitte.ai/', |
| 39 | + mainnet: 'https://wallet.bitte.ai', |
| 40 | +}; |
| 41 | + |
| 42 | +export const BitteWalletAuth = { |
| 43 | + walletSelectorComponents: { |
| 44 | + selector: null, |
| 45 | + modal: null, |
| 46 | + }, |
| 47 | + setupBitteWalletSelector: async ( |
| 48 | + callbackUrl, |
| 49 | + onlyMbWallet = false, |
| 50 | + network?, |
| 51 | + contractAddress?, |
| 52 | + options?: { additionalWallets?: Array<WalletModuleFactory> }, |
| 53 | + ): Promise<WalletSelectorComponents> => { |
| 54 | + |
| 55 | + |
| 56 | + if (onlyMbWallet === false) { |
| 57 | + |
| 58 | + BitteWalletAuth.walletSelectorComponents.selector = await setupWalletSelector({ |
| 59 | + network: network, |
| 60 | + debug: mbjs.keys.debugMode, |
| 61 | + modules: [ |
| 62 | + setupBitteWallet({ |
| 63 | + walletUrl: walletUrls[network], |
| 64 | + deprecated: false, |
| 65 | + callbackUrl: callbackUrl, |
| 66 | + }), |
| 67 | + ...(options?.additionalWallets || []), |
| 68 | + ...SUPPORTED_NEAR_WALLETS, |
| 69 | + ], |
| 70 | + }); |
| 71 | + } else { |
| 72 | + BitteWalletAuth.walletSelectorComponents.selector = await setupWalletSelector({ |
| 73 | + network: network, |
| 74 | + debug: mbjs.keys.debugMode, |
| 75 | + modules: [ |
| 76 | + setupBitteWallet({ |
| 77 | + walletUrl: walletUrls[network], |
| 78 | + deprecated: false, |
| 79 | + callbackUrl: callbackUrl, |
| 80 | + }), |
| 81 | + ...(options?.additionalWallets || []), |
| 82 | + ], |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + BitteWalletAuth.walletSelectorComponents.modal = setupModal( BitteWalletAuth.walletSelectorComponents.selector, { |
| 87 | + contractId: contractAddress, |
| 88 | + }); |
| 89 | + |
| 90 | + return BitteWalletAuth.walletSelectorComponents; |
| 91 | + }, |
| 92 | + setupWalletSelectorComponents: async ( |
| 93 | + network?, |
| 94 | + contractAddress?, |
| 95 | + options?: { additionalWallets?: Array<WalletModuleFactory> }, |
| 96 | + ): Promise<WalletSelectorComponents> => { |
| 97 | + const selector = await setupWalletSelector({ |
| 98 | + network: network, |
| 99 | + debug: mbjs.keys.debugMode, |
| 100 | + modules: [ |
| 101 | + ...SUPPORTED_NEAR_WALLETS, |
| 102 | + ...(options?.additionalWallets || []), |
| 103 | + ], |
| 104 | + }); |
| 105 | + |
| 106 | + const modal = setupModal(selector, { |
| 107 | + contractId: contractAddress, |
| 108 | + }); |
| 109 | + |
| 110 | + BitteWalletAuth.walletSelectorComponents = { |
| 111 | + selector, |
| 112 | + modal, |
| 113 | + }; |
| 114 | + return BitteWalletAuth.walletSelectorComponents; |
| 115 | + }, |
| 116 | + SetupNotCalledError: class extends Error { |
| 117 | + constructor(message?: string) { |
| 118 | + super(message); |
| 119 | + this.name = 'SetupNotCalledError'; |
| 120 | + } |
| 121 | + }, |
| 122 | + ConnectionTimeoutError: class extends Error { |
| 123 | + message: string |
| 124 | + }, |
| 125 | + validateWalletComponentsAreSetup:(): void => { |
| 126 | + if (!BitteWalletAuth.walletSelectorComponents.selector) { |
| 127 | + throw new BitteWalletAuth.SetupNotCalledError(ERROR_MESSAGES.WALLET_SETUP_NOT_CALLED_ERROR); |
| 128 | + } |
| 129 | + }, |
| 130 | + registerWalletAccountsSubscriber: ( |
| 131 | + callback: (accounts: AccountState[]) => void, |
| 132 | + ): Subscription => { |
| 133 | + BitteWalletAuth.validateWalletComponentsAreSetup(); |
| 134 | + |
| 135 | + return BitteWalletAuth.walletSelectorComponents.selector.store.observable |
| 136 | + .pipe( |
| 137 | + map((state:any) => state.accounts), |
| 138 | + distinctUntilChanged(), |
| 139 | + ) |
| 140 | + .subscribe(callback); |
| 141 | + }, |
| 142 | + timerReference: null, |
| 143 | + pollForWalletConnection: async (): Promise<AccountState[]> => { |
| 144 | + BitteWalletAuth.validateWalletComponentsAreSetup(); |
| 145 | + // clear any existing timer |
| 146 | + clearTimeout(BitteWalletAuth.timerReference); |
| 147 | + |
| 148 | + const tryToResolveAccountsFromState = ( |
| 149 | + resolve: (value: AccountState[]) => void, |
| 150 | + reject: (err: ConnectionTimeoutError) => void, |
| 151 | + elapsed = 0, |
| 152 | + ): void => { |
| 153 | + const { accounts } = |
| 154 | + BitteWalletAuth.walletSelectorComponents.selector.store.getState() || {}; |
| 155 | + |
| 156 | + // accounts present in state |
| 157 | + if (accounts) { |
| 158 | + resolve(accounts); |
| 159 | + } |
| 160 | + |
| 161 | + // timed out |
| 162 | + if (elapsed > WALLET_CONNECTION_TIMEOUT) { |
| 163 | + reject( |
| 164 | + new ConnectionTimeoutError(ERROR_MESSAGES.WALLET_CONNECTION_NOT_FOUND), |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | + // try again |
| 169 | + clearTimeout(BitteWalletAuth.timerReference); |
| 170 | + BitteWalletAuth.timerReference = setTimeout( |
| 171 | + () => |
| 172 | + tryToResolveAccountsFromState( |
| 173 | + resolve, |
| 174 | + reject, |
| 175 | + elapsed + WALLET_CONNECTION_POLL_INTERVAL, |
| 176 | + ), |
| 177 | + WALLET_CONNECTION_POLL_INTERVAL, |
| 178 | + ); |
| 179 | + }; |
| 180 | + |
| 181 | + return new Promise((resolve, reject) => |
| 182 | + tryToResolveAccountsFromState(resolve, reject), |
| 183 | + ); |
| 184 | + }, |
| 185 | + getWallet: async (): Promise<Wallet> => { |
| 186 | + BitteWalletAuth.validateWalletComponentsAreSetup(); |
| 187 | + |
| 188 | + return await BitteWalletAuth.walletSelectorComponents.selector.wallet(); |
| 189 | + }, |
| 190 | + connectWalletSelector:(): void => { |
| 191 | + BitteWalletAuth.validateWalletComponentsAreSetup(); |
| 192 | + |
| 193 | + BitteWalletAuth.walletSelectorComponents.modal.show(); |
| 194 | + }, |
| 195 | + disconnectFromWalletSelector: async (): Promise<void> => { |
| 196 | + BitteWalletAuth.validateWalletComponentsAreSetup(); |
| 197 | + |
| 198 | + const wallet = await BitteWalletAuth.walletSelectorComponents.selector.wallet(); |
| 199 | + wallet.signOut(); |
| 200 | + }, |
| 201 | + getVerifiedOwner: async ( |
| 202 | + params: VerifyOwnerParams, |
| 203 | + ): Promise<VerifiedOwner | undefined> => { |
| 204 | + BitteWalletAuth.validateWalletComponentsAreSetup(); |
| 205 | + |
| 206 | + const { message, callbackUrl, meta } = params; |
| 207 | + |
| 208 | + const wallet = await BitteWalletAuth.walletSelectorComponents.selector.wallet(); |
| 209 | + |
| 210 | + const owner = (await wallet.verifyOwner({ |
| 211 | + message: message, |
| 212 | + callbackUrl: callbackUrl, |
| 213 | + meta: meta, |
| 214 | + })) as VerifiedOwner; |
| 215 | + |
| 216 | + return owner; |
| 217 | + }, |
| 218 | + signMessage: async ( |
| 219 | + params: VerifyOwnerParams, |
| 220 | + ): Promise<VerifiedOwner> => { |
| 221 | + const owner = await BitteWalletAuth.getVerifiedOwner(params); |
| 222 | + |
| 223 | + return owner; |
| 224 | + }, |
| 225 | +}; |
0 commit comments