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