From dcc9fc7bbd799c52a5d0130b0af55b832874a9f9 Mon Sep 17 00:00:00 2001 From: Ruben Marcus Date: Mon, 29 Jul 2024 12:45:01 +0100 Subject: [PATCH 1/8] add bitte wallet support --- packages/react/src/wallet/wallet.ts | 43 +++++++++++++++++------------ 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/packages/react/src/wallet/wallet.ts b/packages/react/src/wallet/wallet.ts index 84f30bc3..af035f80 100644 --- a/packages/react/src/wallet/wallet.ts +++ b/packages/react/src/wallet/wallet.ts @@ -5,24 +5,25 @@ import { Wallet, } from '@near-wallet-selector/core'; import { setupModal } from '@near-wallet-selector/modal-ui'; -import { map, distinctUntilChanged, Subscription } from 'rxjs'; +import { distinctUntilChanged, map, Subscription } from 'rxjs'; import { WALLET_CONNECTION_POLL_INTERVAL, WALLET_CONNECTION_TIMEOUT, } from './constants'; -import { setupMeteorWallet } from '@near-wallet-selector/meteor-wallet'; import { setupHereWallet } from '@near-wallet-selector/here-wallet'; +import { setupMeteorWallet } from '@near-wallet-selector/meteor-wallet'; import { setupMyNearWallet } from '@near-wallet-selector/my-near-wallet'; + +import { setupBitteWallet, setupMintbaseWallet } from '@mintbase-js/wallet'; import type { - WalletSelector, AccountState, WalletModuleFactory, + WalletSelector, } from '@near-wallet-selector/core'; import type { WalletSelectorModal } from '@near-wallet-selector/modal-ui'; -import { setupMintbaseWallet } from '@mintbase-js/wallet'; // error messages const SUPPORT = '- further help available on our telegram channel: https://t.me/mintdev'; @@ -76,17 +77,24 @@ export const setupMintbaseWalletSelector = async ( ): Promise => { + const baseModuleWallet = { + walletUrl: walletUrls[network], + callbackUrl: callbackUrl, + contractId: contractAddress, + }; + if (onlyMbWallet === false) { + const moduleWallet = { + ...baseModuleWallet, + successUrl: successUrl || window.location.href, + failureUrl: successUrl || window.location.href, + }; + walletSelectorComponents.selector = await setupWalletSelector({ network: network, modules: [ - setupMintbaseWallet({ - walletUrl: walletUrls[network], - callbackUrl: callbackUrl, - successUrl: successUrl || window.location.href, - failureUrl: successUrl || window.location.href, - contractId: contractAddress, - }), + setupBitteWallet(moduleWallet), + setupMintbaseWallet(moduleWallet), ...(options?.additionalWallets || []), ...SUPPORTED_NEAR_WALLETS, ], @@ -95,11 +103,12 @@ export const setupMintbaseWalletSelector = async ( walletSelectorComponents.selector = await setupWalletSelector({ network: network, modules: [ - setupMintbaseWallet({ - walletUrl: walletUrls[network], - callbackUrl: callbackUrl, - contractId: contractAddress, - }), + setupBitteWallet( + baseModuleWallet, + ), + setupMintbaseWallet( + baseModuleWallet, + ), ...(options?.additionalWallets || []), ], }); @@ -273,4 +282,4 @@ export const signMessage = async ( // // const verified = createdPublicKey.verify(new Uint8Array(sha256.array(stringified)), Buffer.from(signature, 'base64')); // return false; -// }; +// }; \ No newline at end of file From 709b014a6018e02f61ddb587722ba512f21f570c Mon Sep 17 00:00:00 2001 From: Ruben Marcus Date: Mon, 29 Jul 2024 13:09:07 +0100 Subject: [PATCH 2/8] fix configs --- packages/wallet/src/bitte-wallet.ts | 18 ++++---- packages/wallet/src/utils.ts | 65 ++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 10 deletions(-) diff --git a/packages/wallet/src/bitte-wallet.ts b/packages/wallet/src/bitte-wallet.ts index a9b104b5..c0bc7ef3 100644 --- a/packages/wallet/src/bitte-wallet.ts +++ b/packages/wallet/src/bitte-wallet.ts @@ -6,7 +6,7 @@ import type { FinalExecutionOutcome, WalletBehaviourFactory, } from '@near-wallet-selector/core'; -import { getCallbackUrl } from './utils'; +import { getCallbackUrlBitte } from './utils'; import { createAction } from '@near-wallet-selector/wallet-utils'; @@ -76,7 +76,7 @@ export const BitteWallet: WalletBehaviourFactory< //make near-api-js not throw without lak if (acc && !contractId) { localStorage.setItem( - 'mintbase-wallet_wallet_auth_key', + 'bitte-wallet:wallet_auth_key', JSON.stringify({ accountId: acc as string, allKeys: [], @@ -85,8 +85,8 @@ export const BitteWallet: WalletBehaviourFactory< } const nearConnection = await connect(connectionConfig); - const wallet = new WalletConnection(nearConnection, 'mintbase-wallet'); - localStorage.setItem('mintbase-wallet:callback_url', callback); + const wallet = new WalletConnection(nearConnection, 'bitte-wallet'); + localStorage.setItem('bitte-wallet:callback_url', callback); return { wallet, @@ -123,7 +123,7 @@ export const BitteWallet: WalletBehaviourFactory< }; const signOut = async (): Promise => { - window.localStorage.removeItem('mintbase-wallet:account-data'); + window.localStorage.removeItem('bitte-wallet:account-data'); if (state.wallet.isSignedIn()) { state.wallet.signOut(); @@ -153,7 +153,7 @@ export const BitteWallet: WalletBehaviourFactory< // callbackUrl: cbUrl, // }); - const { cbUrl } = getCallbackUrl(callbackUrl ?? ''); + const { cbUrl } = getCallbackUrlBitte(callbackUrl ?? ''); for (const { signerId } of transactions) { assertValidSigner(signerId); @@ -213,7 +213,7 @@ export const BitteWallet: WalletBehaviourFactory< }; const signMessage = async ({ message, nonce, recipient, callbackUrl }): Promise => { - const { cbUrl } = getCallbackUrl(callbackUrl ?? ''); + const { cbUrl } = getCallbackUrlBitte(callbackUrl ?? ''); const newUrl = new URL(`${metadata.walletUrl}/sign-message`); newUrl.searchParams.set('message', message); @@ -260,7 +260,7 @@ export const BitteWallet: WalletBehaviourFactory< } const currentAccount: string = window.localStorage.getItem( - 'mintbase-wallet:account-creation-data', + 'bitte-wallet:account-creation-data', )!; return [ @@ -280,7 +280,7 @@ export const BitteWallet: WalletBehaviourFactory< const setActiveAccountId = (accountId: string): null => { activeAccountId = accountId; - window.localStorage.setItem('mintbase-wallet:activeAccountId', accountId); + window.localStorage.setItem('bitte-wallet:activeAccountId', accountId); return null; }; diff --git a/packages/wallet/src/utils.ts b/packages/wallet/src/utils.ts index 160ffb90..8fc2a11b 100644 --- a/packages/wallet/src/utils.ts +++ b/packages/wallet/src/utils.ts @@ -3,6 +3,45 @@ */ +const checkCallbackUrlBitte = (callbackUrl: string): string => { + function isValidURL(url): boolean { + const urlPattern = /^(https?|ftp|http?):\/\/[^\s/$.?#].[^\s]*$/; + return urlPattern.test(url); + } + + if (callbackUrl !== null && callbackUrl.length > 0) { + if (isValidURL(callbackUrl)) { + if ( + callbackUrl.startsWith('https://') || + callbackUrl.startsWith('http://') + ) { + return callbackUrl.endsWith('/') + ? callbackUrl.slice(0, -1) + : callbackUrl; + } + } else { + console.error( + 'callbackUrl set with wrong format. please use an URL with http:// or https:// instead.Further help available on our telegram channel: https://t.me/mintdev', + ); + return new URL(window.location.href).toString(); + } + } else { + const globalCallBackUrl = localStorage.getItem( + 'bitte-wallet:callback_url', + ); + + if (isValidURL(globalCallBackUrl)) { + return globalCallBackUrl; + } else { + console.warn('We recommend you to set the callbackUrl property on setupMintbaseWallet \n read more here: \n https://docs.mintbase.xyz/dev/mintbase-sdk-ref/wallet#setupmintbasewallet \n \n further help available on our telegram channel: \n https://t.me/mintdev'); + return new URL(window.location.href).toString(); + } + } + + return new URL(window.location.href).toString(); +}; + + const checkCallbackUrl = (callbackUrl: string): string => { function isValidURL(url): boolean { const urlPattern = /^(https?|ftp|http?):\/\/[^\s/$.?#].[^\s]*$/; @@ -65,6 +104,30 @@ const getCallbackUrl = (callbackUrl?: string): { cbUrl: string } | null => { return null; }; +const getCallbackUrlBitte = (callbackUrl?: string): { cbUrl: string } | null => { + if (typeof window !== undefined) { + + let mbjsCallbackUrl = ''; + + if ( window?.['mbjs']?.keys?.callbackUrl && window?.['mbjs']?.keys?.callbackUrl.length > 0 ) { + mbjsCallbackUrl = window?.['mbjs']?.keys?.callbackUrl; + } + + const globalCBUrl = localStorage?.getItem('bitte-wallet:callback_url') || mbjsCallbackUrl; + + // it gets always the passed callBackUrl first, if not check for the one on the Wallet Setup or mbjs.keys.callbackUrl + const finalcbURL = callbackUrl ?? globalCBUrl; + + const callBackUrlRes = checkCallbackUrlBitte(finalcbURL); + + // check if callBackUrl sent on the method is valid + // method callbackUrl will always have priority over global callbackUrl , user can set different callbackUrls according to the method.. + + return { cbUrl: callBackUrlRes }; + } + return null; +}; + const resolveWalletUrl = (network: string, walletUrl?: string): string => { if (walletUrl) { return walletUrl; @@ -95,5 +158,5 @@ const resolveBitteWallet = (network: string, walletUrl?: string): string => { } }; -export { checkCallbackUrl, getCallbackUrl, resolveBitteWallet, resolveWalletUrl }; +export { checkCallbackUrl,checkCallbackUrlBitte, getCallbackUrl,getCallbackUrlBitte, resolveBitteWallet, resolveWalletUrl }; From b51c90293cd2f0665176465cfeb5f828cfa63176 Mon Sep 17 00:00:00 2001 From: Ruben Marcus Date: Mon, 29 Jul 2024 13:22:19 +0100 Subject: [PATCH 3/8] fix configs --- packages/wallet/src/bitte-wallet.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/wallet/src/bitte-wallet.ts b/packages/wallet/src/bitte-wallet.ts index c0bc7ef3..3b3c17c1 100644 --- a/packages/wallet/src/bitte-wallet.ts +++ b/packages/wallet/src/bitte-wallet.ts @@ -186,7 +186,7 @@ export const BitteWallet: WalletBehaviourFactory< throw new Error('No receiver found to send the transaction to'); } - const { cbUrl } = getCallbackUrl(callbackUrl ?? ''); + const { cbUrl } = getCallbackUrlBitte(callbackUrl ?? ''); const callback = cbUrl || successUrl; From 593ad6ae6b79721891b590269a8ac8169e1d3785 Mon Sep 17 00:00:00 2001 From: Ruben Marcus Date: Mon, 29 Jul 2024 13:33:17 +0100 Subject: [PATCH 4/8] fix wallet setups --- packages/react/src/wallet/wallet.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/react/src/wallet/wallet.ts b/packages/react/src/wallet/wallet.ts index af035f80..b6357c74 100644 --- a/packages/react/src/wallet/wallet.ts +++ b/packages/react/src/wallet/wallet.ts @@ -65,6 +65,11 @@ const walletUrls = { mainnet: 'https://wallet.mintbase.xyz', }; +const BitteWalletUrls = { + testnet: 'https://testnet.wallet.bitte.ai/', + mainnet: 'https://wallet.bitte.ai/', +}; + // eslint-disable-next-line max-len export const setupMintbaseWalletSelector = async ( callbackUrl, @@ -77,7 +82,13 @@ export const setupMintbaseWalletSelector = async ( ): Promise => { - const baseModuleWallet = { + const BitteModuleWallet = { + walletUrl: walletUrls[network], + callbackUrl: callbackUrl, + contractId: contractAddress, + }; + + const MbModuleWallet = { walletUrl: walletUrls[network], callbackUrl: callbackUrl, contractId: contractAddress, @@ -85,7 +96,7 @@ export const setupMintbaseWalletSelector = async ( if (onlyMbWallet === false) { const moduleWallet = { - ...baseModuleWallet, + ...MbModuleWallet, successUrl: successUrl || window.location.href, failureUrl: successUrl || window.location.href, }; @@ -104,10 +115,10 @@ export const setupMintbaseWalletSelector = async ( network: network, modules: [ setupBitteWallet( - baseModuleWallet, + BitteModuleWallet, ), setupMintbaseWallet( - baseModuleWallet, + MbModuleWallet, ), ...(options?.additionalWallets || []), ], From 1e13d67a330f1776a7ce873c47db3221afe42dd4 Mon Sep 17 00:00:00 2001 From: Ruben Marcus Date: Mon, 29 Jul 2024 13:34:49 +0100 Subject: [PATCH 5/8] rollback --- packages/wallet/src/bitte-wallet.ts | 38 ++++++++++++++--------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/wallet/src/bitte-wallet.ts b/packages/wallet/src/bitte-wallet.ts index 3b3c17c1..41a2a385 100644 --- a/packages/wallet/src/bitte-wallet.ts +++ b/packages/wallet/src/bitte-wallet.ts @@ -6,7 +6,7 @@ import type { FinalExecutionOutcome, WalletBehaviourFactory, } from '@near-wallet-selector/core'; -import { getCallbackUrlBitte } from './utils'; +import { getCallbackUrl } from './utils'; import { createAction } from '@near-wallet-selector/wallet-utils'; @@ -36,7 +36,7 @@ interface BitteWalletAccount { } export type CallBackArgs = { - args: object; + args: Record; type: TransactionSuccessEnum; } @@ -76,7 +76,7 @@ export const BitteWallet: WalletBehaviourFactory< //make near-api-js not throw without lak if (acc && !contractId) { localStorage.setItem( - 'bitte-wallet:wallet_auth_key', + 'mintbase-wallet_wallet_auth_key', JSON.stringify({ accountId: acc as string, allKeys: [], @@ -85,8 +85,8 @@ export const BitteWallet: WalletBehaviourFactory< } const nearConnection = await connect(connectionConfig); - const wallet = new WalletConnection(nearConnection, 'bitte-wallet'); - localStorage.setItem('bitte-wallet:callback_url', callback); + const wallet = new WalletConnection(nearConnection, 'mintbase-wallet'); + localStorage.setItem('mintbase-wallet:callback_url', callback); return { wallet, @@ -123,7 +123,7 @@ export const BitteWallet: WalletBehaviourFactory< }; const signOut = async (): Promise => { - window.localStorage.removeItem('bitte-wallet:account-data'); + window.localStorage.removeItem('mintbase-wallet:account-data'); if (state.wallet.isSignedIn()) { state.wallet.signOut(); @@ -153,7 +153,7 @@ export const BitteWallet: WalletBehaviourFactory< // callbackUrl: cbUrl, // }); - const { cbUrl } = getCallbackUrlBitte(callbackUrl ?? ''); + const { cbUrl } = getCallbackUrl(callbackUrl ?? ''); for (const { signerId } of transactions) { assertValidSigner(signerId); @@ -186,7 +186,7 @@ export const BitteWallet: WalletBehaviourFactory< throw new Error('No receiver found to send the transaction to'); } - const { cbUrl } = getCallbackUrlBitte(callbackUrl ?? ''); + const { cbUrl } = getCallbackUrl(callbackUrl ?? ''); const callback = cbUrl || successUrl; @@ -200,11 +200,11 @@ export const BitteWallet: WalletBehaviourFactory< } const account = state.wallet.account(); - return account.signAndSendTransaction({ + return await account.signAndSendTransaction({ receiverId: receiverId || contractId, actions: actions.map((action) => createAction(action)) as any, walletCallbackUrl: callback, - }); + }) as any; }; @@ -213,7 +213,7 @@ export const BitteWallet: WalletBehaviourFactory< }; const signMessage = async ({ message, nonce, recipient, callbackUrl }): Promise => { - const { cbUrl } = getCallbackUrlBitte(callbackUrl ?? ''); + const { cbUrl } = getCallbackUrl(callbackUrl ?? ''); const newUrl = new URL(`${metadata.walletUrl}/sign-message`); newUrl.searchParams.set('message', message); @@ -235,15 +235,15 @@ export const BitteWallet: WalletBehaviourFactory< newUrl.searchParams.set('callbackUrl', callbackUrl); try { - const response = await fetch(newUrl.toString()) + const response = await fetch(newUrl.toString()); const data = await response.json(); - const { isValid } = data - return isValid + const { isValid } = data; + return isValid; } catch (e) { - return false + return false; } - } + }; const getAvailableBalance = async (): Promise => { // const accountId = state.wallet.getAccountId(); @@ -260,7 +260,7 @@ export const BitteWallet: WalletBehaviourFactory< } const currentAccount: string = window.localStorage.getItem( - 'bitte-wallet:account-creation-data', + 'mintbase-wallet:account-creation-data', )!; return [ @@ -280,7 +280,7 @@ export const BitteWallet: WalletBehaviourFactory< const setActiveAccountId = (accountId: string): null => { activeAccountId = accountId; - window.localStorage.setItem('bitte-wallet:activeAccountId', accountId); + window.localStorage.setItem('mintbase-wallet:activeAccountId', accountId); return null; }; @@ -336,6 +336,6 @@ export const BitteWallet: WalletBehaviourFactory< getAccounts, switchAccount, signAndSendTransactions, - verifyMessage + verifyMessage, }; }; From 626ca6dd34bedb8285b534ec446d16b43af4d099 Mon Sep 17 00:00:00 2001 From: Ruben Marcus Date: Mon, 29 Jul 2024 13:38:01 +0100 Subject: [PATCH 6/8] rollback --- packages/react/src/wallet/wallet.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react/src/wallet/wallet.ts b/packages/react/src/wallet/wallet.ts index b6357c74..c8011261 100644 --- a/packages/react/src/wallet/wallet.ts +++ b/packages/react/src/wallet/wallet.ts @@ -83,7 +83,7 @@ export const setupMintbaseWalletSelector = async ( const BitteModuleWallet = { - walletUrl: walletUrls[network], + walletUrl: BitteWalletUrls[network], callbackUrl: callbackUrl, contractId: contractAddress, }; From 5eb9bf8b0b5e4e9f038db2af5f041076fd0200cc Mon Sep 17 00:00:00 2001 From: Ruben Marcus Date: Mon, 29 Jul 2024 13:46:38 +0100 Subject: [PATCH 7/8] rollback --- packages/react/src/wallet/wallet.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react/src/wallet/wallet.ts b/packages/react/src/wallet/wallet.ts index c8011261..76ff4196 100644 --- a/packages/react/src/wallet/wallet.ts +++ b/packages/react/src/wallet/wallet.ts @@ -104,7 +104,7 @@ export const setupMintbaseWalletSelector = async ( walletSelectorComponents.selector = await setupWalletSelector({ network: network, modules: [ - setupBitteWallet(moduleWallet), + setupBitteWallet(BitteModuleWallet), setupMintbaseWallet(moduleWallet), ...(options?.additionalWallets || []), ...SUPPORTED_NEAR_WALLETS, From 17d2a30499a5311636402d7061399bc51f079c60 Mon Sep 17 00:00:00 2001 From: Ruben Marcus Date: Mon, 29 Jul 2024 13:55:02 +0100 Subject: [PATCH 8/8] remove unneded any --- packages/wallet/src/bitte-wallet.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/wallet/src/bitte-wallet.ts b/packages/wallet/src/bitte-wallet.ts index 41a2a385..d5171de9 100644 --- a/packages/wallet/src/bitte-wallet.ts +++ b/packages/wallet/src/bitte-wallet.ts @@ -204,7 +204,7 @@ export const BitteWallet: WalletBehaviourFactory< receiverId: receiverId || contractId, actions: actions.map((action) => createAction(action)) as any, walletCallbackUrl: callback, - }) as any; + }); };