@privy-io/cross-app-connect
Version:
Privy cross app wallet connectors for wagmi and RainbowKit
2 lines (1 loc) • 3.52 kB
JavaScript
import{createPublicClient as e,http as t}from"viem";import{createPrivyCrossAppClient as a}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 s=s=>{let i=a({providerAppId:s.providerAppId,chains:s.chains,chainId:s.chainId,apiUrl:s.apiUrl,connectionOpts:{smartWalletMode:s.smartWalletMode}}),c=e({chain:i.chain,transport:s.transports?.[i.chain.id]??t()}),h={accountsChanged:[],connect:[],chainChanged:[],disconnect:[],message:[]},l=()=>{i.clearConnection(),h.accountsChanged?.forEach((e=>e([])))};return{on:(e,t)=>{h[e].push(t)},removeListener:(e,t)=>{let a=h[e].indexOf(t);a>-1&&h[e].splice(a)},request:async a=>{s.smartWalletMode&&("eth_sendTransaction"===a.method&&(a.method="privy_sendSmartWalletTx"),"eth_signTransaction"===a.method&&(a.method="privy_signSmartWalletTx"),"personal_sign"===a.method&&(a.method="privy_signSmartWalletMessage"),"eth_signTypedData_v4"===a.method&&(a.method="privy_signSmartWalletTypedData"));let{method:d,params:_}=a;if(console.debug("PrivyWalletProvider.request",{method:d,params:_}),"wallet_requestPermissions"===d||"eth_requestAccounts"===d&&!i.address)return await i.requestConnection(),h.accountsChanged?.forEach((e=>e([i.address]))),void h.connect?.forEach((e=>e({chainId:r(i.chain.id)})));if("eth_chainId"===d)return i.chain.id;if(o(d))return c.request({method:d,params:_});switch(d){case"eth_requestAccounts":case"eth_accounts":return i.address?[i.address]:[];case"wallet_switchEthereumChain":return i.switchChain({id:Number(_[0].chainId)}),c=e({chain:i.chain,transport:s.transports?.[i.chain.id]??t()}),h.chainChanged.forEach((e=>e(r(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(d)&&(_[0].chainId??=i.chain.id);try{return i.sendRequest(d,_)}catch(e){throw e instanceof n&&"clear"===e.action&&l(),e}default:throw Error(`Unsupported request: ${d}`)}}}};let r=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},o=e=>!!i[e];export{s as toPrivyWalletProvider};