Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bitte wallet support to react package 2 #529

Merged
merged 8 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions packages/react/src/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -64,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,
Expand All @@ -76,17 +82,30 @@ export const setupMintbaseWalletSelector = async (
): Promise<WalletSelectorComponents> => {


const BitteModuleWallet = {
walletUrl: BitteWalletUrls[network],
callbackUrl: callbackUrl,
contractId: contractAddress,
};

const MbModuleWallet = {
walletUrl: walletUrls[network],
callbackUrl: callbackUrl,
contractId: contractAddress,
};

if (onlyMbWallet === false) {
const moduleWallet = {
...MbModuleWallet,
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(BitteModuleWallet),
setupMintbaseWallet(moduleWallet),
...(options?.additionalWallets || []),
...SUPPORTED_NEAR_WALLETS,
],
Expand All @@ -95,11 +114,12 @@ export const setupMintbaseWalletSelector = async (
walletSelectorComponents.selector = await setupWalletSelector({
network: network,
modules: [
setupMintbaseWallet({
walletUrl: walletUrls[network],
callbackUrl: callbackUrl,
contractId: contractAddress,
}),
setupBitteWallet(
BitteModuleWallet,
),
setupMintbaseWallet(
MbModuleWallet,
),
...(options?.additionalWallets || []),
],
});
Expand Down Expand Up @@ -273,4 +293,4 @@ export const signMessage = async (
// // const verified = createdPublicKey.verify(new Uint8Array(sha256.array(stringified)), Buffer.from(signature, 'base64'));

// return false;
// };
// };
16 changes: 8 additions & 8 deletions packages/wallet/src/bitte-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface BitteWalletAccount {
}

export type CallBackArgs = {
args: object;
args: Record<string, unknown>;
type: TransactionSuccessEnum;
}

Expand Down Expand Up @@ -200,7 +200,7 @@ 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,
Expand Down Expand Up @@ -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<void> => {
// const accountId = state.wallet.getAccountId();
Expand Down Expand Up @@ -336,6 +336,6 @@ export const BitteWallet: WalletBehaviourFactory<
getAccounts,
switchAccount,
signAndSendTransactions,
verifyMessage
verifyMessage,
};
};
65 changes: 64 additions & 1 deletion packages/wallet/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]*$/;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -95,5 +158,5 @@ const resolveBitteWallet = (network: string, walletUrl?: string): string => {
}
};

export { checkCallbackUrl, getCallbackUrl, resolveBitteWallet, resolveWalletUrl };
export { checkCallbackUrl,checkCallbackUrlBitte, getCallbackUrl,getCallbackUrlBitte, resolveBitteWallet, resolveWalletUrl };