import { ContractKit } from '@celo/contractkit' ; import { AccountAuthResponseSuccess , DappKitRequestMeta , parseDappkitResponseDeeplink , SignTxResponseSuccess , } from '@celo/utils' ; import { checkAccountAuth , checkSignedTxs , requestAccountAddressFactory , requestTxSigFactory , TxParams , } from './common' ; export { AccountAuthRequest , DappKitRequestMeta , serializeDappKitRequestDeeplink , SignTxRequest , } from '@celo/utils' ; export { FeeCurrency , TxParams } from './common' ; const localStorageKey = 'dappkit-web' ; parseURLOnRender ( ) ; export function parseURLOnRender ( ) { if ( typeof window !== 'undefined' ) { const params = new URL ( window . location . href ) . searchParams ; if ( params . get ( 'type' ) && params . get ( 'requestId' ) ) { if ( localStorage ) { localStorage . setItem ( localStorageKey , window . location . href ) ; window . close ( ) ; } } } } async function waitForResponse ( timeout ) { const pollInterval = 100 ; const endTime = Date . now ( ) + timeout ; while ( Date . now ( ) < endTime ) { const value = localStorage . getItem ( localStorageKey ) ; if ( value ) { localStorage . removeItem ( localStorageKey ) ; return value ; } await new Promise ( ( resolve ) => setTimeout ( resolve , pollInterval ) ) ; } throw new Error ( 'Timeout waiting for Valora response' ) ; } async function waitDecorator ( requestId , checkCallback , timeout = 15000 ) { const url = await waitForResponse ( timeout ) ; const dappKitResponse = parseDappkitResponseDeeplink ( url ) ; if ( checkCallback ( requestId , dappKitResponse ) ) { return dappKitResponse ; } throw new Error ( 'Unable to parse Valora response' ) ; } export async function waitForAccountAuth ( requestId ) { return waitDecorator ( requestId , checkAccountAuth ) ; } export async function waitForSignedTxs ( requestId ) { return waitDecorator ( requestId , checkSignedTxs ) ; } export async function requestAccountAddress ( meta ) { return requestAccountAddressFactory ( meta , openURL ) ; } export async function requestTxSig ( kit , txParams , meta ) { return requestTxSigFactory ( kit , txParams , meta , openURL ) ; } async function openURL ( url ) { window . location . href = url ; }