import 'regenerator-runtime/runtime' ; import { get , derived } from 'svelte/store' ; import Onboard from './views/Onboard.svelte' ; import { app , address , network , balance , wallet , state , walletInterface , resetWalletState , initializeStores } from './stores' ; import { getDeviceInfo , getEns } from './utilities' ; import { validateInit , validateConfig , isWalletInit } from './validation' ; import { version } from '../package.json' ; import { Initialization , AppState , API , ConfigOptions , UserState , Wallet , WalletInitOptions , Ens } from './interfaces' ; import initializeModules from './modules' ; import { closeSocketConnection } from './services' ; let onboard ; function init ( initialization ) { if ( typeof window === 'undefined' ) { console . warn ( 'Onboard.js must be run in a browser environment. If you are utilizing server side rendering you can ignore this warning.' ) ; const stubbedAPI = { walletSelect : ( ) => Promise . resolve ( false ) , walletCheck : ( ) => Promise . resolve ( false ) , walletReset : ( ) => { } , config : ( ) => { } , getState : ( ) => get ( state ) , accountSelect : ( ) => Promise . resolve ( false ) } ; return stubbedAPI ; } if ( onboard ) { console . warn ( 'Initializing Onboard and destroying previously initialized instance.' ) ; closeSocketConnection ( ) ; resetWalletState ( ) ; onboard . $destroy ( ) ; } validateInit ( initialization ) ; const { subscriptions , dappId , networkId , networkName , darkMode , apiUrl , hideBranding , blockPollingInterval = 4000 } = initialization ; const { os , browser , isMobile } = getDeviceInfo ( ) ; const initializedModules = initializeModules ( networkId , initialization . walletSelect , initialization . walletCheck , isMobile ) ; let displayBranding ; if ( dappId ) { if ( hideBranding !== false ) { displayBranding = false ; } else { displayBranding = true ; } } else { if ( hideBranding !== true ) { displayBranding = true ; } else { displayBranding = false ; } } app . update ( ( store ) => ( { ... store , dappId , apiUrl , networkId , networkName , version , mobileDevice : isMobile , os , browser , darkMode , displayBranding , checkModules : initializedModules . walletCheck , blockPollingInterval , agreement : initialization . walletSelect ?. agreement || null } ) ) ; initializeStores ( ) ; onboard = new Onboard ( { target : document . body , props : { walletSelectModule : initializedModules . walletSelect , walletSelect , walletCheck } } ) ; if ( subscriptions ) { if ( subscriptions . address ) { address . subscribe ( ( address ) => { if ( address !== null ) { subscriptions . address && subscriptions . address ( address ) ; } } ) ; } if ( subscriptions . ens ) { derived ( [ address , wallet ] , ( [ $address , $wallet ] , set ) => { if ( $address && $wallet && $wallet . provider ) { getEns ( $wallet . provider , $address ) . then ( set ) ; } else { set ( undefined ) ; } } ) . subscribe ( ens => { if ( ens !== null ) { subscriptions . ens && subscriptions . ens ( ens as Ens ) ; } } ) ; } if ( subscriptions . network ) { network . subscribe ( ( networkId ) => { if ( networkId !== null ) { subscriptions . network && subscriptions . network ( networkId ) ; } } ) ; } if ( subscriptions . balance ) { balance . subscribe ( ( balance ) => { if ( balance !== null ) { subscriptions . balance && subscriptions . balance ( balance ) ; } } ) ; } if ( subscriptions . wallet ) { wallet . subscribe ( ( wallet ) => { if ( wallet . provider !== null ) { subscriptions . wallet && subscriptions . wallet ( wallet ) ; } } ) ; } } function walletSelect ( autoSelectWallet ? ) { return new Promise ( resolve => { app . update ( ( store ) => ( { ... store , walletSelectInProgress : true , autoSelectWallet : typeof autoSelectWallet === 'string' && autoSelectWallet } ) ) ; const appUnsubscribe = app . subscribe ( ( store ) => { const { walletSelectInProgress , walletSelectCompleted , walletSelectDisplayedUI } = store ; if ( walletSelectInProgress === false ) { appUnsubscribe ( ) ; walletSelectDisplayedUI ? setTimeout ( ( ) => { resolve ( walletSelectCompleted ) ; app . update ( store => ( { ... store , displayedUI : false } ) ) ; } , 500 ) : resolve ( walletSelectCompleted ) ; } } ) ; } ) ; } function walletCheck ( ) { return new Promise ( resolve => { if ( ! get ( walletInterface ) ) { throw new Error ( 'walletSelect must be called before walletCheck' ) ; } app . update ( ( store ) => ( { ... store , walletCheckInProgress : true } ) ) ; const appUnsubscribe = app . subscribe ( ( store ) => { const { walletCheckInProgress , walletCheckCompleted , walletCheckDisplayedUI , switchingWallets } = store ; if ( ! switchingWallets && walletCheckInProgress === false ) { appUnsubscribe ( ) ; walletCheckDisplayedUI ? setTimeout ( ( ) => { resolve ( walletCheckCompleted ) ; app . update ( store => ( { ... store , displayedUI : false } ) ) ; } , 500 ) : resolve ( walletCheckCompleted ) ; } } ) ; } ) ; } function walletReset ( ) { resetWalletState ( ) ; } function accountSelect ( ) { return new Promise ( resolve => { const { type } = get ( wallet ) ; if ( type !== 'hardware' ) { resolve ( false ) ; } app . update ( ( store ) => ( { ... store , accountSelectInProgress : true } ) ) ; const appUnsubscribe = app . subscribe ( ( store ) => { const { accountSelectInProgress , walletSelectDisplayedUI } = store ; if ( accountSelectInProgress === false ) { appUnsubscribe ( ) ; walletSelectDisplayedUI ? setTimeout ( ( ) => { resolve ( true ) ; app . update ( store => ( { ... store , displayedUI : false } ) ) ; } , 500 ) : resolve ( true ) ; } } ) ; } ) ; } function config ( options ) { validateConfig ( options ) ; app . update ( ( store ) => ( { ... store , ... options } ) ) ; } function getState ( ) { return get ( state ) ; } const { label : gnosisWalletName = 'Gnosis Safe' } = ( initialization . walletSelect ?. wallets ?. find ( wallet => isWalletInit ( wallet ) && wallet . walletName === 'gnosis' ) || { } ) as WalletInitOptions ; if ( gnosisWalletName ) { import ( './modules/select/wallets/gnosis' ) . then ( ( { checkGnosisSafeContext } ) => checkGnosisSafeContext ( ( ) => walletSelect ( gnosisWalletName ) ) ) ; } return { walletSelect , walletCheck , walletReset , config , getState , accountSelect } ; } export default init ;