@getpara/wagmi-v2-connector 0.0.1-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ import { EIP1474Methods, EIP1193Provider, EIP1193RequestFn, Transport } from 'viem';
2
+ import { EventEmitter } from 'eventemitter3';
3
+ import { Chain } from 'wagmi/chains';
4
+ import ParaWeb from '@getpara/web-sdk';
5
+ interface ParaEIP1193ProviderOpts {
6
+ para: ParaWeb;
7
+ chainId: string;
8
+ chains: Chain[];
9
+ disableModal?: boolean;
10
+ storageOverride?: Pick<Storage, 'setItem' | 'getItem'>;
11
+ transports?: Record<number, Transport>;
12
+ renderModal?: (onClose: () => void) => void;
13
+ openModal?: () => void;
14
+ }
15
+ export declare class ParaEIP1193Provider extends EventEmitter implements EIP1193Provider {
16
+ private currentHexChainId;
17
+ private walletClient;
18
+ private chainTransportSubscribe?;
19
+ private chains;
20
+ private viemChains;
21
+ private para;
22
+ private disableModal;
23
+ private storage;
24
+ private isModalClosed;
25
+ private transports?;
26
+ private openModal?;
27
+ constructor(opts: ParaEIP1193ProviderOpts);
28
+ private get accounts();
29
+ private getStorageChainId;
30
+ private setChainId;
31
+ private getRpcUrlsFromViemChain;
32
+ private wagmiChainToAddEthereumChainParameters;
33
+ private wagmiChainsToAddEthereumChainParameters;
34
+ private accountFromAddress;
35
+ private setCurrentChain;
36
+ closeModal: () => void;
37
+ request: EIP1193RequestFn<EIP1474Methods>;
38
+ }
39
+ export {};
@@ -0,0 +1,2 @@
1
+ export * from './paraConnector.js';
2
+ export * from './ParaEIP1193Provider.js';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ "use client";
2
+ import{injected as H}from"wagmi/connectors";import{ProviderRpcError as d,webSocket as T,publicActions as f,http as E,formatTransaction as C}from"viem";import{EventEmitter as S}from"eventemitter3";import{extractRpcUrls as y}from"@wagmi/core";import{getViemChain as g,createParaViemClient as x,createParaAccount as D}from"@getpara/viem-v2-integration";import{decimalToHex as o,hexToDecimal as u}from"@getpara/web-sdk";var I="@CAPSULE/chainId",N=6e5,_={setItem:()=>{},getItem:()=>null},c=class extends S{constructor(e){super();this.getRpcUrlsFromViemChain=e=>y({chain:e,transports:this.transports});this.wagmiChainToAddEthereumChainParameters=e=>{let s=o(`${e.id}`);return[s,{chainId:s,chainName:e.name,nativeCurrency:e.nativeCurrency,rpcUrls:this.getRpcUrlsFromViemChain(e)}]};this.wagmiChainsToAddEthereumChainParameters=e=>Object.fromEntries(e.map(this.wagmiChainToAddEthereumChainParameters));this.accountFromAddress=e=>D(this.para,e);this.setCurrentChain=e=>{let s=this.chains[e];this.setChainId(e);let t=this.viemChains[e]||g(u(e)),a=this.getRpcUrlsFromViemChain(t),r;this.transports[t.id]?r=this.transports[t.id]:a[0].startsWith("ws")?r=T(s.rpcUrls[0]):(r=E(a[0]),this.chainTransportSubscribe=void 0);let i=r({chain:t});i.config.type==="ws"&&(this.chainTransportSubscribe=i.value.subscribe),this.walletClient=x(this.para,{chain:t,transport:r},{noAccount:!0}).extend(f),this.emit("chainChanged",this.currentHexChainId)};this.closeModal=()=>{this.isModalClosed=!0};this.request=async e=>{let{method:s,params:t}=e;switch(s){case"eth_accounts":return this.accounts;case"eth_chainId":return this.currentHexChainId;case"eth_requestAccounts":{if(await this.para.isFullyLoggedIn())return this.accounts;this.isModalClosed=!1,this.openModal();let a=Date.now();for(;Date.now()-a<N;){if(await new Promise(r=>setTimeout(r,2e3)),await this.para.isFullyLoggedIn()){let r=this.accounts;return this.emit("accountsChanged",r),r}if(this.isModalClosed)throw new d(new Error("user closed modal"),{code:4001,shortMessage:"user closed modal"})}throw new d(new Error("timed out waiting for user to log in"),{code:4001,shortMessage:"timed out waiting for user to log in"})}case"eth_sendTransaction":{let a=t[0].from;return this.walletClient.sendTransaction({...C(t[0]),chain:void 0,account:this.accountFromAddress(a)})}case"eth_sign":case"personal_sign":return this.walletClient.signMessage({message:{raw:t[0]},account:this.accountFromAddress(t[1])});case"eth_signTransaction":{let a=t[0].from;return this.accountFromAddress(a).signTransaction(C(t[0]))}case"eth_signTypedData_v4":{let a=t[0],r=t[1];return typeof r=="string"&&(r=JSON.parse(r)),this.walletClient.signTypedData({...r,account:this.accountFromAddress(a)})}case"eth_subscribe":{if(!this.chainTransportSubscribe)throw new d(new Error("chain does not support subscriptions"),{code:4200,shortMessage:"chain does not support subscriptions"});return(await this.chainTransportSubscribe({params:t,onData:r=>{this.emit("message",{type:"eth_subscription",data:r})}})).subscriptionId}case"wallet_addEthereumChain":return this.chains[t[0].chainId]||(this.chains[t[0].chainId]=t[0]),null;case"wallet_getPermissions":return[];case"wallet_requestPermissions":return[];case"wallet_switchEthereumChain":{if(!this.chains[t[0].chainId]){let a=g(u(t[0].chainId)),[r,i]=this.wagmiChainToAddEthereumChainParameters(a);this.chains[r]=i,this.setCurrentChain(t[0].chainId)}return this.currentHexChainId!==t[0].chainId&&this.setCurrentChain(t[0].chainId),null}case"wallet_watchAsset":return!1;default:return this.walletClient.request({method:s,params:t})}};this.storage=e.storageOverride||typeof window>"u"?_:sessionStorage,this.isModalClosed=!0,this.para=e.para,this.disableModal=!!e.disableModal,this.viemChains=e.chains.reduce((a,r)=>(a[o(`${r.id}`)]=r,a),{}),this.chains=this.wagmiChainsToAddEthereumChainParameters(e.chains),this.transports=e.transports,this.openModal=e.openModal,this.disableModal||e.renderModal?.(this.closeModal);let s=this.getStorageChainId()||e.chainId,t=this.chains[o(s)]?s:`${e.chains[0].id}`;this.setCurrentChain(o(t)),this.emit("connect",{chainId:this.currentHexChainId})}get accounts(){return this.para.getWalletsByType("EVM").map(e=>e.address)}getStorageChainId(){return this.storage.getItem(I)}setChainId(e){this.currentHexChainId=e,this.storage.setItem(I,u(e))}};import{createConnector as R}from"wagmi";var b="para",w="Para",W="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjE2IiBoZWlnaHQ9IjIwNCIgdmlld0JveD0iMCAwIDIxNiAyMDQiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxwYXRoIGQ9Ik02MCAwSDE0NEMxODMuNzY0IDAgMjE2IDMyLjIzNTUgMjE2IDcyQzIxNiAxMTEuNzY1IDE4My43NjQgMTQ0IDE0NCAxNDRIOTZDODIuNzQ1MiAxNDQgNzIgMTU0Ljc0NSA3MiAxNjhWMjA0SDBWMTMySDM2QzQ5LjI1NDggMTMyIDYwIDEyMS4yNTUgNjAgMTA4TDYwIDBaIiBmaWxsPSJibGFjayIvPgo8L3N2Zz4K",j=({para:n,chains:P,disableModal:e,storageOverride:s,options:t,iconOverride:a,nameOverride:r,idOverride:i,transports:M,renderModal:v,openModal:A})=>R(h=>{let m=[...h.chains],l=new c({para:n,chainId:`${m[0].id}`,chains:m,disableModal:e,storageOverride:s,transports:M||h.transports,renderModal:v,openModal:A}),p=H({target:{name:w,id:i??b,provider:l},...t})(h);return{...p,type:i??b,name:r??w,icon:a??W,disconnect:async()=>{l.closeModal(),await p.disconnect(),n.logout()}}}),se=n=>j(n);export{c as ParaEIP1193Provider,j as createParaConnector,se as paraConnector};
Binary file
Binary file