UNPKG

@privy-io/cross-app-connect

Version:

Privy cross app wallet connectors for wagmi and RainbowKit

3 lines (2 loc) 4.12 kB
import{createPublicClient as e,http as t,ProviderRpcError as r}from"viem";import{createPrivyCrossAppClient as n}from"./client.mjs";import{PopupRequestError as a}from"./request.mjs";import"./getCrossAppProviderDetails.mjs";import"./sendConnectionRequestToPopup.mjs";import"./crypto.mjs";import"@noble/curves/secp256k1";import"@scure/base";import"buffer";import"fflate";import"@privy-io/encoding";import"./triggerPopup.mjs";import"@privy-io/popup";import"./constants.mjs";import"./storage.mjs";const s=s=>{let i=n({providerAppId:s.providerAppId,chains:s.chains,chainId:s.chainId,apiUrl:s.apiUrl,connectionOpts:{smartWalletMode:s.smartWalletMode},defaultPopupTimeout:s.defaultPopupTimeout,overrideConnectUrl:s.overrideConnectUrl,overrideTransactUrl:s.overrideTransactUrl}),h=e({chain:i.chain,transport:s.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 r=d[e].indexOf(t);r>-1&&d[e].splice(r)},request:async n=>{s.smartWalletMode&&("eth_sendTransaction"===n.method&&(n.method="privy_sendSmartWalletTx"),"eth_signTransaction"===n.method&&(n.method="privy_signSmartWalletTx"),"personal_sign"===n.method&&(n.method="privy_signSmartWalletMessage"),"eth_signTypedData_v4"===n.method&&(n.method="privy_signSmartWalletTypedData"));let{method:m,params:_}=n;if(console.debug("PrivyWalletProvider.request",{method:m,params:_}),"wallet_requestPermissions"===m||"eth_requestAccounts"===m&&!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"===m)return i.chain.id;if(c(m))return h.request({method:m,params:_});switch(m){case"eth_requestAccounts":case"eth_accounts":return i.address?[i.address]:[];case"wallet_switchEthereumChain":return i.switchChain({id:Number(_[0].chainId)}),h=e({chain:i.chain,transport:s.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 a&&("clear"===e.action&&l(),void 0===e.code&&"User rejected request"===e.message))throw new r(Error("User rejected request"),{code:4001,shortMessage:"User rejected request"});throw new r(e,{code:e.code??-1,shortMessage:e.message})}default:throw new r(Error("Unsupported method"),{code:4200,shortMessage:`Unsupported method: ${m}`})}}}};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_fillTransaction:!0,eth_gasPrice:!0,eth_getBalance:!0,eth_getBlockByHash:!0,eth_getBlockByNumber:!0,eth_getBlockReceipts:!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_getTransactionBySenderAndNonce:!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{s as toPrivyWalletProvider}; //# sourceMappingURL=provider.mjs.map