@privy-io/cross-app-connect
Version: 
Privy cross app wallet connectors for wagmi and RainbowKit
2 lines (1 loc) • 3.8 kB
JavaScript
import{createPublicClient as e,http as t,ProviderRpcError as a}from"viem";import{createPrivyCrossAppClient as s}from"./client.mjs";import{PopupRequestError as n}from"./request.mjs";import"./getCrossAppProviderDetails.mjs";import"./sendConnectionRequestToPopup.mjs";import"./crypto.mjs";import"@noble/curves/secp256k1";import"@scure/base";import"buffer";import"fflate";import"./triggerPopup.mjs";import"./storage.mjs";const r=r=>{let i=s({providerAppId:r.providerAppId,chains:r.chains,chainId:r.chainId,apiUrl:r.apiUrl,connectionOpts:{smartWalletMode:r.smartWalletMode}}),h=e({chain:i.chain,transport:r.transports?.[i.chain.id]??t()}),d={accountsChanged:[],connect:[],chainChanged:[],disconnect:[],message:[]},l=()=>{i.clearConnection(),d.accountsChanged?.forEach((e=>e([])))};return{on:(e,t)=>{d[e].push(t)},removeListener:(e,t)=>{let a=d[e].indexOf(t);a>-1&&d[e].splice(a)},request:async s=>{r.smartWalletMode&&("eth_sendTransaction"===s.method&&(s.method="privy_sendSmartWalletTx"),"eth_signTransaction"===s.method&&(s.method="privy_signSmartWalletTx"),"personal_sign"===s.method&&(s.method="privy_signSmartWalletMessage"),"eth_signTypedData_v4"===s.method&&(s.method="privy_signSmartWalletTypedData"));let{method:_,params:m}=s;if(console.debug("PrivyWalletProvider.request",{method:_,params:m}),"wallet_requestPermissions"===_||"eth_requestAccounts"===_&&!i.address)return await i.requestConnection(),d.accountsChanged?.forEach((e=>e([i.address]))),void d.connect?.forEach((e=>e({chainId:o(i.chain.id)})));if("eth_chainId"===_)return i.chain.id;if(c(_))return h.request({method:_,params:m});switch(_){case"eth_requestAccounts":case"eth_accounts":return i.address?[i.address]:[];case"wallet_switchEthereumChain":return i.switchChain({id:Number(m[0].chainId)}),h=e({chain:i.chain,transport:r.transports?.[i.chain.id]??t()}),d.chainChanged.forEach((e=>e(o(i.chain.id)))),null;case"wallet_revokePermissions":return void l();case"eth_sendTransaction":case"eth_signTransaction":case"eth_signTypedData_v4":case"eth_sign":case"privy_signSmartWalletMessage":case"privy_signSmartWalletTypedData":case"privy_signSmartWalletTx":case"privy_sendSmartWalletTx":case"personal_sign":["eth_sendTransaction","eth_signTransaction","privy_sendSmartWalletTx","privy_signSmartWalletTx"].includes(_)&&(m[0].chainId??=i.chain.id);try{return await i.sendRequest(_,m)}catch(e){if(e instanceof n&&("clear"===e.action&&l(),void 0===e.code&&"User rejected request"===e.message))throw new a(Error("User rejected request"),{code:4001,shortMessage:"User rejected request"});throw new a(e,{code:e.code??-1,shortMessage:e.message})}default:throw new a(Error("Unsupported method"),{code:4200,shortMessage:`Unsupported method: ${_}`})}}}};let o=e=>`0x${e.toString(16)}`,i={web3_clientVersion:!0,web3_sha3:!0,net_listening:!0,net_peerCount:!0,net_version:!0,eth_blobBaseFee:!0,eth_blockNumber:!0,eth_call:!0,eth_chainId:!0,eth_coinbase:!0,eth_estimateGas:!0,eth_feeHistory:!0,eth_gasPrice:!0,eth_getBalance:!0,eth_getBlockByHash:!0,eth_getBlockByNumber:!0,eth_getBlockTransactionCountByHash:!0,eth_getBlockTransactionCountByNumber:!0,eth_getCode:!0,eth_getFilterChanges:!0,eth_getFilterLogs:!0,eth_getLogs:!0,eth_getProof:!0,eth_getStorageAt:!0,eth_getTransactionByBlockHashAndIndex:!0,eth_getTransactionByBlockNumberAndIndex:!0,eth_getTransactionByHash:!0,eth_getTransactionCount:!0,eth_getTransactionReceipt:!0,eth_getUncleByBlockHashAndIndex:!0,eth_getUncleByBlockNumberAndIndex:!0,eth_getUncleCountByBlockHash:!0,eth_getUncleCountByBlockNumber:!0,eth_maxPriorityFeePerGas:!0,eth_newBlockFilter:!0,eth_newFilter:!0,eth_newPendingTransactionFilter:!0,eth_protocolVersion:!0,eth_sendRawTransaction:!0,eth_uninstallFilter:!0,zks_estimateFee:!0,eth_createAccessList:!0,eth_simulateV1:!0},c=e=>!!i[e];export{r as toPrivyWalletProvider};