import { Provider , Platform } from './provider' ; import { Address , AddressType } from '../models' ; import ENS from 'ethereum-ens' ; import { verifyEthAddress } from '../utils' ; export class Web3ModalProvider extends Provider { onAddressChanged : ( newAddress : Address ) => void ; constructor ( readonly web3 , onAddressChanged ? ) { super ( Platform . eth ) ; this . onAddressChanged = onAddressChanged ; } async init ( ) { const accounts = await this . web3 . eth . getAccounts ( ) ; if ( ! verifyEthAddress ( accounts [ 0 ] ) ) { throw new Error ( 'get ethereum address failed' ) ; } this . address = new Address ( accounts [ 0 ] , AddressType . eth ) ; if ( this . web3 . currentProvider . on ) { this . web3 . currentProvider . on ( 'accountsChanged' , async ( newAccounts ) => { this . address = new Address ( newAccounts [ 0 ] , AddressType . eth ) ; if ( this . onAddressChanged ) { this . onAddressChanged ( this . address ) ; } } ) ; } return this ; } async ensResolver ( ens ) { try { return await new ENS ( this . web3 . currentProvider ) . resolver ( ens ) . addr ( ) ; } catch ( e ) { return 'Unknown ENS Name' ; } } async sign ( message ) { let result = await this . web3 . eth . personal . sign ( message , this . address . addressString , null ) ; let v = Number . parseInt ( result . slice ( - 2 ) , 16 ) ; if ( v >= 27 ) v -= 27 ; result = result . slice ( 0 , - 2 ) + v . toString ( 16 ) . padStart ( 2 , '0' ) ; return result ; } async close ( ) { if ( this . web3 && this . web3 . currentProvider && this . web3 . currentProvider . close ) { await this . web3 . currentProvider . close ( ) ; } } }