import { ContractKit } from '@celo/contractkit' ; import { AccountAuthResponseSuccess , DappKitRequestMeta , DappKitRequestTypes , DappKitResponseStatus , parseDappkitResponseDeeplink , SignTxResponseSuccess , } from '@celo/utils' ; import { Linking , Platform } from 'react-native' ; import { ANDROID_STORE_URL , checkAccountAuth , checkSignedTxs , IOS_STORE_URL , requestAccountAddressFactory , requestTxSigFactory , TxParams , VALORA_APP_URL , } from './common' ; export { AccountAuthRequest , DappKitRequestMeta , serializeDappKitRequestDeeplink , SignTxRequest , } from '@celo/utils' ; export { FeeCurrency , TxParams } from './common' ; export function listenToAccount ( callback ) { return Linking . addEventListener ( 'url' , ( { url } ) => { try { const dappKitResponse = parseDappkitResponseDeeplink ( url ) ; if ( dappKitResponse . type === DappKitRequestTypes . ACCOUNT_ADDRESS && dappKitResponse . status === DappKitResponseStatus . SUCCESS ) { callback ( dappKitResponse . address ) ; } } catch ( error ) { } } ) ; } export function listenToSignedTxs ( callback ) { return Linking . addEventListener ( 'url' , ( { url } ) => { try { const dappKitResponse = parseDappkitResponseDeeplink ( url ) ; if ( dappKitResponse . type === DappKitRequestTypes . SIGN_TX && dappKitResponse . status === DappKitResponseStatus . SUCCESS ) { callback ( dappKitResponse . rawTxs ) ; } } catch ( error ) { } } ) ; } function waitDecorator ( requestId , checkCallback ) { return new Promise ( ( resolve , reject ) => { const handler = ( { url } ) => { try { const dappKitResponse = parseDappkitResponseDeeplink ( url ) ; if ( checkCallback ( requestId , dappKitResponse ) ) { Linking . removeEventListener ( 'url' , handler ) ; resolve ( dappKitResponse ) ; } } catch ( error ) { reject ( error ) ; } } ; Linking . addEventListener ( 'url' , handler ) ; } ) ; } export function waitForAccountAuth ( requestId ) { return waitDecorator ( requestId , checkAccountAuth ) ; } export function waitForSignedTxs ( requestId ) { return waitDecorator ( requestId , checkSignedTxs ) ; } export function requestAccountAddress ( meta ) { requestAccountAddressFactory ( meta , openURLOrAppStore ) ; } export async function requestTxSig ( kit , txParams , meta ) { return requestTxSigFactory ( kit , txParams , meta , openURLOrAppStore ) ; } async function openURLOrAppStore ( url ) { const openAppStore = async ( ) => { let storeURL ; switch ( Platform . OS ) { case 'ios' : { storeURL = IOS_STORE_URL ; break ; } case 'android' : { storeURL = ANDROID_STORE_URL ; break ; } default : { storeURL = VALORA_APP_URL ; break ; } } await Linking . openURL ( storeURL ) ; } ; try { await Linking . openURL ( url ) ; } catch ( error ) { console . error ( error ) ; await openAppStore ( ) ; throw Error ( 'Error opening deeplink: App likely not installed' ) ; } }