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