@rango-dev/wallets-core 0.17.0 → 0.17.1-next.0
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.
- package/CHANGELOG.md +0 -4
- package/dist/index.js +1 -1
- package/dist/index.js.map +3 -3
- package/dist/wallet.d.ts +4 -3
- package/dist/wallet.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/wallet.ts +48 -31
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var S=Object.defineProperty;var s=(i,t)=>S(i,"name",{value:t,configurable:!0});var m=(a=>(a.CONNECTED="connected",a.CONNECTING="connecting",a.REACHABLE="reachable",a.INSTALLED="installed",a.ACCOUNTS="accounts",a.NETWORK="network",a))(m||{});var d=class{getItem(t){let e=localStorage.getItem(t);return e?JSON.parse(e):null}setItem(t,e){localStorage.setItem(t,JSON.stringify(e))}removeItem(t){localStorage.removeItem(t)}};s(d,"Persistor");function N(i,t){return`${t||""}:${i}`}s(N,"formatAddressWithNetwork");function u(i,t){return i?i.map(e=>N(e,t)):[]}s(u,"accountAddressesWithNetwork");function T(i){let[t,e]=i.split(":");return{network:t,address:e}}s(T,"readAccountAddress");function f(i){let{checkInstallation:t=!0}=i.config;return t}s(f,"needsCheckInstallation");import{getBlockChainNameFromId as w,Networks as
|
|
1
|
+
var S=Object.defineProperty;var s=(i,t)=>S(i,"name",{value:t,configurable:!0});var m=(a=>(a.CONNECTED="connected",a.CONNECTING="connecting",a.REACHABLE="reachable",a.INSTALLED="installed",a.ACCOUNTS="accounts",a.NETWORK="network",a))(m||{});var d=class{getItem(t){let e=localStorage.getItem(t);return e?JSON.parse(e):null}setItem(t,e){localStorage.setItem(t,JSON.stringify(e))}removeItem(t){localStorage.removeItem(t)}};s(d,"Persistor");function N(i,t){return`${t||""}:${i}`}s(N,"formatAddressWithNetwork");function u(i,t){return i?i.map(e=>N(e,t)):[]}s(u,"accountAddressesWithNetwork");function T(i){let[t,e]=i.split(":");return{network:t,address:e}}s(T,"readAccountAddress");function f(i){let{checkInstallation:t=!0}=i.config;return t}s(f,"needsCheckInstallation");import{getBlockChainNameFromId as w,Networks as h}from"@rango-dev/wallets-shared";var k=class{constructor(t,e){this.actions=e,this.options=t,this.provider=null,this.meta=[],this.state={connected:!1,connecting:!1,reachable:!1,installed:!1,accounts:null,network:null},f(t)||this.setInstalledAs(!0)}async getConnectionFromState(){return this.state.connected&&this.provider?{accounts:this.state.accounts,network:this.state.network,provider:this.provider}:null}async connect(t){if(this.state.connecting)throw new Error("Connecting...");let e=await this.getConnectionFromState(),n=this.state.network,o=t||n||this.options.config.defaultNetwork;if(e){let l=n!==o&&!!o;if(n===o)return e;let r=!0;if(this.actions.canSwitchNetworkTo&&(r=this.actions.canSwitchNetworkTo({provider:this.provider,meta:this.meta,network:o||""})),l&&r&&this.actions.switchNetwork)return await this.actions.switchNetwork({instance:this.provider,meta:this.meta,network:o,newInstance:this.tryGetInstance.bind(this)}),o!==this.state.network&&this.updateState({network:t}),{accounts:e.accounts,network:o,provider:this.provider}}let c=await this.tryGetInstance({network:t});this.updateState({connecting:!0}),this.setInstalledAs(!0);try{var a=await this.actions.connect({instance:c,network:o||void 0,meta:this.meta||[]})}catch(l){throw this.resetState(),l}this.updateState({connected:!0,reachable:!0,connecting:!1});let p=[],g=null;if(Array.isArray(a))p=a.flatMap(r=>{let y=r.chainId||h.Unknown,I=w(y,this.meta)||h.Unknown;return u(r.accounts,I)}).filter(Boolean),g=o||this.options.config.defaultNetwork;else{let l=a.chainId||h.Unknown,r=w(l,this.meta)||h.Unknown;p=u(a.accounts,r),g=r}return p.length>0&&this.updateState({accounts:p,network:g}),{accounts:this.state.accounts,network:this.state.network,provider:this.provider}}async disconnect(){this.resetState(),this.actions.disconnect&&this.actions.disconnect({instance:this.provider,destroyInstance:()=>{this.setProvider(null)}})}async eagerConnect(){let t=await this.tryGetInstance({network:void 0}),{canEagerConnect:e}=this.actions,n=`can't restore connection for ${this.options.config.type} .`;if(e){if(await e({instance:t,meta:this.meta}))return this.connect();throw new Error(n)}else throw new Error(n)}getSigners(t){return this.actions.getSigners(t)}getWalletInfo(t){return this.actions.getWalletInfo(t)}canSwitchNetworkTo(t,e){let n=this.actions.canSwitchNetworkTo;return n?n({network:t,meta:this.meta,provider:e}):!1}onInit(){this.options.config.isAsyncInstance?f(this.options)&&this.actions.getInstance().then(t=>{t&&this.setInstalledAs(!0)}):this.actions.getInstance()&&!this.state.installed&&this.setInstalledAs(!0)}setProvider(t){this.provider=t,t&&this.actions.subscribe&&this.actions.subscribe({instance:t,state:this.state,meta:this.meta,connect:this.connect.bind(this),disconnect:this.disconnect.bind(this),updateAccounts:(e,n)=>{let o=this.state.network;n&&(o=w(n,this.meta)||h.Unknown);let c=u(e,o);c.length>0&&this.updateState({accounts:c})},updateChainId:this.updateChainId.bind(this)})}setMeta(t){this.meta=t}setHandler(t){this.options.handler=t}getState(){return this.state}updateState(t){let e=[];typeof t.connected<"u"&&(this.state.connected=t.connected,e.push(["connected",t.connected])),typeof t.connecting<"u"&&(this.state.connecting=t.connecting,e.push(["connecting",t.connecting])),typeof t.reachable<"u"&&(this.state.reachable=t.reachable,e.push(["reachable",t.reachable])),typeof t.installed<"u"&&(this.state.installed=t.installed,e.push(["installed",t.installed])),typeof t.accounts<"u"&&(this.state.accounts=t.accounts,e.push(["accounts",t.accounts])),typeof t.network<"u"&&(this.state.network=t.network,e.push(["network",t.network]));let n=this.getState();e.forEach(([o,c])=>{this.options.handler(this.options.config.type,o,c,n,this.meta)})}resetState(){this.updateState({connected:!1,connecting:!1,reachable:!1,accounts:null,network:null})}updateChainId(t){let e=t?w(t,this.meta):h.Unknown;this.updateState({network:e})}setInstalledAs(t){!f(this.options)&&t===!1||this.updateState({installed:t})}async tryGetInstance({network:t,force:e}){let n=null;if(this.setProvider(null),this.options.config.isAsyncInstance){let o={currentProvider:this.provider,meta:this.meta,force:e||!1,updateChainId:this.updateChainId.bind(this),getState:this.getState.bind(this)};t&&(o.network=t),n=await this.actions.getInstance(o)}else n=this.actions.getInstance();if(!n){this.setInstalledAs(!1),this.resetState();let o=`It seems your selected wallet (${this.options.config.type}) isn't installed.`;throw new Error(o)}return this.setProvider(n),n}};s(k,"Wallet");var v=k;export{m as Events,d as Persistor,u as accountAddressesWithNetwork,v as default,N as formatAddressWithNetwork,f as needsCheckInstallation,T as readAccountAddress};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/types.ts", "../src/persistor.ts", "../src/helpers.ts", "../src/wallet.ts"],
|
|
4
|
-
"sourcesContent": ["import { Network, WalletType, WalletInfo } from '@rango-dev/wallets-shared';\nimport { State as WalletState } from './wallet';\nimport { SignerFactory, BlockchainMeta } from 'rango-types';\n\nexport type State = {\n [key: string]: WalletState | undefined;\n};\n\nexport type ConnectResult = {\n accounts: string[] | null;\n network: Network | null;\n provider: any;\n};\n\nexport type Providers = { [type in WalletType]?: any };\n\nexport enum Events {\n CONNECTED = 'connected',\n CONNECTING = 'connecting',\n REACHABLE = 'reachable',\n INSTALLED = 'installed',\n ACCOUNTS = 'accounts',\n NETWORK = 'network',\n}\n\nexport type ProviderConnectResult = {\n accounts: string[];\n chainId: string;\n};\n\nexport type GetInstanceOptions = {\n network?: Network;\n currentProvider: any;\n meta: BlockchainMeta[];\n getState: () => WalletState;\n /**\n * We always get the instance once and reuse it whenever we needs. By using this option\n * We can force the library to get a new instance and replace it with the old one.\n *\n * Originally, we used this option for wallet connect 1 and its switching network challenge.\n */\n force?: boolean;\n updateChainId: (chainId: number | string) => void;\n};\n\nexport type GetInstance =\n | (() => any)\n | ((options: GetInstanceOptions) => Promise<any>);\n\nexport type TryGetInstance =\n | (() => any)\n | ((options: Pick<GetInstanceOptions, 'force' | 'network'>) => Promise<any>);\n\nexport type Connect = (options: {\n instance: any;\n network?: Network;\n meta: BlockchainMeta[];\n}) => Promise<ProviderConnectResult | ProviderConnectResult[]>;\n\nexport type Disconnect = (options: {\n instance: any;\n destroyInstance: () => void;\n}) => Promise<void>;\n\nexport type Subscribe = (options: {\n instance: any;\n state: WalletState;\n meta: BlockchainMeta[];\n updateChainId: (chainId: string) => void;\n updateAccounts: (accounts: string[], chainId?: string) => void;\n connect: (network?: Network) => void;\n disconnect: () => void;\n}) => void;\n\nexport type SwitchNetwork = (options: {\n instance: any;\n network: Network;\n meta: BlockchainMeta[];\n newInstance?: TryGetInstance;\n}) => Promise<void>;\n\nexport type CanSwitchNetwork = (options: {\n network: Network;\n meta: BlockchainMeta[];\n provider: any;\n}) => boolean;\n\nexport type CanEagerConnect = (options: {\n instance: any;\n meta: BlockchainMeta[];\n}) => Promise<boolean>;\n\nexport interface WalletActions {\n connect: Connect;\n getInstance: any;\n disconnect?: Disconnect;\n subscribe?: Subscribe;\n // unsubscribe, // coupled to subscribe.\n\n // Optional, but should be provided at the same time.\n switchNetwork?: SwitchNetwork;\n getSigners: (provider: any) => SignerFactory;\n canSwitchNetworkTo?: CanSwitchNetwork;\n canEagerConnect?: CanEagerConnect;\n getWalletInfo(allBlockChains: BlockchainMeta[]): WalletInfo;\n}\n\nexport interface WalletConfig {\n type: WalletType;\n defaultNetwork?: Network;\n checkInstallation?: boolean;\n isAsyncInstance?: boolean;\n}\n\nexport type WalletProviders = Map<\n WalletType,\n {\n actions: WalletActions;\n config: WalletConfig;\n }\n>;\n\nexport type ProviderInterface = { config: WalletConfig } & WalletActions;\n", "export interface PersistStorage<T> {\n getItem: (name: string) => T | null;\n setItem: (name: string, value: T) => void;\n removeItem: (name: string) => void;\n}\n\nexport class Persistor<T> implements PersistStorage<T> {\n getItem(name: string) {\n const item = localStorage.getItem(name);\n const parsedItem = item ? (JSON.parse(item) as T) : null;\n return parsedItem;\n }\n setItem(name: string, value: T) {\n localStorage.setItem(name, JSON.stringify(value));\n }\n removeItem(name: string) {\n localStorage.removeItem(name);\n }\n}\n", "import { Network } from '@rango-dev/wallets-shared';\nimport { Options } from './wallet';\n\nexport function formatAddressWithNetwork(\n address: string,\n network?: Network | null\n) {\n return `${network || ''}:${address}`;\n}\n\nexport function accountAddressesWithNetwork(\n addresses: string[] | null,\n network?: Network | null\n) {\n if (!addresses) return [];\n\n return addresses.map((address) => {\n return formatAddressWithNetwork(address, network);\n });\n}\n\nexport function readAccountAddress(addressWithNetwork: string): {\n address: string;\n network: Network;\n} {\n const [network, address] = addressWithNetwork.split(':');\n\n return {\n network,\n address,\n };\n}\n\nexport function needsCheckInstallation(options: Options) {\n const { checkInstallation = true } = options.config;\n return checkInstallation;\n}\n", "import {\n getBlockChainNameFromId,\n Network,\n Networks,\n WalletType,\n} from '@rango-dev/wallets-shared';\nimport { accountAddressesWithNetwork, needsCheckInstallation } from './helpers';\nimport {\n Events,\n GetInstanceOptions,\n WalletActions,\n WalletConfig,\n} from './types';\nimport { BlockchainMeta } from 'rango-types';\n\nexport type EventHandler = (\n type: WalletType,\n event: Events,\n value: any,\n coreState: State,\n supportedChains: BlockchainMeta[]\n) => void;\n\nexport interface State {\n connected: boolean;\n connecting: boolean;\n reachable: boolean;\n installed: boolean;\n accounts: string[] | null;\n network: Network | null;\n}\n\nexport interface Options {\n config: WalletConfig;\n handler: EventHandler;\n}\n\nclass Wallet<InstanceType = any> {\n private actions: WalletActions;\n private state: State;\n private options: Options;\n private meta: BlockchainMeta[];\n public provider: InstanceType | null;\n\n constructor(options: Options, actions: WalletActions) {\n this.actions = actions;\n this.options = options;\n this.provider = null;\n this.meta = [];\n this.state = {\n connected: false,\n connecting: false,\n // TODO: Remove\n reachable: false,\n installed: false,\n accounts: null,\n network: null,\n };\n\n if (!needsCheckInstallation(options)) {\n this.setInstalledAs(true);\n }\n }\n\n private async getConnectionFromState() {\n // Already connected, so we return provider that we have in memory.\n\n // For switching network on Trust Wallet (WalletConnect),\n // We only kill the session (and not restting the whole state)\n // So we are relying on this.provider for achieving this functionality.\n if (this.state.connected && !!this.provider) {\n return {\n accounts: this.state.accounts,\n network: this.state.network,\n provider: this.provider,\n };\n }\n\n return null;\n }\n async connect(network?: Network) {\n // If it's connecting, nothing do.\n if (this.state.connecting) {\n throw new Error('Connecting...');\n }\n\n const connectionFromState = await this.getConnectionFromState();\n const currentNetwork = this.state.network;\n // If a network hasn't been provided and also we have `lastNetwork`\n // We will use lastNetwork to make sure we will not\n // Ask the user to switch his network wrongly.\n const requestedNetwork =\n network || currentNetwork || this.options.config.defaultNetwork;\n\n if (connectionFromState) {\n const networkChanged =\n currentNetwork !== requestedNetwork && !!requestedNetwork;\n\n // Reuse current connection if nothing has changed and we already have the connection in memory.\n if (currentNetwork === requestedNetwork) {\n return connectionFromState;\n }\n if (networkChanged && !!this.actions.switchNetwork) {\n await this.actions.switchNetwork({\n instance: this.provider,\n meta: this.meta,\n // TODO: Fix type error\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n network: requestedNetwork,\n newInstance: this.tryGetInstance.bind(this),\n });\n\n // We assume if we reach here (`switchNetwork` not throwing error), Switch successfully has been done.\n if (requestedNetwork !== this.state.network) {\n this.updateState({\n network,\n });\n }\n\n return {\n // Only network has been changed, so we reuse accounts from what we have already.\n accounts: connectionFromState.accounts,\n network: requestedNetwork,\n provider: this.provider,\n };\n }\n\n // If none of the above conditions didn't match, continute to connect.\n }\n\n // We are connecting to wallet for the first time\n\n // Trying to get wallet's instance, if it's not available, raise an error.\n const instance = await this.tryGetInstance({ network });\n\n // Instance exists, trying to connect\n this.updateState({\n connecting: true,\n });\n this.setInstalledAs(true);\n\n try {\n // eslint-disable-next-line no-var\n var connectResult = await this.actions.connect({\n instance,\n network: requestedNetwork || undefined,\n meta: this.meta || [],\n });\n } catch (e) {\n this.resetState();\n throw e;\n }\n\n this.updateState({\n connected: true,\n reachable: true,\n connecting: false,\n });\n\n // TODO: Handle accounts.length > 0\n\n // Inserting accounts into our state.\n let nextAccounts: string[] = [];\n let nextNetwork: Network | null | undefined = null;\n if (Array.isArray(connectResult)) {\n const accounts = connectResult.flatMap((blockchain) => {\n const chainId = blockchain.chainId || Networks.Unknown;\n // Try to map chainId with a Network, if not found, we use chainId directly.\n const network =\n getBlockChainNameFromId(chainId, this.meta) || Networks.Unknown;\n // TODO: second parameter should be `string` when we decided to open source the package.\n return accountAddressesWithNetwork(blockchain.accounts, network);\n });\n // Typescript can not detect we are filtering out null values:(\n nextAccounts = accounts.filter(Boolean) as string[];\n nextNetwork = requestedNetwork || this.options.config.defaultNetwork;\n } else {\n const chainId = connectResult.chainId || Networks.Unknown;\n const network =\n getBlockChainNameFromId(chainId, this.meta) || Networks.Unknown;\n // We fallback to current active network if `chainId` not provided.\n nextAccounts = accountAddressesWithNetwork(\n connectResult.accounts,\n network\n );\n nextNetwork = network;\n }\n\n if (nextAccounts.length > 0) {\n this.updateState({\n accounts: nextAccounts,\n network: nextNetwork,\n });\n }\n\n return {\n accounts: this.state.accounts,\n network: this.state.network,\n provider: this.provider,\n };\n }\n\n async disconnect() {\n this.resetState();\n\n if (this.actions.disconnect) {\n this.actions.disconnect({\n instance: this.provider,\n // On wallet connect, we need to destory the instance and get a whole new instance when we are going to connect\n destroyInstance: () => {\n this.setProvider(null);\n },\n });\n }\n }\n\n // This method is only used for auto connection\n async eagerConnect() {\n const instance = await this.tryGetInstance({ network: undefined });\n const { canEagerConnect } = this.actions;\n const error_message = `can't restore connection for ${this.options.config.type} .`;\n\n if (canEagerConnect) {\n // Check if we can eagerly connect to the wallet\n const eagerConnection = await canEagerConnect({\n instance: instance,\n meta: this.meta,\n });\n\n if (eagerConnection) {\n // Connect to wallet as usual\n return this.connect();\n } else {\n throw new Error(error_message);\n }\n } else {\n throw new Error(error_message);\n }\n }\n\n getSigners(provider: any) {\n return this.actions.getSigners(provider);\n }\n getWalletInfo(allBlockChains: BlockchainMeta[]) {\n return this.actions.getWalletInfo(allBlockChains);\n }\n canSwitchNetworkTo(network: Network, provider: any) {\n const switchTo = this.actions.canSwitchNetworkTo;\n if (!switchTo) return false;\n\n return switchTo({\n network,\n meta: this.meta,\n provider,\n });\n }\n\n onInit() {\n if (!this.options.config.isAsyncInstance) {\n const instance = this.actions.getInstance();\n if (!!instance && !this.state.installed) {\n this.setInstalledAs(true);\n }\n } else if (needsCheckInstallation(this.options)) {\n this.actions.getInstance().then((data: any) => {\n if (data) this.setInstalledAs(true);\n });\n }\n }\n\n setProvider(value: any) {\n this.provider = value;\n if (!!value && !!this.actions.subscribe) {\n this.actions.subscribe({\n instance: value,\n state: this.state,\n meta: this.meta,\n connect: this.connect.bind(this),\n disconnect: this.disconnect.bind(this),\n updateAccounts: (accounts, chainId) => {\n let network = this.state.network;\n if (chainId) {\n network =\n getBlockChainNameFromId(chainId, this.meta) || Networks.Unknown;\n }\n\n const nextAccounts = accountAddressesWithNetwork(accounts, network);\n if (nextAccounts.length > 0) {\n this.updateState({\n accounts: nextAccounts,\n });\n }\n },\n updateChainId: this.updateChainId.bind(this),\n });\n }\n }\n\n setMeta(value: BlockchainMeta[]) {\n this.meta = value;\n }\n\n setHandler(handler: EventHandler) {\n this.options.handler = handler;\n }\n\n getState(): State {\n return this.state;\n }\n updateState(states: Partial<State>) {\n // We will notify handler after updating all the states.\n // Because when we call `handler` it will has latest states.\n const updates: [Events, any][] = [];\n\n if (typeof states.connected !== 'undefined') {\n this.state.connected = states.connected;\n updates.push([Events.CONNECTED, states.connected]);\n }\n if (typeof states.connecting !== 'undefined') {\n this.state.connecting = states.connecting;\n updates.push([Events.CONNECTING, states.connecting]);\n }\n if (typeof states.reachable !== 'undefined') {\n this.state.reachable = states.reachable;\n updates.push([Events.REACHABLE, states.reachable]);\n }\n if (typeof states.installed !== 'undefined') {\n this.state.installed = states.installed;\n updates.push([Events.INSTALLED, states.installed]);\n }\n if (typeof states.accounts !== 'undefined') {\n this.state.accounts = states.accounts;\n updates.push([Events.ACCOUNTS, states.accounts]);\n }\n if (typeof states.network !== 'undefined') {\n this.state.network = states.network;\n updates.push([Events.NETWORK, states.network]);\n }\n\n const state = this.getState();\n updates.forEach(([name, value]) => {\n this.options.handler(\n this.options.config.type,\n name,\n value,\n state,\n this.meta\n );\n });\n }\n\n resetState() {\n this.updateState({\n connected: false,\n connecting: false,\n reachable: false,\n accounts: null,\n network: null,\n });\n }\n\n private updateChainId(chainId: string | number) {\n const network = chainId\n ? getBlockChainNameFromId(chainId, this.meta)\n : Networks.Unknown;\n\n this.updateState({\n network,\n });\n }\n\n private setInstalledAs(value: boolean) {\n if (!needsCheckInstallation(this.options) && value === false) return;\n\n this.updateState({\n installed: value,\n });\n }\n private async tryGetInstance({\n network,\n force,\n }: {\n network?: Network;\n force?: boolean;\n }) {\n let instance = null;\n // For switching network on Trust Wallet (WalletConnect),\n // We only kill the session (and not restting the whole state)\n // So we are relying on this.provider for achieving this functionality.\n this.setProvider(null);\n if (this.options.config.isAsyncInstance) {\n // Trying to connect\n const instanceOptions: GetInstanceOptions = {\n currentProvider: this.provider,\n meta: this.meta,\n force: force || false,\n updateChainId: this.updateChainId.bind(this),\n getState: this.getState.bind(this),\n };\n\n if (network) {\n instanceOptions.network = network;\n }\n instance = await this.actions.getInstance(instanceOptions);\n } else {\n instance = this.actions.getInstance();\n }\n\n if (!instance) {\n this.setInstalledAs(false);\n this.resetState();\n\n const error_message = `It seems your selected wallet (${this.options.config.type}) isn't installed.`;\n throw new Error(error_message);\n }\n\n this.setProvider(instance);\n return instance;\n }\n}\n\nexport default Wallet;\n"],
|
|
5
|
-
"mappings": "+EAgBO,IAAKA,OACVA,EAAA,UAAY,YACZA,EAAA,WAAa,aACbA,EAAA,UAAY,YACZA,EAAA,UAAY,YACZA,EAAA,SAAW,WACXA,EAAA,QAAU,UANAA,OAAA,ICVL,IAAMC,EAAN,KAAgD,CACrD,QAAQC,EAAc,CACpB,IAAMC,EAAO,aAAa,QAAQD,CAAI,EAEtC,OADmBC,EAAQ,KAAK,MAAMA,CAAI,EAAU,IAEtD,CACA,QAAQD,EAAcE,EAAU,CAC9B,aAAa,QAAQF,EAAM,KAAK,UAAUE,CAAK,CAAC,CAClD,CACA,WAAWF,EAAc,CACvB,aAAa,WAAWA,CAAI,CAC9B,CACF,EAZaG,EAAAJ,EAAA,aCHN,SAASK,EACdC,EACAC,EACA,CACA,MAAO,GAAGA,GAAW,MAAMD,GAC7B,CALgBE,EAAAH,EAAA,4BAOT,SAASI,EACdC,EACAH,EACA,CACA,OAAKG,EAEEA,EAAU,IAAKJ,GACbD,EAAyBC,EAASC,CAAO,CACjD,EAJsB,CAAC,CAK1B,CATgBC,EAAAC,EAAA,+BAWT,SAASE,EAAmBC,EAGjC,CACA,GAAM,CAACL,EAASD,CAAO,EAAIM,EAAmB,MAAM,GAAG,EAEvD,MAAO,CACL,QAAAL,EACA,QAAAD,CACF,CACF,CAVgBE,EAAAG,EAAA,sBAYT,SAASE,EAAuBC,EAAkB,CACvD,GAAM,CAAE,kBAAAC,EAAoB,EAAK,EAAID,EAAQ,OAC7C,OAAOC,CACT,CAHgBP,EAAAK,EAAA,
|
|
6
|
-
"names": ["Events", "Persistor", "name", "item", "value", "__name", "formatAddressWithNetwork", "address", "network", "__name", "accountAddressesWithNetwork", "addresses", "readAccountAddress", "addressWithNetwork", "needsCheckInstallation", "options", "checkInstallation", "getBlockChainNameFromId", "Networks", "Wallet", "options", "actions", "needsCheckInstallation", "network", "connectionFromState", "currentNetwork", "requestedNetwork", "networkChanged", "instance", "connectResult", "e", "nextAccounts", "nextNetwork", "blockchain", "chainId", "Networks", "getBlockChainNameFromId", "accountAddressesWithNetwork", "canEagerConnect", "error_message", "provider", "allBlockChains", "switchTo", "data", "value", "accounts", "handler", "states", "updates", "state", "name", "force", "instanceOptions", "__name", "wallet_default"]
|
|
4
|
+
"sourcesContent": ["import { Network, WalletType, WalletInfo } from '@rango-dev/wallets-shared';\nimport { State as WalletState } from './wallet';\nimport { SignerFactory, BlockchainMeta } from 'rango-types';\n\nexport type State = {\n [key: string]: WalletState | undefined;\n};\n\nexport type ConnectResult = {\n accounts: string[] | null;\n network: Network | null;\n provider: any;\n};\n\nexport type Providers = { [type in WalletType]?: any };\n\nexport enum Events {\n CONNECTED = 'connected',\n CONNECTING = 'connecting',\n REACHABLE = 'reachable',\n INSTALLED = 'installed',\n ACCOUNTS = 'accounts',\n NETWORK = 'network',\n}\n\nexport type ProviderConnectResult = {\n accounts: string[];\n chainId: string;\n};\n\nexport type GetInstanceOptions = {\n network?: Network;\n currentProvider: any;\n meta: BlockchainMeta[];\n getState: () => WalletState;\n /**\n * We always get the instance once and reuse it whenever we needs. By using this option\n * We can force the library to get a new instance and replace it with the old one.\n *\n * Originally, we used this option for wallet connect 1 and its switching network challenge.\n */\n force?: boolean;\n updateChainId: (chainId: number | string) => void;\n};\n\nexport type GetInstance =\n | (() => any)\n | ((options: GetInstanceOptions) => Promise<any>);\n\nexport type TryGetInstance =\n | (() => any)\n | ((options: Pick<GetInstanceOptions, 'force' | 'network'>) => Promise<any>);\n\nexport type Connect = (options: {\n instance: any;\n network?: Network;\n meta: BlockchainMeta[];\n}) => Promise<ProviderConnectResult | ProviderConnectResult[]>;\n\nexport type Disconnect = (options: {\n instance: any;\n destroyInstance: () => void;\n}) => Promise<void>;\n\nexport type Subscribe = (options: {\n instance: any;\n state: WalletState;\n meta: BlockchainMeta[];\n updateChainId: (chainId: string) => void;\n updateAccounts: (accounts: string[], chainId?: string) => void;\n connect: (network?: Network) => void;\n disconnect: () => void;\n}) => void;\n\nexport type SwitchNetwork = (options: {\n instance: any;\n network: Network;\n meta: BlockchainMeta[];\n newInstance?: TryGetInstance;\n}) => Promise<void>;\n\nexport type CanSwitchNetwork = (options: {\n network: Network;\n meta: BlockchainMeta[];\n provider: any;\n}) => boolean;\n\nexport type CanEagerConnect = (options: {\n instance: any;\n meta: BlockchainMeta[];\n}) => Promise<boolean>;\n\nexport interface WalletActions {\n connect: Connect;\n getInstance: any;\n disconnect?: Disconnect;\n subscribe?: Subscribe;\n // unsubscribe, // coupled to subscribe.\n\n // Optional, but should be provided at the same time.\n switchNetwork?: SwitchNetwork;\n getSigners: (provider: any) => SignerFactory;\n canSwitchNetworkTo?: CanSwitchNetwork;\n canEagerConnect?: CanEagerConnect;\n getWalletInfo(allBlockChains: BlockchainMeta[]): WalletInfo;\n}\n\nexport interface WalletConfig {\n type: WalletType;\n defaultNetwork?: Network;\n checkInstallation?: boolean;\n isAsyncInstance?: boolean;\n}\n\nexport type WalletProviders = Map<\n WalletType,\n {\n actions: WalletActions;\n config: WalletConfig;\n }\n>;\n\nexport type ProviderInterface = { config: WalletConfig } & WalletActions;\n", "export interface PersistStorage<T> {\n getItem: (name: string) => T | null;\n setItem: (name: string, value: T) => void;\n removeItem: (name: string) => void;\n}\n\nexport class Persistor<T> implements PersistStorage<T> {\n getItem(name: string) {\n const item = localStorage.getItem(name);\n const parsedItem = item ? (JSON.parse(item) as T) : null;\n return parsedItem;\n }\n setItem(name: string, value: T) {\n localStorage.setItem(name, JSON.stringify(value));\n }\n removeItem(name: string) {\n localStorage.removeItem(name);\n }\n}\n", "import { Network } from '@rango-dev/wallets-shared';\nimport { Options } from './wallet';\n\nexport function formatAddressWithNetwork(\n address: string,\n network?: Network | null\n) {\n return `${network || ''}:${address}`;\n}\n\nexport function accountAddressesWithNetwork(\n addresses: string[] | null,\n network?: Network | null\n) {\n if (!addresses) return [];\n\n return addresses.map((address) => {\n return formatAddressWithNetwork(address, network);\n });\n}\n\nexport function readAccountAddress(addressWithNetwork: string): {\n address: string;\n network: Network;\n} {\n const [network, address] = addressWithNetwork.split(':');\n\n return {\n network,\n address,\n };\n}\n\nexport function needsCheckInstallation(options: Options) {\n const { checkInstallation = true } = options.config;\n return checkInstallation;\n}\n", "import type { GetInstanceOptions, WalletActions, WalletConfig } from './types';\nimport type { Network, WalletType } from '@rango-dev/wallets-shared';\nimport type { BlockchainMeta } from 'rango-types';\n\nimport { getBlockChainNameFromId, Networks } from '@rango-dev/wallets-shared';\n\nimport { accountAddressesWithNetwork, needsCheckInstallation } from './helpers';\nimport { Events } from './types';\n\nexport type EventHandler = (\n type: WalletType,\n event: Events,\n value: any,\n coreState: State,\n supportedChains: BlockchainMeta[]\n) => void;\n\nexport interface State {\n connected: boolean;\n connecting: boolean;\n reachable: boolean;\n installed: boolean;\n accounts: string[] | null;\n network: Network | null;\n}\n\nexport interface Options {\n config: WalletConfig;\n handler: EventHandler;\n}\n\nclass Wallet<InstanceType = any> {\n private actions: WalletActions;\n private state: State;\n private options: Options;\n private meta: BlockchainMeta[];\n public provider: InstanceType | null;\n\n constructor(options: Options, actions: WalletActions) {\n this.actions = actions;\n this.options = options;\n this.provider = null;\n this.meta = [];\n this.state = {\n connected: false,\n connecting: false,\n // TODO: Remove\n reachable: false,\n installed: false,\n accounts: null,\n network: null,\n };\n\n if (!needsCheckInstallation(options)) {\n this.setInstalledAs(true);\n }\n }\n\n private async getConnectionFromState() {\n // Already connected, so we return provider that we have in memory.\n\n /*\n * For switching network on Trust Wallet (WalletConnect),\n * We only kill the session (and not restting the whole state)\n * So we are relying on this.provider for achieving this functionality.\n */\n if (this.state.connected && !!this.provider) {\n return {\n accounts: this.state.accounts,\n network: this.state.network,\n provider: this.provider,\n };\n }\n\n return null;\n }\n async connect(network?: Network) {\n // If it's connecting, nothing do.\n if (this.state.connecting) {\n throw new Error('Connecting...');\n }\n\n const connectionFromState = await this.getConnectionFromState();\n const currentNetwork = this.state.network;\n /*\n * If a network hasn't been provided and also we have `lastNetwork`\n * We will use lastNetwork to make sure we will not\n * Ask the user to switch his network wrongly.\n */\n const requestedNetwork =\n network || currentNetwork || this.options.config.defaultNetwork;\n\n if (connectionFromState) {\n const networkChanged =\n currentNetwork !== requestedNetwork && !!requestedNetwork;\n\n // Reuse current connection if nothing has changed and we already have the connection in memory.\n if (currentNetwork === requestedNetwork) {\n return connectionFromState;\n }\n\n let canSwitch = true;\n if (this.actions.canSwitchNetworkTo) {\n canSwitch = this.actions.canSwitchNetworkTo({\n provider: this.provider,\n meta: this.meta,\n network: requestedNetwork || '',\n });\n }\n\n if (networkChanged && canSwitch && !!this.actions.switchNetwork) {\n await this.actions.switchNetwork({\n instance: this.provider,\n meta: this.meta,\n // TODO: Fix type error\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n network: requestedNetwork,\n newInstance: this.tryGetInstance.bind(this),\n });\n\n // We assume if we reach here (`switchNetwork` not throwing error), Switch successfully has been done.\n if (requestedNetwork !== this.state.network) {\n this.updateState({\n network,\n });\n }\n\n return {\n // Only network has been changed, so we reuse accounts from what we have already.\n accounts: connectionFromState.accounts,\n network: requestedNetwork,\n provider: this.provider,\n };\n }\n\n // If none of the above conditions didn't match, continute to connect.\n }\n\n // We are connecting to wallet for the first time\n\n // Trying to get wallet's instance, if it's not available, raise an error.\n const instance = await this.tryGetInstance({ network });\n\n // Instance exists, trying to connect\n this.updateState({\n connecting: true,\n });\n this.setInstalledAs(true);\n\n try {\n // eslint-disable-next-line no-var\n var connectResult = await this.actions.connect({\n instance,\n network: requestedNetwork || undefined,\n meta: this.meta || [],\n });\n } catch (e) {\n this.resetState();\n throw e;\n }\n\n this.updateState({\n connected: true,\n reachable: true,\n connecting: false,\n });\n\n // TODO: Handle accounts.length > 0\n\n // Inserting accounts into our state.\n let nextAccounts: string[] = [];\n let nextNetwork: Network | null | undefined = null;\n if (Array.isArray(connectResult)) {\n const accounts = connectResult.flatMap((blockchain) => {\n const chainId = blockchain.chainId || Networks.Unknown;\n // Try to map chainId with a Network, if not found, we use chainId directly.\n const network =\n getBlockChainNameFromId(chainId, this.meta) || Networks.Unknown;\n // TODO: second parameter should be `string` when we decided to open source the package.\n return accountAddressesWithNetwork(blockchain.accounts, network);\n });\n // Typescript can not detect we are filtering out null values:(\n nextAccounts = accounts.filter(Boolean);\n nextNetwork = requestedNetwork || this.options.config.defaultNetwork;\n } else {\n const chainId = connectResult.chainId || Networks.Unknown;\n const network =\n getBlockChainNameFromId(chainId, this.meta) || Networks.Unknown;\n // We fallback to current active network if `chainId` not provided.\n nextAccounts = accountAddressesWithNetwork(\n connectResult.accounts,\n network\n );\n nextNetwork = network;\n }\n\n if (nextAccounts.length > 0) {\n this.updateState({\n accounts: nextAccounts,\n network: nextNetwork,\n });\n }\n\n return {\n accounts: this.state.accounts,\n network: this.state.network,\n provider: this.provider,\n };\n }\n\n async disconnect() {\n this.resetState();\n\n if (this.actions.disconnect) {\n this.actions.disconnect({\n instance: this.provider,\n // On wallet connect, we need to destory the instance and get a whole new instance when we are going to connect\n destroyInstance: () => {\n this.setProvider(null);\n },\n });\n }\n }\n\n // This method is only used for auto connection\n async eagerConnect() {\n const instance = await this.tryGetInstance({ network: undefined });\n const { canEagerConnect } = this.actions;\n const error_message = `can't restore connection for ${this.options.config.type} .`;\n\n if (canEagerConnect) {\n // Check if we can eagerly connect to the wallet\n const eagerConnection = await canEagerConnect({\n instance: instance,\n meta: this.meta,\n });\n\n if (eagerConnection) {\n // Connect to wallet as usual\n return this.connect();\n }\n throw new Error(error_message);\n } else {\n throw new Error(error_message);\n }\n }\n\n getSigners(provider: any) {\n return this.actions.getSigners(provider);\n }\n getWalletInfo(allBlockChains: BlockchainMeta[]) {\n return this.actions.getWalletInfo(allBlockChains);\n }\n canSwitchNetworkTo(network: Network, provider: any) {\n const switchTo = this.actions.canSwitchNetworkTo;\n if (!switchTo) {\n return false;\n }\n\n return switchTo({\n network,\n meta: this.meta,\n provider,\n });\n }\n\n onInit() {\n if (!this.options.config.isAsyncInstance) {\n const instance = this.actions.getInstance();\n if (!!instance && !this.state.installed) {\n this.setInstalledAs(true);\n }\n } else if (needsCheckInstallation(this.options)) {\n this.actions.getInstance().then((data: any) => {\n if (data) {\n this.setInstalledAs(true);\n }\n });\n }\n }\n\n setProvider(value: any) {\n this.provider = value;\n if (!!value && !!this.actions.subscribe) {\n this.actions.subscribe({\n instance: value,\n state: this.state,\n meta: this.meta,\n connect: this.connect.bind(this),\n disconnect: this.disconnect.bind(this),\n updateAccounts: (accounts, chainId) => {\n let network = this.state.network;\n if (chainId) {\n network =\n getBlockChainNameFromId(chainId, this.meta) || Networks.Unknown;\n }\n\n const nextAccounts = accountAddressesWithNetwork(accounts, network);\n if (nextAccounts.length > 0) {\n this.updateState({\n accounts: nextAccounts,\n });\n }\n },\n updateChainId: this.updateChainId.bind(this),\n });\n }\n }\n\n setMeta(value: BlockchainMeta[]) {\n this.meta = value;\n }\n\n setHandler(handler: EventHandler) {\n this.options.handler = handler;\n }\n\n getState(): State {\n return this.state;\n }\n updateState(states: Partial<State>) {\n /*\n * We will notify handler after updating all the states.\n * Because when we call `handler` it will has latest states.\n */\n const updates: [Events, any][] = [];\n\n if (typeof states.connected !== 'undefined') {\n this.state.connected = states.connected;\n updates.push([Events.CONNECTED, states.connected]);\n }\n if (typeof states.connecting !== 'undefined') {\n this.state.connecting = states.connecting;\n updates.push([Events.CONNECTING, states.connecting]);\n }\n if (typeof states.reachable !== 'undefined') {\n this.state.reachable = states.reachable;\n updates.push([Events.REACHABLE, states.reachable]);\n }\n if (typeof states.installed !== 'undefined') {\n this.state.installed = states.installed;\n updates.push([Events.INSTALLED, states.installed]);\n }\n if (typeof states.accounts !== 'undefined') {\n this.state.accounts = states.accounts;\n updates.push([Events.ACCOUNTS, states.accounts]);\n }\n if (typeof states.network !== 'undefined') {\n this.state.network = states.network;\n updates.push([Events.NETWORK, states.network]);\n }\n\n const state = this.getState();\n updates.forEach(([name, value]) => {\n this.options.handler(\n this.options.config.type,\n name,\n value,\n state,\n this.meta\n );\n });\n }\n\n resetState() {\n this.updateState({\n connected: false,\n connecting: false,\n reachable: false,\n accounts: null,\n network: null,\n });\n }\n\n private updateChainId(chainId: string | number) {\n const network = chainId\n ? getBlockChainNameFromId(chainId, this.meta)\n : Networks.Unknown;\n\n this.updateState({\n network,\n });\n }\n\n private setInstalledAs(value: boolean) {\n if (!needsCheckInstallation(this.options) && value === false) {\n return;\n }\n\n this.updateState({\n installed: value,\n });\n }\n private async tryGetInstance({\n network,\n force,\n }: {\n network?: Network;\n force?: boolean;\n }) {\n let instance = null;\n /*\n * For switching network on Trust Wallet (WalletConnect),\n * We only kill the session (and not restting the whole state)\n * So we are relying on this.provider for achieving this functionality.\n */\n this.setProvider(null);\n if (this.options.config.isAsyncInstance) {\n // Trying to connect\n const instanceOptions: GetInstanceOptions = {\n currentProvider: this.provider,\n meta: this.meta,\n force: force || false,\n updateChainId: this.updateChainId.bind(this),\n getState: this.getState.bind(this),\n };\n\n if (network) {\n instanceOptions.network = network;\n }\n instance = await this.actions.getInstance(instanceOptions);\n } else {\n instance = this.actions.getInstance();\n }\n\n if (!instance) {\n this.setInstalledAs(false);\n this.resetState();\n\n const error_message = `It seems your selected wallet (${this.options.config.type}) isn't installed.`;\n throw new Error(error_message);\n }\n\n this.setProvider(instance);\n return instance;\n }\n}\n\nexport default Wallet;\n"],
|
|
5
|
+
"mappings": "+EAgBO,IAAKA,OACVA,EAAA,UAAY,YACZA,EAAA,WAAa,aACbA,EAAA,UAAY,YACZA,EAAA,UAAY,YACZA,EAAA,SAAW,WACXA,EAAA,QAAU,UANAA,OAAA,ICVL,IAAMC,EAAN,KAAgD,CACrD,QAAQC,EAAc,CACpB,IAAMC,EAAO,aAAa,QAAQD,CAAI,EAEtC,OADmBC,EAAQ,KAAK,MAAMA,CAAI,EAAU,IAEtD,CACA,QAAQD,EAAcE,EAAU,CAC9B,aAAa,QAAQF,EAAM,KAAK,UAAUE,CAAK,CAAC,CAClD,CACA,WAAWF,EAAc,CACvB,aAAa,WAAWA,CAAI,CAC9B,CACF,EAZaG,EAAAJ,EAAA,aCHN,SAASK,EACdC,EACAC,EACA,CACA,MAAO,GAAGA,GAAW,MAAMD,GAC7B,CALgBE,EAAAH,EAAA,4BAOT,SAASI,EACdC,EACAH,EACA,CACA,OAAKG,EAEEA,EAAU,IAAKJ,GACbD,EAAyBC,EAASC,CAAO,CACjD,EAJsB,CAAC,CAK1B,CATgBC,EAAAC,EAAA,+BAWT,SAASE,EAAmBC,EAGjC,CACA,GAAM,CAACL,EAASD,CAAO,EAAIM,EAAmB,M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N,EACAK,EACA,KAAK,IACP,CACF,CAAC,CACH,CAEA,YAAa,CACX,KAAK,YAAY,CACf,UAAW,GACX,WAAY,GACZ,UAAW,GACX,SAAU,KACV,QAAS,IACX,CAAC,CACH,CAEQ,cAAcf,EAA0B,CAC9C,IAAMZ,EAAUY,EACZE,EAAwBF,EAAS,KAAK,IAAI,EAC1CC,EAAS,QAEb,KAAK,YAAY,CACf,QAAAb,CACF,CAAC,CACH,CAEQ,eAAesB,EAAgB,CACjC,CAACvB,EAAuB,KAAK,OAAO,GAAKuB,IAAU,IAIvD,KAAK,YAAY,CACf,UAAWA,CACb,CAAC,CACH,CACA,MAAc,eAAe,CAC3B,QAAAtB,EACA,MAAA6B,CACF,EAGG,CACD,IAAIvB,EAAW,KAOf,GADA,KAAK,YAAY,IAAI,EACjB,KAAK,QAAQ,OAAO,gBAAiB,CAEvC,IAAMwB,EAAsC,CAC1C,gBAAiB,KAAK,SACtB,KAAM,KAAK,KACX,MAAOD,GAAS,GAChB,cAAe,KAAK,cAAc,KAAK,IAAI,EAC3C,SAAU,KAAK,SAAS,KAAK,IAAI,CACnC,EAEI7B,IACF8B,EAAgB,QAAU9B,GAE5BM,EAAW,MAAM,KAAK,QAAQ,YAAYwB,CAAe,OAEzDxB,EAAW,KAAK,QAAQ,YAAY,EAGtC,GAAI,CAACA,EAAU,CACb,KAAK,eAAe,EAAK,EACzB,KAAK,WAAW,EAEhB,IAAMW,EAAgB,kCAAkC,KAAK,QAAQ,OAAO,yBAC5E,MAAM,IAAI,MAAMA,CAAa,EAG/B,YAAK,YAAYX,CAAQ,EAClBA,CACT,CACF,EAtZMyB,EAAAnC,EAAA,UAwZN,IAAOoC,EAAQpC",
|
|
6
|
+
"names": ["Events", "Persistor", "name", "item", "value", "__name", "formatAddressWithNetwork", "address", "network", "__name", "accountAddressesWithNetwork", "addresses", "readAccountAddress", "addressWithNetwork", "needsCheckInstallation", "options", "checkInstallation", "getBlockChainNameFromId", "Networks", "Wallet", "options", "actions", "needsCheckInstallation", "network", "connectionFromState", "currentNetwork", "requestedNetwork", "networkChanged", "canSwitch", "instance", "connectResult", "e", "nextAccounts", "nextNetwork", "blockchain", "chainId", "Networks", "getBlockChainNameFromId", "accountAddressesWithNetwork", "canEagerConnect", "error_message", "provider", "allBlockChains", "switchTo", "data", "value", "accounts", "handler", "states", "updates", "state", "name", "force", "instanceOptions", "__name", "wallet_default"]
|
|
7
7
|
}
|
package/dist/wallet.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { BlockchainMeta } from 'rango-types';
|
|
1
|
+
import type { WalletActions, WalletConfig } from './types';
|
|
2
|
+
import type { Network, WalletType } from '@rango-dev/wallets-shared';
|
|
3
|
+
import type { BlockchainMeta } from 'rango-types';
|
|
4
|
+
import { Events } from './types';
|
|
4
5
|
export type EventHandler = (type: WalletType, event: Events, value: any, coreState: State, supportedChains: BlockchainMeta[]) => void;
|
|
5
6
|
export interface State {
|
|
6
7
|
connected: boolean;
|
package/dist/wallet.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wallet.d.ts","sourceRoot":"","sources":["../src/wallet.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"wallet.d.ts","sourceRoot":"","sources":["../src/wallet.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAsB,aAAa,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC/E,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACrE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAKlD,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC,MAAM,MAAM,YAAY,GAAG,CACzB,IAAI,EAAE,UAAU,EAChB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,GAAG,EACV,SAAS,EAAE,KAAK,EAChB,eAA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GED
|
@@ -1,17 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import type { GetInstanceOptions, WalletActions, WalletConfig } from './types';
|
|
2
|
+
import type { Network, WalletType } from '@rango-dev/wallets-shared';
|
|
3
|
+
import type { BlockchainMeta } from 'rango-types';
|
|
4
|
+
|
|
5
|
+
import { getBlockChainNameFromId, Networks } from '@rango-dev/wallets-shared';
|
|
6
|
+
|
|
7
7
|
import { accountAddressesWithNetwork, needsCheckInstallation } from './helpers';
|
|
8
|
-
import {
|
|
9
|
-
Events,
|
|
10
|
-
GetInstanceOptions,
|
|
11
|
-
WalletActions,
|
|
12
|
-
WalletConfig,
|
|
13
|
-
} from './types';
|
|
14
|
-
import { BlockchainMeta } from 'rango-types';
|
|
8
|
+
import { Events } from './types';
|
|
15
9
|
|
|
16
10
|
export type EventHandler = (
|
|
17
11
|
type: WalletType,
|
|
@@ -65,9 +59,11 @@ class Wallet<InstanceType = any> {
|
|
|
65
59
|
private async getConnectionFromState() {
|
|
66
60
|
// Already connected, so we return provider that we have in memory.
|
|
67
61
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
62
|
+
/*
|
|
63
|
+
* For switching network on Trust Wallet (WalletConnect),
|
|
64
|
+
* We only kill the session (and not restting the whole state)
|
|
65
|
+
* So we are relying on this.provider for achieving this functionality.
|
|
66
|
+
*/
|
|
71
67
|
if (this.state.connected && !!this.provider) {
|
|
72
68
|
return {
|
|
73
69
|
accounts: this.state.accounts,
|
|
@@ -86,9 +82,11 @@ class Wallet<InstanceType = any> {
|
|
|
86
82
|
|
|
87
83
|
const connectionFromState = await this.getConnectionFromState();
|
|
88
84
|
const currentNetwork = this.state.network;
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
85
|
+
/*
|
|
86
|
+
* If a network hasn't been provided and also we have `lastNetwork`
|
|
87
|
+
* We will use lastNetwork to make sure we will not
|
|
88
|
+
* Ask the user to switch his network wrongly.
|
|
89
|
+
*/
|
|
92
90
|
const requestedNetwork =
|
|
93
91
|
network || currentNetwork || this.options.config.defaultNetwork;
|
|
94
92
|
|
|
@@ -100,7 +98,17 @@ class Wallet<InstanceType = any> {
|
|
|
100
98
|
if (currentNetwork === requestedNetwork) {
|
|
101
99
|
return connectionFromState;
|
|
102
100
|
}
|
|
103
|
-
|
|
101
|
+
|
|
102
|
+
let canSwitch = true;
|
|
103
|
+
if (this.actions.canSwitchNetworkTo) {
|
|
104
|
+
canSwitch = this.actions.canSwitchNetworkTo({
|
|
105
|
+
provider: this.provider,
|
|
106
|
+
meta: this.meta,
|
|
107
|
+
network: requestedNetwork || '',
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (networkChanged && canSwitch && !!this.actions.switchNetwork) {
|
|
104
112
|
await this.actions.switchNetwork({
|
|
105
113
|
instance: this.provider,
|
|
106
114
|
meta: this.meta,
|
|
@@ -173,7 +181,7 @@ class Wallet<InstanceType = any> {
|
|
|
173
181
|
return accountAddressesWithNetwork(blockchain.accounts, network);
|
|
174
182
|
});
|
|
175
183
|
// Typescript can not detect we are filtering out null values:(
|
|
176
|
-
nextAccounts = accounts.filter(Boolean)
|
|
184
|
+
nextAccounts = accounts.filter(Boolean);
|
|
177
185
|
nextNetwork = requestedNetwork || this.options.config.defaultNetwork;
|
|
178
186
|
} else {
|
|
179
187
|
const chainId = connectResult.chainId || Networks.Unknown;
|
|
@@ -231,9 +239,8 @@ class Wallet<InstanceType = any> {
|
|
|
231
239
|
if (eagerConnection) {
|
|
232
240
|
// Connect to wallet as usual
|
|
233
241
|
return this.connect();
|
|
234
|
-
} else {
|
|
235
|
-
throw new Error(error_message);
|
|
236
242
|
}
|
|
243
|
+
throw new Error(error_message);
|
|
237
244
|
} else {
|
|
238
245
|
throw new Error(error_message);
|
|
239
246
|
}
|
|
@@ -247,7 +254,9 @@ class Wallet<InstanceType = any> {
|
|
|
247
254
|
}
|
|
248
255
|
canSwitchNetworkTo(network: Network, provider: any) {
|
|
249
256
|
const switchTo = this.actions.canSwitchNetworkTo;
|
|
250
|
-
if (!switchTo)
|
|
257
|
+
if (!switchTo) {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
251
260
|
|
|
252
261
|
return switchTo({
|
|
253
262
|
network,
|
|
@@ -264,7 +273,9 @@ class Wallet<InstanceType = any> {
|
|
|
264
273
|
}
|
|
265
274
|
} else if (needsCheckInstallation(this.options)) {
|
|
266
275
|
this.actions.getInstance().then((data: any) => {
|
|
267
|
-
if (data)
|
|
276
|
+
if (data) {
|
|
277
|
+
this.setInstalledAs(true);
|
|
278
|
+
}
|
|
268
279
|
});
|
|
269
280
|
}
|
|
270
281
|
}
|
|
@@ -309,8 +320,10 @@ class Wallet<InstanceType = any> {
|
|
|
309
320
|
return this.state;
|
|
310
321
|
}
|
|
311
322
|
updateState(states: Partial<State>) {
|
|
312
|
-
|
|
313
|
-
|
|
323
|
+
/*
|
|
324
|
+
* We will notify handler after updating all the states.
|
|
325
|
+
* Because when we call `handler` it will has latest states.
|
|
326
|
+
*/
|
|
314
327
|
const updates: [Events, any][] = [];
|
|
315
328
|
|
|
316
329
|
if (typeof states.connected !== 'undefined') {
|
|
@@ -371,7 +384,9 @@ class Wallet<InstanceType = any> {
|
|
|
371
384
|
}
|
|
372
385
|
|
|
373
386
|
private setInstalledAs(value: boolean) {
|
|
374
|
-
if (!needsCheckInstallation(this.options) && value === false)
|
|
387
|
+
if (!needsCheckInstallation(this.options) && value === false) {
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
375
390
|
|
|
376
391
|
this.updateState({
|
|
377
392
|
installed: value,
|
|
@@ -385,9 +400,11 @@ class Wallet<InstanceType = any> {
|
|
|
385
400
|
force?: boolean;
|
|
386
401
|
}) {
|
|
387
402
|
let instance = null;
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
403
|
+
/*
|
|
404
|
+
* For switching network on Trust Wallet (WalletConnect),
|
|
405
|
+
* We only kill the session (and not restting the whole state)
|
|
406
|
+
* So we are relying on this.provider for achieving this functionality.
|
|
407
|
+
*/
|
|
391
408
|
this.setProvider(null);
|
|
392
409
|
if (this.options.config.isAsyncInstance) {
|
|
393
410
|
// Trying to connect
|