UNPKG

@dynamic-labs/sdk-react-core

Version:

A React SDK for implementing wallet web3 authentication and authorization to your website.

61 lines (58 loc) 2.51 kB
'use client' import { __awaiter } from '../../../../../_virtual/_tslib.js'; import { generateMessageToSign } from '@dynamic-labs/multi-wallet'; import { DynamicError, PlatformService } from '@dynamic-labs/utils'; const getChainIdForMessage = (walletConnector) => __awaiter(void 0, void 0, void 0, function* () { // If it's a Solana wallet, we should not specify a chainId // since it breaks the message signature in some wallets (e.g. Trust Wallet) if (walletConnector.connectedChain === 'SOL') { return undefined; } if (walletConnector.connectedChain === 'SUI') { return undefined; } const chain = yield walletConnector.getNetwork(true); if (chain) { return chain; } if (walletConnector.key === 'bloctoevm') { return 137; } if (walletConnector.connectedChain === 'EVM') { return 1; } return chain; }); const generateMessages = (publicWalletAddress, walletConnector, nonce, projectEnvironmentId, displaySiweStatement, siweStatement) => __awaiter(void 0, void 0, void 0, function* () { if (publicWalletAddress === undefined) { throw new DynamicError('Unable to fetch the public address from the wallet'); } if (!walletConnector.connectedChain) { throw new DynamicError('Wallet is not connected'); } const chainId = yield getChainIdForMessage(walletConnector); const parsedAddress = walletConnector.parseAddress(publicWalletAddress); const currentUrl = PlatformService.getUrl(); const messageToSign = generateMessageToSign({ blockchain: walletConnector.connectedChain, chainId: chainId, domain: currentUrl.host, nonce, // The SIWE parser library used to validate signed messages for EVM/Ethereum // checks that an address is in EIP55 format, so make sure we do that here. // see: https://eips.ethereum.org/EIPS/eip-55 publicKey: parsedAddress, requestId: projectEnvironmentId, resources: walletConnector.providerResources, statement: displaySiweStatement ? siweStatement.replace(/(\r\n|\n|\r)/gm, ' ').trim() : undefined, uri: currentUrl.origin + currentUrl.pathname, }); const signedMessage = yield walletConnector.proveOwnership(parsedAddress, messageToSign); if (!signedMessage) { throw new DynamicError('Unable to sign the message'); } return { messageToSign, signedMessage }; }); export { generateMessages };